Developing a MVC Component

Published on November 2016 | Categories: Documents | Downloads: 42 | Comments: 0 | Views: 281
of 64
Download PDF   Embed   Report

Programming

Comments

Content

From Joomla! Documentation ......................................................................................................... 22 

Developing a MVC Component 

Contents ................................................................................................................................................. 22 
Introduction ........................................................................................................................................... 22 

Contents

Adding a variable request in the menu type ....................................................................................... 22 

Developing a MVC Component/Developing a Basic Component ................................................................. 5 

Packaging the component ..................................................................................................................... 25 

From Joomla! Documentation ........................................................................................................... 5 

Developing a MVC Component/Using the database .................................................................................. 26 

Contents ................................................................................................................................................... 5 

From Joomla! Documentation ......................................................................................................... 26 

The first basic component ...................................................................................................................... 6 

Contents ................................................................................................................................................. 26 

Public display ...................................................................................................................................... 6 

Introduction ........................................................................................................................................... 26 

Administrator management ............................................................................................................... 6 

Using the database ................................................................................................................................ 27 

Packaging an installation zip file ....................................................................................................... 6 

Adding a new field type ........................................................................................................................ 29 

Developing a MVC Component/Adding a view to the site part .................................................................... 8 

Display the chosen message .................................................................................................................. 31 

From Joomla! Documentation ........................................................................................................... 8 

Packaging the component ..................................................................................................................... 33 

Contents ................................................................................................................................................... 9 

Developing a MVC Component/Basic backend .......................................................................................... 33 

Introduction ............................................................................................................................................. 9 

From Joomla! Documentation ......................................................................................................... 33 

Setting the controller .............................................................................................................................. 9 

Contents ................................................................................................................................................. 34 

Setting the view ..................................................................................................................................... 11 

Introduction ........................................................................................................................................... 34 

Packaging the component ..................................................................................................................... 12 

Basic backend ........................................................................................................................................ 34 

Developing a MVC Component/Adding a menu type to the site part ........................................................ 14 

Create the general controller ............................................................................................................... 35 

From Joomla! Documentation ......................................................................................................... 14 

Create the view ...................................................................................................................................... 35 

Contents ................................................................................................................................................. 14 

Create the model ................................................................................................................................... 38 

Introduction ........................................................................................................................................... 14 

Packaging the component ..................................................................................................................... 39 

Abstract .................................................................................................................................................. 15 

Developing a MVC Component/Adding language management ................................................................ 41 

Adding a menu item type ..................................................................................................................... 15 

From Joomla! Documentation ......................................................................................................... 41 

Packaging the component ..................................................................................................................... 17 

Contents ................................................................................................................................................. 41 

Developing a MVC Component/Adding a model to the site part ............................................................... 17 

Introduction ........................................................................................................................................... 42 

From Joomla! Documentation ......................................................................................................... 17 

Adding language translation in the public site ................................................................................... 42 

Contents ................................................................................................................................................. 18 

Adding language translation when managing the component .......................................................... 42 

Introduction ........................................................................................................................................... 18 

Adding language translation when managing the menus in the backend ........................................ 43 

Adding a model ..................................................................................................................................... 18 

Language File Location Options .......................................................................................................... 43 

Packaging the component ..................................................................................................................... 21 

Adding translation when installing the component ........................................................................... 44 

Developing a MVC Component/Adding a variable request in the menu type ........................................... 22 

Packaging the component ..................................................................................................................... 45 


 


 

Developing a MVC Component/Adding backend actions ........................................................................... 47 

Managing the submenu ........................................................................................................................ 79 

From Joomla! Documentation ......................................................................................................... 47 

Adding some ACL ................................................................................................................................. 81 

Contents ................................................................................................................................................. 48 

Adding some translation strings .......................................................................................................... 82 

Introduction ........................................................................................................................................... 48 

Packaging the component ..................................................................................................................... 82 

Adding a toolbar ................................................................................................................................... 48 

Developing a MVC Component/Adding configuration ............................................................................... 86 

Adding specific controllers ................................................................................................................... 50 

From Joomla! Documentation ......................................................................................................... 86 

Adding an editing view ......................................................................................................................... 51 

Contents ................................................................................................................................................. 86 

Adding a model and modifying the existing one ................................................................................ 53 

Introduction ........................................................................................................................................... 86 

Packaging the component ..................................................................................................................... 55 

Adding configuration parameters ....................................................................................................... 87 

Developing a MVC Component/Adding decorations to the backend......................................................... 58 

Using configuration parameters as default value ............................................................................... 89 

From Joomla! Documentation ......................................................................................................... 58 

Modifying the SQL ............................................................................................................................... 90 

Contents ................................................................................................................................................. 58 

Modifying the backend ......................................................................................................................... 92 

Introduction ........................................................................................................................................... 58 

Modifying the frontend ......................................................................................................................... 93 

Adding some icons ................................................................................................................................. 58 

Adding some translation strings .......................................................................................................... 96 

Modifying the views .............................................................................................................................. 58 

Packaging the component ..................................................................................................................... 97 

Modifying the main entry file .............................................................................................................. 61 

Developing a MVC Component/Adding ACL ............................................................................................. 100 

Adding some strings in the language file ............................................................................................ 62 

From Joomla! Documentation ....................................................................................................... 100 

Packaging the component ..................................................................................................................... 62 

Contents ............................................................................................................................................... 100 

Developing a MVC Component/Adding verifications ................................................................................. 65 

Introduction ......................................................................................................................................... 101 

From Joomla! Documentation ......................................................................................................... 65 

Adding Access Control ....................................................................................................................... 101 

Contents ................................................................................................................................................. 66 

Minimal ACL requirements at the component level ....................................................................... 101 

Introduction ........................................................................................................................................... 66 

Add the 2 minimal component level actions to access.xml .......................................................... 102 

Verifying the form (client side) ............................................................................................................ 66 

Add the permissions fieldset to config.xml ................................................................................... 102 

Verifying the form (server side) ........................................................................................................... 72 

Add the 'Options' toolbar button when user is authorised for it ................................................ 103 

Packaging the component ..................................................................................................................... 73 

Restrict the access to the component's backend to authorised usergroups ............................... 103 

Developing a MVC Component/Adding categories .................................................................................... 76 

Adding more actions, also at category level and item level ............................................................. 103 

From Joomla! Documentation ......................................................................................................... 76 

Describing the actions you want to control the access to ............................................................. 104 

Contents ................................................................................................................................................. 76 

Adding the setting of permissions in the component's Preferences ............................................ 104 

Introduction ........................................................................................................................................... 76 

Displaying only the right toolbar buttons ..................................................................................... 105 

Modifying the SQL ............................................................................................................................... 77 

Restricting access to the component .............................................................................................. 110 

Modifying the form ............................................................................................................................... 77 

Add the asset_id column to the database table ............................................................................. 111 

Modifying the menu type ...................................................................................................................... 78 

Restricting access to the messages ................................................................................................. 111 



 

 

Setting the permission values in the assets table .......................................................................... 112 
Showing the setting of permissions on the item level ................................................................... 115 

This tutorial is part of the Developing a Model-View-Controller (MVC) Component for
Joomla!2.5 tutorial.

Adding language strings ................................................................................................................. 116 
Further reading ................................................................................................................................... 117 
Deprecated classes ............................................................................................................................... 117 

The first basic component
Let's create a Hello World! component.

Packaging the component ................................................................................................................... 117 
Developing a MVC Component/Adding an install‐uninstall‐update script file ......................................... 121 
From Joomla! Documentation ....................................................................................................... 121 

Public display

Contents ............................................................................................................................................... 121 

With your favorite file manager and editor, create a file
yoursite/components/com_helloworld/helloworld.php containing

Introduction ......................................................................................................................................... 121 

Hello world

Creating the extension script file ....................................................................................................... 121 
Adding some language keys ............................................................................................................... 123 
Packaging the component ................................................................................................................... 124 

You can test this basic component by putting index.php?option=com_helloworld in your
browser address (don't forget to prefix this address with your Joomla!2.5 installation path) after
installing this component.

 

Administrator management

Developing a MVC Component/Developing a Basic Component

With your favorite file manager and editor, create a file
yoursite/administrator/components/com_helloworld/helloworld.php containing

From Joomla! Documentation

Hello world administration

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 01)

You can test this basic component by putting administrator/index.php?option=com_helloworld
in your browser address after installing the component.

Packaging an installation zip file

This tutorial is for Joomla! CMS Version(s)

Contents





If you have used Joomla before reading this tutorial, you have noticed that extensions are
installed using a compressed file containing all the things which are needed for installing and
uninstalling them.

1 The first basic component
o 1.1 Public display
o 1.2 Administrator management
o 1.3 Packaging an installation zip file
2 Navigate
3 Contributors

With your favorite file manager, create a directory (outside your Joomla installation directory)
containing









[Expand]

Articles in this Series


 

helloworld.xml
site/helloworld.php
site/index.html
admin/index.html
admin/helloworld.php
admin/sql/index.html
admin/sql/updates/index.html
admin/sql/updates/mysql/index.html


 



<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
</files>

admin/sql/updates/mysql/0.0.1.sql

admin/sql/updates/mysql/0.0.1.sql is an empty file allowing to initialise schema version of the
com_helloworld component.
admin/sql/updates/mysql/0.0.1.sql

Create a compressed file of this directory or directly download the archive and install it using the
extension manager of Joomla. You can test this basic component by putting
index.php?option=com_helloworld or administrator/index.php?option=com_helloworld in your
browser address. You can also notice that the Hello World! component is visible in the
administrator site of your Joomla installation under the Components menu.

<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the

Note: <name>Hello World!</name> is tricking you into thinking that this is actually only a
name, and you could call it 'fluffy' or 'bippo', but this is not true. As far as I understand the name
in between the tags is getting automagically lowercased, white spaces and special characters are
removed (the ! for example) and joomla starts to look for files/folders responding to this name.
So the name you are giving there is actually very crucial for your component to work. In other
words, if you would change "Hello World!" to "helloworld" everyhting would still work. If you
would change it to "helloworld2" the component would be looking for different folders and file
names.

package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>
</extension>

helloworld.xml
site/helloworld.php
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

Hello World

<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.1</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component
...</description>

admin/helloworld.php

<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

From Joomla! Documentation

Hello World administration

index.html common to all folders
<html><body bgcolor="#FFFFFF"></body></html>

 

Developing a MVC Component/Adding a view to the site part

< J2.5:Developing a MVC Component
See: JVersion 3.2


 


 

This tutorial is for Joomla! CMS Version(s)

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

Contents







// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

1 Introduction
2 Setting the controller
3 Setting the view
4 Packaging the component
5 Navigate
6 Contributors

// Redirect if set by the controller
$controller->redirect();

