Skip to content

011 Component Scripts

Amigo edited this page Jun 10, 2019 · 17 revisions

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 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 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 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 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. (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 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 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

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 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 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 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 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 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 the parameters for the system regarding this new user. Sends the email and then returns the ID. We need the ID. 00:06:41 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 <<<<<<<<<<<<<<<<<

Add UiKit

This little switch is just adding that Method to your helper class. That's all it does. Where as this switch adds the Uikit library to your media folder. As well as integrates it dynamically wherever you use 00:07:19 the Uikit class names. You won't need to write that in any custom way, it automatically adds it. The same goes with FooTable.

Add Global CSS To Admin Back End

This CSS is global CSS for all the back end views, entire back end. If you want to add CSS that would target the whole back end, then 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. 00:07:47 As you can see I have a whole custom scripting here for Sermon distributor. For the admin area, as well as an event.

Add Global Admin Event

I spoke about this event concept briefly before, but what we want to achieve with the event is basically on every page, 00:08:09 if you look at this sermon distributor, the back end part of it, it has a file which is the in main file which Joomla calls first. Calls sermon distributor, 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 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. We don't need to clear it there. If I was to open the helper class, which we have here, and scroll to the top, it says here 00:09:08 loaddropboxajax. This again it's 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 job 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 Here 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 I added that script. That's all they added. And that script was taken and placed inside of the global event. The trigger(globalevent) is set by component builder itself you don't need to write that. 00:10:03 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 at the admin helper class. 00:10:29 I added that function here. 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 a pass to save information. In my method I'm adding JavaScript to the dorm to the document object. I want that document and then I set addScriptDeclaration. 00:10:59 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. So that's an automatic feature I wanted to add to my component. And this is way that I did it. And then I felt that I want to add it that you can do this in other components. That's why I added this event switch.

Add Custom PhP Helper Site View

This same goes for the site area. 00:11:37 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 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 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 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.

Add MySQL Dump

You can also add a SQL dump. 00:13:50 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 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 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.

I could spend more time in explaining and less on typing. I created a 00:15:26 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 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 <?php echo and then closing it again there(>?). I'm checking 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 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 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 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

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 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 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 And this area here.

Now I could have made this much simpler right so maybe not look that intense to you. But this could have been a simple class. In fact you could have let this out completely. And just put a comment. You could have done this(see video). You can set it 'not needed' and then in here(Dashboard Tab - Add) you could just do HTML. If you just want to give information to the user about, who made the component, maybe some would like to run a rss feed here, you could have done it right here. I'm just showing you that you can reach into PHP here, and that the PHP that you reach in, has the convention 00:20:34 of dropping the get and then using that front area, usage data, to place information in which you can parse in your HTML here. That you would do in any other component view. That is just quickly looking at the script area of a component. I realize there's so much more to say but I really can't elaborate too much on this. Since it requires skill in PHP. It's something that I think will only become very useful if you are a developer, as developed a few components. And then see how you can easily add custom scripting to his component through this methods. Well next up will look at the ReadMe, 00:21:27 and maybe their FTP server concepts.

Clone this wiki locally