Skip to content

024 How to integrate the Create User Helper Method in your Components

Amigo edited this page Aug 15, 2019 · 25 revisions

HOW TO INTEGRATE THE CREATE USER HELPER METHOD IN YOUR COMPONENTS

00:00:00 (Click on these time links to see Youtube video)

What is the User Creation function? It is a part of the code's helper function that is added to the Helper class.

Helper Class

00:00:32

If the repository is opened, find Joomla Install and go to Administrator. There is a Helper class for the front end of every component as well as for the back end. In a back end in component, there is 'com_expertdatabase'. and under it 'expertdatabase.php'. 00:00:55 Go to Helpers. This file is the Helper class. (See video.) If it is opened the abstract 'expertdatabaseHelper' may be seen. This Helper class gets included in the component on every page. If the main back end file with 'expertdatabase' is opened it gets loaded by Joomla first. It includes the Helper file on every page. 00:01:28 Everything in the class is available everywhere in the component. Return to the interface, Component Builder Dashboard, and go to components and 'expertdatabase'. Library & Helpers be seen there. The switch 'add create user helper method', is in the 'yes' position. 00:02:11 It is the switch that should be used by which Component Builder is commanded that the Helper class, creating Users, should be added to the Helper class of the front and back end of the component. 00:02:44 If the area is closed and you return to the code and a simple search for User is done, a few users can be seen, as can 'Create User'.

Create User

00:03:00

This is a public static function. It may be called with the class name 'expertdatabasehelper', 'colon', 'colon' and the name of the function: Create User. It is set to '$new'. 00:03:26 '$new' has a few variables in it; it is an array. It may be noticed that it has a password in this class: 'password2'. It has a user name, a name, and an e-mail. This class then creates the user. It calls in the Joomla component users. 00:04:00 It calls in its registration model and uses the model to create a user. That's what the switch does, adding this class to the component. It may be used in the component. It doesn't just set everything else in place, the developer is still required to add the correct code since everything else is not automatically set in place.

Expert Field

00:04:30

There is an Admin view called 'Experts'. If there is a need in this view, for example, the option for someone to create a User, this demonstrates how it works. Go to 'expertdatabase', click on 'Expert' and on 'New'. 00:04:55 A field called 'Expert' may be seen in which you can select users within a specific group. Underneath is the button Create User. It has a pop-up where a user name may be entered. For instance, 'Testing123' may be used for the name as well as the username, and 'Testing123 @vdm.io' as the e-mail and the password. 00:05:26 If 'Create' is clicked, it creates and sets it up, sending out the email. Once it's done it informs you that the user was created successfully and the login details had been e-mailed to the user. 00:06:05 Then it indicates that it is ready to be selected and 'testing123', which has been created from the user list in the above field, may now be selected. Click on the blue user icon, type in 'testing123' and click 'Search'. 'Enter' may be clicked as the user had now been created. A strange conversion may be noticed. It converted the username's numbers to letters. 00:06:40 This application doesn't allow numbers in the user name, but this integrated user had been connected to an 'Expert'. If you were to click there again, it may still be selected; but once selected, you can not create a new 'Expert' and select the user again. So one profile connects to one user to set up an 'Expert'. 00:07:24

A lot of JavaScript may be noticed on this page. Here this job tracking extension has been written. (See video.) If for example, a new job order has to be created, it can indicate whether it is a new client or an existing one. If it is an existing client, select from the list. 00:08:04 If it's new, click 'A New Client'. All the details of the user can be entered and when 'save' is clicked, it creates the user, stores the data in the user table, but not in the job order table, and deletes all these values. (The job order table never stores any user values in the relation of this job order). If this closed, open a 'New client'. The same values can be seen. 00:08:44 This is another way to integrate user integration.

Make Use Of The Ajax Concept

00:09:30

In previous tutorials, it had been explained how to make use of the Ajax concept within Component Builder. Here is a quick overview for those of you who would like to understand what happens at code level. How does the code that had been used for an Ajax look? 00:09:26

Return to the Expert view.

Within every component that makes use of Ajax, a model and a controller are created. The controller is named 'Ajax.json.php' and the model 'Ajax'. 00:10:04 Open the model and the controller. As may be seen the controller gives the register commands within the construct method, adds the tasks to a switch, and makes an attempt to run the task. 00:10:31 In the model it gets the model Ajax, then runs the method that corresponds to the task.

