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. So you would basically call it 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, aswell 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 to 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 'Experts' 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. <<<<<<

If you click on it, it basically has a pop-up where in you enter a user name. I am just going to say 'Testing123' as the username. 00:05:26 And as the password, there we go, got the user name. So everything is testing123. If I click create, it basically has this nice effect where it is busy creating it, setting it up, sending out the email, doing all of that for you. And once it's done it lets you know that the user was created successfully and the login details was e-mailed to the user. Then it says here ready to select. 00:06:05 You can now select testing123 that you just created from the user list in the above field. Simply click on the blue user icon. Click here and you can type in that testing123, click search. There is the user created and we click enter. You see that it basically used a very strange conversion here. Basically converting the username away from the numbers. Now you can you can fix that by clicking edit. 00:06:40 Then changing it to 123 and say save the update. Clearly my little application doesn't want to allow you to use any numbers in the user name. That's not the point. The point is that we have this integrated user now that is connected to an expert. If you were to click there again, you can still select it, but once you select it, you can't create a new expert and select that user again. So it's basically one profile connecting to one user to set up a expert. 00:07:24 So that's the feature I'm using it for there is different ways that I have implemented this.

Let me show you another one. Obviously you realise I'm using a lot of JavaScript on this page. I will show you some of that JavaScript but for the most part you need to write that yourself. For example over here I wrote this job tracking extension. Let's say we want to create a new job order, then I can say whether it's a new client or a existing. If it is an existing I simply select from this list. 00:08:04 If it's new I click here(A new Client) I can enter all the details of the user and when I click save, it creates the user, stores the data in the user table, not in the job order table. Deletes all these values, that the job order table never stores any user values in the relation of this job order of course. And so that's another way that I have implemented. If we were to close this out, open a client, a new one. You would see it's basically it's the same values. 00:08:44 So that's another way that we integrated this whole user integration. So there are really many different ways to this. The way I have done it I've just listen to my clients expectation. What I really would like to see and how they would like it to work, then simply make it happen that way. I personally prefer the Ajax way much more. It just seems to me easier.

Make Use Of The Ajax Concept

So let me illustrate some of or let me show you some of the code that I used to do that. 00:09:26 So we are back in the experts view. As I've done before I previously, in other tutorials explained how to make use of the Ajax concept within Component Builder. Now for those of you that like to understand what's going on let me give you a quick runover at the code level. 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 00:10:04 Ajax. Let me open the model, let me also open the controller. As you can see the controller basic fires off the register tasks within the construct method. And then it adds the tasks into a switch, and tries to run the task. Also in 00:10:31 the model it gets the model Ajax, and then runs the method that basically corresponds to the task. And so that is how it runs.

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