The getInstance static method of the JController class will create a controller. In the code above,
it will instantiate a controller object of a class named HelloWorldController. Joomla will look for
the declaration of that class in an aptly named file called controller.php (it's a default behavior).

[Expand]

Articles in this Series

Now, controller.php needs to be created and HelloWorldController needs to be declared and
defined. So with your favorite file manager and editor, create a site/controller.php file containing
site/controller.php

Introduction

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

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

// import Joomla controller library
jimport('joomla.application.component.controller');

In the Joomla!2.5 framework, third party component authors divide their code into three main
parts:




/**
* Hello World Component Controller
*/
class HelloWorldController extends JController
{
}

models They manage the data
controllers They perform tasks, set and get the states of the models and ask the views to
display
views They display the content according to the type (error, feed, html, json, raw, xml)
and the layout chosen by the controllers

When no task is given in the request variables, the default task will be executed. It's the display
task by default. The JController class has such a task. In our example, it will display a view
named HelloWorld.

Setting the controller

So with task simply a public function display() of JController is refered to.

In the core code of Joomla, there is a class able to manage controllers: JController. This class
has to be extended to be used in our component. In the file site/helloworld.php (entry point of
our Hello World component), put these lines
site/helloworld.php

A Tip!

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

Just a side note for completion, you could call another function aside from display() by using an URL like
this one:

// import joomla controller library
jimport('joomla.application.component.controller');

http://localhost/index.php?option=com_helloworld&task=insert


 

10 
 

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>

This would try to call a function insert() of our controller (which we would actually have to
implement in HelloWorldController ).

Setting the view

This template file will be included by the JView class. Therefore, here, $this refers to the
HelloWorldViewHelloWorld class.

When JController wants to display a view, it will look for certain files in the
component/com_[component_name]/views/[name of view]/ folder.
The name of the folder of the default view is the name of the component itself. In our case the
path is component/com_helloworld/views/helloworld/.

The file that will contain the code of the view is called view.[view_mode].php. The default view
mode, and probably the only view a component might need is the html mode. So this give us our
file name which is view.html.php.

So to give an example, one could call the view inside the
component/com_[component_name]/views/[name of view]/ folder by calling:
http://localhost/joomla/index.php?option=com_helloworld

With your favorite file manager and editor, create a file site/views/helloworld/view.html.php able
to display the default view and containing

(this would default to the component/com_helloworld/views/helloworld/ folder) or we could
explicitly call the folder by calling

site/views/helloworld/view.html.php

http://localhost/joomla/index.php?option=com_helloworld&view=helloworld
if we change &view=helloworld to something else, e.g. &view=fluffy we would have to create a
folder:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

component/com_helloworld/views/fluffy/

// import Joomla view library
jimport('joomla.application.component.view');

Copy the contents of views/helloworld to views/fluffy

/**
* HTML View class for the HelloWorld Component
*/
class HelloWorldViewHelloWorld extends JView
{
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = 'Hello World';

The classname of the file view.html.php of the fluffy folder would be HelloWorldViewFluffy.
Afterwards you can customize the contents of default.php of the fluffy subfolder for custom
output and see the output by calling:
http://localhost/joomla/index.php?option=com_helloworld&view=fluffy

Packaging the component

// Display the view
parent::display($tpl);

Content of your code directory

}









}

The display method of the JView class is called with the display task of the JController class. In
our case, this method will display data using the tmpl/default.php file. With your favorite file
manager and editor, create a file site/views/helloworld/tmpl/default.php able to display the
default view and containing
site/views/helloworld/tmpl/default.php
11 
 

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
12 

 









<!-- Note the folder attribute: This attribute describes the

site/views/helloworld/tmpl/default.php
admin/index.html
admin/helloworld.php
admin/sql/index.html
admin/sql/updates/index.html
admin/sql/updates/mysql/index.html
admin/sql/updates/mysql/0.0.1.sql

folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>

Create a compressed file of this directory or directly download the archive and install it using the
extension manager of Joomla. You can test this basic component by putting
index.php?option=com_helloworld in your browser address.
helloworld.xml

</extension>

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

Result: You will see by default the message contained in the variable $this->msg in the
view.html.php file.

<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.2</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component
...</description>

Developing a MVC Component/Adding a menu type to the site part
From Joomla! Documentation
< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 03)
This tutorial is for Joomla! CMS Version(s)

Contents

<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
</files>








[Expand]

Articles in this Series

Introduction

<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->

14 

13 
 

1 Introduction
2 Abstract
3 Adding a menu item type
4 Packaging the component
5 Navigate
6 Contributors

 

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

Abstract
Generally speaking, this article describes how to get a link on your joomla page to open a
specific page of your component. This gets simply done by adding an xml file to your specific
page into your view folder.
E.g.
site/views/helloworld/tmpl/
Contains your view page default.php that we want to open.
A file default.xml is placed next to this file with some xml. This makes joomla able to recognize
the view file default.php as a menu item.

<extension type="component" version="2.5.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.3</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component
...</description>
<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
</files>

Adding a menu item type
In the Joomla framework, components are executed using menu items. If you go in the menu
manager of your Joomla installation a HelloWorld menu item type does not yet exist. Adding this
functionality is easy in Joomla. Simply put a site/views/helloworld/tmpl/default.xml file
containing:

<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the

site/views/helloworld/tmpl/default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
<message>

folder
to copy FROM in the package to install therefore
files copied

<![CDATA[COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC]]>
</message>
</layout>
</metadata>

in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>

For the moment the strings won't be translated in the administrator interface. We will see in a
later article how translation is performed.
Also modify your helloworld.xml file to indicate a new version:

</extension>

helloworld.xml

15 
 

16 
 

Packaging the component

Contents

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
admin/index.html
admin/helloworld.php
admin/sql/index.html
admin/sql/updates/index.html
admin/sql/updates/mysql/index.html
admin/sql/updates/mysql/0.0.1.sql

1 Introduction
2 Adding a model
3 Packaging the component
4 Navigate
5 Contributors

[Expand]

Articles in this Series

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

Adding a model

Selecting the Menu Item Type
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. To do so select "Add New Menu Item" from the one of the menus in the
"Menus" menu; then you can select
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE for Menu Item Type.
Once selected you can see the Link information is populated with the URL for the view.

Developing a MVC Component/Adding a model to the site part

In the Joomla framework, models are responsible for managing the data. The first function that
has to be written for a model is a get function. It returns data to the caller. In our case, the caller
will be the HelloWorldViewHelloWorld view. By default, the model named
HelloWorldModelHelloWorld residing in site/models/helloworld.php is the main model
associated to this view.

So let's have a quick look at the naming conventions with an example, since the naming
convention are the actual magic, that make everything work:
The class HelloWorldViewHelloWorld resides in site/views/helloworld/view.html.php and will
make use of the class HelloWorldModelHelloWorld in the file site/models/helloworld.php

So let's just assume we want to use an imaginary view fluffy, you would have to have:

From Joomla! Documentation

The class HelloWorldViewFluffy which resides in site/views/fluffy/view.html.php. The view will
make use of HelloWorldModelFluffy in the file site/models/fluffy.php. Note: the actual screen of
the view: site/views/fluffy/tmpl/default.php is required as well to make this example work.

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 04)
This tutorial is for Joomla! CMS Version(s)

Breaking any of these bold conventions will lead to errors or a blank page.
17 

 

18 
 

class HelloWorldViewHelloWorld extends JView
{
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = $this->get('Msg');

So back to the actual implementation of the single classes:
With your favorite file manager and editor put a site/models/helloworld.php file containing:
site/models/helloworld.php

// Check for errors.
if (count($errors = $this->get('Errors')))
{
JLog::add(implode('<br />', $errors), JLog::WARNING,

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

'jerror');
return false;
}
// Display the view
parent::display($tpl);

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
/**
* HelloWorld Model
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var string msg
*/
protected $msg;

}
}

Note: $this->get() is a member of JView::get which is a proxy to get* methods of the default
model where * is populated with the value of the first parameter passed to get()
Also modify your helloworld.xml file to indicate use of models and the new version:
helloworld.xml

/**
* Get the message
* @return string The message to be displayed to the user
*/
public function getMsg()
{
if (!isset($this->msg))
{
$this->msg = 'Hello World!';
}
return $this->msg;
}

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

}

The HelloWorldViewHelloWorld class asks the model for data using the get method of the JView
class:
site/views/helloworld/view.html.php

<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.4</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component
...</description>
<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied

/**
* HTML View class for the HelloWorld Component
*/

19 
 

20 
 



in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>

admin/sql/updates/mysql/0.0.1.sql

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.

Developing a MVC Component/Adding a variable request in the menu
type

<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the

From Joomla! Documentation

folder
to copy FROM in the package to install therefore

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 05)

files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>

This tutorial is for Joomla! CMS Version(s)

Contents






</extension>

Packaging the component

[Expand]

Content of your code directory


















Articles in this Series

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
admin/index.html
admin/helloworld.php
admin/sql/index.html
admin/sql/updates/index.html
admin/sql/updates/mysql/index.html

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

Adding a variable request in the menu type
For the moment, the displayed message is always Hello World!. Joomla!2.5 gives the possibility
to add parameters to menu types. In our case, this is done in the
site/views/helloworld/tmpl/default.xml file:
site/views/helloworld/tmpl/default.xml
22 

21 
 

1 Introduction
2 Adding a variable request in the menu type
3 Packaging the component
4 Navigate
5 Contributors

 

<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">

* @return string The message to be displayed to the user
*/
public function getMsg()
{
if (!isset($this->msg))
{
//Uses JInput if magic quotes is turned off. Falls
back to use JRequest.
if(!get_magic_quotes_gpc()) {
$id = JFactory::getApplication()->input>get('id', 1, 'INT' );
} else {
$id = JRequest::getInt('id');
}

<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
</layout>
<fields name="request">
<fieldset name="request">
<field
name="id"
type="list"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
default="1"
>
<option value="1">Hello World!</option>
<option value="2">Good bye World!</option>
</field>
</fieldset>
</fields>
</metadata>

switch ($id)
{
case 2:
$this->msg = 'Good bye World!';
break;
default:
case 1:
$this->msg = 'Hello World!';
break;
}

Two important things to note:



}
return $this->msg;

the request group of fields indicates mandatory fields
the name attribute can be passed to the component in the URL. In this case
?option=com_helloworld&id=1 would indicate that you had chosen option 1.

}
}

Also modify your helloworld.xml file to indicate the new version:
The model has to be modified in order to switch between the two different messages (which is
chosen by the user with the field defined above):

helloworld.xml

site/models/helloworld.php

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.5</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component
...</description>

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
/**
* HelloWorld Model
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var string msg
*/
protected $msg;

<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>

