Thursday, 14 March 2013

Joomla Component Development Example-I (Controller)

          In the previous post I described the file structure for Joomla! component. To learn what code goes into each of the files, lets take an example for a component for student records. This is just a basic example that illustrates the basic component development in joomla, including use of JDatabase and JTable. 

         As Joomla is an MVC, we know the basic working of Model, View and Component. We build a component named com_student, in which Model, View and Controller are controller.php, Views/view.html.php and Models/student.php respectively. The file structure remains the same as described in my previous post, just replace 'hello' by 'student' in it.

      The component for site and administrator can be thought of as two different components. Here we build a component that allows the user to edit the student name at the administrator and the component at the site just displays all student names. We first take a look at the component for administrator.

         The code for student.php is described below. What each line does here, is described in the comments:


<?php

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Require the base controller
require_once( JPATH_COMPONENT.DS.'controller.php' );

// Require specific controller if requested

if($controller = JRequest::getWord('controller')) 
       {
    
        $path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
                
        if (file_exists($path)) 
                    {require_once $path;    }
        else 
            {$controller = '';  }
}

// Create the controller
$classname    = 'StudentController'.$controller;
$controller   = new $classname( );

// Perform the Request task
$controller->execute( JRequest::getWord( 'task' ) );

// Redirect if set by the controller
$controller->redirect();



                           Our controller.php will contain functions for every tasks that user can issue. The tasks can be edit, remove, save, publish, etc..

<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');

class StudentController extends JController
{
public function display()
{
parent::display();
}

public function edit()
{
JRequest::setVar( 'layout', 'form'  );
JRequest::setVar( 'hidemainmenu', 1 );
parent::display();
}

       }



  • The first line of the code, as the comment shows is to ensure that the file is not accessed directly. This is for the matter of security.
  • Next the ‘jimport(…);’ will include the php file "/libraries/joomla/application/component/controller.php". This file holds the class JController.
  • From the next line starts our controller class, which extends the JController.
  • The naming convention for our controller class is controller_nameController.
  • The display() function will display the default.php file,  and this is the default function called when there are no other tasks to be done.
  • Now, for tasks save and delete only the database operations are done so you make those functions same as the 'display()' function.
  • For the tasks edit and new we need to display a form that allows the user to select a new image. So for these tasks we need to set a layout different from the default layout, as shown in the function 'edit()'.  And the toolbar also wont be same as the default layout so we hide that using the second line of 'edit()' function ( we will see this later in detail ).

  • For all the database operations we use the JTable class of Joomla!
  • We make an extension for that class as follows, and this file resides at admin/tables/slider.php.
   <?php
   defined('_JEXEC') or die();
   class TableSlider extends JTable
   {
        var $id = null;
        var $name = null;
        var $published = 0;
        function __construct(&$db)
        {
                parent::__construct( '#__slider', 'id', $db );
        }
   }


  • The variables inside this class corresponds to the columns in our database.
  • Then create a constructor for the class that accepts a reference to the current database instance. This will call the parent constructor which needs the name of the table, the name of the primary key column, and the database instance. The name of the table uses #__ instead of jos_, as the administrator can pick any table prefix desired during Joomla! installation.

  • Now, we can it can used throughout Joomla! for creating, reading, updating, and deleting records in the database table.
  • In our publish() function we get the instance of our table class, and set the publish value for the image as ‘1’. First line of the function includes our Table class and then we get the instance of our JTable extension and use it to publish the student name.

    function publish()
    {
    JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
    $cid = JRequest::getVar ('cid', array(), '', 'array');
    $task=JRequest::getWord('task');
    if($task=='publish')
    {             
    $publish=1;
    }
    $sliderTable =& JTable::getInstance('student', 'Table');
    $sliderTable->publish($cid, $publish);     
    parent::display();
    }

  • Same thing is done with the unpublish() function. In that we just do $publish=0.

Monday, 11 March 2013

