Desarrollando un componente MVC : Añadiendo configuración
[extoc]
Adding configuration parameters
The Joomla framework allows the use of parameters stored in each component. With your favorite file manager and editor, put a file admin/config.xml file containing these lines:
admin/config.xml
This file will be read by the com_config component of the Joomla core. For the moment, we defined only one parameter: is the category title displayed or not in the frontend?.
The best way to set the parameters is to put a Preferences button in a toolbar.
With your favorite editor, put these lines in admin/views/helloworlds/view.html.php
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);
// Set the document
$this->setDocument();
}
/**
* Setting the toolbar
*/
protected function addToolBar()
{
JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'), 'helloworld');
JToolBarHelper::deleteListX('', 'helloworlds.delete');
JToolBarHelper::editListX('helloworld.edit');
JToolBarHelper::addNewX('helloworld.add');
JToolBarHelper::preferences('com_helloworld');
}
/**
* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
$document = JFactory::getDocument();
$document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
}
}
Using configuration parameters as default value
We want to define this parameter individually on all HelloWorld data. With your favorite editor, put these lines into the admin/models/forms/helloworld.xml
admin/models/forms/helloworld.xml
We define the same parameter for each message with an additional value: Use global.
Modifying the SQL
Data now contains a new parameter: params. The SQL structure has to be modified.
With your favorite editor, put these lines into admin/sql/install.mysql.utf8.sql:
admin/sql/install.mysql.utf8.sql
DROP TABLE IF EXISTS `#__helloworld`;
CREATE TABLE `#__helloworld` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`greeting` varchar(25) NOT NULL,
`catid` int(11) NOT NULL DEFAULT '0',
`params` TEXT NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Good bye World!');
With your favorite editor, put these lines into admin/sql/updates/mysql/0.0.13.sql:
admin/sql/updates/mysql/0.0.13.sql
ALTER TABLE `#__helloworld` ADD `params` VARCHAR(1024) NOT NULL DEFAULT '';
The TableHelloWorld has to be modified in order to deal with these parameters: they will be stored in a JSON format and get in a JParameter class. We have to overload the bind and the load method. With your favorite editor, put these lines into admin/tables/helloworld.php
admin/tables/helloworld.php
loadArray($array['params']);
$array['params'] = (string)$parameter;
}
return parent::bind($array, $ignore);
}
/**
* Overloaded load function
*
* @param int $pk primary key
* @param boolean $reset reset data
* @return boolean
* @see JTable:load
*/
public function load($pk = null, $reset = true)
{
if (parent::load($pk, $reset))
{
// Convert the params field to a registry.
$params = new JRegistry;
// loadJSON is @deprecated 12.1 Use loadString passing JSON as the format instead.
// $params->loadString($this->item->params, 'JSON');
// "item" should not be present.
$params->loadJSON($this->params);
$this->params = $params;
return true;
}
else
{
return false;
}
}
}
Modifying the backend
The backend edit view has to display the options to the administrator. With your favorite editor, put these lines into the admin/views/helloworld/tmpl/edit.php file:
admin/views/helloworld/tmpl/edit.php
form->getFieldsets('params');
?>
Modifying the frontend
The frontend has to be modified according to the new show_category parameter.
We have to modify the model:
- it has to merge global parameters and individual parameters
- it has to provide the category
With your favorite editor, put these lines into the site/models/helloworld.php file:
site/models/helloworld.php
input;
$id = $input->getInt('id');
$this->setState('message.id', $id);
// Load the parameters.
$params = $app->getParams();
$this->setState('params', $params);
parent::populateState();
}
/**
* 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);
}
/**
* Get the message
* @return object The message to be displayed to the user
*/
public function getItem()
{
if (!isset($this->item))
{
$id = $this->getState('message.id');
$this->_db->setQuery($this->_db->getQuery(true)
->from('#__helloworld as h')
->leftJoin('#__categories as c ON h.catid=c.id')
->select('h.greeting, h.params, c.title as category')
->where('h.id=' . (int)$id));
if (!$this->item = $this->_db->loadObject())
{
$this->setError($this->_db->getError());
}
else
{
// Load the JSON string
$params = new JRegistry;
// loadJSON is @deprecated 12.1 Use loadString passing JSON as the format instead.
//$params->loadString($this->item->params, 'JSON');
$params->loadJSON($this->item->params);
$this->item->params = $params;
// Merge global params with item params
$params = clone $this->getState('params');
$params->merge($this->item->params);
$this->item->params = $params;
}
}
return $this->item;
}
}
The view has to ask the model for the category. With your favorite editor, put these lines into the site/views/helloworld/view.html.php
site/views/helloworld/view.html.php
item = $this->get('Item');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('
', $errors));
return false;
}
// Display the view
parent::display($tpl);
}
}
The layout can now display correctly the category or not. With your favorite editor, put these lines into site/views/helloworld/tmpl/default.php
site/views/helloworld/tmpl/default.php
item->greeting.(($this->item->category and $this->item->params->get('show_category'))
? (' ('.$this->item->category.')') : ''); ?>
Adding some translation strings
Some strings have to be added in the admin/language/en-GB/en-GB.com_helloworld.ini file
admin/language/en-GB/en-GB.com_helloworld.ini
COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
COM_HELLOWORLD_ADMINISTRATION_CATEGORIES="HelloWorld - Categories"
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE="Some values are unacceptable"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC="The category the messages belongs to"
COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL="Category"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL="Show category"
COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC="If set to Show, the title of the message’s category will show."
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="Id"
COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT="HelloWorld manager: Edit Message"
COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW="HelloWorld manager: New Message"
COM_HELLOWORLD_MANAGER_HELLOWORLDS="HelloWorld manager"
COM_HELLOWORLD_N_ITEMS_DELETED_1="One message deleted"
COM_HELLOWORLD_N_ITEMS_DELETED_MORE="%d messages deleted"
COM_HELLOWORLD_SUBMENU_MESSAGES="Messages"
COM_HELLOWORLD_SUBMENU_CATEGORIES="Categories"
COM_HELLOWORLD_CONFIGURATION="HelloWorld Configuration"
COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL="Messages settings"
COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC="Settings that will be applied to all messages by default"
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/config.xml
- 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/sql/updates/mysql/0.0.12.sql
- admin/sql/updates/mysql/0.0.13.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/forms/helloworld.js
- admin/models/rules/index.html
- admin/models/rules/greeting.php
- 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/submitbutton.js
- admin/views/helloworld/tmpl/index.html
- admin/views/helloworld/tmpl/edit.php
- admin/helpers/index.html
- admin/helpers/helloworld.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
- media/index.html
- media/images/index.html
- media/images/tux-16×16.png
- media/images/tux-48×48.png
Create a compressed file of this directory or directly download the archive 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
COM_HELLOWORLD
November 2009
John Doe
john.doe@example.org
http://www.example.org
Copyright Info
License Info
0.0.13
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
images
index.html
config.xml
helloworld.php
controller.php
sql
tables
models
views
controllers
helpers
language/en-GB/en-GB.com_helloworld.ini
language/en-GB/en-GB.com_helloworld.sys.ini