/**
* Get the message

23 
 

24 
 











</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>
<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the

site/views/helloworld/tmpl/default.php
site/models/index.html
site/models/helloworld.php
admin/index.html
admin/helloworld.php
admin/sql/index.html
admin/sql/updates/index.html
admin/sql/updates/mysql/index.html
admin/sql/updates/mysql/0.0.1.sql

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.

Developing a MVC Component/Using the database

folder

From Joomla! Documentation

to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 06)

package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>

This tutorial is for Joomla! CMS Version(s)

Contents








</extension>

You can test this variable request by putting index.php?option=com_helloworld&id=1 or
index.php?option=com_helloworld&id=2 in your browser address.

Packaging the component

1 Introduction
2 Using the database
3 Adding a new field type
4 Display the chosen message
5 Packaging the component
6 Navigate
7 Contributors

[Expand]

Content of your code directory










Articles in this Series

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

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

25 
 

 

Using the database
Components usually manage their contents using the database. During the install/uninstall/update
phase of a component, you can execute SQL queries through the use of SQL text files.
With your favorite file manager and editor create two files called admin/sql/install.mysql.utf8.sql
and admin/sql/updates/mysql/0.0.6.sql. They should both have the same content, as follows:

<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.6</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component
...</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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

admin/sql/install.mysql.utf8.sql and admin/sql/updates/mysql/0.0.6.sql
DROP TABLE IF EXISTS `#__helloworld`;
CREATE TABLE `#__helloworld` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`greeting` varchar(25) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Good bye World!');

The file install.mysql.utf8.sql will be executed when you install this component. The file
0.0.6.sql is executed when you do an update.
Important Note: When the component is installed, the files in the SQL updates folder (for
example, admin/sql/updates/mysql) are read and the name of the last file alphabetically is used to
populate the component's version number in the #__schemas table. This value must be in this
table in order for the automatic update to execute the update SQL files for future versions. For
this reason, it is good practice to create a SQL update file for each version (even if it is empty or
just has a comment). This way the #__schemas version will always match the component
version.

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>

Important Note: When saving the SQL files in utf8, be sure to save them as utf8 NO BOM or
the query will fail with MySQL error #1064.

<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the

This is the install file. It will be executed if you put an appropriate order in the helloworld.xml
file
helloworld.xml

folder

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

files copied

to copy FROM in the package to install therefore
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>

<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>

27 
 

28 
 

<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>

It introduces a new field type and tells Joomla to look for the field definition in the
/administrator/components/com_helloworld/models/fields folder.
With your favorite file manager and editor put a file admin/models/fields/helloworld.php file
containing:

</files>
</administration>
</extension>

admin/models/fields/helloworld.php

Do the same for the uninstall file:

<?php
// No direct access to this file
defined('_JEXEC') or die;

With your favorite file manager and editor put a file admin/sql/uninstall.mysql.utf8.sql
containing:

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

admin/sql/uninstall.mysql.utf8.sql

/**
* HelloWorld Form Field class for the HelloWorld component
*/
class JFormFieldHelloWorld extends JFormFieldList
{
/**
* The field type.
*
* @var
string
*/
protected $type = 'HelloWorld';

DROP TABLE IF EXISTS `#__helloworld`;

Adding a new field type
For the moment, we have used a hard coded field type for messages. We need to use our
database for choosing the message.
Modify the site/views/helloworld/tmpl/default.xml file and put these lines
site/views/helloworld/tmpl/default.xml

/**
* 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 = $db->getQuery(true);
$query->select('id,greeting');
$query->from('#__helloworld');
$db->setQuery((string)$query);
$messages = $db->loadObjectList();
$options = array();
if ($messages)
{
foreach($messages as $message)
{
$options[] = JHtml::_('select.option',
$message->id, $message->greeting);
}
}
$options = array_merge(parent::getOptions(), $options);
return $options;
}
}

<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
</layout>
<fields
name="request"
addfieldpath="/administrator/components/com_helloworld/models/fields"
>
<fieldset name="request">
<field
name="id"
type="helloworld"
label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
/>
</fieldset>
</fields>
</metadata>

29 
 

30 
 

$this->messages = array();

The new field type displays a drop-down list of messages to choose from. You can see the result
of this change in the menu manager section for the helloworld item.

}
if (!isset($this->messages[$id]))
{
//request the selected id
$jinput = JFactory::getApplication()->input;
$id = $jinput->get('id', 1, 'INT' );

Display the chosen message
When a menu item of this component is created/updated, Joomla stores the identifier of the
message. The HelloWorldModelHelloWorld model has now to compute the message according
to this identifier and the data stored in the database.

// Get a TableHelloWorld instance
$table = $this->getTable();

Modify the site/models/helloworld.php file:

// Load the message
$table->load($id);

site/models/helloworld.php

// Assign the message
$this->messages[$id] = $table->greeting;

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

}
return $this->messages[$id];
}

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

}

/**
* HelloWorld Model
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var array messages
*/
protected $messages;

The model now asks the TableHelloWorld to get the message. This table class has to be defined
in admin/tables/helloworld.php file

/**
* 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
* @param int
The corresponding id of the message to be retrieved
* @return string The message to be displayed to the user
*/
public function getMsg($id = 1)
{
if (!is_array($this->messages))
{

// import Joomla table library
jimport('joomla.database.table');

admin/tables/helloworld.php
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');

/**
* Hello Table class
*/
class HelloWorldTableHelloWorld extends JTable
{
/**
* Constructor
*
* @param object Database connector object
*/
function __construct(&$db)
{
parent::__construct('#__helloworld', 'id', $db);
}
}

You shouldn't see any differences, but if you access the database you should see a table named
jos_helloworld with two columns: id and greeting. And two entries: Hello World! and Good bye
World

31 
 

32 
 

Packaging the component

This tutorial is for Joomla! CMS Version(s)

Contents

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
admin/index.html
admin/helloworld.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/tables/index.html
admin/tables/helloworld.php

1 Introduction
2 Basic backend
3 Create the general controller
4 Create the view
5 Create the model
6 Packaging the component
7 Navigate
8 Contributors

[Expand]

Articles in this Series

Introduction
This tutorial is part of the Developing a Model-View-Controller (MVC) Component for
Joomla!2.5 tutorial. You are encouraged to read the previous parts of the tutorial before reading
this.
And check the "discussion" tab for some clarifications.

Basic backend

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.

Designing the backend interface leads us to create at least a Model-View-Controller triptych. We
have to modify the administrator entry point of our component, the admin/helloworld.php file
admin/helloworld.php

Note: site/models/helloworld.php is not up-to-date as mentioned on this page. You need to copy
the code yourself.

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

Developing a MVC Component/Basic backend

// import joomla controller library
jimport('joomla.application.component.controller');

From Joomla! Documentation

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 07)

// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );

34 

33 
 

 

// Perform the Request task
$controller->execute($task);

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Redirect if set by the controller
$controller->redirect();

// import Joomla view library
jimport('joomla.application.component.view');

Create the general controller

/**
* HelloWorlds View
*/
class HelloWorldViewHelloWorlds extends JView
{
/**
* HelloWorlds view display method
* @return void
*/
function display($tpl = null)
{
// Get data from the model
$items = $this->get('Items');
$pagination = $this->get('Pagination');

The entry point now gets an instance of a HelloWorld prefixed controller. Let's create a basic
controller for the administrator part:
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');

// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign data to the view
$this->items = $items;
$this->pagination = $pagination;

/**
* General Controller of HelloWorld component
*/
class HelloWorldController extends JController
{
/**
* display task
*
* @return void
*/
function display($cachable = false, $urlparams = false)
{
// set default view if not set
$input = JFactory::getApplication()->input;
$input->set('view', $input->getCmd('view', 'HelloWorlds'));

// Display the template
parent::display($tpl);
}
}

In Joomla, views display data using layout. With your favorite file manager and editor, put a file
admin/views/helloworlds/tmpl/default.php containing

// call parent behavior
parent::display($cachable);

admin/views/helloworlds/tmpl/default.php

}
}

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');

This controller will display the 'HelloWorlds' view by default.

Create the view
With your favorite file manager and editor, create a file admin/views/helloworlds/view.html.php
containing:
admin/views/helloworlds/view.html.php

// load tooltip behavior
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>"
method="post" name="adminForm">
<table class="adminlist">
<thead><?php echo $this->loadTemplate('head');?></thead>
<tfoot><?php echo $this->loadTemplate('foot');?></tfoot>
<tbody><?php echo $this->loadTemplate('body');?></tbody>

35 
 

36 
 

</table>

</td>
<td>

</form>

<?php echo $item->greeting; ?>

This layout calls several sub-layout (head, foot and body). Each sub-layout corresponds to a file
prefixed by the name of the main layout (default), and an underscore.
Put a file admin/views/helloworlds/tmpl/default_head.php containing

</td>
</tr>
<?php endforeach; ?>

JHtml::_ is a helper function able to display several HTML output. In this case, it will display a
checkbox for the item.

admin/views/helloworlds/tmpl/default_head.php

Put a file admin/views/helloworlds/tmpl/default_foot.php containing

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<tr>
<th width="5">
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_ID');
?>
</th>
<th width="20">
<input type="checkbox" name="toggle" value=""
onclick="checkAll(<?php echo count($this->items); ?>);" />
</th>
<th>
<?php echo
JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING'); ?>
</th>
</tr>

admin/views/helloworlds/tmpl/default_foot.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<tr>
<td colspan="3"><?php echo $this->pagination->getListFooter();
?></td>
</tr>

JPagination is a Joomla class able to manage and display pagination object.

Create the model
The HelloWorlds view asks the model for data. In Joomla, there is a class able to manage a list of
data: JModelList. Class JModelList and inherited classes need only one method:

COM_HELLOWORLD_HELLOWORLD_HEADING_ID and
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING are placeholders which will
later be replaced with language-specific text. The JText::_ method translates a string into the
current language.



and two states:

checkAll is a javascript function defined in the Joomla core able to check all items.




Put a file admin/views/helloworlds/tmpl/default_body.php containing

list.start for determining the list offset
list.limit for determining the list length

The getItems and getPagination methods are defined in JModelList class. They don't need to be
defined in the HelloWorldModelHelloWorlds class.

admin/views/helloworlds/tmpl/default_body.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<?php foreach($this->items as $i => $item): ?>
<tr class="row<?php echo $i % 2; ?>">
<td>
<?php echo $item->id; ?>
</td>
<td>
<?php echo JHtml::_('grid.id', $i, $item->id); ?>

admin/models/helloworlds.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');
/**
* HelloWorldList Model
*/

37 
 

getListQuery which constructs an SQL query

38 
 
















class HelloWorldModelHelloWorlds extends JModelList
{
/**
* Method to build an SQL query to load the list data.
*
* @return
string An SQL query
*/
protected function getListQuery()
{
// Create a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Select some fields
$query->select('id,greeting');
// From the hello table
$query->from('#__helloworld');
return $query;
}
}

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.

The _populateState method is, by default, automatically called when a state is read by the
getState method.

helloworld.xml

Packaging the component

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>Hello World!</name>
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<version>0.0.7</version>
<description>Description of the Hello World component
...</description>

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

<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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

39 
 

admin/models/index.html
admin/models/fields/index.html
admin/models/fields/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/tables/index.html
admin/tables/helloworld.php

40 
 

Articles in this Series

<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
</files>

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

<administration>
<menu>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>
<!-- views files section -->
<folder>views</folder>
</files>
</administration>
</extension>

Joomla!2.5 manages languages for components in four different situations:





displaying a component in the public site
managing a component in the backend
managing menus in the backend
installing a component (new in 1.7)

Joomla!2.5 uses two different location folder for languages:



Now you can see in your component hello-world an array with two colums, two rows and
checkboxes. You can click the checkboxes in order to select the different options you want.

one in administrator/language or language
one in the component folder (administrator/components/*component*/language or
components/*component*/language)

Developing a MVC Component/Adding language management

It depends how the component is installed.

From Joomla! Documentation

Adding language translation in the public site

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 08)

With your favorite file manager and editor, put a file site/language/en-GB/enGB.com_helloworld.ini. This file will contain translation for the public part. For the moment, this
file is empty

This tutorial is for Joomla! CMS Version(s)

site/language/en-GB/en-GB.com_helloworld.ini

Contents










1 Introduction
2 Adding language translation in the public site
3 Adding language translation when managing the component
4 Adding language translation when managing the menus in the backend
5 Language File Location Options
6 Adding translation when installing the component
7 Packaging the component
8 Navigate
9 Contributors

For the moment, there are no translations strings in this file.

Adding language translation when managing the component
With your favorite file manager and editor, put a file admin/language/en-GB/enGB.com_helloworld.ini. This file will contain translation for the backend part.
admin/language/en-GB/en-GB.com_helloworld.ini

[Expand]

41 
 

42 
 

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"

[...]
</files>

and/or then (1.5 way):

With your favorite file manager and editor, put a file admin/language/en-GB/enGB.com_helloworld.sys.ini. This file will contain translation for the backend part.

<languages folder="joomlacorelanguagefolders"> // if using another language
folder for cleanliness (any folder name will fit)
<language tag="en-GB">en-GB/en-GB.whatever.ini</language> // or
<language tag="en-GB">en-GB.whatever.ini</language> if no tagged subfolder
<language tag="en-GB">en-GB/en-GB.whatever.sys.ini</language> // or
<language tag="en-GB">en-GB.whatever.sys.ini</language> if no tagged
subfolder
</languages>

admin/language/en-GB/en-GB.com_helloworld.sys.ini

or simply in ROOT

COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE="Hello World"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected
message"
COM_HELLOWORLD_MENU="Hello World!"

<languages>
<language tag="en-GB">en-GB.whatever.ini</language>
<language tag="en-GB">en-GB.whatever.sys.ini</language>
</languages>

Adding language translation when managing the menus in
the backend

Language File Location Options
Starting with version 1.7 there are 2 ways to install language files for an extension. One can use
one or the other or a combination of both.
The 1.5 way will install the files in the CORE language folders (ROOT/administrator/language/
and ROOT/language/ ). The new way —since 1.6— includes the files in a "language" folder
installed at the root of the extension.
Therefore an extension can include a language folder with a .sys.ini different from the one
installed in joomla core language folders (this last one not being included in that language folder
but in root or any other folder not installed). This let's display 2 different descriptions: one from
the sys.ini in the "language" folder is used as a message displayed when install has been done,
the other (from the .ini) is used for "normal" operation, i.e. when the extension is edited in backend. This can be extremely useful when installing also uses some scripts and requires a different
value for the description.
Note: The sys.ini file is also used to translate the name of the extensions in some back-end
Managers and to provide menu translation for components

Language file used by the install script during install of a component(the first install not
upgrade) obeys specific rules described at
http://docs.joomla.org/Specification_of_language_files. During the first install, only the
language file included in the component folder
(/administrator/components/com_helloworld/language) is used when present. If this file is only
provided in the CORE language folder (/administrator/language), no translation occurs. This also
applies to KEYs used in the manifest file.
When upgrading or uninstalling the extension (not installing), it is the sys.ini file present in the
extension root language folder which will display the result of the install from the description
key/value. Thereafter, if present, the sys.ini as well as the ini installed in CORE language folder
will have priority over the files present in the root language folder of the extension.
Note: One advantage of installing the files in the extension "language" folder is that these are
not touched when updating a language pack. The other advantage is that this folder can include
multiple languages (en-GB always, fr-FR, it-IT, etc.) not requiring the user to install the
corresponding language pack. This is handy as they are available if, later on, a user installs the
corresponding pack.

Adding translation when installing the component
See: Language File Location Options

Therefore, the xml would include since 1.6

With your favorite file manager and editor, put a file EXTENSIONROOT/language/en-GB/enGB.myextension.sys.ini. This file will contain translation for the install.

<files>
<[...]
<folder>language</folder> // This folder HAS to include the right
subfolders, i.e. language/en-GB/ ... language/fr-FR/
<filename>whatever</filename>

language/en-GB/en-GB.myextension.sys.ini
43 

 

44 
 







COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"

The COM_HELLOWORLD_DESCRIPTION can be used in the helloworld.xml file

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
language/en-GB/en-GB.ini

Packaging the component
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.

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

helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.8</version>
<!-- The description is optional and defaults to the name -->
<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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>

45 
 

46 
 

<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
<folder>language</folder>

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 09)
This tutorial is for Joomla! CMS Version(s)

