-
-
Notifications
You must be signed in to change notification settings - Fork 178
024 How to integrate the Create User Helper Method in your Components
Hi. I would like to give you a quick tutorial or demonstration on how to use the user creation method or function in your component. 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.
If you don't know where the helper class is let me quickly show you. If you go to your repository where you find your Joomla install, you would 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, we go to component, 00:00:55 expertdatabase You go to helpers, it's basically that file there, that is the helper class. If we open it we see that it's the abstract expertdatabasehelper. That helper class gets included into your component on every page. If you were to open the main back end file with that one(expertdatabase), which get loaded by Joomla first. You'll see that it actually includes that helper file in every page. 00:01:28 So that means everything inside of that class is available to you everywhere in your component. When we actually go back to our interface, we go to components and then expertdatabase. You would see that there is library and helpers. We have this switch ticked yes, 'add create user helper method', set to yes. That is the 00:02:11 switch that you need to trip, that component builder knows that you actually want the helper class that creates users to be added to the helper class of your front and back end of your 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 search for user. 00:02:44
Here is quite a few users, there it is create user. As you can see 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. You will see that it is set to 'new'. 00:03:26 Now new has in it a few variables is basically an array. You can go through this class and you'd see 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 then it basically uses that model to create a user. That's what that switch does, it adds this class to your component. You can then use it in your component. It doesn't automatically just sets everything else in place, you still required to do as a developer to add to correct code. I'll show you what I have done and how I am using it.
There is a admin view called experts. It's in this view that I would actually like the option for someone to create a user. Let me first demonstrate how it works. I would go to expertdatabase and click on experts. Let's click on new. So you would see there is a 00:04:55 field called expert which you can select users within a specific group. I will also explain some of that more later. But underneath there is this 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.
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.
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.
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.
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.
- Home
- Beta Testing
- Custom Code
- PHP Settings
- Demo Component
-
Tutorials
- Hello World JCB
- Intro JCB Guide
- JCB Installation Steps
- Planning Components
- Field Type Overview
- Basic Fields Creation
- Admin View Management
- Advanced Field Usage
- Admin Component Integration
- Component Setting Customization
- Scripting Components
- Component FTP Options
- Dynamic Get Method
- Site View DynamicGet
- Site View Templates
- Template Setup Guide
- Layout Configuration Steps
- Custom Admin Management
- Adding Site Views
- Custom Admin Integration
- MySQL Demo Tweaking
- Global JCB Settings
- Custom Time Field
- User Helper Integration
- Email Helper Usage
- Message Store Email
- List View Unescape
- Export Import Customization
- Overwrite Custom Fields
- List Field Filtering
- Automatic Code Import
- Manual Code Implementation
- Component Export Import
- Custom Admin Buttons
- Translation Management
- Site View Permissions
- Component SQL Updates
- Site Edit Configuration
- JCB Backup System
- Helper Structure Integration
- JCB v2.5 Upgrade
- Tab Setup Guide
- JCB v2.6 Release
- Extended HelloWorld
- Field Rule Validation
- Community Snippets Intro
- Snippet Forking Tutorial
- Pull Request Snippets
- Library Manager Area
- Excel-based Translation
- Dynamic Router Details
- Database Auto Updates
- Subform Quick Demo
- VDM Package Import
- Dynamic File Inclusion
- File Field Upload
- Drag-n-Drop Upload
- Quick HelloWorld JCB
- Non-database Fields
- Dashboard Customization
- Menu Prefix Toggle
- Community JCB Packages
- Collaborative JCB Workflow
- JCB Package Install
- JCB JAB18 Event
- Convenient New Fields
- Component Language Strings
- Library Functionality Anticipation
- Join Field Relations
- License Template Change
- Code Reusability
- Local Dev Environment
- Extended Field Types
- Joomla Custom Fields
- Custom Field Expansion
- Site View Listing
- Run Expansion Method
- Form Site View
- Field URL Update
- Additional Helper Methods
- Field Validation Rules
- New Placeholder Feature
- Component Config Params
- Per-field Default Values