Updated 011 Component Scripts (markdown)

Amigo 2019-06-10 09:58:16 +02:00
parent 390f8768b3
commit 3047c61ae4

@ -1,16 +1,24 @@
# COMPONENT SCRIPTS
Hi now next we be looking at the scripts tab. Here is an area where you most probably add a lot of custom scripting. This area is focused on giving you area an interface to add custom scripting to your component in a global way. You can have custom scripting per admin back end view. [00:00:28](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m28s) You can also do that within the global scope with regards to many certain values. The one thing that is nice to have here is, you can add create user helper method to the component.
Hi now next we be looking at the scripts tab. Here is an area where you most probably add a lot of custom scripting. This area is focused on giving your area an interface to add custom scripting to your component in a global way. You can have custom scripting per admin back end view. [00:00:28](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m28s) You can also do that within the global scope with regards to many certain values. The one thing that is nice to have here is, you can add create user helper method to the component.
### Create User Helper Method
Now the user helper method is simply code. If you do not know how to write php this method will be of no use to you at all. [00:00:54](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m54s) If you do, then this method will come in very handy. Let me illustrate that to you. If you click this yes, I have a component in which I've already implemented this feature, and it's called the cost-benefit projection tool. If you click on companies, you can actually click 'new'. [00:01:19](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m19s) It has an area for you to create a company.
Now the 'user helper method' is simply code. If you do not know how to write PHP this method will be of no use to you at all. [00:00:54](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m54s) If you do, then this method will come in very handy. Let me illustrate that to you. If you click this 'yes', I have a component in which I've already implemented this feature, and it's called the 'cost-benefit projection tool'. If you click on companies, you can actually click 'new'. [00:01:19](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m19s) It has an area for you to create a company.
### Example Helper Method(Create User)
Ideally you would connect that company to a user in the system, if you don't add that company to a user. Because a user can have multiple companies. That's why if you click on this tab(select user), you could select the user. But if you haven't selected a user and you've entered an email address and a name [00:01:49](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m49s) and the needed information, then once you click save, close or save, the system will actually create a user for you. Automatically email the user details to that user. That is because of that specific helper class that I added. Clicking helper class, [00:02:14](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m14s) it doesn't do it on its own you need to write the code for it. Let me show you where I did it in the controller. You have an area if we open up company, you have an area called: PostsaveHook. [00:02:38](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m38s)
Ideally you would connect that company to a user in the system. (It can happen that you are not going to add that company to a user because a user can have multiple companies). If you click on this tab'select user', you could select the user. But if you haven't selected a user and you've entered an email address and a name [00:01:49](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m49s) and the needed information, then once you click save, close or save, the system will actually create a user for you
and will automatically email the user details to that user. That is because of that specific helper class that I added. Clicking 'helper class', [00:02:14](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m14s) it doesn't do it on its own, you need to write the code for it. Let me show you where I did it in the controller(See video). There is an area if we open up company, called: 'PostsaveHook'. [00:02:38](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m38s)
Remember when we looked at the admin views we could target this area. That's exactly what I did. This is how we go about: I'm in the 'admin view' for 'company'. If I go to PHP you would see that I have some Ajax there and I have an area which is the 'postsavehook method'. [00:03:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m03s) In the postsavehook method, I'm basically checking some things, some values. If the user does not exist, A check is done (See video). Then the 'create user' is used. [00:03:41](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m41s) This placeholder convention has been mentioned before. Here you are using the brackets convention not the # one. [[[Component]]]Helper and this will be replaced with the components code name. It gets the helper class: create_user. In the helper class of this component, [00:04:13](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m13s) if this is closed, go to the helper and open the 'helper class'. That is the name 'costbenefitprojection'. It is the components name and then it's called helper. In helper class there is a feature called 'create_user'. [00:04:39](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m39s) It gets an array in it, and that array has some values which we will be using, like password 1, password 2. and it will set the password. I'm not going to explain this code. Basically it just shows you where the function is. The function was added to the components helper class and in that script, I'm simply calling that function after having build the user. I set the array as; 'name' and the email as 'validData'. The function, if there is no password set, will set a random password and again use another method which is also part of the helper class we've exported with all helper classes, this 'random [00:05:28](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m28s) method' where you can get a random string. That is creating a user and then it sets up the password and then it gets the array ready. It gets a model. The model, I got it from there, 'getmodel'. Again we are using a component builder custom method which gets the specific model from the system and then fires the register method upon this dataset and then updates [00:06:11](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m11s) the parameters for the system regarding this new user. Sends the email and then returns the ID. We need the ID. [00:06:41](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m41s) If you don't have the ID, it will send a message to the user and if we have the ID, it will store it into that database to connect that item to that user automatically and then click 'save'. That is custom scripting. [00:06:59](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m59s) <<<<<<<<<<<<<<<<<
Remember when we'd looking at the admin views we could target this area. That's exactly what I did. Let me show you. I'm in the admin view for company. And if I go to PHP you would see that I have some Ajax there. And then I have an area which is the postsavehook method. [00:03:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m03s) In the postsavehook method, I'm basically checking some things, some values. When I realized that this user does not exist, if the user does not exist, I'm doing a check here. Then I'm using the create user. [00:03:41](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m41s) I've spoken about this placeholder convention before. Here you are using the brackets convention not the # one. [[[Component]]]Helper and this will be replaced with the components code name. It gets the helper class: create_user. In the helper class of this component, [00:04:13](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m13s) if I close this down, I go to the helper. I open the helper class. That is the name(costbenefitprojection). It is the components name. And then it's called helper. In helper class there is a feature called create_user. [00:04:39](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m39s) It gets an array in it, and that array has some values which we will be using, like password 1, password 2. And it will set the password. I'm not going to explain this code. Basically it just shows you where the function is. The function was added to the components helper class, in that script, I'm simply calling that function after having build the user. I set the array is name and the email is that(validData). The function if there is no password set it will set a random password. Again use another method which also is part of the helper class. We exported it with all helper classes is random [00:05:28](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m28s) method. We can get a random string. That is creating a user and then it sets up the password and then it gets the array ready. It gets a model. The model, I got it from there, getmodel. Again we are using a component builder custom method which gets the specific model from the system. And then fires the register method upon this dataset. And then updates [00:06:11](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m11s) the parameters for the system regarding this new user. Sends the email. And then returns the ID. We are looking for an ID. We want to have the ID. [00:06:41](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m41s) If you don't have the ID, it will send a message to the user. And if we have the ID, it will store it into that database to connect that item to that user automatically and then click save. That is custom scripting. [00:06:59](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m59s)
### Add UiKit