While the majority of the request going to Joomla end up displaying a view, this is not the case for all of them. When records are published from a backend list screen, a controller task is called to do the publishing. To handle publishing in backend tasks Joomla has specially coded controllers and models.
So let's add some of those controllers and models now to our backend. First, let's login to the backend and now let's go to Components>Noida City>Noida City Places.
So this is a list screen that we have created in our previous tutorial. If we click on Publish or Unpublish buttons right now, nothing happens because we haven't added some things that we need for the view and we also need to add some controllers and model code to make this all work.
Let's get it working, go to administrator>component>com_noidacity>models folder and create a file called place.php and add the following code.
So this is a model and it's a type of model that is the admin model. It's not a list model or does a plain model, it's an admin model and Joomla knows that admin models are used to work with specific rows in the database and because of that it's going to need to know which kind of row we want to work with.
So to do that we have a getTable function and that is going to return a JTable object, just like we used in the front end to display a single record from the database and we are just adding the type of record that it is and the prefix and it's going to assemble all of that together to load up the NoidaCityTable Place class, create an object and return it.
And then next the model also makes us define the getForm function. We are not really dealing with the form that's going to edit the place yet, but we're still required to define it by the JmodelAdmin class. So we just define getForm and we calling this loadForm and that's enough to make Joomla be happy with the modelAdmin class that we are defining here.
So after we are done with that, let's go and add the controller that is going to receive the tasks from the toolbar buttons and talk with the model.
So just go to administrator>components>com_noidacity create a folder called controllers in the backend. And inside that create a file called places.php and inside that file write the following code.
We have a JControllerAdmin class, It starts off with a protected property called text_prefix and this is used for the language strings because we're going to have some status message updates that appear on screen and we need to know what prefix to use to assemble those language strings. So COM_NOIDACITY_PLACES is a prefix we are using and we are just defining it here.
And now the JControllerAdmin class wants us to define a getmodel function, so that it knows what model to get so that we can use that to talk to the database, when we are dealing with individual records. That getmodel function takes the name of the model and then the prefix with their model, and then it assembles all that for NoidaCityPlace, and then it loads up the Place model and just returns it. And in that way the different tasks that are embedded in JControllerAdmin can call up that model and work with the table.
So finally we need to update the view with a little bit of code that allows those toolbar buttons to work, so that way it actually does something and submits the form.
Go to views>places>tmpl folder and open default.php file and add the following three lines of code just above the closing form tag.
So now your default.php file which we created earlier will be like this:
So you'll notice the first element is the hidden task element and this element is here so that whenever we click on one of the toolbar buttons, the toolbar button will take the task that's been assigned to it and assigned it as the value here and then submit the form.
Next, we have the boxchecked hidden element and what this element does, is it keeps track of how many check boxes have been checked off on the left side of the screen.
And then finally we have this form token element at the bottom and what this does is it checks cross site request forgery attacks. It makes sure that the person that is submitting this form is the actual person that did it and not some other script that's on the Internet.
Go back to the backend and hit Refresh and when we check off a couple of these records and hit Unpublish, it now unpublishes those records and it says 2 places unpublished.
So what happened is there is a task for Unpublish called places.unpublish that we added to our view in the toolbar in our previous tutorial, it's submitted that the form along with the values in these two check boxes, told the controller to unpublish those records, then the control pass those to the model and the model did the work of unpublishing the records. Then the controller redirected the browser back to the places view.
So when you want to do something aside from displaying views, that task built into ControllerAdmin will handle most of your backend needs. Hooking a modelAdmin class will give your ControllerAdmin the ability to talk to the database. And having the right HTML variables into view, make it possible for users to fire specific task by clicking at toolbar button.