Basics of Component Development in Joomla!


  • The file structure for Joomla component is as under.
  • Consider a component com_hello:
    • Controller.php
    • Controllers
      •    Controller1.php
      •    Controller2.php
    • Hello.php
    • Models
      • Hello.php
      • Index.html
    • Views
      •    Hello
      •   Tmpl
        • Default.php
        • Index.html
      • View.html.php
      • Index.html
    • Index.html

  • This is the basic file structure for a component in joomla.        
  • Controller.php is the main controller and one can add controllers for specific purposes in the ‘controllers’ folder.
  • The file hello.php will instantiate a controller object of a class named HelloController. Joomla will look for the declaration of that class in an aptly named file called controller.php (it's a default behavior).
  • Models as we know takes care of data, which is written in hello.php inside 'models' folder.
  • In the views part, we have a folder named ‘tmpl’, which contains default.php, which is the view used by joomla by default unless we specify one. And it uses the default template set by us for the site.
  • The component for site and admin part can be thought of as two different components, based on the functionality required at the two parts.
  • The files for both these can kept in site and admin folders respectively in case they are different components, and if they have some common files no need to make two folders and put all in one folder com_hello. Hence the zip file that we install contains these two folders and a .xml file that describes which files goes to admin part and which ones to the site part during the installation of extension.

Saturday, 9 March 2013

Installing and using Joomla! extensions.

So guys...ready to go ahead with the Extensions...!! Here we will see how to install and use various extensions in Joomla!

INSTALLATION:

  •   Extensions are available in the form of component, module or a plugin.
  •  To use an extension in joomla site, download the zip folder for that extension.
  •  You can get a huge variety of extensions at extensions.joomla.org and many others.
  • Go to Extension Manager from Control Panel or Extensions->Extension Manager from  menu.


  • A message will be displayed whether installation is successful or not.



    USAGE:
  • If it is a Component:
  • Create a new menu item to display the component. As I said in my previous post, there is only one component for each page in a Joomla! website. So we link the menu item to the component type, we want for that page.
  • Select the required component from the list of menu item types. Eg. If the component is hello_world, you can view it in the list of menu item types as shown in the figure:


  • Let us take an example of component for FAQ extension:  
  • After installation it is available in the Component menu at the administrator.
  • You can select it from there and make the configuration you want.


  • Now it is also a menu item type.


  • You can view the component on clicking the menu item in the website.


  • If it is a Module:
  • Go to Module Manager in Control Panel or go to Extensions->Module Manager and then press New.
  • Following list for the type of module will appear.



  • Select the Module that you installed.
  • Enter the Title, Status-Published.
  • Change 'Module assignment' field to 'On All Pages'. This shows that the module is assigned to all the pages in the site and can be used in any of them.
  • Open the article in which you want to use the module.
  • Write {loadmodule module_name} wherever you want to use the module in the article.
  • Let us take an example of Lof Scrollbar extension as a module.
  • After installing the module, go to Extensions->Module Manager, Click on New to create a new module of type Lof Scrollbar.
  • Select Lof Slider from the module type list, which will appear when you click New.







  • In this case you need to specify the source of the content for the slider.
  • You may specify the article ids which you want in the slider or you may choose Category.
  • Save & Close the module.
  • Now you can add the module to an article by writing the {module Lof ScrollBar Module|division} in the article.
  • On the website you can view the scrollbar.

  • If it is a Plugin:
  • Go to Extensions->Plugin Manager.
  • Now Enable the plugin you installed.
  • Let us take an example of social media extension as plugin.
  • Figure shows the article before the plugin was Enabled.


  •  Make the status of the plugin you installed as Enabled.


  • And here it is..!! Now share your article on any social network with this..!!

  • Let us take another example of Modules Anywhere extension as a plugin.
  • This extension is used to add modules in an article.So this is not for the website, it is for the administrator.
  • Install the zip folder for the plugin.
  •  Now go to Extensions->Plugin Manager Enable the plugin.



  •  Now you can add a module to an article using Add Module button.


  • This way you can create extensions for any type of functionality you want in you website or for the administrator.
  • To start with the component development for your website, check out my next post. Its coming soon...:)


Thursday, 7 March 2013

Creating an Article and Menu for Joomla website..!!

We saw the types of extensions we can use with joomla...

An Article in joomla is, you can say, a container in which you can put some content for your website. And a component can be made up of a single article or even a bunch of articles...!! 

What represents a component, depends on the menu item type we choose, for the menu item linked to the component...!! Confused?? okay here we start all from the scratch...:)



Login to Backend:

  • Enter the address of site.
    • Eg. http://localhost/joomla_2.5/Sitename
  • Enter Username and Password to login.
 


Create an article:

  •  Login to Joomla administrator backend, you can see the control panel as shown below.



  •      Go to Article Manager in Control Panel and then new or Content->Article Manager->Add New Article in Menu.




  •  Enter the Title of the article.
  •  Enter the content for the article in the editor.
  •  To put an image in article, click on Image button below the editor.
  •  To add a link in article, select the text or image that is to be linked and click                                       on link/unlink option.
  • Status of article:  
    •   Publish: Makes the article available to visitors of your website.
    •   Unpublish: Makes the article unavailable to visitors of your website.
  • Save & Close the article.

 

Create a menu:

  •  Login to Joomla administrator backend.
  •  Go to Menu Manager in Control Panel and then new or Menus->Menu Manager->Add New Menu from Menu.




  • Enter the Title (eg. User) and Menu Type.
  • Save & Close the menu.


Create a menu item in the above menu:

  •  Go to Menu->User->Add New Menu Item




  • Press Select to select a menu type.
  • And here comes what we talked at the start..!!
  • The list that you see for the menu item types, is actually, the types of components. 
  • In a Joomla! website, in a single page, there is only ONE component always and so in this way you can link your menu item with the type of component for that page.




  • Select a menu item type from this.
  •  Eg. If you want your menu item to display an article, select Single Article.
  •  This means your component for that page will be a single article.
  •  In this case you will find a field in right column where you need to mention which article is to be linked to this menu item.


I hope its fun playing with the joomla User Interface...!!! isnt it?!! :)

Wednesday, 6 March 2013

Introduction to an award winning CMS!!

Now a days Joomla is a widely used CMS and everyone might be aware of it..!!

Joomla installation is really simple :) Just download a joomla package from www.joomla.org/download.html. The latest version I guess is 3.0,I work with 2.5.
Unzip the package . Type the url localhost/{ur joomla folder name} in your browser, then follow the steps as shown in http://docs.joomla.org/Installing_Joomla!#tab=Intro 

Joomla provides a great user interface to manage what you want to show on your website.  
Template manages how your website looks..!! You can add functionalities to your site using extensions. Extensions as the name suggests are packages which adds some features to your website and templates are also one of them.

The figure shows the Extensions: