Actions

J2.5

Developing a MVC Component/Adding backend actions

From Joomla! Documentation

< J2.5:Developing a MVC Component
Revision as of 20:54, 20 November 2010 by Cdemko (Talk | contribs)

Template:Future

Articles in this series


Introduction

This tutorial is part of the Developing a Model-View-Controller (MVC) Component for Joomla!1.6 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

Adding a toolbar

In Joomla!1.6, the administrator interacts generally with components through the use of a toolbar. In the file admin/views/helloworlds/view.html.php put this content. It will create a basic toolbar and a title for the component.

admin/views/helloworlds/view.html.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
 * HelloWorldList View
 */
class HelloWorldViewHelloWorlds extends JView
{
 
        /**
         * HelloWorldList view display method
         * @return void
         */
        function display($tpl = null) 
        {
                // Get data from the model
                $items = $this->get('Items');
                $pagination = $this->get('Pagination');
 
                // Check for errors.
                if (count($errors = $this->get('Errors'))) 
                {
                        JError::raiseError(500, implode("<br />", $errors));
                        return false;
                }
 
                // Assign data to the view
                $this->items = $items;
                $this->pagination = $pagination;
                // Set the toolbar
                $this->addToolBar();
                // Display the template
                parent::display($tpl);
        }
 
        /**
         * Setting the toolbar
         */
        protected function addToolBar() 
        {
                JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'));
                JToolBarHelper::deleteListX('', 'helloworlds.delete');
                JToolBarHelper::editListX('helloworld.edit');
                JToolBarHelper::addNewX('helloworld.add');
        }
}

You can find others classic backend actions in the administrator/includes/toolbar.php file of your Joomla!1.6 installation.

Adding specific controllers

Three actions has been added:

  • helloworlds.delete
  • helloworld.edit
  • helloworld.add

These are compound tasks (controller.task). So two new controllers HelloWorldControllerHelloWorlds and HelloWorldControllerHelloWorld have to coded.

admin/controllers/helloworlds.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
 
class HelloWorldControllerHelloWorlds extends JControllerAdmin
{
 
        /**
         * Proxy for getModel.
         * @since       1.6
         */
        public function getModel($name = 'HelloWorld', $prefix = 'HelloWorldModel') 
        {
                $model = parent::getModel($name, $prefix, array('ignore_request' => true));
                return $model;
        }
}

admin/controllers/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
 * HelloWorld Controller
 */
class HelloWorldControllerHelloWorld extends JControllerForm
{
}

Adding an editing view

With your favorite file manager and editor, put a file admin/views/helloworld/view.html.php containing:

admin/views/helloworld/view.html.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
 * HelloWorld View
 */
class HelloWorldViewHelloWorld extends JView
{
 
        /**
         * display method of Hello view
         * @return void
         */
        public function display($tpl = null) 
        {
                // get the Data
                $form = $this->get('Form');
                $item = $this->get('Item');
 
                // Check for errors.
                if (count($errors = $this->get('Errors'))) 
                {
                        JError::raiseError(500, implode("<br />", $errors));
                        return false;
                }
 
                // Assign the Data
                $this->form = $form;
                $this->item = $item;
                // Set the toolbar
                $this->addToolBar();
                // Display the template
                parent::display($tpl);
        }
 
