Partiamo dal presupposto che esistono diversi metodi per modificare il form di registazione di Joomla!, quello che viene utilizzato è Chronoforms che è gratuito, oppure esistono RSForms oppure ancora Breezing Forms che sono a pagamento, tutti offrono un’interfaccia grafica più o meno semplice per poter realizzare il modulo di registrazione personalizzato e sostituire quello nativo per la registrazione utenti.
In questo articolo invece non utilizzo nessun tipo di estensione ma vado a programmare un plugin che aggiunge dei campi personalizzati al form di registrazione di Joomla!
Come aggiungere campi al modulo registrazione utenti di Joomla! sviluppando un plugin
Bene la prima cosa da fare è creare una cartella che chiamiamo profile5 , al suo interno creiamo la cartella profiles .
Ora procediamo in questo ordine
profile5.xml –> da copiare nella cartella profile5
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
<?xml version="1.0" encoding="utf-8"?> <!-- $Id: --> <extension version="3.5" type="plugin" group="user"> <name>plg_user_profile5</name> <author>Alessio Angeloro</author> <creationDate>Giugno 2016</creationDate> <copyright>(C) All rights reserved.</copyright> <license>GNU General Public License version 2 or later</license> <authorEmail>mail@dominio.com</authorEmail> <authorUrl>https://alessioangeloro.it</authorUrl> <version>1.0.0</version> <description><![CDATA[ <strong>Joomla custom registration form.</strong></br> This plugin has been developed by Alessio Angeloro. For more information please visit my blog site <a target="_blank" rel="nofollow" href="https://alessioangeloro.it">Alessio Angeloro Web Developer</a> , and have a fun :-P ]]></description> <files> <filename plugin="profile5">profile5.php</filename> <filename>index.html</filename> <folder>profiles</folder> </files> <config> <fields name="params"> <fieldset name="basic"> <field name="choose_fields" type="spacer" label="Choose which kind the fields to display in the Joomla! registration form" /> <field name="state_field" type="list" label="State"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="company_name_field" type="list" label="Company name"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="website_field" type="list" label="Web site"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="surname_field" type="list" label="Surname"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="phone_field" type="list" label="Phone"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="address_field" type="list" label="Address"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="zipcode_field" type="list" label="ZIP code"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="city_field" type="list" label="City"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> <field name="policy_field" type="list" label="Policy"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> </fieldset> </fields> </config> </extension> |
<![CDATA[….]]> = questo codice mi permette di inserire codice HTML puro all’interno del file xml
|
1 2 3 4 5 |
<field name="state_field" type="list" label="State"> <option value="2">JOPTION_REQUIRED</option> <option value="1">JOPTION_OPTIONAL</option> <option value="0">JDISABLED</option> </field> |
Analizziamo ora questa parte di codice.
In buona sostanza oltre ad essere il file per l’installazione del plugin, ci permette anche di avere il controllo a backend su ogni campo (field) creato, infatti crea una select che permette di impostare ogni singolo campo su obbligatorio, facoltativo e disabilitato.
name=”state_field” = questo valore è importante che sia diverso per ogni campo perchè lo utilizzeremo per renderizzare il form a frontend
JOPTION_REQUIRED, JOPTION… = questi tre valori vengono valorizzati dal file di lingua di Joomla e corrispondo appunto a : obbligatorio, facoltativo e disabilitato
Ecco la gestione del plugin a backend
profile5.php –> da copiare nella cartella profile5
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
<?php /** * @version * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ defined('JPATH_BASE') or die; /** * An example custom profile plugin. * * @package Joomla.Plugins * @subpackage user.profile * @version 1.6 */ class plgUserProfile5 extends JPlugin { /** * @param string The context for the data * @param int The user id * @param object * @return boolean * @since 1.6 */ function onContentPrepareData($context, $data) { // Check we are manipulating a valid form. if (!in_array($context, array('com_users.profile','com_users.registration','com_users.user','com_admin.profile'))){ return true; } $userId = isset($data->id) ? $data->id : 0; // Load the profile data from the database. $db = JFactory::getDbo(); $db->setQuery( 'SELECT profile_key, profile_value FROM #__user_profiles' . ' WHERE user_id = '.(int) $userId . ' AND profile_key LIKE \'profile5.%\'' . ' ORDER BY ordering' ); $results = $db->loadRowList(); // Check for a database error. if ($db->getErrorNum()) { $this->_subject->setError($db->getErrorMsg()); return false; } // Merge the profile data. $data->profile5 = array(); foreach ($results as $v) { $k = str_replace('profile5.', '', $v[0]); $data->profile5[$k] = json_decode($v[1], true); } return true; } /** * @param JForm The form to be altered. * @param array The associated data for the form. * @return boolean * @since 1.6 */ function onContentPrepareForm($form, $data) { // Load user_profile plugin language $lang = JFactory::getLanguage(); $lang->load('plg_user_profile5', JPATH_ADMINISTRATOR); if (!($form instanceof JForm)) { $this->_subject->setError('JERROR_NOT_A_FORM'); return false; } // Check we are manipulating a valid form. if (!in_array($form->getName(), array('com_users.profile', 'com_users.registration','com_users.user','com_admin.profile'))) { return true; } if ($form->getName()=='com_users.profile') { // Add the profile fields to the form. JForm::addFormPath(dirname(__FILE__).'/profiles'); $form->loadFile('profile', false); // Toggle whether the company_name field is required. if ($this->params->get('website_field', 1) > 0) { $form->setFieldAttribute('website', 'required', $this->params->get('website') == 2, 'profile5'); } else { $form->removeField('website', 'profile5'); } } //In this example, we treat the frontend registration and the back end user create or edit as the same. elseif ($form->getName()=='com_users.registration' || $form->getName()=='com_users.user' ) { // Add the registration fields to the form. JForm::addFormPath(dirname(__FILE__).'/profiles'); $form->loadFile('profile', false); // Toggle whether the company_name field is required. if ($this->params->get('company_name_field', 1) > 0) { $form->setFieldAttribute('company_name', 'required', $this->params->get('company_name_field') == 2, 'profile5'); } else { $form->removeField('company_name', 'profile5'); } if ($this->params->get('website_field', 1) > 0) { $form->setFieldAttribute('website', 'required', $this->params->get('website_field') == 2, 'profile5'); } else { $form->removeField('website', 'profile5'); } if ($this->params->get('state_field', 1) > 0) { $form->setFieldAttribute('state', 'required', $this->params->get('state_field') == 2, 'profile5'); } else { $form->removeField('state', 'profile5'); } if ($this->params->get('surname_field', 1) > 0) { $form->setFieldAttribute('surname', 'required', $this->params->get('surname_field') == 2, 'profile5'); } else { $form->removeField('surname', 'profile5'); } if ($this->params->get('phone_field', 1) > 0) { $form->setFieldAttribute('phone', 'required', $this->params->get('phone_field') == 2, 'profile5'); } else { $form->removeField('phone', 'profile5'); } if ($this->params->get('address_field', 1) > 0) { $form->setFieldAttribute('address', 'required', $this->params->get('address_field') == 2, 'profile5'); } else { $form->removeField('address', 'profile5'); } if ($this->params->get('zipcode_field', 1) > 0) { $form->setFieldAttribute('zipcode', 'required', $this->params->get('zipcode_field') == 2, 'profile5'); } else { $form->removeField('zipcode', 'profile5'); } if ($this->params->get('city_field', 1) > 0) { $form->setFieldAttribute('city', 'required', $this->params->get('city_field') == 2, 'profile5'); } else { $form->removeField('city', 'profile5'); } if ($this->params->get('policy_field', 1) > 0) { $form->setFieldAttribute('policy', 'required', $this->params->get('policy_field') == 2, 'profile5'); } else { $form->removeField('policy', 'profile5'); } } } function onUserAfterSave($data, $isNew, $result, $error) { $userId = JArrayHelper::getValue($data, 'id', 0, 'int'); if ($userId && $result && isset($data['profile5']) && (count($data['profile5']))) { try { $db = JFactory::getDbo(); $db->setQuery('DELETE FROM #__user_profiles WHERE user_id = '.$userId.' AND profile_key LIKE \'profile5.%\''); if (!$db->query()) { throw new Exception($db->getErrorMsg()); } $tuples = array(); $order = 1; foreach ($data['profile5'] as $k => $v) { $tuples[] = '('.$userId.', '.$db->quote('profile5.'.$k).', '.$db->quote(json_encode($v)).', '.$order++.')'; } $db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', ', $tuples)); if (!$db->query()) { throw new Exception($db->getErrorMsg()); } } catch (JException $e) { $this->_subject->setError($e->getMessage()); return false; } } return true; } /** * Remove all user profile information for the given user ID * * Method is called after user data is deleted from the database * * @param array $user Holds the user data * @param boolean $success True if user was succesfully stored in the database * @param string $msg Message */ function onUserAfterDelete($user, $success, $msg) { if (!$success) { return false; } $userId = JArrayHelper::getValue($user, 'id', 0, 'int'); if ($userId) { try { $db = JFactory::getDbo(); $db->setQuery( 'DELETE FROM #__user_profiles WHERE user_id = '.$userId . " AND profile_key LIKE 'profile5.%'" ); if (!$db->query()) { throw new Exception($db->getErrorMsg()); } } catch (JException $e) { $this->_subject->setError($e->getMessage()); return false; } } return true; } } |
come si può notare dal codice, gli id dei campi presenti nel file xml vengono richiamati nel file php
profile.xml –> da copiare nell cartella profile5\profiles
|
1 |
index.html –> da copiare nella cartella profile5
|
1 |
<body background="#ffffff"></body> |
L’elenco di file e cartelle corrette dovrebbe essere questo
Per sviluppare questo plugin ho studiato la documentazione ufficiale di Joomla.org Creating a profile plugin
