Create Joomla! 3.x extensions - Manage the Back End- Part 1

Creating extensions for Joomla! 2.5 & 3.x - Managing the Back - Part 1 view records

Writing extensions with Marco's Component Maker for Joomla! is quite easy since it is the program that takes care to write the skeleton for models, views and templates for editing. You have only to define the business logic for data managing, but this is your own task.


You need to install the example component created in previous article.

Back End - Part 1 view records

Each component of Joomla! has, whether we speak of front end or back end, an entry point. This is a PHP file, named as the component, which receives the requests that must be managed by the component. This file will then define which view and which controller to load.


Component Entry point

Since version 1.5.0 you don't need to edit entry point to make component work because a sub menu entry is created automatically for any controller (/model/table) to allow record listing. Anyway you may need to change the menu order or remove items for auxiliary controllers

The file generated by marco's component maker includes code definition for menu generation in line 23:

... [administrator/components/com_mybooks/mybooks.php]
$views = explode(',', 'authorlist,booklist,editorlist');
$view = $views[0];

... omissis

if (in_array($view, $views)) foreach($views as $v){
    $link = JRoute::_("index.php?option=com_mybooks&view={$v}");
    $selected = ($v == $view);
    /* ... simplified code! */
    JSubMenuHelper::addEntry(JText::_( strtoupper('COM_mybooks_MENU_' . $v) ), "index.php?option=com_mybooks&view={$v}", $selected);

$views are defined on line 23, modify definition to change item's order in sub menu or to remove submenu items, change order to make booklist the default view:

$views = explode(',', 'booklist,authorlist,editorlist');

File mybooks.php, loads the default controller, which contains the code needed to load the required files (view & model) and to perform the required tasks ($task) if any, or the default one (display). If no view is set in $_REQUEST the first item in $views array is selected.

For details on execution flow in Joomla! framework refer to official documentation. See also doc on building an MVC component to understand how this extension works.


Listing records

We set booklist (\views\booklist\view.html.php) as default view, it means that Joomla! will also loads booklist controller (\controllers\booklist.php) and model (\models\booklist.php). booklist model references to a database table with two foreign keys (author_id and editor_id): if we want to display author's and editor's name we have to modify the query in the model code.

Open \models\booklist.php and search for getListQuery() method. This is a protected (model's only) function, used to select the data to retreive.

$query = '
SELECT `t1`.* , t2.`name` AS `t2___name`, t3.`name` AS `t3___name` 
FROM `#__mybook_books` `t1` 
INNER JOIN `#__mybook_authors` t2 ON t1.`author_id`=t2.`author_id` 
INNER JOIN `#__mybook_editors` t3 ON t1.`editor_id`=t3.`editor_id`  ' . 
$this->_buildQueryWhere() . $this->_buildQueryOrderBy();

This is the code generated by the team version of the Joomla! component generator, you can write the query as you like. Please note that the generator writes always INNER JOINs, for performances reasons; so, if you need an OUTER JOIN, you have to change the query code.

Within the file /components/com_mybooks/views/booklist/tmpl/default.php you will find two areas marked by the delimiters "<! - Joomla!Component Builder - begin code ->" and "<! - Joomla!Component Builder - end code ->";these are placed in the header of the table and within the table itself. In these areas, Joomla Component Builder writes the fields retrieved from the table in the db so they are viewed:/edited. Fields in this View are selected during component creation in fields table editing ('Show in List'& 'Show in Edit').

We have to edit the template to show the new fields for author and editor name

Into the head of the table search for:

<th class="jcb_fieldDiv jcb_fieldLabel"><?php $numCols++; ?>
  <?php echo JHTML::_('grid.sort', JText::_('COM_MYBOOKS_BOOK_AUTHOR_ID_LABEL'), 'AUTHOR_ID', $this->lists['order_Dir'], $this->lists['order']); ?>
<th class="jcb_fieldDiv jcb_fieldLabel"><?php $numCols++; ?>
  <?php echo JHTML::_('grid.sort', JText::_('COM_MYBOOKS_BOOK_EDITOR_ID_LABEL'), 'EDITOR_ID', $this->lists['order_Dir'], $this->lists['order']); ?>

and replace with:

<th class="jcb_fieldDiv jcb_fieldLabel"><?php $numCols++; ?>
  <?php echo JHTML::_('grid.sort', JText::_('COM_MYBOOKS_AUTHOR_NAME_LABEL'), 't2___name', $this->lists['order_Dir'], $this->lists['order']); ?>
<th class="jcb_fieldDiv jcb_fieldLabel"><?php $numCols++; ?>
  <?php echo JHTML::_('grid.sort', JText::_('COM_MYBOOKS_EDITOR_NAME_LABELL'), 't3___name', $this->lists['order_Dir'], $this->lists['order']); ?>

Note: string constant are already defined in translation files.


Now, change the listing, into the body of the table search for:

<td class="jcb_fieldDiv jcb_fieldValue">
  <?php echo $row->author_id; ?>
<td class="jcb_fieldDiv jcb_fieldValue">
  <?php echo $row->editor_id; ?>

and replace with:

<td class="jcb_fieldDiv jcb_fieldValue">
  <?php echo $row->t2___name; ?>
<td class="jcb_fieldDiv jcb_fieldValue">
  <?php echo $row->t3___name; ?>


Ordering Records

If you use only the fields in the table, list sorting works out of the box; but we have inserted two related fields and we have referenced them with an alias (t2___name, t3___name), so we have to modify the function which builds the query. The alias is obtained by joining the alias of the table (tx) to the field name using three times the character '_'.

Using Marco's Component Builder v2.5.04+, field are matched with /^t\d___/ and automatically spitted in to table prefix and field name, but in previous versions we have to check if the field name for sorting starts with `t[n]___` and we need to split it.

search into the _buildQueryOrderBy() private method, for the following instruction:

$prefix = $this->getPrefix($filter_order); 

and replace with:

if(preg_match("/^t\d___/", $filter_order)){
  list ($prefix, $filter_order) = explode ('___', $fieldName, 2);
  $prefix = $this->getPrefix($filter_order); 

or write the code you prefer to split $filter_order. This method writes the code to append to the default query built in getListQuery() function.

Now, you have to add both new fields name (t2___name, t3___name) in $allowedOrders definition, the list of sortable fields.

For safery reasons you have to modify $allowedOrders, in _buildQueryOrderBy(), to specify the fields allowed in ordering. Fields name passed to SQL MUST be safe.
Se also $allowedSearch in _buildQueryWhere() method.


Searching records

If you use only the fields in the table, list sorting works out of the box, anyway a warning to check the code will appear. If you modify the query, to include related fields from other tables, and if you want to search also in these tables, you have to modify the _buildQueryWhere() private method: see previous point (Ordering records)


Next Step

Since version 1.5.0 Marco's Component Maker for Joomla! writes codes fully functional out of the box, anywhere, here and there, adjustments are needed (ie: to manage select list and relation): in the next article Create Joomla! extensions - Manage the Back End- Part 2 we will see how to do this .


Aggiungi commento

Please note: URL in text are not linked and site address is only for internal use.

Comments are human checked. All spam will be removed, so don't waste your time!

Codice di sicurezza

L'estate si avvicina hai già trovato l'albergo per le vacanze? cerca un albergo al mare o un hotel in montagna ove trascorrere le tue vacanze.