</files>

Contents

<administration>
<!-- Administration Menu Section -->
<menu>COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the










folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
</files>
<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>

[Expand]

Articles in this Series

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

Adding a toolbar
In Joomla, the administrator interacts generally with components through the use of a toolbar. In
the file admin/views/helloworlds/view.html.php put this content. It will create a basic toolbar and
a title for the component.

</extension>

In this helloworld.xml file, languages are installed in:



1 Introduction
2 Adding a toolbar
3 Adding specific controllers
4 Adding an editing view
5 Adding a model and modifying the existing one
6 Packaging the component
7 Navigate
8 Contributors

administrator/language for the admin part (look at the xml languages tag)
components/com_helloworld/language for the site part (there are no xml languages tag in
the site part of the xml description file, but the language folder is included)

The corrected zip file, download and delete the .png extension before use: [1]

The args used in ex. JToolBarHelper::addNew is used to set a controller instance which will be
used after button is clicked.
admin/views/helloworlds/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

Developing a MVC Component/Adding backend actions

// import Joomla view library
jimport('joomla.application.component.view');

From Joomla! Documentation
/**

48 

47 
 

 

* HelloWorlds View
*/
class HelloWorldViewHelloWorlds extends JView
{
/**
* HelloWorlds view display method
* @param
string $tpl The name of the template file to parse;
automatically searches through the template paths.
*
* @return mixed A string if successful, otherwise a JError object.
*/
function display($tpl = null)
{
// Get data from the model
$items = $this->get('Items');
$pagination = $this->get('Pagination');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign data to the view
$this->items = $items;
$this->pagination = $pagination;

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
// load tooltip behavior
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>"
method="post" name="adminForm" id="adminForm">
<table class="adminlist">
<thead><?php echo $this->loadTemplate('head');?></thead>
<tfoot><?php echo $this->loadTemplate('foot');?></tfoot>
<tbody><?php echo $this->loadTemplate('body');?></tbody>
</table>
<div>
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>

Adding specific controllers
Three actions have been added:




// Set the toolbar
$this->addToolBar();
// Display the template
parent::display($tpl);

helloworlds.delete
helloworld.edit
helloworld.add

read more about subcontrollers...

}

These are compound tasks (controller.task). So two new controllers
HelloWorldControllerHelloWorlds and HelloWorldControllerHelloWorld have to be coded.

/**
* Setting the toolbar
*/
protected function addToolBar()
{

admin/controllers/helloworlds.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'));
JToolBarHelper::deleteList('', 'helloworlds.delete');
JToolBarHelper::editList('helloworld.edit');
JToolBarHelper::addNew('helloworld.add');
}
}

// import Joomla controlleradmin library
jimport('joomla.application.component.controlleradmin');

You can find other classic backend actions in the administrator/includes/toolbar.php file of your
Joomla installation.
Since the view can perform some actions, we have to add some input data. With your favorite
file manager and editor, put in the file admin/views/helloworlds/tmpl/default.php
admin/views/helloworlds/tmpl/default.php

/**
* HelloWorlds Controller
*/
class HelloWorldControllerHelloWorlds extends JControllerAdmin
{
/**
* Proxy for getModel.
* @since
2.5
*/

49 
 

50 
 

public function getModel($name = 'HelloWorld', $prefix =
'HelloWorldModel')
{
$model = parent::getModel($name, $prefix,
array('ignore_request' => true));
return $model;
}
}

// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign the Data
$this->form = $form;
$this->item = $item;

admin/controllers/helloworld.php

// Set the toolbar
$this->addToolBar();

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Display the template
parent::display($tpl);
}

// import Joomla controllerform library
jimport('joomla.application.component.controllerform');

/**
* 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');
}
}

/**
* HelloWorld Controller
*/
class HelloWorldControllerHelloWorld extends JControllerForm
{
}

Adding an editing view
With your favorite file manager and editor, put a file admin/views/helloworld/view.html.php
containing:
admin/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

This view will display data using a layout.

// import Joomla view library
jimport('joomla.application.component.view');

Put a file admin/views/helloworld/tmpl/edit.php containing

/**
* HelloWorld View
*/
class HelloWorldViewHelloWorld extends JView
{
/**
* display method of Hello view
* @return void
*/
public function display($tpl = null)
{
// get the Data
$form = $this->get('Form');
$item = $this->get('Item');

admin/views/helloworld/tmpl/edit.php
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo
JRoute::_('index.php?option=com_helloworld&layout=edit&id='.(int) $this>item->id); ?>"
method="post" name="adminForm" id="adminForm">
<fieldset class="adminform">
<legend><?php echo JText::_(
'COM_HELLOWORLD_HELLOWORLD_DETAILS' ); ?></legend>

51 
 

52 
 

<ul class="adminformlist">
<?php foreach($this->form->getFieldset() as $field): ?>
<li><?php echo $field->label;echo $field>input;?></li>
<?php endforeach; ?>
</ul>
</fieldset>
<div>
<input type="hidden" name="task" value="helloworld.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>

* @since
2.5
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->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;
}
}

Adding a model and modifying the existing one
The HelloWorldViewHelloWorld view asks form and data from a model. This model has to
provide a getTable, a getForm method and a loadData method (called from the JModelAdmin
controller)
admin/models/helloworld.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
/**
* HelloWorld Model
*/
class HelloWorldModelHelloWorld extends JModelAdmin
{
/**
* 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);
}
/**
* Method to get the record form.
*
* @param
array
$data
Data for the form.
* @param
boolean $loadData
True if the form is to load
its own data (default case), false if not.
* @return
mixed
A JForm object on success, false on failure

This model inherits from the JModelAdmin class and uses its loadForm method. This method
searches for forms in the forms folder. With your favorite file manager and editor, put a file
admin/models/forms/helloworld.xml containing:
admin/models/forms/helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
size="40"
class="inputbox"
default=""

53 
 

54 
 
















/>
</fieldset>
</form>

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

Create a compressed file of this directory or directly download the archive, modify the code in
/admin/models/helloworld.php 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
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.9</version>
<!-- The description is optional and defaults to the name -->
<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; New in 2.5 -->

55 
 

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

56 
 

Developing a MVC Component/Adding decorations to the backend

<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

From Joomla! Documentation

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<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>

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 10)
This tutorial is for Joomla! CMS Version(s)

Contents









<administration>
<!-- Administration Menu Section -->
<menu>COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the

1 Introduction
2 Adding some icons
3 Modifying the views
4 Modifying the main entry file
5 Adding some strings in the language file
6 Packaging the component
7 Navigate
8 Contributors

folder
to copy FROM in the package to install therefore

[Expand]

files copied
in this section are copied from /admin/ in the

Articles in this Series

package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
</files>

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

Adding some icons
With your favorite file manager put a 16x16 image and a 48x48 image (I choose tux) in a
media/images/ folder and add a media tag in your install file. Modify the menu tag in order to use
the new icon.

<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>

Modifying the views
In the admin/views/helloworlds/view.html.php file put these lines:

</extension>

admin/views/helloworlds/view.html.php

 
58 

57 
 

 

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
$document = JFactory::getDocument();
$document>setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
}
}

// import Joomla view library
jimport('joomla.application.component.view');
/**
* HelloWorlds View
*/
class HelloWorldViewHelloWorlds extends JView
{
/**
* HelloWorlds view display method
* @return void
*/
function display($tpl = null)
{
// Get data from the model
$items = $this->get('Items');
$pagination = $this->get('Pagination');

This view uses a second parameter for the JToolBarHelper::title function. It will be used to
construct the css class for the title. The _setDocument method sets the browser title.
In admin/views/helloworld/view.html.php, put these lines:
admin/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign data to the view
$this->items = $items;
$this->pagination = $pagination;

// import Joomla view library
jimport('joomla.application.component.view');
/**
* HelloWorld View
*/
class HelloWorldViewHelloWorld extends JView
{
/**
* View form
*
* @var
form
*/
protected $form = null;

// Set the toolbar and number of found items
$this->addToolBar($this->pagination->total);
// Display the template
parent::display($tpl);
// Set the document
$this->setDocument();
}

/**
* display method of Hello view
* @return void
*/
public function display($tpl = null)
{
// get the Data
$form = $this->get('Form');
$item = $this->get('Item');

/**
* Setting the toolbar
*/
protected function addToolBar($total=null)
{
JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS').
//Reflect number of items in title!
($total?' <span style="font-size: 0.5em; verticalalign: middle;">('.$total.')</span>':'')
, 'helloworld');
JToolBarHelper::deleteListX('', 'helloworlds.delete');
JToolBarHelper::editListX('helloworld.edit');
JToolBarHelper::addNewX('helloworld.add');
}

// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}

59 
 

60 
 

// Assign the Data
$this->form = $form;
$this->item = $item;

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// Set some global property
$document = JFactory::getDocument();
$document->addStyleDeclaration('.icon-48-helloworld {background-image:
url(../media/com_helloworld/images/tux-48x48.png);}');

// Set the toolbar
$this->addToolBar();
// Display the template
parent::display($tpl);

// import joomla controller library
jimport('joomla.application.component.controller');

// Set the document
$this->setDocument();

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

}
/**
* 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'), 'helloworld');
JToolBarHelper::save('helloworld.save');
JToolBarHelper::cancel('helloworld.cancel', $isNew ?
'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
}
/**
* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
$isNew = ($this->item->id < 1);
$document = JFactory::getDocument();
$document->setTitle($isNew ?
JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING')
:
JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING'));
}
}

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));
// Redirect if set by the controller
$controller->redirect();

Adding some strings in the language file
Modify the admin/language/en-GB/en-GB.com_helloworld.ini and put these lines
admin/language/en-GB/en-GB.com_helloworld.ini
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
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"

Packaging the component

This view also uses the second parameter of the JToolBarHelper::title function and set the
browser title

Content of your code directory






Modifying the main entry file
In the admin/helloworld.php file, put these lines in order to use the 48x48 icon:
<?php

61 
 

helloworld.xml
site/index.html
site/helloworld.php
site/controller.php
site/views/index.html
62 

 






















































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.menu.ini
admin/controllers/index.html
admin/controllers/helloworld.php
admin/controllers/helloworlds.php

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
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.10</version>
<!-- The description is optional and defaults to the name -->
<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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>

63 
 

language/en-GB/en-GB.ini
media/index.html
media/images/index.html
media/images/tux-16x16.png
media/images/tux-48x48.png

64 
 

<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>
<folder>language</folder>

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 11)
This tutorial is for Joomla! CMS Version(s)

</files>

Contents

<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
</media>








<administration>
<!-- Administration Menu Section -->
<menu img="../media/com_helloworld/images/tux16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
</files>

[Expand]

Articles in this Series

Introduction
This tutorial is part of the Developing a Model-View-Controller (MVC) Component for
Joomla!2.5 tutorial. You are encouraged to read the previous parts of the tutorial before reading
this.
See Form validation for some more general info on form validation (rules).

Verifying the form (client side)
Forms can be verified on the client side using javascript code.

<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>

In the admin/views/helloworld/tmpl/edit.php file, put these lines
admin/views/helloworld/tmpl/edit.php
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<form action="<?php echo
JRoute::_('index.php?option=com_helloworld&layout=edit&id='.(int) $this>item->id); ?>"
method="post" name="adminForm" id="helloworld-form" class="formvalidate">

</extension>

Developing a MVC Component/Adding verifications
From Joomla! Documentation

66 

65 
 

1 Introduction
2 Verifying the form (client side)
3 Verifying the form (server side)
4 Packaging the component
5 Navigate
6 Contributors

 

<fieldset class="adminform">
<legend><?php echo JText::_(
'COM_HELLOWORLD_HELLOWORLD_DETAILS' ); ?></legend>
<?php foreach($this->form->getFieldset() as $field): ?>
<?php if (!$field->hidden): ?>
<?php echo $field->label; ?>
<?php endif; ?>
<?php echo $field->input; ?>
<?php endforeach; ?>
</fieldset>
<div>
<input type="hidden" name="task" value="helloworld.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>

admin/models/forms/helloworld.js
window.addEvent('domready', function() {
document.formvalidator.setHandler('greeting',
function (value) {
regex=/^[^0-9]+$/;
return regex.test(value);
});
});

It adds a handler to the form validator of Joomla for fields having the "validate-greeting" css
class. Each time the greeting field is modified, the handler will be executed to verify its validity
(no digits).
The final step is to verify the form when the save button is clicked.

You may have noted that the html form contained in the admin/views/helloworld/tmpl/edit.php
file now has the form-validate css class. And that we added a
JHTML::_('behavior.formvalidation'); call to tell Joomla to use its javascript form validation.

With your favorite file manager and editor put a file admin/views/helloworld/submitbutton.js
containing
admin/views/helloworld/submitbutton.js

Modify the admin/models/forms/helloworld.xml file to indicate that the greeting field has to be
verified:
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"
label="COM_HELLOWORLD_HELLOWORLD_GREETING_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_GREETING_DESC"
size="40"
class="inputbox validate-greeting"
validate="greeting"
required="true"
default=""
/>
</fieldset>
</form>

Joomla.submitbutton = function(task)
{
if (task == '')
{
return false;
}
else
{
var isValid=true;
var action = task.split('.');
if (action[1] != 'cancel' && action[1] != 'close')
{
var forms = $$('form.form-validate');
for (var i=0;i<forms.length;i++)
{
if
(!document.formvalidator.isValid(forms[i]))
{
isValid = false;
break;
}
}
}
if (isValid)
{
Joomla.submitform(task);
return true;
}
else
{

Note for the moment that the css class is now "inputbox validate-greeting" and that the attribute
required is set to true. It means that this field is required and has to be verified by a handler of
the form validator framework of Joomla
With your favorite file manager and editor put a file admin/models/forms/helloworld.js
containing

alert(Joomla.JText._('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE',

67 
 

68 
 

'Some values are

$this->setDocument();

unacceptable'));

}
return false;
}

/**
* 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'), 'helloworld');
JToolBarHelper::save('helloworld.save');
JToolBarHelper::cancel('helloworld.cancel', $isNew ?
'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
}
/**
* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
$isNew = ($this->item->id < 1);
$document = JFactory::getDocument();
$document->setTitle($isNew ?
JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING')
:
JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING'));
$document->addScript(JURI::root() . $this->script);
$document->addScript(JURI::root() .
"/administrator/components/com_helloworld"
.
"/views/helloworld/submitbutton.js");

}
}

This function will verify that all forms which have the "form-validate" css class are valid. Note
that it will display an alert message translated by the Joomla framework.
The HelloWorldViewHelloWorld view class has to be modified to use these javascript files:
admin/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HelloWorld View
*/
class HelloWorldViewHelloWorld extends JView
{
/**
* display method of Hello view
* @return void
*/
public function display($tpl = null)
{
// get the Data
$form = $this->get('Form');
$item = $this->get('Item');
$script = $this->get('Script');

JText::script('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE');
}
}

// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign the Data
$this->form = $form;
$this->item = $item;
$this->script = $script;

This view now




The final step is to implement a getScript function in the HelloWorldModelHelloWorld model:

// Set the toolbar
$this->addToolBar();

admin/models/helloworld.php

// Display the template
parent::display($tpl);

<?php
// No direct access to this file

// Set the document

69 
 

verifies if the model has no error;
adds two javascript files;
injects javascript translation using the Joomla JText::script function.

70 
 

defined('_JEXEC') or die('Restricted access');

* 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;
}
}