        /**
         * Setting the toolbar
         */
        protected function addToolBar() 
        {
                JRequest::setVar('hidemainmenu', true);
                $isNew          = ($this->item->id == 0);
 
                JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_BANNER_NEW') : JText::_('COM_HELLOWORLD_MANAGER_BANNER_EDIT'));
                JToolBarHelper::save('helloworld.save');
                JToolBarHelper::cancel('helloworld.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
        }
}

This view will display data using a layout.

Put a file admin/views/helloworld/tmpl/edit.php containing

admin/views/helloworld/tmpl/edit.php

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHTML::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="helloworld-form">
        <fieldset class="adminform">
                <legend><?php echo JText::_( 'com_helloworld_HelloWorld_Details' ); ?></legend>
                <?php foreach($this->form->getFieldset() as $field): ?>
                        <?php if (!$field->hidden): ?>
                                <?php echo $field->label; ?>
                        <?php endif; ?>
                        <?php echo $field->input; ?>
                <?php endforeach; ?>
        </fieldset>
        <input type="hidden" name="task" value="" />
        <?php echo JHtml::_('form.token'); ?>
</form>

Adding a model and modifying the existing one

The HelloWorldViewHelloWorld view asks form and data from a model. This model has to provide a getTable, a getForm method and a loadData method (called from the JModelAdmin controller)

admin/models/helloworld.php

<?php
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
 
/**
 * HelloWorld Model
 */
class HelloWorldModelHelloWorld extends JModelAdmin
{
 
        public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
        {
                return JTable::getInstance($type, $prefix, $config);
        }
 
        public function getForm($data = array(), $loadData = true)
        {
                // Get the form.
                $form = $this->loadForm('com_helloworld.helloworld', 'helloworld', array('control' => 'jform', 'load_data' => $loadData));
                if (empty($form)) {
                        return false;
                }
 
                return $form;
        }
 
        protected function loadFormData()
        {
                // Check the session for previously entered form data.
                $data = JFactory::getApplication()->getUserState('com_helloworld.edit.helloworld.data', array());
 
                if (empty($data)) {
                        $data = $this->getItem();
                }
 
                return $data;
        }
}

This model inherits from the JModelAdmin class and uses its loadForm method. This method searches for forms in the forms folder. With your favorite file manager and editor, put a file admin/models/forms/helloworld.xml containing:

admin/models/forms/helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<form>
        <fieldset>
                <field
                        name="id"
                        type="hidden"
                />
                <field
                        name="greeting"
                        type="text"
                        label="COM_HELLOWORLD_HELLOWORLD_GREETING"
                        description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
                        size="40"
                        class="inputbox"
                        default=""
                />
        </fieldset>
</form>

Packaging the component

Content of your code directory


Create a compressed file of this directory or directly download the archive, modify the code in /admin/models/helloworld.php and install it using the extension manager of Joomla!1.6. You can add a menu item of this component using the menu manager in the backend.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">
        <name>COM_HELLOWORLD</name>
        <creationDate>November 2009</creationDate>
        <author>John Doe</author>
        <authorEmail>john.doe@example.org</authorEmail>
        <authorUrl>http://www.example.org</authorUrl>
        <copyright>Copyright Info</copyright>
        <license>License Info</license>
        <version>0.0.9</version>
        <description>COM_HELLOWORLD_DESC</description>
 
        <install> <!-- Runs on install -->
                <sql>
                        <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
                </sql>
        </install>
        <uninstall> <!-- Runs on uninstall -->
                <sql>
                        <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
                </sql>
        </uninstall>
 
        <files folder="site">
                <filename>index.html</filename>
                <filename>helloworld.php</filename>
                <filename>controller.php</filename>
                <folder>views</folder>
                <folder>models</folder>
                <folder>language</folder>
        </files>
 
        <administration>
                <menu>COM_HELLOWORLD_MENU</menu>
                <files folder="admin">
                        <filename>index.html</filename>
                        <filename>helloworld.php</filename>
                        <filename>controller.php</filename>
                        <folder>sql</folder>
                        <folder>tables</folder>
                        <folder>models</folder>
                        <folder>views</folder>
                        <folder>controllers</folder>
                </files>                
                <languages folder="admin">
                        <language tag="en-GB">language/en-GB/en-GB.com_helloworld.ini</language>
                        <language tag="en-GB">language/en-GB/en-GB.com_helloworld.sys.ini</language>
                </languages>
        </administration>
</extension>

Navigate

Prev: Adding language management Next: Adding decorations to the backend

Contributors