Skip to content

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

Amigo edited this page Jun 28, 2019 · 25 revisions

HOW TO INTEGRATE THE CREATE USER HELPER METHOD IN YOUR COMPONENTS

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

Helper Class

If the repository is opened you will find Joomla Install, go to administrator. There is a Helper class for the front end of every component as well as for the back end. Looking in a back end, in component there is 'com_expertdatabase'. and under it 'expertdatabase.php'. 00:00:55 Go to Helpers, that file(See video), is the Helper class. If it is opened the abstract 'expertdatabaseHelper' may be seen. That Helper class gets included into the component on every page. If the main back end file with that one 'expertdatabase' is opened, which get loaded by Joomla first. It includes that Helper file in every page. 00:01:28 It means that everything in that class is available everywhere in the component. Returning to the interface, Component Builder Dashboard, and then go to components and to 'expertdatabase'. There can Library & Helpers be seen . The switch 'add create user helper method', is in the 'yes' position. 00:02:11 That is the switch that should be used, by which Component Builder is commanded that the Helper class that creates Users should be added to the Helper class of the front and back end of the component.

I'm going to close out here. I am going to go back to the code. Then I am going to do a simple . 00:02:44

If this area is closed and you return to the code and a simple search for user is done, quite a few users can be seen as well as 'Create User'.

Create User

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

Expert Field

There is a admin view called 'Experts'. For example: If there is a need in this view like the option for someone to create a User. To demonstrate how it works. Go to 'expertdatabase' and click on 'Expert' and click 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 e-mail and as the password. 00:05:26 If 'Create' is clicked, it creates it, setting 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 and type in that 'testing123' and click 'Search'. As the user is now created, 'enter' may be clicked . A very strange conversion may be noticed, it converted the username's numbers to letters. 00:06:40 This application doesn't allow any numbers in the user name, but anyhow this integrated user has been connected to an 'Expert'. If you were to click there again, it may still be selected, but once it is selected, you can not create a new 'Expert' and select that user again. Thus it is one profile connecting to one user to set up a 'Expert'. 00:07:24

A lot of JavaScript may be noticed on this page and here(See video) has this job tracking extension been written. If for example a new job order needs to be created, then it can be indicated whether it is a new client or a existing one. If it is an existing, simply 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 this whole user integration.

Make Use Of The Ajax Concept

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 that would like to understand what happens at code level. How does the code look which had been used for a Ajax? 00:09:26

Return to the Expert view.

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

It uses the Joomla's default filtering method in the way that it gets values that are being posted. And then value dates that those values actually are there. Then sends the values over to the model. 00:11:05 Now that's looking at it from a code side. If you look at the model it simply shows you various methods. Some of them are protected, and some of them are public. Those(getcreateuserfields) are usually the ones that gets called from the controller once the Ajax gets the verification level and it's verified. Here is the createuser class. 00:11:31 We first check whether this user, (now this is custom script I mean this I wrote custom, component build does not write this) so basically when you go back to the user interface, you will see that if I scroll down here, create user fields, then there's create user. You see that I wrote this right here, and that basically gets placed into the model. 00:12:07 So that's the Ajax method. In that Ajax input I annotate the specific tasks, the task name, and specific variables, and again the method name. You could have different method names from task names. What should be validated, whether it's integer, float or whatever. And if there's no value, what is the value by default. So this is where you set up there basically the controllers information. 00:12:39 And then here your customisely write your script. So basically create user gets data and seems from what view is it, then validates with this current login user has the permission to the experts area. Then it converts the data because it's a json set of datasets that I'm sending across. Checks whether it's an array, does some other checks.

Global Helper Class

