Archived

Difference between revisions of "Developing a MVC Component/Adding categories"

From Joomla! Documentation

< Archived:Developing a MVC Component
Line 382: Line 382:
 
* ''media/images/tux-48x48.png''
 
* ''media/images/tux-48x48.png''
  
Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/46017/com_helloworld-1.6-part08.zip 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.
+
Create a compressed file of this directory or directly download the [http://joomlacode.org/gf/download/frsrelease/11394/46150/com_helloworld-1.6-part12.zip 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.
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">

Revision as of 16:37, 14 November 2009

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Documentation all together tranparent small.png
Under Construction

This article or section is in the process of an expansion or major restructuring. You are welcome to assist in its construction by editing it as well. If this article or section has not been edited in several days, please remove this template.
This article was last edited by Cdemko (talk| contribs) 14 years ago. (Purge)

Template:Future

Articles in this series[edit]

Indroduction[edit]

This tutorial is part of the Developing a Model-View-Controller (MVC) Component for Joomla!1.6 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

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

Modifying the SQL[edit]

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/update.mysql.utf8.sql

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

The TableHelloWorld class must know that there exists a catid field.

In admin/tables/helloworld.php file, put these lines:

admin/tables/helloworld.php

<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
jimport('joomla.database.table');
/**
 * Hello Table class
 */
class TableHelloWorld extends JTable
{
	/**
	 * Primary Key
	 *
	 * @var int
	 */
	var $id = null;
	/**
	 * @var string
	 */
	var $greeting = null;
	/**
	 * @var int
	 */
	var $catid = null;
	/**
	 * Constructor
	 *
	 * @param object Database connector object
	 */
	function TableHelloWorld(&$db) 
	{
		parent::__construct('#__helloworld', 'id', $db);
	}
}

Modifying the form[edit]

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

<?xml version="1.0" encoding="utf-8"?>
<form>
	<fields>
		<field
			id="id"
			name="id"
			type="hidden"
		/>
		<field
			id="greeting"
			name="greeting"
			type="text"
			size="40"
			class="inputbox validate-greeting"
			validate="greeting"
			required="true"
			default=""
			label="com_helloworld_HelloWorld_Greeting"
			description="com_helloworld_HelloWorld_Greeting_Desc"
		/>
		<field
			id="catid"
			name="catid"
			type="Categories"
			extension="com_helloworld"
			allow_none="true"
			class="inputbox"
			default=""
			label="com_helloworld_HelloWorld_Category"
			description="com_helloworld_HelloWorld_Category_Desc"
			required="true"
		>
			<option value="0">JOption_No_Category</option>
		</field>
	</fields>
</form>

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

Modifying the menu type[edit]

The HelloWorld menu type display 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

<?php
// No direct access to this file
defined('_JEXEC') or die;
// import the list field type
jimport('joomla.html.html.list');
/**
 * HelloWorld Form Field class for the HelloWorld component
 */
class JFormFieldHelloWorld extends JFormFieldList
{
	/**
	 * The field type.
	 *
	 * @var		string
	 */
	protected $type = 'HelloWorld';
	/**
	 * Method to get a list of options for a list input.
	 *
	 * @return	array		An array of JHtml options.
	 */
	protected function _getOptions() 
	{
		$db = JFactory::getDBO();
		$query = new JQuery;
		$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();
		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[edit]

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

admin/helpers/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
/**
 * HelloWorld component helper.
 */
class HelloWorldHelper
{
	/**
	 * Configure the Linkbar.
	 */
	public static function addSubmenu($submenu)
	{
		JSubMenuHelper::addEntry(
			JText::_('com_helloworld_Messages'),
			'index.php?option=com_helloworld',
			$submenu=='messages');
		JSubMenuHelper::addEntry(
			JText::_('com_helloworld_Categories'),
			'index.php?option=com_categories&view=categories&extension=com_helloworld',
			$submenu=='categories');
		$document = &JFactory::getDocument();
		$document->addStyleDeclaration('.icon-48-categories {background-image: url(../media/com_helloworld/images/tux-48x48.png)!important;}');
		$document->addStyleDeclaration('.icon-48-helloworld {background-image: url(../media/com_helloworld/images/tux-48x48.png)!important;}');
		if ($submenu=='categories') $document->setTitle(JText::_('com_helloworld_Administration').' - '.JText::_('com_helloworld_Categories'));
	}
}

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

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

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
 * General Controller of HelloWorld component
 */
class HelloWorldController extends JController
{
	/**
	 * display task
	 *
	 * @return void
	 */
	function display($cachable = false) 
	{
		// set default view if not set
		JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorldList'));
		// call parent behavior
		parent::display($cachable);
		// Add submenu and icons
		require_once JPATH_COMPONENT . DS . 'helpers' . DS . 'helloworld.php';
		HelloWorldHelper::addSubmenu('messages');
	}
}

admin/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
// Redirect if set by the controller
$controller->redirect();

Adding some translation strings[edit]

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

# Joomla16.Tutorials
# Copyright (C) 2005 - 2009 Open Source Matters. All rights reserved.
# License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
# Note : All ini files need to be saved as UTF-8 - No BOM

COM_HELLOWORLD_ADMINISTRATION=HelloWorld administration
COM_HELLOWORLD_CATEGORIES=Categories
COM_HELLOWORLD_MANAGER=HelloWorld manager
COM_HELLOWORLD_MESSAGES=Messages
COM_HELLOWORLD_HELLOWORLD_CATEGORY=Category
COM_HELLOWORLD_HELLOWORLD_CATEGORY_DESC=Category of the message
COM_HELLOWORLD_HELLOWORLD_CREATING=Creating
COM_HELLOWORLD_HELLOWORLD_DETAILS=Details
COM_HELLOWORLD_HELLOWORLD_EDITING=Editing
COM_HELLOWORLD_HELLOWORLD_ERROR_SOME_VALUES_ARE_UNACCEPTABLE=Some values are unacceptable
COM_HELLOWORLD_HELLOWORLD_GREETING_DESC=Message to be displayed
COM_HELLOWORLD_HELLOWORLD_GREETING=Greeting
COM_HELLOWORLD_HELLOWORLDLIST_ARE_YOU_SURE_YOU_WANT_TO_DELETE_THESE_GREETINGS=Are you sure you want to delete these greetings?
COM_HELLOWORLD_HELLOWORLDLIST_GREETING=Greeting
COM_HELLOWORLD_HELLOWORLDLIST_GREETINGS_REMOVED=Greetings removed
COM_HELLOWORLD_HELLOWORLDLIST_ID=Id
COM_HELLOWORLD_HELLOWORLDLIST_ONE_OR_MORE_GREETINGS_COULD_NOT_BE_DELETED=One or more greetings could not be deleted: %s
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_MSG_DESC=This message will be displayed
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_MSG_LABEL=Message

Packaging the component[edit]

Content of your code directory

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

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">
	<name>Hello World!</name>
	<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>
	<version>0.0.12</version>
	<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 -->
		<sql>
			<file driver="mysql" charset="utf8">sql/update.mysql.utf8.sql</file>
		</sql>
	</update>

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

	<media destination="com_helloworld" folder="media">
		<filename>index.html</filename>
		<folder>images</folder>
	</media>
	
	<administration>
		<menu img="../media/com_helloworld/images/tux-16x16.png">Hello World!</menu>
		<files folder="admin">
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<folder>sql</folder>
			<folder>tables</folder>
			<folder>models</folder>
			<folder>views</folder>
			<folder>controllers</folder>
			<folder>helpers</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.menu.ini</language>
		</languages>
	</administration>
</extension>

Contributors[edit]