diff --git a/011-Component-Scripts.md b/011-Component-Scripts.md index 200cee6..980b181 100644 --- a/011-Component-Scripts.md +++ b/011-Component-Scripts.md @@ -27,28 +27,31 @@ This is a place for you to add your own custom scripting to the helper_admin Cla ### Add Global Admin Event -This event concept has been briefly mentioned before, but what we want to achieve with the event is basically: Let me explain. [00:08:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m09s) If you look at the back end part of 'sermon distributor', it has a file which is the in main file which Joomla calls 'first', and in that file it basically triggers that global admin event. The global admin event is nothing else than a function inside of your helper function. So it always triggers it, [00:08:40](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m40s) if you add that switch. Then it passes the document object. The document object is being built up here. I'm just passing it over to the global event. There is no need to clear it. If I was to open the helper class, which we have here(See video), and scroll to the top, it says here [00:09:08](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m08s) 'loaddropboxajax'. This again is just another function. I wrote that function that you can add whatever functions you want to run globally every time a page opens. That is a way to run it 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 at doing that. [00:09:35](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m35s) In this instance we are loading the 'LoadDropboxAjax'. If we go back to the component, you see that I am just adding that script. First I just clicked global admin event 'yes' and added that script. That's all they added. That script was taken and placed inside of the global event. The trigger'globalevent' is set by component builder you don't need to write that. [00:10:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m03s) It will insert this'globalevent' trigger itself just because you clicked 'yes'. You need to then just add the script that must run inside of the global event. Which I decided to be another function and then this function which I want to run, I added to the admin view, you see here(See video) at the admin helper class. [00:10:29](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m29s) I added that function . So I basically just used that'loadDropboxAjax' name, and called it 'self', because we are inside of that 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) That means on every page of this component in the back end, it adds this little script which then calls by Ajax which I explained previously and it starts checking the Dropbox listing automatically. This automatic feature has been added to the component so that you can do the same in other components. That's the purpose of the event switch.<<<<<<<<<<<<<<<<<<<<<< - - - - - +This event concept has been briefly mentioned before, but what we want to achieve with the event is basically: Let me explain. [00:08:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m09s) If you look at the back end part of 'sermon distributor', it has a file which is the in main file which Joomla calls 'first', and in that file it basically triggers that global admin event. The global admin event is nothing else than a function inside of your helper function. So it always triggers it, [00:08:40](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m40s) if you add that switch. Then it passes the document object. The document object is being built up here. I'm just passing it over to the global event. There is no need to clear it. If I was to open the helper class, which we have here(See video), and scroll to the top, it says here [00:09:08](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m08s) 'loaddropboxajax'. This again is just another function. I wrote that function that you can add whatever functions you want to run globally every time a page opens. That is a way to run it 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 at doing that. [00:09:35](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m35s) In this instance we are loading the 'LoadDropboxAjax'. If we go back to the component, you see that I am just adding that script. First I just clicked global admin event 'yes' and added that script. That's all they added. That script was taken and placed inside of the global event. The trigger'globalevent' is set by component builder you don't need to write that. [00:10:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m03s) It will insert this'globalevent' trigger itself just because you clicked 'yes'. You need to then just add the script that must run inside of the global event. Which I decided to be another function and then this function which I want to run, I added to the admin view, you see here(See video) at the admin helper class. [00:10:29](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m29s) I added that function . So I basically just used that'loadDropboxAjax' name, and called it 'self', because we are inside of that 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) That means on every page of this component in the back end, it adds this little script which then calls by Ajax which I explained previously and it starts checking the Dropbox listing automatically. This automatic feature has 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) I also added that same class. ### Add Custom PhP Helper Site View -This same goes for the site area. [00:11:37](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m37s) I also added that same class. You might ask why I have different helper classes for the front and the back. Well it's about security, and permissions. There are things that I wanted to do in the back end, which I'm not asking that much questions. Where on the front I might be more protective. So for that reason and implementation also is often different in the front then in the back. That's why we have two different files. Helpers site class, [00:12:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m09s) and the Helper admin class. And you can add custom scripting. I added that same script to the front. And I also added a site event. Which also triggers that same script. Now you might say well if everybody triggers the script, then it's going to be running multiple times. Well [00:12:32](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m32s) that's exactly part of what I did. I actually added a feature in the Ajax call. That logs whether it's already active. And if it's active, it just ignores that call. So Basically it has a controller over whether it should run or not. Meaning that if it needs to run. Because it even has a timer you can set [00:12:56](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m56s) for the sermon distributor. You can set how often you want Dropbox links to be updated. Every day, every 2 days, and so this script. If it checks wants to update and it's not time it just dies, it doesn't do anything. It's very light 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 then fire that Ajax call to the server. Which will starts this whole process on the server. So that's primarily what this is all about. You need to know programming and you need to know how to secure all that. And I'm not going to try explain that to you now. I'm just saying that this is all possible inside of component builder, simply through these custom areas. +Why is there different helper classes for the front and the back? Well, it's about security, and permissions. + + There are things that I wanted to do in the back end, where not many questions are asked. Where on the front I might be more protective. So for that reason and implementation also is often different in the front than in the back. That's why we have two different files: Helpers site class, [00:12:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m09s) and the Helper admin class , custom scripting can be added. I added that same script to the front and also added a site event which also triggers that same script. What if everybody triggers the script? Is it not going to run multiple times? Well, [00:12:32](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m32s) that's exactly part of what has been done. I actually added a feature in the Ajax call. That logs whether it's already active. If it is active, it just ignores that call. So basically it has a controller over whether it should run or not. It has a timer you can set [00:12:56](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m56s) for the sermon distributor. You can set how often you want Dropbox links to be updated. Every day, every 2 days, and so this script(See video). If it wants to update and it's not time it just dies. 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 then fire that Ajax call to the server. Which will starts this whole process on the server. This is all possible inside of component builder, simply 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(MySQL). It will be actually added to the same file. So don't do it twice. This is another area to add a SQL dump file. +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'MySQL'. It will be actually 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) -Then we have this nice feature called dashboard methods. Let me show you what I've done with it. All this data that I'm showing you is dummy. [00:14:23](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m23s) It is not related to this application but it is just me testing it. I wanted to add this usage statistics to the component, so you could see which country has done what, when in what area. I needed to take a lot of different database tables and calculate, by using certain ways. I wanted to have that on the dashboard. The dashboard already have all these icons. I didn't want it [00:14:53](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m53s) to add another custom view, which I could have done, custom admin view. I wanted it simply right there at the moment of the page load. And so we added that feature component builder and we called it dashboard methods. Now let me go to one where I actually implementing it. +Then we have this nice feature called dashboard methods. Let me show you what I've done with it. All this data that I'm showing you is dummy. [00:14:23](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m23s) It is not related to this application but it is just me testing it. I wanted to add this usage statistics to the component, so you could see which country has done what, when in what area. I needed to take a lot of different database tables and calculate, by using certain ways. I wanted to have that on the dashboard. The dashboard already have all these icons. I didn't want it [00:14:53](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m53s) to add another custom view, which I could have done, custom admin view. I wanted it simply right there at the moment of the page load. So we added that feature to Component Builder and we called it dashboard methods. Now let me go to one where I actually had it implemented. -I could spend more time in explaining and less on typing. I created a [00:15:26](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m26s) method called getusagedata. It's a public function method. And inside it I added my script. Inside of this script I'm calling other methods which I made it protected. The script that must be called from the dashboard tab, I'll show you that at the moment. 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 we made 'Table', [00:16:01](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m01s) and here(see video) is the HTML. Which we are using. and in the HTML You see that I have PHP. and the way we do that is with that convention of ?). I'm checking whether the usage data is there. +I could spend more time in explaining and less on typing. I created a [00:15:26](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m26s) method called 'getusagedata'. It's a public function method and inside it I added my script. Inside of this script I'm calling other methods which I made it protected. 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 we made 'Table', [00:16:01](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m01s) and here(see video) is the 'HTML'. Which we are using. and in the HTML you see PHP and the way we do that is with that convention of '?' and checks whether the usage data is there. -How do I know that I should target 'this usage data' and then 'items'? Well this is a convention this area there(usagedata). The way that, that convention works, is [00:16:46](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m46s) by that one there. 'Usage data' relates to 'usage data'(see video). The moment you add the word get, in front of a function, it will be added to a global, the result of that get will be added to 'this' and without the get name, so that the convention. [00:17:15](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m15s) The convention is you could've called that get 'manonthemoon'. Then this would be 'manonthemoon' all in lowercase. That(get) will be dropped and this(UsageData) is going to be used lowercase, just as the 'this'. [00:17:37](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m37s) That's the convention. I know it might not be that obvious but it works. So your other functions, if they are protected shouldn't have the word get in it. At the moment it's set, to 'set'. That one is set, this one is 'visitcheck'. This one is set 'companies' and set 'countries'. [00:18:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m03s) +How do I know that I should target 'this usage data' and then 'items'? Well, this area 'usagedata' is a convention area. The way that, that convention works, is [00:16:46](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m46s) by that one there. 'Usage data' relates to 'usage data'(see video).<<<<<<<<<<<<< + + + + + + The moment you add the word 'get', in front of a function, it will be added to a global, the result of that 'get' will be added to 'this' and without the 'get' name, so that the convention. [00:17:15](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m15s) The convention is you could've called that 'get' 'manonthemoon'. Then this would be 'manonthemoon' all in lowercase. That(get) will be dropped and this(UsageData) is going to be used lowercase, just as the 'this'. [00:17:37](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m37s) That's the convention. I know it might not be that obvious but it works. So your other functions, if they are protected shouldn't have the word get in it. At the moment it's set, to 'set'. That one is set, this one is 'visitcheck'. This one is set 'companies' and set 'countries'. [00:18:03](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m03s) I could add a lot of PHP in here. And all these PHP together gets the data that we want on the page. When I get the data I placed it in what is seen as the item. I'm returning a field here or a method value. [00:18:43](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m43s) This method value is being populated in this sub functions. And in one of them it is adding over here, the name items as a pointer. So it becomes an object, it is an object in fact I think I set it is an object up here. [00:19:09](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m09s) Then I look through the companies, and as I look through them I'm busy totaling and adding and going on. I'm not going explain the code itself, but this(usagedata) is getting returned, because it's an object. If I look here I'm able to target 'items' as an object. Then if it's exist, if it's set, I start looping through items and certain places I'm using totals and different values. That is just adding some of that data to the page. [00:19:45](https://www.youtube.com/watch?v=xY9TWQrF8AQ&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m45s) And this area here.