Updated 011 Component Scripts (markdown)

Amigo 2019-07-15 16:57:19 +02:00
parent 956190eaee
commit 4b2fc360e9

@ -1,44 +1,64 @@
# COMPONENT SCRIPTS
[00:00:00](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m00s)
Now we will look at the scripts tab. Here is an area where you might 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 certain values and add create user helper method to the component.
### Create User Helper Method
The 'user helper method' is simply code. If you don't know how to write PHP this method will be of no use to you. [00:00:54](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m54s) If you do, this method will come in very handy. Let me illustrate that to you. I have a component in which I've already implemented this feature. It's called the 'cost-benefit projection tool'. If you click on companies, you can 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.
[00:00:47](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m47s)
The 'user helper method' is simply code. If you don't know how to write PHP this method will be of no use to you. If you do, this method will come in very handy. Let me illustrate that to you. I have a component in which I've already implemented this feature. It's called the 'cost-benefit projection tool'. If you click on companies, you can 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)
[00:01:29](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m29s)
Ideally you would connect that company to a user in the system. (It might be that you won't add that company to a user because a user can have multiple companies.) If you click on 'select user', you could select the user. [00:01:49](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m49s) But if you haven't selected a user, and have entered an email address, a name, and the needed information, then when you close or save the system will create a user for you and will automatically email the user details to that user. This is because of that specific helper class that I added. [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.) If we open up company there is an area called: 'PostsaveHook'. [00:02:38](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m38s) When we looked at the admin views we could target this area. That is what I did.
This is how we go about it: Go in the 'admin view' for 'company'. If you go to PHP you'd see some Ajax there and 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 checking some values. If the user does not exist, A check is done, then the 'create user' used. (See video.) [00:03:41](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m41s) This placeholder convention had been mentioned before. Here you use the brackets convention, not the # one.'[[[Component]]]Helper.' This will be replaced with the component's code name. It gets the helper class, create_user. If this is closed in the helper class of this component, go to the helper and open the 'helper class'. [00:04:13](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m13s) The component's name is 'costbenefitprojection' and is 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 values which we will be using, like password 1, password 2. It will set the password. This shows you where the function is. The function was added to the components helper class and in that script, I'm 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 use another method. This 'random method' where you can get a random string is part of the helper class we've exported with all helper classes. [00:05:28](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m28s) It creates a user, sets up the password, and then gets the array ready. It gets a model. I got the model from there, 'getmodel'. Once more we are using a component builder custom method. It gets the specific model from the system then fires the register method upon this dataset and updates the parameters for the system regarding this new user. [00:06:11](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m11s) It sends the email and returns the ID. [00:06:41](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m41s) We need the ID. If you don't have the ID, it will send a message to the user; if we have the ID, it will store it into that database to connect that item to that user automatically. Save. [00:06:59](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m59s)
### Add UiKit
### Add UIKit
This switch adds the method to your helper class. (See video.) Whereas this switch adds the Uikit library to your media folder as well as integrates it dynamically wherever you use the Uikit class names. [00:07:19](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m19s) You won't need to write it in any custom way; it automatically adds it. The same goes with FooTable.
[00:07:01](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m01s)
This switch adds the method to your helper class. (See video.) Whereas this switch adds the Uikit library to your media folder as well as integrates it dynamically wherever you use the Uikit class names. You won't need to write it in any custom way; it automatically adds it. The same goes with FooTable.
### Add Global CSS To Admin Back End
[00:07:32](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m32s)
This CSS is global CSS for all the back end views. If you want to add CSS that would target the entire back end, you do it here.
### Add Custom PHP Helper Admin Class
This is a place for you to add your own custom scripting to the helper_admin Class. (See video.) [00:07:47](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m47s) Here is a whole custom scripting for Sermon distributor and the admin area, as well as an event.
[00:07:46](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m46s)
This is a place for you to add your own custom scripting to the helper_admin Class. (See video.) Here is a whole custom scripting for Sermon distributor and the admin area, as well as an event.
### Add Global Admin Event
This event concept has been mentioned before, but this is what we want to achieve with the event. [00:08:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m09s) In the back end part of 'sermon distributor' there is a file. It is the in main file which Joomla calls 'first', and in that file it triggers that global admin event. The global admin event is nothing but a function inside of your helper function. It always triggers it if you add that switch and passes the document object. [00:08:40](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m40s) The document object is being built up here. I'm passing it over to the global event. There is no need to clear it. If you open the helper class and scroll to the top, it says loaddropboxajax'. [00:09:08](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m08s) This is just another function. I wrote it so that you can add whatever functions you want to run globally every time a page opens. It can run like a chronicle task on the front and the back end of the component. If you have things that you want to be done chronically, this feature is targeted to do that. [00:09:35](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m35s) For instance, we are loading the 'LoadDropboxAjax'. If we go back to the component, you'd see that I am adding that script. First I clicked global admin event 'yes' and added the script. That's all they added. The script was taken and placed inside the global event. The trigger'globalevent' is set by component builder; you don't need to write it. [00:10:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m03s) It will insert this 'globalevent' trigger because you clicked 'yes'. Then you need to add the script that must run inside of the global event. This function, which I want to run, I added to the admin view at the admin helper class. [00:10:29](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m29s) I used the 'loadDropboxAjax' name, and called it 'self', because we are inside of the class. That's how you call 'static class method' and pass same information. In my method I'm adding JavaScript to the document object. I want that document and then I set 'addScriptDeclaration'. [00:10:59](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m59s) It means that on every page of this component in the back end, this little script is added which then calls by Ajax and checks the Dropbox listing automatically. This automatic feature had been added to the component so that you can do the same in other components. That's the purpose of the event switch. The same is applicable to the site area. [00:11:37](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m37s)
[00:08:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m03s)
This event concept has been mentioned before, but this is what we want to achieve with the event. In the back end part of 'sermon distributor' there is a file. It is the in main file which Joomla calls 'first', and in that file it triggers that global admin event. The global admin event is nothing but a function inside of your helper function. It always triggers it if you add that switch and passes the document object. [00:08:40](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m40s) The document object is being built up here. I'm passing it over to the global event. There is no need to clear it. If you open the helper class and scroll to the top, it says loaddropboxajax'. [00:09:08](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m08s) This is just another function. I wrote it so that you can add whatever functions you want to run globally every time a page opens. It can run like a chronicle task on the front and the back end of the component. If you have things that you want to be done chronically, this feature is targeted to do that. [00:09:35](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m35s) For instance, we are loading the 'LoadDropboxAjax'. If we go back to the component, you'd see that I am adding that script. First I clicked global admin event 'yes' and added the script. That's all they added. The script was taken and placed inside the global event. The trigger'globalevent' is set by component builder; you don't need to write it. [00:10:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m03s) It will insert this 'globalevent' trigger because you clicked 'yes'. Then you need to add the script that must run inside of the global event. This function, which I want to run, I added to the admin view at the admin helper class. [00:10:29](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m29s) I used the 'loadDropboxAjax' name, and called it 'self', because we are inside of the class. That's how you call 'static class method' and pass same information. In my method I'm adding JavaScript to the document object. I want that document and then I set 'addScriptDeclaration'. [00:10:59](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m59s) It means that on every page of this component in the back end, this little script is added which then calls by Ajax and checks the Dropbox listing automatically. This automatic feature had been added to the component so that you can do the same in other components. That's the purpose of the event switch. The same is applicable to the site area. [00:11:37](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m37s)
### Add Custom PHP Helper Site View
[00:11:44](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m44s)
Why are there different helper classes for the front and the back? It's about security and permissions. For example, there are things that I want to do in the back end where many questions aren't asked; on the front I might be more protective. Hence the implementation is often different in the front than in the back. That's why we have two different files: the Helpers site class and the Helper admin class where custom scripting can be added. [00:12:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m09s) I added the same script to the front and added a site event which triggers it. What if everybody could trigger the script? Is it going to run multiple times? [00:12:32](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m32s) The answer is that it is exactly part of what has been done. I added a feature in the Ajax call that logs whether it's active or not. If it is active, it ignores that call. It has a controller determining whether it should run or not and a timer that you can set for the sermon distributor. [00:12:56](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m56s) You can set how often you want Dropbox links to be updated, or whether it wants to update or not. Since it's Ajax the user himself don't even notice a delay in his page. The page opens as fast as it always do and fires that Ajax call to the server which will start the whole process on the server. This is all possible inside of component builder through these custom areas.
### Add MySQL Dump
You can also add a SQL dump. [00:13:50](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m50s) Instead of adding it to the actual views you can export all the views and add them here. (See video.) It will be added to the same file, so don't do it twice. This is another area to add a SQL dump file.
[00:13:49](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m49s)
You can also add a SQL dump. Instead of adding it to the actual views you can export all the views and add them here. (See video.) It will be added to the same file, so don't do it twice. This is another area to add a SQL dump file.
### Dashboard Methods (with example)
Let me show you what I've done with dashboard methods. All this data that I'm showing you is dummy. (See video.) [00:14:23](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m23s) It is not related to the application but I'm just testing it. I wanted to add this usage statistics to the component, so you can see which country has done what, when, and in what area. I took a lot of different database tables and calculated by using certain ways. I wanted to have that on the dashboard. The dashboard have all these icons already. I didn't want it to add another custom view. [00:14:53](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m53s) I wanted it right there at the moment of the page load. So we added this feature to Component Builder and called it dashboard methods.
[00:14:23](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m23s)
Let me show you what I've done with dashboard methods. All this data that I'm showing you is dummy. (See video.) It is not related to the application but I'm just testing it. I wanted to add this usage statistics to the component, so you can see which country has done what, when, and in what area. I took a lot of different database tables and calculated by using certain ways. I wanted to have that on the dashboard. The dashboard have all these icons already. I didn't want it to add another custom view. [00:14:53](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m53s) I wanted it right there at the moment of the page load. So we added this feature to Component Builder and called it dashboard methods.
I created a method called 'getusagedata'. [00:15:26](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m26s) It's a public function method inside which I added my script. Inside this script I'm calling other methods which I made to protect the script that must be called from the dashboard tab. You set that up here. ('Dashboar Tab -Add'.) Here is the tab and it's HTML. We called the tab 'Usage Statistics' and the header 'Table.' (See video.) [00:16:01](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m01s) Here is the 'HTML' which we are using. In the HTML you see PHP. We do it with that convention of '<?php echo,' then closing it there '>?' and checking whether the usage data is there.
@ -46,6 +66,8 @@ How do I know that I should target 'this usage data' and the 'items'? This area
### Adding Data to a Page
[00:18:06](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m06s)
A lot of PHP can be added here, all of which together gets the data that is needed on the page. When all the data is collected it is placed in the 'item'. A field or a method value is returned. [00:18:43](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m43s) This method value is being populated in these sub functions. The name, 'items' is added in one of them as a pointer; it is a object. [00:19:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m09s) The code itself will not be explained right now, but this 'usagedata' is getting returned because it's an object. Here you are able to target 'items' as an object; if it exist and is set you can start looping through items and certain places in which you are using totals and different values. [00:19:45](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m45s)
This could have been simple. You could have left this out completely and only placed a comment. You could have done this. (See video.) You can set it 'not needed' and in here ('Dashboard Tab - Add') you can do HTML. If you just want to give information to the user concerning the person who made the component, you might like to run a RSS feed here. (See video.) [00:20:34](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m34s) You can reach into PHP here, and the PHP that you reached in has the convention of dropping the get and using the front area, 'usage data', to place information in which you can parse your HTML as you would do in any other component view.