// import Joomla modelform library
jimport('joomla.application.component.modeladmin');
/**
* HelloWorld Model
*/
class HelloWorldModelHelloWorld extends JModelAdmin
{
/**
* 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);
}
/**
* Method to get the record form.
*
* @param
array
$data
Data for the form.
* @param
boolean $loadData
True if the form is to load
its own data (default case), false if not.
* @return
mixed
A JForm object on success, false on failure
* @since
2.5
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_helloworld.helloworld',
'helloworld',
array('control' => 'jform',
'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the script that have to be included on the form
*
* @return string
Script files
*/
public function getScript()
{
return
'administrator/components/com_helloworld/models/forms/helloworld.js';
}
/**

Verifying the form (server side)
Verifying the form on the server side is done by inheritance of JControllerForm class. We have
specified in the admin/models/forms/helloworld.xml file that the validate server function will use
a greeting.php file.
With your favorite file manager and editor, put a admin/models/rules/greeting.php file
containing:
admin/models/rules/greeting.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla formrule library
jimport('joomla.form.formrule');
/**
* Form Rule class for the Joomla Framework.
*/
class JFormRuleGreeting extends JFormRule
{
/**
* The regular expression.
*
* @access
protected
* @var
string
* @since
2.5
*/
protected $regex = '^[^0-9]+$';
}

Note there is no function here - this is inherited from JFormRule (located in:
libraries/joomla/form /rule.php). All that is needed is the regex string to test against.
71 

 

72 
 




















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

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
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.11</version>
<!-- The description is optional and defaults to the name -->
<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>

73 
 

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/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-16x16.png
media/images/tux-48x48.png

74 
 

<file driver="mysql"
charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>
<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>
</extension>

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<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>

Developing a MVC Component/Adding categories
From Joomla! Documentation
< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 12)
This tutorial is for Joomla! CMS Version(s)

Contents

<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
</media>













<administration>
<!-- Administration Menu Section -->
<menu img="../media/com_helloworld/images/tux16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
</files>

[Expand]

Articles in this Series

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

76 

75 
 

1 Introduction
2 Modifying the SQL
3 Modifying the form
4 Modifying the menu type
5 Managing the submenu
6 Adding some ACL
7 Adding some translation strings
8 Packaging the component
9 Zips
10 Navigate
11 Contributors

 

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

description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
size="40"
class="inputbox validate-greeting"
validate="greeting"
required="true"
default=""
/>
<field
name="catid"
type="category"
extension="com_helloworld"
class="inputbox"
default=""
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"

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;

description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
required="true"
>
<option value="0">JOPTION_SELECT_CATEGORY</option>
</field>
</fieldset>
</form>

INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Good bye World!');

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

admin/sql/updates/mysql/0.0.12.sql

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.

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

Modifying the form

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

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/fields/helloworld.php

admin/models/forms/helloworld.xml

<?php
// No direct access to this file
defined('_JEXEC') or die;

<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"

// import the list field type
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
/**
* HelloWorld Form Field class for the HelloWorld component
*/
class JFormFieldHelloWorld extends JFormFieldList
{
/**
* The field type.
*
* @var
string
*/

label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"

77 
 

78 
 

protected $type = 'HelloWorld';

abstract class HelloWorldHelper
{
/**
* Configure the Linkbar.
*/
public static function addSubmenu($submenu)
{

/**
* Method to get a list of options for a list input.
*
* @return
array
An array of JHtml options.
*/
protected function getOptions()
{
$db = JFactory::getDBO();

JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
'index.php?option=com_helloworld',
$submenu == 'messages');

/// $query = new JDatabaseQuery; WARNING - There's an error
in this line, JDatabaseQuery is an abstract class
$query = $db->getQuery(true); // THIS IS THE FIX, WARNING IT
MUST BE FIXED IN THE ZIP FILES

JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),

$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;
}
}

'index.php?option=com_categories&view=categories&extension=com_helloworld',
$submenu == 'categories');
// set some global property
$document = JFactory::getDocument();
$document->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





It will now display the category between parenthesis.

Managing the submenu

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)

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

admin/controller.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

<?php
// No direct access to this file
defined('_JEXEC') or die;

// import Joomla controller library
jimport('joomla.application.component.controller');

/**
* HelloWorld component helper.
*/

/**
* General Controller of HelloWorld component

79 
 

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

80 
 

*/
class HelloWorldController extends JController
{
/**
* display task
*
* @return void
*/
public function display($cachable = false, $urlparams = false)
{
// set default view if not set
$input = JFactory::getApplication()->input;
$input->set('view', $input->getCmd('view', 'HelloWorlds'));

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// require helper file
JLoader::register('HelloWorldHelper', dirname(__FILE__) . DS . 'helpers' . DS
. 'helloworld.php');
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

// call parent behavior
parent::display($cachable, $urlparams);

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Set the submenu
HelloWorldHelper::addSubmenu('messages');

// Redirect if set by the controller
$controller->redirect();

}
}

Adding some translation strings

Adding some ACL

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

admin/access.xml

admin/language/en-GB/en-GB.com_helloworld.ini

<?xml version="1.0" encoding="utf-8" ?>
<access component="com_helloworld">
<section name="component">
<action name="core.admin" title="JACTION_ADMIN"
description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE"
description="JACTION_MANAGE_COMPONENT_DESC" />
<action name="core.create" title="JACTION_CREATE"
description="JACTION_CREATE_COMPONENT_DESC" />
<action name="core.delete" title="JACTION_DELETE"
description="JACTION_DELETE_COMPONENT_DESC" />
<action name="core.edit" title="JACTION_EDIT"
description="JACTION_EDIT_COMPONENT_DESC" />
</section>
<section name="message">
<action name="core.delete" title="JACTION_DELETE"
description="COM_HELLOWORD_ACCESS_DELETE_DESC" />
<action name="core.edit" title="JACTION_EDIT"
description="COM_OLA_HELLOWORD_EDIT_DESC" />
</section>
</access>

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.

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"

admin/helloworld.php

Packaging the component

<?php

Content of your code directory
81 

 

82 
 



































































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

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
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.12</version>
<!-- The description is optional and defaults to the name -->
<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>

83 
 

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-16x16.png
media/images/tux-48x48.png

84 
 

<file driver="mysql"
charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>
<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

<folder>controllers</folder>
<!-- helpers files section -->
<folder>helpers</folder>
</files>

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<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>

<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>
</extension>

Developing a MVC Component/Adding configuration
From Joomla! Documentation
< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 13)
This tutorial is for Joomla! CMS Version(s)

Contents

<administration>
<!-- Administration Menu Section -->
<menu img="../media/com_helloworld/images/tux16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- WARNING <filename>access.php</filename> has an incorrect file
extension, should be corrected in the ZIP-file -->
<filename>access.xml</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->












[Expand]

Articles in this Series

Introduction

86 

85 
 

1 Introduction
2 Adding configuration parameters
3 Using configuration parameters as default value
4 Modifying the SQL
5 Modifying the backend
6 Modifying the frontend
7 Adding some translation strings
8 Packaging the component
9 Navigate
10 Contributors

 

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

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

*/
class HelloWorldViewHelloWorlds extends JView
{
/**
* HelloWorlds view display method
* @return void
*/
function display($tpl = null)
{
// Get data from the model
$items = $this->get('Items');
$pagination = $this->get('Pagination');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign data to the view
$this->items = $items;
$this->pagination = $pagination;

<?xml version="1.0" encoding="utf-8"?>
<config>
<fieldset
name="greetings"
label="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL"
description="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC"
>
<field
name="show_category"
type="radio"

// Set the toolbar
$this->addToolBar();

label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
// Display the template
parent::display($tpl);

description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
default="0"
>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
</fieldset>
</config>

// Set the document
$this->setDocument();
}
/**
* Setting the toolbar
*/
protected function addToolBar()
{

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

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'));
}

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
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HelloWorlds View

87 
 

88 
 

}
label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"

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
<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/administrator/components/com_helloworld/models/rules"
>
<fieldset name="details">
<field
name="id"
type="hidden"
/>
<field
name="greeting"
type="text"

description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
default=""
>
<option value="">JGLOBAL_USE_GLOBAL</option>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
</fieldset>
</fields>
</form>

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:

