xref: /plugin/combo/action/registration.php (revision 85e82846b0a214bc35e62864fa49d9cad0723d0e)
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\Bootstrap;
11use ComboStrap\Identity;
12use ComboStrap\LogUtility;
13use ComboStrap\Snippet;
14use dokuwiki\Menu\Item\Register;
15
16if (!defined('DOKU_INC')) die();
17require_once(__DIR__ . '/../class/PluginUtility.php');
18
19/**
20 *
21 * Register forms depend on the following configuration
22 * https://www.dokuwiki.org/config:autopasswd
23 * If true, there is no password field
24 */
25class action_plugin_combo_registration extends DokuWiki_Action_Plugin
26{
27
28    const CANONICAL = Identity::CANONICAL;
29    const TAG = "register";
30    const FORM_REGISTER_CLASS = "form-".self::TAG;
31    const CONF_ENABLE_REGISTER_FORM = "enableRegistrationForm";
32
33
34    /**
35     * Return the register text and link paragraph
36     * @return string
37     */
38    public static function getRegisterLinkAndParagraph()
39    {
40
41
42        $registerHtml = "";
43        if (actionOK('register')) {
44
45            /**
46             * The register class does not allow
47             * registration if your are logged in (What ?)
48             * and send an exception
49             */
50            if (!Identity::isLoggedIn()) {
51                $registerLink = (new Register())->asHtmlLink('', false);
52                global $lang;
53                $tag = self::TAG;
54                $registerText = $lang['reghere'];
55                $registerHtml = <<<EOF
56<p class="$tag">$registerText : $registerLink</p>
57EOF;
58            }
59        }
60        return $registerHtml;
61    }
62
63
64    function register(Doku_Event_Handler $controller)
65    {
66        /**
67         * To modify the register form and add class
68         *
69         * Deprecated object passed by the event but still in use
70         * https://www.dokuwiki.org/devel:event:html_registerform_output
71         */
72        $controller->register_hook('HTML_REGISTERFORM_OUTPUT', 'BEFORE', $this, 'handle_register_page', array());
73
74        /**
75         * Event using the new object but not yet used
76         * https://www.dokuwiki.org/devel:event:form_register_output
77         */
78        // $controller->register_hook('FORM_REGISTER_OUTPUT', 'BEFORE', $this, 'handle_register', array());
79
80
81    }
82
83    function handle_register_page(&$event, $param)
84    {
85
86        /**
87         * The register page is created via buffer
88         * We print before the forms
89         * to avoid a FOUC
90         */
91        print Snippet::createCssSnippet("register")
92            ->getHtmlStyleTag();
93
94
95        /**
96         * @var Doku_Form $form
97         */
98        $form = &$event->data;
99        $class = &$form->params["class"];
100        if (isset($class)) {
101            $class = $class . " " . self::FORM_REGISTER_CLASS;
102        } else {
103            $class = self::FORM_REGISTER_CLASS;
104        }
105        $newFormContent = [];
106
107        /**
108         * Header (Logo / Title)
109         */
110        $newFormContent[] = Identity::getHeaderHTML($form, self::FORM_REGISTER_CLASS);
111
112
113        /**
114         * Form Attributes
115         * https://getbootstrap.com/docs/5.0/forms/layout/#horizontal-form
116         */
117        $rowClass = "row";
118        if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFourMajorVersion) {
119            $rowClass .= " form-group";
120        }
121        $firstColWeight = 5;
122        $secondColWeight = 12 - $firstColWeight;
123
124
125        /**
126         * Replace the field
127         *
128         * The password text localized by lang is shared
129         * between the password and the password check field
130         */
131        $passwordText = "Password";
132        foreach ($form->_content as $pos => $field) {
133            if (!is_array($field)) {
134                continue;
135            }
136            $fieldName = $field["name"];
137            if ($fieldName == null) {
138                // this is not an input field
139                if ($field["type"] == "submit") {
140                    /**
141                     * This is important to keep the submit element intact
142                     * for forms integration such as captcha
143                     * The search the submit button to insert before it
144                     */
145                    $classes = "btn btn-primary";
146                    if (isset($field["class"])) {
147                        $field["class"] = $field["class"] . " " . $classes;
148                    } else {
149                        $field["class"] = $classes;
150                    }
151                    $field["tabindex"] = "6";
152                    $newFormContent[] = $field;
153                }
154                continue;
155            }
156            switch ($fieldName) {
157                case "login":
158                    $loginText = $field["_text"];
159                    $loginValue = $field["value"];
160                    $loginHTML = <<<EOF
161<div class="$rowClass">
162    <label for="inputUserName" class="col-sm-$firstColWeight col-form-label">$loginText</label>
163    <div class="col-sm-$secondColWeight">
164      <input type="text" class="form-control" id="inputUserName" placeholder="Username" tabindex="1" name="login" value="$loginValue" required="required">
165    </div>
166</div>
167EOF;
168                    $newFormContent[] = $loginHTML;
169                    break;
170                case "pass":
171                    $passwordText = $field["_text"];
172                    $passwordHtml = <<<EOF
173<div class="$rowClass">
174    <label for="inputPassword" class="col-sm-$firstColWeight col-form-label">$passwordText</label>
175    <div class="col-sm-$secondColWeight">
176      <input type="password" class="form-control" id="inputPassword" placeholder="$passwordText" tabindex="2" name="pass" required="required">
177    </div>
178</div>
179EOF;
180                    $newFormContent[] = $passwordHtml;
181                    break;
182                case "passchk":
183                    $passwordCheckText = $field["_text"];
184                    $passwordCheckHtml = <<<EOF
185<div class="$rowClass">
186    <label for="inputPasswordCheck" class="col-sm-$firstColWeight col-form-label">$passwordCheckText</label>
187    <div class="col-sm-$secondColWeight">
188      <input type="password" class="form-control" id="inputPasswordCheck" placeholder="$passwordText" tabindex="3" name="passchk" required="required">
189    </div>
190</div>
191EOF;
192                    $newFormContent[] = $passwordCheckHtml;
193                    break;
194                case "fullname":
195                    $fullNameText = $field["_text"];
196                    $fullNameValue = $field["value"];
197                    $fullNameHtml = <<<EOF
198<div class="$rowClass">
199    <label for="inputRealName" class="col-sm-$firstColWeight col-form-label">$fullNameText</label>
200    <div class="col-sm-$secondColWeight">
201      <input type="text" class="form-control" id="inputRealName" placeholder="$fullNameText" tabindex="4" name="fullname" value="$fullNameValue" required="required">
202    </div>
203</div>
204EOF;
205                    $newFormContent[] = $fullNameHtml;
206                    break;
207                case "email":
208                    $emailText = $field["_text"];
209                    $emailValue = $field["value"];
210                    $emailHTML = <<<EOF
211<div class="$rowClass">
212    <label for="inputEmail" class="col-sm-$firstColWeight col-form-label">$emailText</label>
213    <div class="col-sm-$secondColWeight">
214      <input type="email" class="form-control" id="inputEmail" placeholder="name@example.com" tabindex="5" name="email" value="$emailValue" required="required">
215    </div>
216</div>
217EOF;
218                    $newFormContent[] = $emailHTML;
219                    break;
220                default:
221                    $tag = self::TAG;
222                    LogUtility::msg("The $tag field name ($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
223
224            }
225        }
226
227
228        /**
229         * Link in the form footer to login and resend
230         */
231        $loginLinkToHtmlForm = action_plugin_combo_login::getLoginParagraphWithLinkToFormPage();
232        if (!empty($loginHTML)) {
233            $newFormContent[] = $loginLinkToHtmlForm;
234        }
235        $resendHtml = action_plugin_combo_resend::getResendPasswordParagraphWithLinkToFormPage();
236        if (!empty($resendHtml)) {
237            $newFormContent[] = $resendHtml;
238        }
239
240        /**
241         * Update
242         */
243        $form->_content = $newFormContent;
244        return true;
245
246
247    }
248
249
250}
251
252