Xem mẫu

Chapter 7 When we perform any modifications to the user`s session, unless we save the changes, the modifications will last only until the session expires. User parameters are not used as a temporary store. To store temporary data we should use the session and the user state; we will see both in the next section. If we store temporary data in user parameters, we run the risk of saving the data accidently to the user`s database record. A common design issue is the extension of the user beyond their predefined attributes. There are three common ways of dealing with this: • Add additional fields to the #__users table. • Create a new table that maintains a one-to-one relationship with the #__users table. • Use the user`s parameters to store additional data. The first option can cause some major problems. If several extensions choose this method, there is a chance that there will be a naming conflict between fields. The second option is a good choice if the extra data is searchable, ordered, or used to modify results returned from the queries. To maintain the table successfully, we would have to create a plugin to deal with the events onAfterStoreUser and onAfterDeleteUser, explained in Chapter 6. The final option is ideal if the extra data is not subject to searches, ordered, or used to restrict query results. We might implement these parameters in one of the three ways: • Manually edit the parameters using the setParam() method. This is suitable if there are not many parameters or the user never modifies the parameters using a form. • Use JParameter as the basis to create a form in which users can modify the parameters. • Allow the user to modify the parameters, via the user`s component. To do this, we need to modify the users.xml file (for more information about editing XML, see Chapter 10). Before we begin, there is something we need to understand. A JUser object essentially has two sets of parameters, a RAW parameters string or array (params) and a JParameter object (_params). Both of these are loaded from the database when the user`s session starts. If we modify either of them, the changes will be present only until the user`s session ends. If we want to save the parameters to the database, as is normally the case, we can use the save()method. This will update the parameters based on the RAW parameters alone. [ 179 ] Extension Design When we use the setParam() method only the JParameter object is modified. It is because of this that we must update the RAW params attribute before saving. We must take extra care when saving changes to the user`s parameters. Poor handling can result in loss of data. This example demonstrates how we can set the user`s foo parameter and save the changes to the database: // get the user and add the foo parameter $user =& JFactory::getUser(); $user->setParam(`foo`, `bar`); // update the raw user parameters $params =& $user->getParameters(); $user->set(`params`, $params->toString()); // save the changes to the database if (!$user->save()) { JError::raiseError(`SOME_ERROR`, JText::_(`Failed to save user`)); } Next we will explore parameters that a user can update via a form. We will begin by creating an XML file that defines the extra parameters. We will see the parameters in detail in the Appendix. The following XML defines two text parameters, myparameter and myotherparameter: We can create form elements using this XML and the user`s JParameter object. We can get a reference to the JParameter object using the getParameters() method: // get the user $user =& JFactory::getUser(); // get the user`s parameters object $params =& $user->getParameters(); [ 180 ] Chapter 7 Once we have the parameters object, we can load the XML file and render the form elements using the render() method, as this example demonstrates: $params->loadSetupFile($pathToXML_File); echo $params->render(`myparams`); A form field is created for each parameter, all of which are treated as a form array. The parameter that we provide to the render() method is used to name the form array. If we do not provide the parameter, the default name `params` is used. Our example will create two text inputs called myparams[myparameter] and myparams[myotherparameter]. This is a screenshot of how these parameters would appear: Alternatively we could use the JParameter renderToArray() method that returns an array of arrays that define the different form elements. Creating a form to deal with extra parameters is only the beginning; we need to process submitted forms. In this example, we retrieve the parameters from the POST array (assuming that the form is submitted using the POST method), add them to the user`s existing parameters, rebind them to the user object, and save the changes: // get the user object and the post array. $user =& JFactory::getUser(); $post = JRequest::get(`post`); // get the existing parameters $params = $user->getParameters(); // add the parameters from the form submission $params->bind($post[`myparams`]); // update and save the user $user->set(`params`, $params->toString()); $user->save(); The last option we will explore is modifying the users.xml file. To do this, we will utilize the JSimpleXML parser. For a complete description of the JSimpleXML parser, please refer to Chapter 10. [ 181 ] Extension Design The first thing we need to do is get hold of the XML file and parse the contents: // get a parser $parser =& JFactory::getXMLParser(`Simple`); // define the path to the XML file $pathToXML_File = JPATH_ADMINISTRATOR.DS.`components`.DS.`com_users`. DS.`users.xml`; // parse the XML $parser->loadFile($pathToXML_File); In order to add new param tags to the XML, we need to navigate to the params tag: // get the root tag (install) $document =& $parser->document; // get the params tag $params =& $document->params[0]; We can now start adding to the XML using the addChild() method to add child param tags, and the addAttribute() method to set the necessary param tag attributes. This example adds the parameters myparameter and myotherparameter, both of which we defined in the previous example: // Add myparameter $myparameter =& $params->addChild(`param`); // modify the myparameter attributes $myparameter->addAttribute(`name`, `myparameter`); $myparameter->addAttribute(`type`, `text`); $myparameter->addAttribute(`label`, `My Parameter`); $myparameter->addAttribute(`description`, `An example user parameter`); // Add myotherparameter $myotherparameter =& $params->addChild(`param`); // modify the myotherparameter attributes $myotherparameter->addAttribute(`name`, `myotherparameter`); $myotherparameter->addAttribute(`type`, `text`); $myotherparameter->addAttribute(`label`, `My Other Parameter`); $myotherparameter->addAttribute(`description`, `An example user parameter`); [ 182 ] Chapter 7 Now that we have made the changes to the XML file, we need to save those changes to the users.xml file. We can do this using the JFile class: // create XML string $xmlString = ``."\n"; $xmlString .= $document->toString(); // get the JFile class jimport(`joomla.filesystem.file`); // save the changes if (!JFile::write($pathToXML_File, $xmlString)) { // handle failed file save } These alterations will enable users to modify myparameter and myotherparameter, when they use the user`s component to modify their details. This screenshot depicts the resultant form with the changes: If one were to employ this technique, the best place to do so would probably be in a component installation file. It is also important to consider making a backup of the existing file, in case of any unexpected difficulties. Modifying this file could also lead to problems if the file is ever updated, for example as part of an upgrade. However, it does mean that all of the user`s details are editable from one central point. [ 183 ] ... - tailieumienphi.vn
nguon tai.lieu . vn