Desarrollando un componente MVC : Añadiendo categorías

  1. Desarrollando un componente MVC : Introducción
  2. Desarrollando un componente MVC: Desarrollando un componente básico
  3. Desarrollando un componente MVC: Añadiendo una vista en el frontend
  4. Desarrollando un componente MVC: Añadiendo un tipo de menu al frontend
  5. Desarrollando un componente MVC: Añadiendo un modelo al frontend
  6. Desarrollando un componente MVC : Añadiendo una variable request en el tipo de menu
  7. Desarrollando un componente MVC : Usando la base de datos
  8. Desarrollando un componente MVC : Basic backend
  9. Desarrollando un componente MVC : Añadiendo gestión de idioma
  10. Desarrollando un componente MVC : Añadiendo acciones backend
  11. Desarrollando un componente MVC : Añadiendo adornos al backend
  12. Desarrollando un componente MVC : Añadiendo verificaciones
  13. Desarrollando un componente MVC : Añadiendo categorías
  14. Desarrollando un componente MVC : Añadiendo configuración
  15. Desarrollando un componente MVC : Añadiendo ACL (Access Control Levels)
  16. Desarrollando un componente MVC : Añadiendo un script de instalación-desinstalación-actualización
  17. Desarrollando un componente MVC : Usando la capacidad de filtro
  18. Desarrollando un componente MVC : Añadiendo un servidor de actualización

[extoc]

Introducción

The Joomla framework has implemented the use of categories for all components. Adding categorized ability to a component is fairly simple.

Modifying the SQL

In order to manage categories, we have to change the SQL tables.

With your favorite editor, modify admin/sql/install.mysql.utf8.sql and put these lines:

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',
   PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
 
INSERT INTO `#__helloworld` (`greeting`) VALUES
        ('Hello World!'),
        ('Good bye World!');
admin/sql/updates/mysql/0.0.12.sql

ALTER TABLE `#__helloworld` ADD `catid` int(11) NOT NULL DEFAULT '0'

Modifying the form

A HelloWorld message can now belong to a category. We have to modify the editing form. In the admin/models/forms/helloworld.xml file, put these lines:

admin/models/forms/helloworld.xml


Note that the category can be 0 (representing no category).

Modifying the menu type

The HelloWorld menu type displays a drop down list of all messages. If the message is categorized, we have to add the category in this display.

In the admin/models/fields/helloworld.php file, put these lines:

admin/models/fields/helloworld.php

getQuery(true); // THIS IS THE FIX, WARNING IT MUST BE FIXED IN THE ZIP FILES
 
                $query->select('#__helloworld.id as id,greeting,#__categories.title as category,catid');
                $query->from('#__helloworld');
                $query->leftJoin('#__categories on catid=#__categories.id');
                $db->setQuery((string)$query);
                $messages = $db->loadObjectList();
                $options = array();
                if ($messages)
                {
                        foreach($messages as $message) 
                        {
                                $options[] = JHtml::_('select.option', $message->id, $message->greeting .
                                                      ($message->catid ? ' (' . $message->category . ')' : ''));
                        }
                }
                $options = array_merge(parent::getOptions(), $options);
                return $options;
        }
}

It will now display the category between parenthesis.

Managing the submenu

The com_categories component allows to set the submenu using a helper file. With your favorite file manager and editor, put a admin/helpers/helloworld.php file containing these lines:

admin/helpers/helloworld.php

addStyleDeclaration('.icon-48-helloworld ' .
                                               '{background-image: url(../media/com_helloworld/images/tux-48x48.png);}');
                if ($submenu == 'categories') 
                {
                        $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
                }
        }
}

NOTE: You MUST use your component name (without com_) for the helper file name, or else your submenus won’t show in category view.

This function will be automatically called by the com_categories component. Note that it will

  • change the submenu
  • change some css properties (for displaying icons)
  • change the browser title if the submenu is categories
  • change the title and add a preferences button

We have to change the general controller to call this function and modify the component entry point (the .icon-48-helloworld css class is now set in the addSubmenu function)

admin/controller.php

input;
                $input->set('view', $input->getCmd('view', 'HelloWorlds'));
 
                // call parent behavior
                parent::display($cachable, $urlparams);
 
                // Set the submenu
                HelloWorldHelper::addSubmenu('messages');
        }
}

Adding some ACL

admin/access.xml



        

NOTE: If you don’t add this file, buttons «New» «Edit» and … don’t show in category view . for more information read section Adding ACL on top of the page.

admin/helloworld.php

input;
$controller->execute($input->getCmd('task'));
 
// Redirect if set by the controller
$controller->redirect();

Adding some translation strings

Some strings have to be translated. In the admin/language/en-GB/en-GB.com_helloworld.ini file, put these lines:

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_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"

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/access.xml
  • 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/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/helloworlds/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.menu.ini
  • admin/controllers/index.html
  • admin/controllers/helloworld.php
  • admin/controllers/helloworldlist.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. 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.12
        
        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
        
 
        
                
                COM_HELLOWORLD_MENU
                
                
                
                        
                        index.html
                        helloworld.php
                        controller.php
           
                        access.xml
                        
                        sql
                        
                        tables
                        
                        models
                        
                        views
                        
                        controllers
                        
                        helpers
                
 
                
                        language/en-GB/en-GB.com_helloworld.ini
                        language/en-GB/en-GB.com_helloworld.sys.ini
                
        
 

Zips

Download the zip file for this Part: [1]

Deja un comentario