Desarrollando un componente MVC : Añadiendo acciones backend
[extoc]
Añadiendo una barra de herramientas
En Joomla, el administrador interactua generalmente con los componentes mediante el uso de una barra de herramientas. En el fichero admin/views/helloworlds/view.html.php pon este contenido. Creará una barra de herramientas básica y un título para el componente.
Los argumentos usados en el ejemplo. JToolBarHelper::addNew se utiliza para establecer una instancia del controlador que será utilizado cuando se pulse el botón.
admin/views/helloworlds/view.html.php
get('Items');
$pagination = $this->get('Pagination');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('
', $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');
}
}
Puedes encontrar otras acciones clásicas del backend en el fichero administrator/includes/toolbar.php de tu instalación Joomla.
Ya que la vista puede realizar algunas acciones, tenemos que introducir algunos datos. Con tu editor de texto favorito, pon lo siguiente en el fichero admin/views/helloworlds/tmpl/default.php
Añadiendo controladores específicos
Se han añadido tres acciones:
- helloworlds.delete
- helloworld.edit
- helloworld.add
Estaos son tareas compuestas (controller.task). Así que tenemos que codificar dos nuevos controladores HelloWorldControllerHelloWorlds y HelloWorldControllerHelloWorld.
admin/controllers/helloworlds.php
true));
return $model;
}
}
admin/controllers/helloworld.php
Añadiendo una vista de edición
Crea el fichero admin/views/helloworld/view.html.php con el siguiente contenido:
get('Form');
$item = $this->get('Item');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('
', $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');
}
}
Esta vista mostrará datos mediante un diseño (layout).
Crea el fichero admin/views/helloworld/tmpl/edit.php que contenga:
Añadiendo un modelo y modificando el existente
La vista HelloWorldViewHelloWorld solicita el formulario y los datos de un modelo. Este modelo tiene que proveer un método getTable, un método getForm y un método loadData (llamado desde el controlador JModelAdmin)
admin/models/helloworld.php
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;
}
}
Este modelo hereda de otra clase JModelAdmin y utiliza el método loadForm. Este método buscará formularios en el directorio forms.
Con tu editor favorito, crea el fichero admin/models/forms/helloworld.xml con el siguiente contenido:
Empaquetando el componente
Contenido de tu directorio
- 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
Crea un archivo comprimido de este directorio o directamente descarga el archivo, modifica el código en /admin/models/helloworld.php e instalalo mediante el gestor de extensiones de Joomla. Puedes añadir un elemento de menú de este componente usando el administrador de menús en el backend.
helloworld.xml
Hello World!
November 2009
John Doe
john.doe@example.org
http://www.example.org
Copyright Info
License Info
0.0.9
COM_HELLOWORLD_DESCRIPTION
sql/install.mysql.utf8.sql
sql/uninstall.mysql.utf8.sql
sql/updates/mysql
index.html
helloworld.php
controller.php
views
models
language
index.html
helloworld.php
controller.php
sql
tables
models
views
controllers
language/en-GB/en-GB.com_helloworld.ini
language/en-GB/en-GB.com_helloworld.sys.ini