J2.5:Developing a MVC Component/Adding backend actions
m (Removed external link.) |
|||
| (40 intermediate revisions by 15 users not shown) | |||
| Line 1: | Line 1: | ||
| − | {{ | + | {{version/tutor|2.5}} |
| − | + | {{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Contents}} | |
| − | + | ||
| − | {{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla! | + | |
== Introduction == | == Introduction == | ||
| − | This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla! | + | This tutorial is part of the [[Developing a Model-View-Controller (MVC) Component for Joomla!2.5]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this. |
== Adding a toolbar == | == Adding a toolbar == | ||
| − | In Joomla | + | In Joomla, 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. |
<span id ="admin/views/helloworlds/view.html.php"> | <span id ="admin/views/helloworlds/view.html.php"> | ||
| Line 16: | Line 14: | ||
// No direct access to this file | // No direct access to this file | ||
defined('_JEXEC') or die('Restricted access'); | defined('_JEXEC') or die('Restricted access'); | ||
| + | |||
// import Joomla view library | // import Joomla view library | ||
jimport('joomla.application.component.view'); | jimport('joomla.application.component.view'); | ||
| + | |||
/** | /** | ||
| − | * | + | * HelloWorlds View |
*/ | */ | ||
class HelloWorldViewHelloWorlds extends JView | class HelloWorldViewHelloWorlds extends JView | ||
{ | { | ||
| − | |||
/** | /** | ||
| − | * | + | * HelloWorlds view display method |
* @return void | * @return void | ||
*/ | */ | ||
| Line 37: | Line 36: | ||
if (count($errors = $this->get('Errors'))) | if (count($errors = $this->get('Errors'))) | ||
{ | { | ||
| − | JError::raiseError(500, implode( | + | JError::raiseError(500, implode('<br />', $errors)); |
return false; | return false; | ||
} | } | ||
| − | |||
// Assign data to the view | // Assign data to the view | ||
$this->items = $items; | $this->items = $items; | ||
$this->pagination = $pagination; | $this->pagination = $pagination; | ||
| + | |||
// Set the toolbar | // Set the toolbar | ||
$this->addToolBar(); | $this->addToolBar(); | ||
| + | |||
// Display the template | // Display the template | ||
parent::display($tpl); | parent::display($tpl); | ||
| Line 56: | Line 56: | ||
{ | { | ||
JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS')); | JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS')); | ||
| − | JToolBarHelper:: | + | JToolBarHelper::deleteList('', 'helloworlds.delete'); |
| − | JToolBarHelper:: | + | JToolBarHelper::editList('helloworld.edit'); |
| − | JToolBarHelper:: | + | JToolBarHelper::addNew('helloworld.add'); |
} | } | ||
} | } | ||
| Line 64: | Line 64: | ||
</span> | </span> | ||
| − | You can find | + | You can find other classic backend actions in the ''administrator/includes/toolbar.php'' file of your Joomla installation. |
| + | |||
| + | Since the view can perform some actions, we have to add some input data. With your favorite file manager and editor, put in the file ''admin/views/helloworlds/tmpl/default.php'' | ||
| + | |||
| + | <span id="admin/views/helloworlds/tmpl/default.php"> | ||
| + | ''admin/views/helloworlds/tmpl/default.php'' | ||
| + | <source lang="php"> | ||
| + | <?php | ||
| + | // No direct access to this file | ||
| + | defined('_JEXEC') or die('Restricted Access'); | ||
| + | // load tooltip behavior | ||
| + | JHtml::_('behavior.tooltip'); | ||
| + | ?> | ||
| + | <form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>" method="post" name="adminForm" id="adminForm"> | ||
| + | <table class="adminlist"> | ||
| + | <thead><?php echo $this->loadTemplate('head');?></thead> | ||
| + | <tfoot><?php echo $this->loadTemplate('foot');?></tfoot> | ||
| + | <tbody><?php echo $this->loadTemplate('body');?></tbody> | ||
| + | </table> | ||
| + | <div> | ||
| + | <input type="hidden" name="task" value="" /> | ||
| + | <input type="hidden" name="boxchecked" value="0" /> | ||
| + | <?php echo JHtml::_('form.token'); ?> | ||
| + | </div> | ||
| + | </form> | ||
| + | </source> | ||
| + | </span> | ||
== Adding specific controllers == | == Adding specific controllers == | ||
| − | Three actions | + | Three actions have been added: |
* ''helloworlds.delete'' | * ''helloworlds.delete'' | ||
* ''helloworld.edit'' | * ''helloworld.edit'' | ||
* ''helloworld.add'' | * ''helloworld.add'' | ||
| + | read more about [[JController_and_its_subclass_usage_overview|subcontrollers...]] | ||
| − | These are compound tasks (''controller''.''task''). So two new controllers ''HelloWorldControllerHelloWorlds'' and ''HelloWorldControllerHelloWorld'' have to coded. | + | These are compound tasks (''controller''.''task''). So two new controllers ''HelloWorldControllerHelloWorlds'' and ''HelloWorldControllerHelloWorld'' have to be coded. |
<span id="admin/controllers/helloworlds.php"> | <span id="admin/controllers/helloworlds.php"> | ||
| Line 80: | Line 107: | ||
// No direct access to this file | // No direct access to this file | ||
defined('_JEXEC') or die('Restricted access'); | defined('_JEXEC') or die('Restricted access'); | ||
| − | |||
| − | |||
| + | // import Joomla controlleradmin library | ||
| + | jimport('joomla.application.component.controlleradmin'); | ||
| + | |||
| + | /** | ||
| + | * HelloWorlds Controller | ||
| + | */ | ||
class HelloWorldControllerHelloWorlds extends JControllerAdmin | class HelloWorldControllerHelloWorlds extends JControllerAdmin | ||
{ | { | ||
| − | |||
/** | /** | ||
* Proxy for getModel. | * Proxy for getModel. | ||
| − | * @since | + | * @since 2.5 |
*/ | */ | ||
public function getModel($name = 'HelloWorld', $prefix = 'HelloWorldModel') | public function getModel($name = 'HelloWorld', $prefix = 'HelloWorldModel') | ||
| Line 105: | Line 135: | ||
// No direct access to this file | // No direct access to this file | ||
defined('_JEXEC') or die('Restricted access'); | defined('_JEXEC') or die('Restricted access'); | ||
| − | // import Joomla | + | |
| − | jimport('joomla.application.component. | + | // import Joomla controllerform library |
| + | jimport('joomla.application.component.controllerform'); | ||
| + | |||
/** | /** | ||
* HelloWorld Controller | * HelloWorld Controller | ||
| Line 125: | Line 157: | ||
// No direct access to this file | // No direct access to this file | ||
defined('_JEXEC') or die('Restricted access'); | defined('_JEXEC') or die('Restricted access'); | ||
| + | |||
// import Joomla view library | // import Joomla view library | ||
jimport('joomla.application.component.view'); | jimport('joomla.application.component.view'); | ||
| + | |||
/** | /** | ||
* HelloWorld View | * HelloWorld View | ||
| Line 132: | Line 166: | ||
class HelloWorldViewHelloWorld extends JView | class HelloWorldViewHelloWorld extends JView | ||
{ | { | ||
| − | |||
/** | /** | ||
* display method of Hello view | * display method of Hello view | ||
| Line 146: | Line 179: | ||
if (count($errors = $this->get('Errors'))) | if (count($errors = $this->get('Errors'))) | ||
{ | { | ||
| − | JError::raiseError(500, implode( | + | JError::raiseError(500, implode('<br />', $errors)); |
return false; | return false; | ||
} | } | ||
| − | |||
// Assign the Data | // Assign the Data | ||
$this->form = $form; | $this->form = $form; | ||
$this->item = $item; | $this->item = $item; | ||
| + | |||
// Set the toolbar | // Set the toolbar | ||
$this->addToolBar(); | $this->addToolBar(); | ||
| + | |||
// Display the template | // Display the template | ||
parent::display($tpl); | parent::display($tpl); | ||
| Line 164: | Line 198: | ||
protected function addToolBar() | protected function addToolBar() | ||
{ | { | ||
| − | + | $input = JFactory::getApplication()->input; | |
| − | $isNew | + | $input->set('hidemainmenu', true); |
| − | + | $isNew = ($this->item->id == 0); | |
| − | JToolBarHelper::title($isNew ? JText::_(' | + | JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW') |
| + | : JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT')); | ||
JToolBarHelper::save('helloworld.save'); | JToolBarHelper::save('helloworld.save'); | ||
| − | JToolBarHelper::cancel('helloworld.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE'); | + | JToolBarHelper::cancel('helloworld.cancel', $isNew ? 'JTOOLBAR_CANCEL' |
| + | : 'JTOOLBAR_CLOSE'); | ||
} | } | ||
} | } | ||
| Line 185: | Line 221: | ||
// No direct access | // No direct access | ||
defined('_JEXEC') or die('Restricted 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"> | + | <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"> | <fieldset class="adminform"> | ||
| − | <legend><?php echo JText::_( ' | + | <legend><?php echo JText::_( 'COM_HELLOWORLD_HELLOWORLD_DETAILS' ); ?></legend> |
| − | <?php foreach($this->form->getFieldset() as $field): ?> | + | <ul class="adminformlist"> |
| − | < | + | <?php foreach($this->form->getFieldset() as $field): ?> |
| − | + | <li><?php echo $field->label;echo $field->input;?></li> | |
| − | + | <?php endforeach; ?> | |
| − | + | </ul> | |
| − | + | ||
</fieldset> | </fieldset> | ||
| − | <input type="hidden" name="task" value="" /> | + | <div> |
| − | + | <input type="hidden" name="task" value="helloworld.edit" /> | |
| + | <?php echo JHtml::_('form.token'); ?> | ||
| + | </div> | ||
</form> | </form> | ||
</source> | </source> | ||
| Line 211: | Line 249: | ||
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
| + | // No direct access to this file | ||
defined('_JEXEC') or die('Restricted access'); | defined('_JEXEC') or die('Restricted access'); | ||
| + | |||
// import Joomla modelform library | // import Joomla modelform library | ||
jimport('joomla.application.component.modeladmin'); | jimport('joomla.application.component.modeladmin'); | ||
| Line 220: | Line 260: | ||
class HelloWorldModelHelloWorld extends JModelAdmin | class HelloWorldModelHelloWorld extends JModelAdmin | ||
{ | { | ||
| − | + | /** | |
| − | public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array()) | + | * Returns a reference to the a Table object, always creating it. |
| + | * | ||
| + | * @param type The table type to instantiate | ||
| + | * @param string A prefix for the table class name. Optional. | ||
| + | * @param array Configuration array for model. Optional. | ||
| + | * @return JTable A database object | ||
| + | * @since 2.5 | ||
| + | */ | ||
| + | public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array()) | ||
{ | { | ||
return JTable::getInstance($type, $prefix, $config); | return JTable::getInstance($type, $prefix, $config); | ||
} | } | ||
| − | + | /** | |
| − | public function getForm($data = array(), $loadData = true) | + | * Method to get the record form. |
| + | * | ||
| + | * @param array $data Data for the form. | ||
| + | * @param boolean $loadData True if the form is to load its own data (default case), false if not. | ||
| + | * @return mixed A JForm object on success, false on failure | ||
| + | * @since 2.5 | ||
| + | */ | ||
| + | public function getForm($data = array(), $loadData = true) | ||
{ | { | ||
// Get the form. | // Get the form. | ||
| − | $form = $this->loadForm('com_helloworld.helloworld', 'helloworld', array('control' => 'jform', 'load_data' => $loadData)); | + | $form = $this->loadForm('com_helloworld.helloworld', 'helloworld', |
| − | if (empty($form)) { | + | array('control' => 'jform', 'load_data' => $loadData)); |
| + | if (empty($form)) | ||
| + | { | ||
return false; | return false; | ||
} | } | ||
| − | |||
return $form; | return $form; | ||
} | } | ||
| − | + | /** | |
| − | protected function loadFormData() | + | * Method to get the data that should be injected in the form. |
| + | * | ||
| + | * @return mixed The data for the form. | ||
| + | * @since 2.5 | ||
| + | */ | ||
| + | protected function loadFormData() | ||
{ | { | ||
// Check the session for previously entered form data. | // Check the session for previously entered form data. | ||
$data = JFactory::getApplication()->getUserState('com_helloworld.edit.helloworld.data', array()); | $data = JFactory::getApplication()->getUserState('com_helloworld.edit.helloworld.data', array()); | ||
| − | + | if (empty($data)) | |
| − | if (empty($data)) { | + | { |
$data = $this->getItem(); | $data = $this->getItem(); | ||
} | } | ||
| − | |||
return $data; | return $data; | ||
} | } | ||
| Line 267: | Line 327: | ||
name="greeting" | name="greeting" | ||
type="text" | type="text" | ||
| − | label=" | + | label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL" |
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC" | description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC" | ||
size="40" | size="40" | ||
| Line 274: | Line 334: | ||
/> | /> | ||
</fieldset> | </fieldset> | ||
| − | </form></source> | + | </form> |
| + | </source> | ||
</span> | </span> | ||
| Line 281: | Line 342: | ||
Content of your code directory | Content of your code directory | ||
* ''[[#helloworld.xml|helloworld.xml]]'' | * ''[[#helloworld.xml|helloworld.xml]]'' | ||
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_02#site/helloworld.php|site/helloworld.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_02#site/controller.php|site/controller.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_04#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_02#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/models/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#site/models/helloworld.php|site/models/helloworld.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/language/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|site/language/en-GB/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/helloworld.php|admin/helloworld.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/controller.php|admin/controller.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!1. | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/index.html]]'' |
| + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/fields/index.html]]'' | ||
| + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]'' | ||
| + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/models/forms/index.html]]'' | ||
* ''[[#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]'' | * ''[[#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]'' | ||
* ''[[#admin/models/helloworld.php|admin/models/helloworld.php]]'' | * ''[[#admin/models/helloworld.php|admin/models/helloworld.php]]'' | ||
* ''[[#admin/models/helloworlds.php|admin/models/helloworlds.php]]'' | * ''[[#admin/models/helloworlds.php|admin/models/helloworlds.php]]'' | ||
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworlds/index.html]]'' |
* ''[[#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]'' | * ''[[#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]'' | ||
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworlds/tmpl/index.html]]'' |
| − | * ''[[ | + | * ''[[#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/views/helloworlds/tmpl/default_head.php|admin/views/helloworlds/tmpl/default_head.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/views/helloworlds/tmpl/default_body.php|admin/views/helloworlds/tmpl/default_body.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_07#admin/views/helloworlds/tmpl/default_foot.php|admin/views/helloworlds/tmpl/default_foot.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworld/index.html]]'' |
* ''[[#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]'' | * ''[[#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]'' | ||
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/views/helloworld/tmpl/index.html]]'' |
| − | * ''[[#admin/views/helloworld/tmpl/ | + | * ''[[#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/tables/index.html]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06#admin/tables/helloworld.php|admin/tables/helloworld.php]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#admin/language/en-GB/en-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#admin/language/en-GB/en-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]'' |
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01#index.html|admin/controllers/index.html]]'' |
* ''[[#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]'' | * ''[[#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]'' | ||
* ''[[#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]'' | * ''[[#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]'' | ||
| − | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla! | + | * ''[[Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_08#language/en-GB/en-GB.ini|language/en-GB/en-GB.ini]]'' |
| − | Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/ | + | Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/58407/com_helloworld-1.6-part09.zip archive], modify the code in /admin/models/helloworld.php and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend. |
<span id="helloworld.xml"> | <span id="helloworld.xml"> | ||
| Line 337: | Line 401: | ||
<source lang="xml"> | <source lang="xml"> | ||
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||
| − | <extension type="component" version=" | + | <extension type="component" version="2.5.0" method="upgrade"> |
| − | <name> | + | |
| + | <name>Hello World!</name> | ||
| + | <!-- The following elements are optional and free of formatting constraints --> | ||
<creationDate>November 2009</creationDate> | <creationDate>November 2009</creationDate> | ||
<author>John Doe</author> | <author>John Doe</author> | ||
| Line 345: | Line 411: | ||
<copyright>Copyright Info</copyright> | <copyright>Copyright Info</copyright> | ||
<license>License Info</license> | <license>License Info</license> | ||
| + | <!-- The version string is recorded in the components table --> | ||
<version>0.0.9</version> | <version>0.0.9</version> | ||
| − | <description> | + | <!-- The description is optional and defaults to the name --> |
| + | <description>COM_HELLOWORLD_DESCRIPTION</description> | ||
<install> <!-- Runs on install --> | <install> <!-- Runs on install --> | ||
| Line 358: | Line 426: | ||
</sql> | </sql> | ||
</uninstall> | </uninstall> | ||
| + | <update> <!-- Runs on update; New in 2.5 --> | ||
| + | <schemas> | ||
| + | <schemapath type="mysql">sql/updates/mysql</schemapath> | ||
| + | </schemas> | ||
| + | </update> | ||
| + | <!-- Site Main File Copy Section --> | ||
| + | <!-- Note the folder attribute: This attribute describes the folder | ||
| + | to copy FROM in the package to install therefore files copied | ||
| + | in this section are copied from /site/ in the package --> | ||
<files folder="site"> | <files folder="site"> | ||
<filename>index.html</filename> | <filename>index.html</filename> | ||
| Line 369: | Line 446: | ||
<administration> | <administration> | ||
| + | <!-- Administration Menu Section --> | ||
<menu>COM_HELLOWORLD_MENU</menu> | <menu>COM_HELLOWORLD_MENU</menu> | ||
| + | <!-- Administration Main File Copy Section --> | ||
| + | <!-- Note the folder attribute: This attribute describes the folder | ||
| + | to copy FROM in the package to install therefore files copied | ||
| + | in this section are copied from /admin/ in the package --> | ||
<files folder="admin"> | <files folder="admin"> | ||
| + | <!-- Admin Main File Copy Section --> | ||
<filename>index.html</filename> | <filename>index.html</filename> | ||
<filename>helloworld.php</filename> | <filename>helloworld.php</filename> | ||
<filename>controller.php</filename> | <filename>controller.php</filename> | ||
| + | <!-- SQL files section --> | ||
<folder>sql</folder> | <folder>sql</folder> | ||
| + | <!-- tables files section --> | ||
<folder>tables</folder> | <folder>tables</folder> | ||
| + | <!-- models files section --> | ||
<folder>models</folder> | <folder>models</folder> | ||
| + | <!-- views files section --> | ||
<folder>views</folder> | <folder>views</folder> | ||
| + | <!-- controllers files section --> | ||
<folder>controllers</folder> | <folder>controllers</folder> | ||
| − | </files> | + | </files> |
| + | |||
<languages folder="admin"> | <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.ini</language> | ||
| Line 385: | Line 474: | ||
</languages> | </languages> | ||
</administration> | </administration> | ||
| + | |||
</extension> | </extension> | ||
</source> | </source> | ||
| Line 391: | Line 481: | ||
== Navigate == | == Navigate == | ||
| − | [[Developing a Model-View-Controller (MVC) Component for Joomla! | + | [[Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 08|Prev: Adding language management]] |
| − | [[Developing a Model-View-Controller (MVC) Component for Joomla! | + | [[Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 10|Next: Adding decorations to the backend]] |
== Contributors == | == Contributors == | ||
*[[User:cdemko|Christophe Demko]] | *[[User:cdemko|Christophe Demko]] | ||
| + | *[[User:oaksu|Ozgur Aksu]] | ||
[[Category:Development]] | [[Category:Development]] | ||
| − | [[ | + | [[Category:Joomla! 1.6]] |
| − | [[ | + | [[Category:Joomla! 1.7]] |
| + | [[Category:Joomla! 2.5]] | ||
Revision as of 18:09, 30 September 2012
Contents |
Introduction
This tutorial is part of the Developing a Model-View-Controller (MVC) Component for Joomla!2.5 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
Adding a toolbar
In Joomla, 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'); /** * HelloWorlds View */ class HelloWorldViewHelloWorlds extends JView { /** * HelloWorlds 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::deleteList('', 'helloworlds.delete'); JToolBarHelper::editList('helloworld.edit'); JToolBarHelper::addNew('helloworld.add'); } }
You can find other classic backend actions in the administrator/includes/toolbar.php file of your Joomla installation.
Since the view can perform some actions, we have to add some input data. With your favorite file manager and editor, put in the file admin/views/helloworlds/tmpl/default.php
admin/views/helloworlds/tmpl/default.php
<?php // No direct access to this file defined('_JEXEC') or die('Restricted Access'); // load tooltip behavior JHtml::_('behavior.tooltip'); ?> <form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>" method="post" name="adminForm" id="adminForm"> <table class="adminlist"> <thead><?php echo $this->loadTemplate('head');?></thead> <tfoot><?php echo $this->loadTemplate('foot');?></tfoot> <tbody><?php echo $this->loadTemplate('body');?></tbody> </table> <div> <input type="hidden" name="task" value="" /> <input type="hidden" name="boxchecked" value="0" /> <?php echo JHtml::_('form.token'); ?> </div> </form>
Adding specific controllers
Three actions have been added:
- helloworlds.delete
- helloworld.edit
- helloworld.add
read more about subcontrollers...
These are compound tasks (controller.task). So two new controllers HelloWorldControllerHelloWorlds and HelloWorldControllerHelloWorld have to be coded.
admin/controllers/helloworlds.php
<?php // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla controlleradmin library jimport('joomla.application.component.controlleradmin'); /** * HelloWorlds Controller */ class HelloWorldControllerHelloWorlds extends JControllerAdmin { /** * Proxy for getModel. * @since 2.5 */ 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 controllerform library jimport('joomla.application.component.controllerform'); /** * 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() { $input = JFactory::getApplication()->input; $input->set('hidemainmenu', true); $isNew = ($this->item->id == 0); JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW') : JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_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> <ul class="adminformlist"> <?php foreach($this->form->getFieldset() as $field): ?> <li><?php echo $field->label;echo $field->input;?></li> <?php endforeach; ?> </ul> </fieldset> <div> <input type="hidden" name="task" value="helloworld.edit" /> <?php echo JHtml::_('form.token'); ?> </div> </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 // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla modelform library jimport('joomla.application.component.modeladmin'); /** * HelloWorld Model */ class HelloWorldModelHelloWorld extends JModelAdmin { /** * Returns a reference to the a Table object, always creating it. * * @param type The table type to instantiate * @param string A prefix for the table class name. Optional. * @param array Configuration array for model. Optional. * @return JTable A database object * @since 2.5 */ public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array()) { return JTable::getInstance($type, $prefix, $config); } /** * Method to get the record form. * * @param array $data Data for the form. * @param boolean $loadData True if the form is to load its own data (default case), false if not. * @return mixed A JForm object on success, false on failure * @since 2.5 */ 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; } /** * Method to get the data that should be injected in the form. * * @return mixed The data for the form. * @since 2.5 */ 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_LABEL" description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC" size="40" class="inputbox" default="" /> </fieldset> </form>
Packaging the component
Content of your code directory
- helloworld.xml
- site/index.html
- site/helloworld.php
- site/controller.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.xml
- site/views/helloworld/tmpl/default.php
- site/models/index.html
- site/models/helloworld.php
- site/language/index.html
- site/language/en-GB/index.html
- site/language/en-GB/en-GB.com_helloworld.ini
- admin/index.html
- admin/helloworld.php
- admin/controller.php
- admin/sql/index.html
- admin/sql/install.mysql.utf8.sql
- admin/sql/uninstall.mysql.utf8.sql
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
- admin/sql/updates/mysql/0.0.6.sql
- admin/models/index.html
- admin/models/fields/index.html
- admin/models/fields/helloworld.php
- admin/models/forms/index.html
- admin/models/forms/helloworld.xml
- admin/models/helloworld.php
- admin/models/helloworlds.php
- admin/views/index.html
- admin/views/helloworlds/index.html
- admin/views/helloworlds/view.html.php
- admin/views/helloworlds/tmpl/index.html
- admin/views/helloworlds/tmpl/default.php
- admin/views/helloworlds/tmpl/default_head.php
- admin/views/helloworlds/tmpl/default_body.php
- admin/views/helloworlds/tmpl/default_foot.php
- admin/views/helloworld/index.html
- admin/views/helloworld/view.html.php
- admin/views/helloworld/tmpl/index.html
- admin/views/helloworld/tmpl/edit.php
- admin/tables/index.html
- admin/tables/helloworld.php
- admin/language/en-GB/en-GB.com_helloworld.ini
- admin/language/en-GB/en-GB.com_helloworld.sys.ini
- admin/controllers/index.html
- admin/controllers/helloworld.php
- admin/controllers/helloworlds.php
- language/en-GB/en-GB.ini
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. 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="2.5.0" method="upgrade"> <name>Hello World!</name> <!-- The following elements are optional and free of formatting constraints --> <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> <!-- The version string is recorded in the components table --> <version>0.0.9</version> <!-- The description is optional and defaults to the name --> <description>COM_HELLOWORLD_DESCRIPTION</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> <update> <!-- Runs on update; New in 2.5 --> <schemas> <schemapath type="mysql">sql/updates/mysql</schemapath> </schemas> </update> <!-- Site Main File Copy Section --> <!-- Note the folder attribute: This attribute describes the folder to copy FROM in the package to install therefore files copied in this section are copied from /site/ in the package --> <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> <!-- Administration Menu Section --> <menu>COM_HELLOWORLD_MENU</menu> <!-- Administration Main File Copy Section --> <!-- Note the folder attribute: This attribute describes the folder to copy FROM in the package to install therefore files copied in this section are copied from /admin/ in the package --> <files folder="admin"> <!-- Admin Main File Copy Section --> <filename>index.html</filename> <filename>helloworld.php</filename> <filename>controller.php</filename> <!-- SQL files section --> <folder>sql</folder> <!-- tables files section --> <folder>tables</folder> <!-- models files section --> <folder>models</folder> <!-- views files section --> <folder>views</folder> <!-- controllers files section --> <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>
Prev: Adding language management Next: Adding decorations to the backend