So you can see I'm still using that 00:13:17 global helper class and methods. Here are number of custom methods obviously that is available in that class. Go through it. We just add it in there as freebies and you don't want to always see the need of them but I'm using them often. Even if you have improvements on those methods, please make a pull request and we'll make those changes. Then over here(see video) once we've become very sure that we've got enough data on the right data. We basically get that same method and we need to create user. 00:13:55 We get result. And if it's not the correct result we deal with it accordingly. And if it is the correct result we add the user to the user group. That's something that create user does not do. We have a user group setup 00:14:17 basically in the component. We get that user group, set into the correct user group. This is a return message, if it's an error or it's a success. So down here(see video) is the error message. If it's successful this is what we do if it's unsuccessful this is what we do. 00:14:45 That's just a quick look, I mean obviously we can pause this video, and look at what I have done.

Get User Field

And then here is getUser when the page loads initially. We get the id, we get the data. Packs it into some html and sends it off. This is the php site obviously where we do this create user. Then there is a ajax 00:15:16 site in Javascript. In the JavaScript when the page loads, you would get user with the user id, we get the user id from the field. And then it loads that data that you saw. Basically shows you that is the data it loads. And that data can be edited except over here(username). 00:15:51 You cannot add numbers in the username but you can do that(see video) and it will update it as you see and will say that it is updated. Obviously if there is a error with the email already used or something it will also give you an error. And this is all part of how we basically have setup the JavaScript. I've just added Javascript to the view file. Here is getuser on the server site, and there is the function. 00:16:24 I have two functions usually one that deals with waiting and then eventually setting up the data into the view. Here is the one making the call. This is how the call structure would usually look in index.php?option=your components name, and the task which must go to the Ajax controller. And then this(getuser) is the task. 00:16:54 And here you can stipulate what format you would like the result set returned in. If you want the result set returned raw, which in is sometimes the case, there is if you look here, there is a raw variable. You can just set the raw variable to true. Then it will give you the results set just like that without braces around them, 00:17:23 or a call back or anything. You see when you make calls from JavaScript, it usually wants this call back to be added. If you make calls from other JavaScript libraries, you might just want the braces around it. But if you want it raw which also is often the case you just add the variable raw to the URL = true and then end up passing it back to you with this. So that's a little heads up on Ajax function. 00:17:56 The format must stay json, that's important.

Use Tokens

Always use a token. Have the token on the page. This token is necessary to prevent cross site scripting of course. I usually add the token to the page, through php. 00:18:28 If you are working in a back end in the admin area, you don't need to have the token. It gets added to the page dynamically. Since we parsed the JavaScript areas. If we see token we actually know token is to be there and we add the token for you. Where as in the front end you must actually literally add the token. There was just too many reasons for us not to do the automation in the front end. You could literally change the variable name as well 00:19:03 to something of your choice. Where as now it's just token, it's back end it's more secure. You need to always add a token to the request and check whether the token is there, if it's not that it wouldn't make a request at all. That's simply how we create a user and integrate the user concept within this component.

I hope that was helpful. I realize if you not that fluent in programming in JavaScript or in jQuery 00:19:41 as well as in php. This might not fully resolved your questions. Like I said where the beginning of these tutorials I wasn't planning on making a component that is very easy to use, but rather a component that is very powerful to use. And leaving it up to you to decide how to implement this user integration is really the most powerful way of doing it. At least we are adding the the class which you can create the user. Even there you can go look at that class. 00:20:20 If you feel my implementation needs some improvement, you can either shoot me a mail or you could fork this component builder on GitHub. Pull it down to your own repository. Make the changes push it back. The place inside of component builder where this class is constructed, 00:20:47 is within the compilers files. Now the compiler have quite a few files and you need to do a search. Go to component builder, go to helper, and then use the compiler. Here is all its files. Do a search in here 00:21:09 for the function create user. Just search for that and I'm sure you're fine it. It is in that area where you need to make the adaptation and the changes because it's dynamically added. It can't be edited right in the script, it needs to be edit in the function where we add it. Just go search those functions. 00:21:38 That's how you create users and connect them to your components.

Clone this wiki locally