label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"

admin/sql/install.mysql.utf8.sql

description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
size="40"
class="inputbox validate-greeting"
validate="greeting"
required="true"
default=""
/>
<field
name="catid"
type="category"
extension="com_helloworld"
class="inputbox"
default=""
label="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL"

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:

description="COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC"
required="true"
>
<option value="0">JOPTION_SELECT_CATEGORY</option>
</field>
</fieldset>
<fields name="params">
<fieldset
name="params"
label="JGLOBAL_FIELDSET_DISPLAY_OPTIONS"
>
<field
name="show_category"
type="list"

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

89 
 

90 
 

// No direct access
defined('_JEXEC') or die('Restricted access');

// $params->loadString($this->item->params, 'JSON');
// "item" should not be present.
$params->loadJSON($this->params);

// import Joomla table library
jimport('joomla.database.table');

$this->params = $params;
return true;

/**
* Hello Table class
*/
class HelloWorldTableHelloWorld extends JTable
{
/**
* Constructor
*
* @param object Database connector object
*/
function __construct(&$db)
{
parent::__construct('#__helloworld', 'id', $db);
}
/**
* Overloaded bind function
*
* @param
array
named array
* @return
null|string
null is operation was satisfactory,
otherwise returns an error
* @see JTable:bind
* @since 1.5
*/
public function bind($array, $ignore = '')
{
if (isset($array['params']) && is_array($array['params']))
{
// Convert the params field to a string.
$parameter = new JRegistry;
$parameter->loadArray($array['params']);
$array['params'] = (string)$parameter;
}
return parent::bind($array, $ignore);
}

}
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
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
$params = $this->form->getFieldsets('params');
?>
<form action="<?php echo
JRoute::_('index.php?option=com_helloworld&layout=edit&id='.(int) $this>item->id); ?>"
method="post" name="adminForm" id="helloworld-form" class="formvalidate">
<div class="width-60 fltlft">
<fieldset class="adminform">
<legend><?php echo JText::_(
'COM_HELLOWORLD_HELLOWORLD_DETAILS' ); ?></legend>
<ul class="adminformlist">
<?php
foreach($this->form->getFieldset('details') as
$field): ?>
<li><?php echo $field->label;echo $field>input;?></li>
<?php
endforeach; ?>
</ul>
</fieldset>
</div>

/**
* 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.

<div class="width-40 fltrt">
echo JHtml::_('sliders.start', 'helloworld-slider');
foreach ($params as $name => $fieldset):
echo JHtml::_('sliders.panel', JText::_($fieldset->label),
$name.'-params');
<?php

91 
 

92 
 

if (isset($fieldset->description) && trim($fieldset>description)): ?>
<p class="tip"><?php echo $this>escape(JText::_($fieldset->description));?></p>
<?php
endif;?>
<fieldset class="panelform" >
<ul class="adminformlist">
<?php
foreach ($this->form->getFieldset($name) as
$field) : ?>
<li><?php echo $field->label; ?><?php echo
$field->input; ?></li>
<?php
endforeach; ?>
</ul>
</fieldset>
<?php
endforeach; ?>

protected $item;
/**
* Method to auto-populate the model state.
*
* This method should only be called once per instantiation and is
designed
* to be called on the first call to the getState() method unless the
model
* configuration flag to ignore the request is set.
*
* Note. Calling getState in this method will result in recursion.
*
* @return
void
* @since
2.5
*/
protected function populateState()
{
$app = JFactory::getApplication();
// Get the message id
$input = JFactory::getApplication()->input;
$id = $input->getInt('id');
$this->setState('message.id', $id);

<?php echo JHtml::_('sliders.end'); ?>
</div>
<div>
<input type="hidden" name="task" value="helloworld.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>

// Load the parameters.
$params = $app->getParams();
$this->setState('params', $params);
parent::populateState();

Modifying the frontend
}

The frontend has to be modified according to the new show_category parameter.

/**
* 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);
}

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
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
* 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')

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
/**
* HelloWorld Model
*/
class HelloWorldModelHelloWorld extends JModelItem
{
/**
* @var object item
*/

93 
 

94 
 

->leftJoin('#__categories as c ON

{

h.catid=c.id')

JError::raiseError(500, implode('<br />', $errors));
return false;

->select('h.greeting, h.params, c.title as
category')

}
// Display the view
parent::display($tpl);

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

}
}

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
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->item->greeting.(($this->item->category and $this->item>params->get('show_category'))
? (' ('.$this->item->category.')') :
''); ?>
</h1>

// Merge global params with item params
$params = clone $this->getState('params');
$params->merge($this->item->params);
$this->item->params = $params;
}
}
return $this->item;

Adding some translation strings

}
}

Some strings have to be added in the admin/language/en-GB/en-GB.com_helloworld.ini file
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

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&rsquo;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"

site/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HTML View class for the HelloWorld Component
*/
class HelloWorldViewHelloWorld extends JView
{
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->item = $this->get('Item');
// Check for errors.
if (count($errors = $this->get('Errors')))

95 
 

96 
 































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

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="2.5.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->

97 
 

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-16x16.png
media/images/tux-48x48.png

98 
 

<version>0.0.13</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>

<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
<!-- helpers files section -->
<folder>helpers</folder>
</files>

<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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>

<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<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>

</extension>

Developing a MVC Component/Adding ACL
From Joomla! Documentation
< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 14)
This tutorial is for Joomla! CMS Version(s)

<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
</media>

Contents




<administration>
<!-- Administration Menu Section -->
<menu img="../media/com_helloworld/images/tux16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>config.xml</filename>
<filename>helloworld.php</filename>



100 

99 
 

1 Introduction
2 Adding Access Control
3 Minimal ACL requirements at the component level
o 3.1 Add the 2 minimal component level actions to access.xml
o 3.2 Add the permissions fieldset to config.xml
o 3.3 Add the 'Options' toolbar button when user is authorised for it
o 3.4 Restrict the access to the component's backend to authorised usergroups
4 Adding more actions, also at category level and item level
o 4.1 Describing the actions you want to control the access to
o 4.2 Adding the setting of permissions in the component's Preferences
o 4.3 Displaying only the right toolbar buttons
o 4.4 Restricting access to the component
o 4.5 Add the asset_id column to the database table

 



4.6 Restricting access to the messages
4.7 Setting the permission values in the assets table
4.8 Showing the setting of permissions on the item level
4.9 Adding language strings
5 Further reading
6 Deprecated classes
7 Packaging the component
8 Navigate
9 Contributors
o
o
o
o









Configure (core.admin): which groups are allowed to configure the component level
permissions via the 'Options' toolbar button?
Access Component (core.manage): which groups are allowed to access the component's
backend?

This basic ACL support is done in 4 simple steps:





[Expand]

Add the 2 minimal component level actions to access.xml
Add the permissions fieldset to config.xml
Add the 'Options' toolbar button
Restrict the access to the component's backend

Articles in this Series

Add the 2 minimal component level actions to access.xml
Add an access.xml file to the root of the admin folder. Put the 2 basic actions for the
com_helloworld component in this file.

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

Adding Access Control
With Joomla!'s Access Control we can define which user groups are allowed or denied to do
which actions in your component. In this example we use actions that are defined in the core. For
the component as a whole: core.admin (access to the configuration) and core.manage (access to
the backend). And at various levels actions like create, delete and edit. Besides those core actions
you can define your own actions, but that is often not necessary and is not shown in this
example. View/Read Access is not managed via those actions but with View Access Levels; see
general documentation about Joomla!'s ACL for that.
In the #__assets table the actual list is stored: which user groups are allowed or denied to do
which actions on which resources (assets). This is the implementation of the Access Control List
(ACL).

admin/access.xml
<?xml version="1.0" encoding="utf-8" ?>
<access component="com_helloworld">
<section name="component">
<action name="core.admin" title="JACTION_ADMIN"
description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE"
description="JACTION_MANAGE_COMPONENT_DESC" />
</section>
</access>

Add the permissions fieldset to config.xml
Add the following permissions fieldset to admin/config.xml in order to be able to set our
component level permissions
<fieldset
name="permissions"
label="JCONFIG_PERMISSIONS_LABEL"
description="JCONFIG_PERMISSIONS_DESC"
>
<field
name="rules"
type="rules"
label="JCONFIG_PERMISSIONS_LABEL"
class="inputbox"
validate="rules"
filter="rules"
component="com_helloworld"
section="component"

In this article we will show how to add and use access permissions at several levels of
granularity: for your component as a whole, for the categories and for the individual items.

Minimal ACL requirements at the component level
There are 2 actions that need to be defined at the component level for a Joomla! 2.5 component
to offer basic ACL support:

101 
 

102 
 




/>
</fieldset>

See the more elaborate config.xml example further downwards for the exact place where to
insert this code.

Describing the actions you want to control the access to
Each component (or part of it) has its own set of permissions that can be controlled. They are
described in an access.xml file located at the root of the admin folder. In this helloworld-example
the actions to which access is controlled are divided in three sections: at the component level, the
category level and the item level. An 'item' is called a 'message' in our example component,
hence the name of the third section.

Add the 'Options' toolbar button when user is authorised for it
Add the following code to admin/views/helloworlds/view.html.php:
// Options button.
if (JFactory::getUser()->authorise('core.admin', 'com_helloworld'))
JToolBarHelper::preferences('com_helloworld');
}

