1<?php
2/**
3 * Action Component
4 * Add a button in the edit toolbar
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Nicolas GERARD
8 */
9
10use ComboStrap\Identity;
11use ComboStrap\IdentityFormsHelper;
12use ComboStrap\LogUtility;
13use dokuwiki\Form\Form;
14use dokuwiki\Menu\Item\Resendpwd;
15
16if (!defined('DOKU_INC')) die();
17require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
18
19
20class action_plugin_combo_resend extends DokuWiki_Action_Plugin
21{
22
23    const CANONICAL = "resend";
24    const FORM_RESEND_PWD_CLASS = "form-" . self::CANONICAL;
25    const CONF_ENABLE_RESEND_PWD_FORM = "enableResendPwdForm";
26
27    /**
28     * @return string
29     */
30    public static function getResendPasswordParagraphWithLinkToFormPage(): string
31    {
32        /**
33         * Resend pwd
34         */
35        $resendPwdHtml = "";
36        if (actionOK('resendpwd')) {
37            $resendPwLink = (new Resendpwd())->asHtmlLink('', false);
38            global $lang;
39            $resentText = $lang['pwdforget'];
40            $resendPwdHtml = <<<EOF
41<p class="resendpwd">$resentText : $resendPwLink</p>
42EOF;
43        }
44        return $resendPwdHtml;
45    }
46
47    private static function updateNewFormResend(Form &$form)
48    {
49        /**
50         * The Login page is an admin page created via buffer
51         * We print before the forms
52         * to avoid a FOUC
53         */
54        print IdentityFormsHelper::getHtmlStyleTag(self::CANONICAL);
55
56
57        $form->addClass(Identity::FORM_IDENTITY_CLASS . " " . self::FORM_RESEND_PWD_CLASS);
58        /**
59         * Heading
60         */
61        $headerHTML = IdentityFormsHelper::getHeaderHTML($form, self::FORM_RESEND_PWD_CLASS);
62        if ($headerHTML != "") {
63            $form->addHTML($headerHTML, 1);
64        }
65
66        IdentityFormsHelper::deleteFieldSetAndBrFromForm($form);
67        IdentityFormsHelper::toBoostrapInputElements($form, self::FORM_RESEND_PWD_CLASS);
68        IdentityFormsHelper::toBootStrapSubmitButton($form);
69
70    }
71
72
73    private static function updateDokuFormResend(Doku_Form &$form)
74    {
75        /**
76         * The Login page is created via buffer
77         * We print before the forms
78         * to avoid a FOUC
79         */
80        print IdentityFormsHelper::getHtmlStyleTag(self::CANONICAL);
81
82
83        /**
84         * @var Doku_Form $form
85         */
86        $class = &$form->params["class"];
87        IdentityFormsHelper::addIdentityClass($class, self::FORM_RESEND_PWD_CLASS);
88        $newFormContent = [];
89
90
91        /**
92         * Header (Logo / Title)
93         */
94        $newFormContent[] = IdentityFormsHelper::getHeaderHTML($form, self::FORM_RESEND_PWD_CLASS);
95
96        /**
97         * Form Attributes
98         *
99         */
100        foreach ($form->_content as $field) {
101            if (!is_array($field)) {
102                continue;
103            }
104            $fieldName = $field["name"];
105            if ($fieldName == null) {
106                // this is not an input field
107                if ($field["type"] == "submit") {
108                    /**
109                     * This is important to keep the submit element intact
110                     * for forms integration such as captcha
111                     * The search the submit button to insert before it
112                     */
113                    $classes = "btn btn-primary btn-block";
114                    if (isset($field["class"])) {
115                        $field["class"] = $field["class"] . " " . $classes;
116                    } else {
117                        $field["class"] = $classes;
118                    }
119                    $newFormContent[] = $field;
120                }
121                continue;
122            }
123            switch ($fieldName) {
124                case "login":
125                    $loginText = $field["_text"];
126                    $loginValue = $field["value"];
127                    $loginHTML = <<<EOF
128<div class="form-floating">
129    <input type="text" id="inputUserName" class="form-control" placeholder="$loginText" required="required" autofocus="" name="u" value="$loginValue">
130    <label for="inputUserName">$loginText</label>
131</div>
132EOF;
133                    $newFormContent[] = $loginHTML;
134                    break;
135                default:
136                    LogUtility::msg("The register field name($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, \ComboStrap\Identity::CANONICAL);
137
138
139            }
140        }
141
142
143        /**
144         * Register and Login HTML paragraph
145         */
146        $registerHtml = action_plugin_combo_registration::getRegisterLinkAndParagraph();
147        if (!empty($registerHtml)) {
148            $newFormContent[] = $registerHtml;
149        }
150        $loginLinkToHtmlForm = action_plugin_combo_login::getLoginParagraphWithLinkToFormPage();
151        if (!empty($loginLinkToHtmlForm)) {
152            $newFormContent[] = $loginLinkToHtmlForm;
153        }
154
155        /**
156         * Update
157         */
158        $form->_content = $newFormContent;
159
160    }
161
162
163    function register(Doku_Event_Handler $controller)
164    {
165        /**
166         * To modify the form and add class
167         *
168         * Deprecated object passed by the event but still in use
169         * https://www.dokuwiki.org/devel:event:html_resendpwdform_output
170         */
171        $controller->register_hook('HTML_RESENDPWDFORM_OUTPUT', 'BEFORE', $this, 'handle_resendpwd_html', array());
172        /**
173         * New Event
174         * https://www.dokuwiki.org/devel:event:form_resendpwd_output
175         *
176         */
177        $controller->register_hook('FORM_RESENDPWD_OUTPUT', 'BEFORE', $this, 'handle_resendpwd_html', array());
178
179
180    }
181
182    function handle_resendpwd_html(&$event, $param)
183    {
184
185        $form = &$event->data;
186        $class = get_class($form);
187        switch ($class) {
188            case Doku_Form::class:
189                /**
190                 * Old one
191                 * @var Doku_Form $form
192                 */
193                self::updateDokuFormResend($form);
194                return;
195            case dokuwiki\Form\Form::class;
196                /**
197                 * New One
198                 * @var Form $form
199                 */
200                self::updateNewFormResend($form);
201                return;
202        }
203
204
205    }
206
207
208}
209
210