17 011 Component Scripts
Amigo edited this page 2019-08-15 17:15:42 +02:00

COMPONENT SCRIPTS

00:00:00 (Click on these time links to see Youtube video)

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 You can also do that within the global scope with regards to certain values and add create a user helper method to the component.

Create User Helper Method

00:00:47

The 'user helper method' is 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 It has an area for you to create a company.

Example Helper Method(Create User)

00:01:29

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 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 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 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 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 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 The component's name is 'costbenefitprojection' and is called helper. In helper class, there is a feature called 'create_user'. 00:04:39 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 built 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 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 It sends the email and returns the ID. 00:06:41 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.

Add UIKit

00:07:01

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 for FooTable.

Add Global CSS To Admin Back End

00:07:32

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

00:07:46

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

00:08:03

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 the 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 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 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 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 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 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 the 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 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

Add Custom PHP Helper Site View

00:11:44

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 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 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 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 does 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

00:13:49

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)

00:14:23

Let me show you what I've done with dashboard methods. All this data that I'm showing you is imitation. (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 has all these icons already. I didn't want it to add another custom view. 00:14:53 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 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. ('Dashboard 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 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.

How do I know that I should target 'this usage data' and the 'items'? This area 'usagedata' is a convention area. The convention works by this one here. (See video.) 00:16:46 'Usage data' relates to 'usage data'. 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->' without the 'get' name. 00:17:15 The convention is that you could have called that 'get', the "manonthemoon". Then this would be 'manonthemoon', all in lowercase. The 'get' will be dropped and this 'UsageData' will be used lowercase like the 'this'. 00:17:37 I know the convention might not be that obvious, but it works. If your other functions are protected they should not have the word 'get' in them. (See video.)

Adding Data to a Page

00:18:06

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 This method value is being populated in these sub-functions. The name, 'items' is added in one of them as a pointer; it is an object. 00:19:09 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 exists and is set you can start looping through items and certain places in which you are using totals and different values. 00:19:45

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 an RSS feed here. (See video.) 00:20:34 You can reach into PHP here, and the PHP that you reached 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.

This has been a quick overview of the script area of a component. It might come in very useful if you are a developer enabling you to add custom scripting to a component through these methods. 00:21:27