{

See further downwards for a more elaborated example of admin/views/helloworlds/view.html.php
where this JToolBarHelper::preferences('com_helloworld') is done in an addToolBar()-method
together with the other toolbar buttons and the JUser->authorise()-check is done in the
admin/helpers/helloworld.php helper file, resulting in the $canDo-property.

Restrict the access to the component's backend to authorised usergroups
To control the access to the backend of the component add the following lines to the
admin/helloworld.php entry-file:
// Access check: is this user allowed to access the backend of this
component?
if (!JFactory::getUser()->authorise('core.manage', 'com_helloworld'))
{
return JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
}

See further downwards for the whole code of the admin/helloworld.php file.

Adding more actions, also at category level and item level
When adding more actions and more levels, the above described 4 steps are done too:





Add the actions to access.xml; here we can add more actions and levels
Add the permissions-fieldset to config.xml
Add the 'Options' toolbar button
Restrict the access to the component's backend

admin/access.xml
<?xml version="1.0" encoding="utf-8" ?>
<access component="com_helloworld">
<section name="component">
<action name="core.admin" title="JACTION_ADMIN"
description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE"
description="JACTION_MANAGE_COMPONENT_DESC" />
<action name="core.create" title="JACTION_CREATE"
description="JACTION_CREATE_COMPONENT_DESC" />
<action name="core.delete" title="JACTION_DELETE"
description="JACTION_DELETE_COMPONENT_DESC" />
<action name="core.edit" title="JACTION_EDIT"
description="JACTION_EDIT_COMPONENT_DESC" />
</section>
<section name="category">
<action name="core.create" title="JACTION_CREATE"
description="COM_CATEGORIES_ACCESS_CREATE_DESC" />
<action name="core.delete" title="JACTION_DELETE"
description="COM_CATEGORIES_ACCESS_DELETE_DESC" />
<action name="core.edit" title="JACTION_EDIT"
description="COM_CATEGORIES_ACCESS_EDIT_DESC" />
<action name="core.edit.state" title="JACTION_EDITSTATE"
description="COM_CATEGORIES_ACCESS_EDITSTATE_DESC" />
<action name="core.edit.own" title="JACTION_EDITOWN"
description="COM_CATEGORIES_ACCESS_EDITOWN_DESC" />
</section>
<section name="message">
<action name="core.delete" title="JACTION_DELETE"
description="COM_HELLOWORLD_ACCESS_DELETE_DESC" />
<action name="core.edit" title="JACTION_EDIT"
description="COM_HELLOWORLD_ACCESS_EDIT_DESC" />
</section>
</access>

Adding the setting of permissions in the component's Preferences

In addition we also have to do the following steps:



Add an asset_id to the item's database table for item level access control
Store the permissions in the assets table. Especially take care of setting the asset_id of the
parent-asset

Since we now use Access Control permissions in our component, we need to be able to set them
at the component level. That is done in the Preferences of this component: the screen you see
after clicking the 'Options' button. The config.xml-file is a form-definition for those Preferences.

103 
 

Make the settings of the permissions at the item level editable
Add some language strings

104 
 

We could define the component level actions here too, as a child of the "rules" field-tag, but it is
now preferred to also put those actions in access.xml: in that way all access rules for this
component are on one spot.
admin/config.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
<fieldset
name="greetings"
label="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_LABEL"
description="COM_HELLOWORLD_CONFIG_GREETING_SETTINGS_DESC"
>
<field
name="show_category"
type="radio"

* HelloWorlds View
*/
class HelloWorldViewHelloWorlds extends JViewLegacy
{
protected $items;
protected $pagination;
protected $canDo;
/**
* HelloWorlds view display method
* @return void
*/
function display($tpl = null)
{
// Get data from the model
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');

(s)he do?

label="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_LABEL"
description="COM_HELLOWORLD_HELLOWORLD_FIELD_SHOW_CATEGORY_DESC"
default="0"
>
<option value="0">JHIDE</option>
<option value="1">JSHOW</option>
</field>
</fieldset>
<fieldset
name="permissions"
label="JCONFIG_PERMISSIONS_LABEL"
description="JCONFIG_PERMISSIONS_DESC"
>
<field
name="rules"
type="rules"
label="JCONFIG_PERMISSIONS_LABEL"
class="inputbox"
validate="rules"
filter="rules"
component="com_helloworld"
section="component"
/>
</fieldset></config>

// Set the toolbar
$this->addToolBar();
// Display the template
parent::display($tpl);
// Set the document
$this->setDocument();
}
/**
* Setting the toolbar
*/
protected function addToolBar()
{

Displaying only the right toolbar buttons
Which toolbar buttons to display depends on the Access Control permissions for the user. We put
all permissions for this user in the $canDo property of the view; so we can eventually refer to it
in layouts (in the edit-form for example).
In the admin/views/helloworlds/view.html.php, put this code
admin/views/helloworlds/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**

JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'),
'helloworld');
if ($this->canDo->get('core.create'))
{
JToolBarHelper::addNew('helloworld.add', 'JTOOLBAR_NEW');
}
if ($this->canDo->get('core.edit'))
{
JToolBarHelper::editList('helloworld.edit',
'JTOOLBAR_EDIT');
}
if ($this->canDo->get('core.delete'))
{
JToolBarHelper::deleteList('', 'helloworlds.delete',
'JTOOLBAR_DELETE');
}
if ($this->canDo->get('core.admin'))
{
JToolBarHelper::divider();
JToolBarHelper::preferences('com_helloworld');
}
}
/**

105 
 

// What Access Permissions does this user have? What can
$this->canDo = HelloWorldHelper::getActions();
// Check for errors
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}

106 
 

* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
$document = JFactory::getDocument();
$document>setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
}
}

// Display the template
parent::display($tpl);
// Set the document
$this->setDocument();
}
/**
* Setting the toolbar
*/
protected function addToolBar()
{
$input = JFactory::getApplication()->input;
$input->set('hidemainmenu', true);
$user = JFactory::getUser();
$userId = $user->id;
$isNew = $this->item->id == 0;
JToolBarHelper::title($isNew ?
JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW')
:
JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT'), 'helloworld');
// Build the actions for new and existing records.
if ($isNew)
{
// For new records, check the create permission.
if ($this->canDo->get('core.create'))
{
JToolBarHelper::apply('helloworld.apply',
'JTOOLBAR_APPLY');
JToolBarHelper::save('helloworld.save',
'JTOOLBAR_SAVE');
JToolBarHelper::custom('helloworld.save2new',
'save-new.png', 'save-new_f2.png',

In the admin/views/helloworld/view.html.php, put this code
admin/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HelloWorld View
*/
class HelloWorldViewHelloWorld extends JViewLegacy
{
protected $form;
protected $item;
protected $script;
protected $canDo;
/**
* display method of Hello view
* @return void
*/
public function display($tpl = null)
{
// get the Data
$this->form = $this->get('Form');
$this->item = $this->get('Item');
$this->script = $this->get('Script');

(s)he do?
>item->id);

'JTOOLBAR_SAVE_AND_NEW', false);
}
JToolBarHelper::cancel('helloworld.cancel', 'JTOOLBAR_CANCEL');
}
else
{
if ($this->canDo->get('core.edit'))
{
// We can save the new record
JToolBarHelper::apply('helloworld.apply',
'JTOOLBAR_APPLY');
JToolBarHelper::save('helloworld.save',
'JTOOLBAR_SAVE');

// What Access Permissions does this user have? What can
$this->canDo = HelloWorldHelper::getActions($this-

// We can save this record, but check the
// Check for errors
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}

create permission to see

// Set the toolbar
$this->addToolBar();

'JTOOLBAR_SAVE_AND_NEW', false);

// if we can return to make a new one.
if ($this->canDo->get('core.create'))
{
JToolBarHelper::custom('helloworld.save2new', 'save-new.png', 'savenew_f2.png',

}
if ($this->canDo->get('core.create'))

107 
 

}
{

108 
 

JToolBarHelper::custom('helloworld.save2copy', 'save-copy.png', 'savecopy_f2.png',

JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_CATEGORIES'),

'JTOOLBAR_SAVE_AS_COPY', false);
}
JToolBarHelper::cancel('helloworld.cancel', 'JTOOLBAR_CLOSE');
}
}
/**
* Method to set up the document properties
*
* @return void
*/
protected function setDocument()
{
$isNew = $this->item->id == 0;
$document = JFactory::getDocument();
$document->setTitle($isNew ?
JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING')
:
JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING'));
$document->addScript(JURI::root() . $this->script);
$document->addScript(JURI::root() .
"/administrator/components/com_helloworld"
.
"/views/helloworld/submitbutton.js");

'index.php?option=com_categories&view=categories&extension=com_helloworld',
$submenu == 'categories');
// set some global property
$document = JFactory::getDocument();
$document->addStyleDeclaration('.icon-48-helloworld ' .
'{background-image:
url(../media/com_helloworld/images/tux-48x48.png);}');
if ($submenu == 'categories')
{
$document>setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION_CATEGORIES'));
}
}
/**
* Get the actions
*/
public static
function getActions($messageId = 0)
{
jimport('joomla.access.access');
$user
=
JFactory::getUser();
$result = new JObject;
if
(empty($messageId)) {
$assetName = 'com_helloworld';
}
else {
$assetName =
'com_helloworld.message.'.(int) $messageId;
}
$actions = JAccess::getActions('com_helloworld',
'component');
foreach ($actions as $action) {
$result->set($action->name, $user->authorise($action->name, $assetName));
}
return $result;
}}

JText::script('COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE');
}
}

Restricting access to the component

These two files use the getActions method defined in the admin/helpers/helloworld.php file

The main idea in ACL is to restrict actions to groups of users. The first action to be restricted is
access to the administrative backend of the component itself. With your favorite file editor, edit
the admin/helloworld.php file and add the lines with the access check.

In the helper-file, put this code:
admin/helpers/helloworld.php

admin/helloworld.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
* HelloWorld component helper.
*/
abstract class HelloWorldHelper
{
/**
* Configure the Linkbar.
*/
public static function addSubmenu($submenu)
{

// Access check: is this user allowed to access the backend of this
component?if (!JFactory::getUser()->authorise('core.manage',
'com_helloworld')) {
return JError::raiseWarning(404,
JText::_('JERROR_ALERTNOAUTHOR'));}
// require helper file
JLoader::register('HelloWorldHelper', dirname(__FILE__) .
'/helpers/helloworld.php');
// import joomla controller library
jimport('joomla.application.component.controller');

JSubMenuHelper::addEntry(JText::_('COM_HELLOWORLD_SUBMENU_MESSAGES'),
'index.php?option=com_helloworld',
$submenu == 'messages');

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

109 
 

110 
 

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

if( !empty( $record->id ) ){
$user = JFactory::getUser();
return $user->authorise( "core.delete",
"com_helloworld.message." . $record->id );
}
}

// Redirect if set by the controller
$controller->redirect();

To check "core.edit" (and core.add if you wish) you need to update the sub-controller (not the
model). I am not sure why this is so, but that's how other standard Joomla components do it. You
need to add the following lines in the file: /admin/controllers/helloworld.php

Add the asset_id column to the database table
In order to be able to work with JTable an asset_id column has to be added to the database
#__helloworld table.
So, admin/sql/install.mysql.utf8.sql becomes:
admin/sql/install.mysql.utf8.sql
DROP TABLE IF EXISTS `#__helloworld`;
CREATE TABLE `#__helloworld` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`asset_id` INT(10) NOT NULL DEFAULT '0',
`catid` int(11) NOT NULL DEFAULT '0',
`params` TEXT NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);

`greeting` varchar(25) NOT NULL,

INSERT INTO `#__helloworld` (`greeting`) VALUES
('Hello World!'),
('Good bye World!');

For updates we add a sql-update-file:
admin/sql/updates/mysql/0.0.14.sql
ALTER TABLE`#__helloworld` ADD COLUMN `asset_id` INT(10) UNSIGNED NOT NULL
DEFAULT '0' AFTER `id`;

Restricting access to the messages
So far we have restricted access to the component itself, but we also need to do that at message
level.
To check the "core.delete" permission you need to modify the model class:
admin/models/helloworld.php by adding these lines:
/**
* Method to check if it's OK to delete a message. Overwrites
JModelAdmin::canDelete
*/
protected function canDelete($record)
{

/**
* Implement to allowAdd or not
*
* Not used at this time (but you can look at how other components use
it....)
* Overwrites: JControllerForm::allowAdd
*
* @param array $data
* @return bool
*/
protected function allowAdd($data = array())
{
return parent::allowAdd($data);
}
/**
* Implement to allow edit or not
* Overwrites: JControllerForm::allowEdit
*
* @param array $data
* @param string $key
* @return bool
*/
protected function allowEdit($data = array(), $key = 'id')
{
$id = isset( $data[ $key ] ) ? $data[ $key ] : 0;
if( !empty( $id ) ){
$user = JFactory::getUser();
return $user->authorise( "core.edit", "com_helloworld.message." .
$id );
}
}

Please note that allowAdd simply calls its parent. I've put it here in case you want to actually use
it in your component. If you look at your admin/access.xml file, you will notice there is no
core.add action defined for "messages", so you will need to add it there as well if you want to be
able to configure it in the interface.

Setting the permission values in the assets table
In order to store permissions for each message in the assets database table, we have to instruct
the table class associated with the model to save those permissions in the assets table.
111 

 

112 
 

JTable not only provides an interface for storing the data of the record itself in the item's
database table, but also for storing the permissions for that record in the assets database table.
Therefore we must add information to the bind()-method about the permission-values. We also
have to provide the asset name, asset title and the id of the asset parent via the helloworld JTable.
Therefore we override 3 methods:




// Bind the rules.
&& is_array($array['rules']))
= new JAccessRules($array['rules']);
>setRules($rules);
}
parent::bind($array, $ignore);

admin/tables/helloworld.php
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
jimport('joomla.database.table');

}

/**
* Method to compute the default name of the
asset.
* The default name is in the form `table_name.id`
*
where id is the value of the primary key of the table.
*
*
@return
string
* @since
2.5
*/
protected
function _getAssetName()
{
$k = $this->_tbl_key;
return 'com_helloworld.message.'.(int) $this->$k;
}
/**
* Method to return the title to use for the asset
table.
*
* @return
string
* @since
2.5
*/
protected function _getAssetTitle()
{
return $this->greeting;
}
/**
* Method to get the
asset-parent-id of the item
*
* @return
int
*/
protected function _getAssetParentId()
{
// We will
retrieve the parent-asset from the Asset-table
$assetParent =
JTable::getInstance('Asset');
// Default: if no asset-parent
can be found we take the global asset
$assetParentId =
$assetParent->getRootId();
// Find the parent-asset
if (($this->catid)&& !empty($this->catid))
{
// The item has a category as asset-parent
$assetParent->loadByName('com_helloworld.category.' . (int) $this->catid);
}
else
{
// The item has
the component as asset-parent
$assetParent>loadByName('com_helloworld');
}
// Return the
found asset-parent-id
if ($assetParent->id)
{
$assetParentId=$assetParent->id;
}
return
$assetParentId;
}}