It uses the Joomla's default filtering method so that it gets values that have been posted and validates if those values are actually there. The values are then transferred to the model. 00:11:05 That is looking at it from a code perspective. Various methods are displayed in the model and some of them are protected and some of them are public. "Getcreateuserfields' are usually the ones that are called from the controller once the Ajax receives the verification level and is verified. Here is the 'createUser' class. 00:11:31 Check this user. (It is custom script; Component Builder does not write this.) Return to the user interface, and scroll down to 'Create user fields where 'Create user' is found. What has been written here gets placed into the model. 00:12:07 That is the Ajax method. In the Ajax input specific tasks are notated; the task name, specific variables, and the method name. Method Names may be different from the Task Names. Select what should be validated. (Select in the 'Select a Filter' area.) Select whether it should be an integer, float, etc. If there's no value in the 'Default' area get the default value. This is where the controller's information is set up. 00:12:39 In PHP Custom Method the custom script may be done. Create user gets data, determines from what view it is, and validates whether this current login user has the permission to the Experts area. Then it converts the data, because it's a JSON set of datasets transferred, checks whether it's an array, and does some other checks.

Global Helper Class

00:13:17

Here is the Global Helper class and methods being used. (See video.) A number of Custom Methods available in that class may be seen. It has been added as freebies and should not necessarily always be used. If anyone can improve on these methods, please make a pull request so that changes may be done. Once the required data had been collected, and the same 'method' received, a 'Create User' is done. (See video.) 00:13:55 Get a result('$returned'). If it's not the correct result it should be dealt with accordingly. If it is the correct user it may be added to the user group. Create User does not do this. There is a user group set up in the component. 00:14:17 The user group needs to be set into the correct user group. The return message will indicate if it has been an error or success. The error message can be seen lower down. 00:14:45

Get User Field

00:14:53

When the page loads initially 'getUser' is there. The ID and the data are collected, packed into some HTML, and sent off. This is a PHP site where 'Create user' is done. 00:15:16 There is an Ajax site in Javascript. As the page loads in the JavaScript, get 'User' with the 'User ID', the User ID from the field and the data is loaded. It shows the data being loaded which can be edited, except here(Username). 00:15:51 Numbers can not be added in the username but this may be done. (See video.) It will update it and indicate that it is updated. If there is an error with the email already used it will also show an error. This is how JavaScript is set up. Javascript has been added to the View file. Here 'getUser' and the function is displayed. 00:16:24 Usually two functions are used in this case; one on standby and eventually setting up the data into the view. This is how the call structure would usually look in index.php?option=your components name, the task going to the Ajax controller. This 'getUser' is the task. 00:16:54 Here can be stipulated in what format you would like the result set returned in. If the result set has to be returned 'raw', which is sometimes the case, there is a 'raw' variable. 00:17:23 The 'raw' variable may be set to true; it will give the results set without braces around them, a callback, etc. When calls are made from JavaScript, it usually wants the call back to be added. If calls are made from other JavaScript libraries, you might want the braces around it. But if it is wanted 'raw' which also is the case, add the variable 'raw' to the URL = true and then end up passing it back with 'json_encode'.

NB.The format should remain 'json'. 00:17:56

Use Tokens

00:18:01

Always use a token. Have the token on the page. This token is necessary to prevent cross-site scripting. It is recommended to add the token to the page through PHP. 00:18:28 If work is done in a back end in the admin area, it is not necessary to add the token as it gets added to the page dynamically since the JavaScript areas had been parsed. If it shows 'token' then 'token' has to be there and the token is added. Whereas in the front end, 'token' should be added manually. 00:13:17 For various reasons it has not been automated in the front-end so that the variable name can be changed as well. 00:19:03 Whereas now it is just token, its back end is more secure. Always add a token to the request and check whether the token is there. If it is not, it would not make a request at all. That is how a user is created and how the user concept is integrated into this component.

If you are not fully equipped to do programming in JavaScript, jQuery or in PHP, this might not completely resolve your questions. This has not been mean to be a user-friendly component as such but a powerful tool to use instead.

00:19:41 This 'Class' had been added by which a user can be created. 00:20:20 If my implementation needs some improvement, Component Builder may be forked on GitHub and copied to your repository. The necessary changes can be done to it before pushing it back again. 00:20:47 The place in Component Builder where this 'Class' is constructed is within the compiler's files. Compiler has quite a few files and a search has to be done. Go to Component Builder, to Helper, and use the compiler. Here are all of its files. A search can be done in there for the function 'Create user'. 00:21:09 As it is dynamically added, it is in that area where the adaption and the changes must be done. It can't be edited right in the script, it must be edited in the function where it is added. Search those functions. 00:21:38 That's how users are created and how they are connected to components.

Clone this wiki locally