/**
* Overridden bind function
*
* @param
array
named array
* @return
null|string
null if operation was satisfactory,
otherwise returns an error
* @see JTable:bind
* @since 1.5
*/
public function bind($array, $ignore = '')
{
if (isset($array['params']) && is_array($array['params']))
{
// Convert the params field to a string.
$parameter = new JRegistry;
$parameter->loadArray($array['params']);
$array['params'] = (string)$parameter;
}

113 
 

if (isset($array['rules'])
$rules
$thisreturn

/**
* Overridden 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;
$params->loadJSON($this->params);
$this->params = $params;
return true;
}
else
{
return false;
}
}

_getAssetName(): a unique name for this asset, by which it can be retrieved
_getAssetTitle(): a more human-friendly way to identify the asset (not necessary unique)
_getAssetParentId(): the asset_id of the parent in the asset database table (from whom
permissions are inherited)

/**
* Hello Table class
*/
class HelloWorldTableHelloWorld extends JTable
{
/**
* Constructor
*
* @param object Database connector object
*/
function __construct(&$db)
{
parent::__construct('#__helloworld', 'id', $db);
}

{

114 
 

This code for _getAssetParentId() above uses JTableAsset to retrieve the asset_id of the assetparent. This is different from the code in the current version of com_content, where the asset_id
of the category is retrieved from the #__categories database table. That is another possibility;
many ways leading to Rome. In com_content however, if an item would not be under a category,
then the asset_id of the global asset is returned. That would of course not be right, but is fixed
there by providing a default category "uncategorised", so that an article is always under a
category. That is why you cannot just copy the code of _getAssetParentId() in com_content to
your own component. The code above is more general.

Showing the setting of permissions on the item level
Adding the rules field to the form-definition of the edit-form
admin/models/forms/helloworld.xml
<fieldset name="accesscontrol">
<field name="asset_id" type="hidden" filter="unset" />
<field name="rules"
type="rules"
label="JFIELD_RULES_LABEL"
translate_label="false"
filter="rules"
validate="rules"
class="inputbox"
component="com_helloworld"
section="message"
/>
</fieldset>

<li><?php echo $field->label;echo $field->input;?></li>
<?php endforeach; ?>
</ul>
</fieldset>
</div>
<div class="width-40 fltrt">
<?php echo JHtml::_('sliders.start', 'helloworld-slider');
foreach ($params as $name => $fieldset):
echo JHtml::_('sliders.panel', JText::_($fieldset->label),
$name.'-params');
if (isset($fieldset->description) && trim($fieldset>description)): ?>
<p class="tip"><?php echo $this->escape(JText::_($fieldset>description));?></p>
<?php endif;?>
<fieldset class="panelform" >
<ul class="adminformlist">
<?php foreach ($this->form->getFieldset($name) as $field) :
?>
<li><?php echo $field->label; ?><?php echo $field>input; ?></li>
<?php endforeach; ?>
</ul>
</fieldset>
<?php endforeach; ?>
<?php echo JHtml::_('sliders.end'); ?>
</div>
<!-- begin ACL definition-->
<div class="clr"></div>
<?php if
($this->canDo->get('core.admin')): ?>
<div class="width-100 fltlft">
<?php echo JHtml::_('sliders.start', 'permissions-sliders-'.$this->item->id,
array('useCookie'=>1)); ?>
<?php echo JHtml::_('sliders.panel',
JText::_('COM_HELLOWORLD_FIELDSET_RULES'), 'access-rules'); ?>
<fieldset class="panelform">
<?php echo $this->form>getLabel('rules'); ?>
<?php echo $this->form>getInput('rules'); ?>
</fieldset>
<?php echo
JHtml::_('sliders.end'); ?>
</div>
<?php endif; ?>
<!-- end ACL
definition-->
<div>
<input type="hidden" name="task" value="helloworld.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>

And display the ACL interface at the bottom of your Helloworld editform
admin/views/helloworld/tmpl/edit.php
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
$params = $this->form->getFieldsets('params');
?>
<form action="<?php echo
JRoute::_('index.php?option=com_helloworld&layout=edit&id='.(int) $this>item->id); ?>"
method="post" name="adminForm" id="helloworld-form" class="formvalidate">

Adding language strings
We used 3 language strings that have to be added to the backend language-file.

<div class="width-60 fltlft">
<fieldset class="adminform">
<legend><?php echo JText::_( 'COM_HELLOWORLD_HELLOWORLD_DETAILS' );
?></legend>
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('details') as $field): ?>

admin/language/en-GB/en-GB.com_helloworld.ini
COM_HELLOWORLD_FIELDSET_RULES="Message Permissions"
COM_HELLOWORLD_ACCESS_DELETE_DESC="Is this group allowed to edit this
message?"

115 
 

116 
 
















































COM_HELLOWORLD_ACCESS_DELETE_DESC="Is this group allowed to delete this
message?"

Further reading
More information on actions, assets and ACL can be found on the following pages:





General information and use: Access Control List/1.6-2.5/Tutorial
Technical information, under construction: ACL Technique in Joomla!
How to implement actions in your code
Adding ACL rules to your component

Deprecated classes
For the moment we leave the deprecated JError-references as they are. They will probably still
be available in Joomla! 3.x. We cannot just change them to JLog::add() statements because in
Joomla! 2.5 the messages will then not be enqueued (as there is no messagequeue-logger added
as is in /libraries/cms.php in Joomla! 3.0). Other solutions, like using $app->enqueueMessage()
or directly throwing PHP-exceptions as showstopper are also possible, but then there would still
be numerous references to JError throughout the application. For instance in the view, we now
check for errors raised in the model with: count($errors = $this->get('Errors')), which uses JError
from the JOBject that was the base for JModel. To get the same functionality without using
JError at all, we would have to change the way the Model raises those errors and warnings now.
If we want to make an application that would work in Joomla! 2.5 and 3.x we can continue using
JError. The moment we want to use our 3.x extensions also in 4.x we will have to change this
(and probably a lot more too). This tutorial is now primarily focussed on Joomla! 2.5. So: we
notice the upcoming change, but leave it there for the moment.
Since Joomla! 2.5.5 the MVC-base-classes JController, JModel and JView got proxies
JControllerLegacy, JModelLegacy and JViewLegacy. You are recommended to use those
proxies instead of the original classes to be forward compatibility with Joomla! CMS 3.x legacy
classes.

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
117 

 

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/access.xml
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
118 

 














</update>

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-16x16.png
media/images/tux-48x48.png

<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<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>

Create a compressed file of this directory or directly download the archive (TODO: zip has to be
updated! Will be done coming days...) 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.

<administration>
<!-- Administration Menu Section -->
<menu img="../media/com_helloworld/images/tux16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>config.xml</filename>
<filename>access.xml</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
<!-- helpers files section -->
<folder>helpers</folder>
</files>

helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.14</version>
<!-- The description is optional and defaults to the name -->
<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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>

<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>

119 
 

120 
 





</languages>
</administration>
</extension>

Developing a MVC Component/Adding an install‐uninstall‐update script
file
From Joomla! Documentation

Writing an extension script consists in declaring an class whose name is
com_ComponentNameInstallerScript with these 5 methods.
script.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

< J2.5:Developing a MVC Component(Redirected from Developing a Model-View-Controller (MVC)
Component for Joomla!2.5 - Part 15)

/**
* Script file of HelloWorld component
*/
class com_helloWorldInstallerScript
{
/**
* method to install the component
*
* @return void
*/
function install($parent)
{
// $parent is the class calling this method
$parent->getParent()>setRedirectURL('index.php?option=com_helloworld');
}

This tutorial is for Joomla! CMS Version(s)

Contents







update
uninstall
postflight which is executed after install and update

1 Introduction
2 Creating the extension script file
3 Adding some language keys
4 Packaging the component
5 Navigate
6 Contributors

[Expand]

/**
* method to uninstall the component
*
* @return void
*/
function uninstall($parent)
{
// $parent is the class calling this method
echo '<p>' . JText::_('COM_HELLOWORLD_UNINSTALL_TEXT') .

Articles in this Series

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

Creating the extension script file
Installing, updating and uninstalling a component may require additional operations that cannot
be achieved by the basic operations described in the main xml file. Joomla offers a new approach
to solve this problem. It consists in using a php script file containing a class using five methods:



preflight which is executed before install and update
install

'</p>';
}
/**
* method to update the component
*
* @return void
*/
function update($parent)
{
// $parent is the class calling this method
echo '<p>' . JText::sprintf('COM_HELLOWORLD_UPDATE_TEXT',
$parent->get('manifest')->version) . '</p>';
}
/**

121 
 

122 
 

* method to run before an install/update/uninstall method
*
* @return void
*/
function preflight($type, $parent)
{
// $parent is the class calling this method
// $type is the type of change (install, update or
discover_install)
echo '<p>' . JText::_('COM_HELLOWORLD_PREFLIGHT_' . $type .
'_TEXT') . '</p>';
}

COM_HELLOWORLD_PREFLIGHT_UPDATE_TEXT="HelloWorld preflight update script"
COM_HELLOWORLD_UNINSTALL_TEXT="HelloWorld Uninstall script"
COM_HELLOWORLD_UPDATE_TEXT="HelloWorld Update script. HelloWorld now updated
to version %s."

note: If you want that these languages KEYs to be used at the first install of the component, the
sys.ini language file must be stored in the component folder (admin/language/en-GB/enGB.com_helloworld.sys.ini), and the xml manifest file must contains a folder tag for copying
language in the component folder. Modify your Manifest file accordingly:

/**
* method to run after an install/update/uninstall method
*
* @return void
*/
function postflight($type, $parent)
{
// $parent is the class calling this method
// $type is the type of change (install, update or
discover_install)
echo '<p>' . JText::_('COM_HELLOWORLD_POSTFLIGHT_' . $type .
'_TEXT') . '</p>';
}
}

<files folder="admin">
<!-- language folder -->
<folder>language</folder>
</files>
<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<!-- com_helloworld.sys.ini no longer needed there-->
</languages>

Packaging the component
Content of your code directory
























This script file will redirect the user to the com_helloworld component when it is installed and
will display messages when it is updated or uninstalled. In the update method we show the new
version using $parent->get('manifest')->version.

Adding some language keys
admin/language/en-GB/en-GB.com_helloworld.sys.ini""
COM_HELLOWORLD="Hello World!"
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected
message"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE="Hello World"
COM_HELLOWORLD_INSTALL_TEXT="HelloWorld Install script"
COM_HELLOWORLD_MENU="Hello World!"
COM_HELLOWORLD_POSTFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld postlight
discover install script"
COM_HELLOWORLD_POSTFLIGHT_INSTALL_TEXT="HelloWorld postflight install script"
COM_HELLOWORLD_POSTFLIGHT_UNINSTALL_TEXT="HelloWorld postflight uninstall
script"
COM_HELLOWORLD_POSTFLIGHT_UPDATE_TEXT="HelloWorld postflight update script"
COM_HELLOWORLD_PREFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld preflight discover
install script"
COM_HELLOWORLD_PREFLIGHT_INSTALL_TEXT="HelloWorld preflight install script"
COM_HELLOWORLD_PREFLIGHT_UNINSTALL_TEXT="HelloWorld preflight uninstall
script"

123 
 

helloworld.xml
script.php
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/access.xml
admin/config.xml
admin/helloworld.php
admin/controller.php
admin/sql/index.html
admin/sql/install.mysql.utf8.sql
124 

 














































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-16x16.png
media/images/tux-48x48.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
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting
constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.15</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>
<!-- Runs on install/uninstall/update; New in 2.5 -->
<scriptfile>script.php</scriptfile>
<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; New in 2.5 -->
<schemas>
<schemapath
type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
<folder>models</folder>

125 
 

126 
 

<folder>language</folder>
</files>
<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
</media>
<administration>
<!-- Administration Menu Section -->
<menu img="../media/com_helloworld/images/tux16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the
folder
to copy FROM in the package to install therefore
files copied
in this section are copied from /admin/ in the
package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>config.xml</filename>
<filename>access.xml</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
<!-- helpers files section -->
<folder>helpers</folder>
</files>
<languages folder="admin">
<language tag="en-GB">language/en-GB/enGB.com_helloworld.ini</language>
<language tag="en-GB">language/en-GB/enGB.com_helloworld.sys.ini</language>
</languages>
</administration>
</extension>

 
 

127 
 

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close