Creating components for Joomla 1.0.x

11 September 2009 Published in Blog

This article will explain how to develop customized components for Joomla in the 1.0.x version, showing the files and basic steps, as well as some useful methods such as access to the database, paged results, etc. It is recommended to have knowledge of PHP and HTML to read this article.

 

Directory structure

Joomla has a directory structure divided into 2 big groups. Administrator, where are all the files needed for the backend to work and the various directories that are located in the root directory.

The directories of interest are the components, both within the administrator directory, as in the root directory, since here is where the files for each component are, which in turn are in sub-directories as follows, com_componentName, example com_users.

If we want to see the files corresponding to the users component, must look at

  • Administrator/components/com_users (backend)
  • Components/com_users (frontend)

Another important is the includes directory, since within it we find files that have classes that are going to be used in the component, for example to connect the database (database.php), to manage user data, paging, etc (joomla.php).

The components are stored in the database, more precisely in the jos_components table.

Files that make up a component

A component to be installed must be compressed in a zip file, with the following format com_ComponentName.zip example com_users.zip.

This file must have at least 8 files that are going to be described in brief:

  • admin.componentName.php (backend)
  • admin. componentName.html.php (backend)
  • toolbar. componentName.php (backend)
  • toolbar. componentName.html.php (backend)
  • componentName.xml (backend)
  • componentName.class.php (opcional)
  • componentName.php (frontend)
  • componentName.html.php (frontend

The following describes each file.

 

 

componentName.xml (Backend)

This file is the one that installs the component.

Basic structure of the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
 Presentation
 01/10/2008
 Serfe
 This component in released under the GNU/GPL License
 info@serfe.com
 www.serfe.com
 1.0.0
 
 example.php
 example.html.php
 example.class.php
 
 
 
 CREATE TABLE `#__example` (
 id INT NOT NULL AUTO_INCREMENT,
 title VARCHAR(100) NOT NULL,
 content TEXT NOT NULL,
 published TINYINT(1) NOT NULL,
 date DATETIME NOT NULL,
 PRIMARY KEY (`id`)
 )
 
 
 
 
 
 
 DROP TABLE IF EXISTS `#__example`;
 
 
 
 
 Example
 
 List
 
 
 admin.example.php
 admin.example.html.php
 toolbar.example.php
 toolbar.example.html.php
 
 
 

 

admin. componentName.php (Backend):

It is the first file to be executed when loading the component in the backend, and is responsible for controlling what action is going to be performed, according to act and task parameters.

The act parameter identifies which backend’s menu item was selected.

The task parameter tells us which action is going to be executed, such as save, delete, etc.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
getPath( 'admin_html' ) );
 require_once( $mainframe->getPath( 'class' ) );
 //Performs the includes of the needed files.
 
$id = mosGetParam( $_REQUEST, 'cid', array(0) );
 //Capture the variable passed by REQUEST.
 
switch($act)
 {
 default
 switch($task)
 {
 default:
 list($option);
 break;
 case "save":
 save($option);
 break;
 }
 break;
 }
 break;
 
//Function that lists the records from a table in the database.
 function list($option)
 {
 global $database, $mainframe;
 $limit = $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', 10 );
 //Get the variable limit that is used for paging
 $limitstart = $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 );
 //Get the variable limitstart that specifies from which record to begin displaying.
 
$query = "SELECT COUNT(id) FROM #__example";
 $database->setQuery($query);
 $total = $database->loadResult();
 //Executes the query and returns the result in a variable, this function is used when the consult returns a single result.
 require_once("includes/pageNavigation.php");
 //Includes the file cointaning the class for the paging.
 $pageNav = new mosPageNav( $total, $limitstart, $limit );
 //Instantiate the page class.
 $query  = "SELECT * FROM #__example";
 $query .= " LIMIT ".$limitstart.", ".$limit;
 $database->setQuery($query);
 $rows = $database->loadObjectList();
 //Executes the query returning the result into an objects array.
 HTML_Example::list($option, $rows, $pageNav);
 //Call the method that will generate the HTML from the list, and it gets as parameters the option the actual component will have, the array with the records, and the object with the paging.
 }
 
//Function that saves a record in the database.
 function save($option) {
 global $database;
 
$row = new presentacionTable($database);
 //Instance the class that to be created in the file componentName.class.php
 $row->bind($_POST));
 //If the form's input names match with the names in the database table's fields, this method takes care to relate each form field with its respective column in the table.
 $row->date = date("Y-m-d H:i");
 // Seats a particular field.
 $row -> store();
 // Saves the values in the database.
 mosRedirect("index2.php?option=".$option, "Example saved!");
 // The page is redirected to a specific address, the second parameter is the message to display.
 }
 ?>

 

This file implements 2 actions: list and save.

The first lines includes the needed files, then receive the variables that come in for REQUEST. Next, we determine which action to execute according to the value of the act and task variables.

Then, we implement each of the necessary functions to perform the required tasks.

admin. componentName.html.php (Backend):

This file defines the class that will contain the different methods responsible for generating the HTML.

The basic structure of the file is:

1
2
3
4
5
6
7
 

 

toolbar. componentName.php (Backend):

This file controls which menu to display according to the received act and task parameters. The basic structure of the file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
getPath( 'toolbar_html' ) );
// Performs an include of the file named toolbar.componentName.html.php which has the HTML for the menu to display.
 
switch ( $task ) {
 default:
 menuExample::DEFAULT_MENU();
 // Load the default menu from the class menuExample.
 break;
 }
?>

 

toolbar. componentName.html.php (Backend):

This file generates the menu to display. The basic structure of the file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 

 

componentName.class.php (opcional):

This file is used to create a class that will make it easier for us to save, edit and delete a record from the table using the methods inherited from the class mosDBTable.

In the example provided in the file admin.componentName.php we have used a class of this king, more specifically when using the methods bind(), store().

This class also allows us to load a record simply by using the load() method, which receives as parameters the record id.

Basic structure of the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
mosDBTable('#__tableName', 'id', $db);
 //This method receives 3 parameters: table name, field with the primary key, database connector.
 }
 }
 ?>

 

componentName.php and componentName.html.php (Frontend):

These 2 files are used to perform actions in the frontend component. The file componentName.php has the same structure as the admin.componentName.php file. The file componentName.html.php has the same structure as the file named admin.componentName.html.php

Serfe info(at)serfe.com https://www.serfe.com/images/serfe_logo_text.png https://www.serfe.com/images/serfe_logo_text.png FALUCHO 2032, S3016LDB, SANTO TOME, SANTA FE, ARGENTINA 1-305-5375397
Cookies & Privacy: 
This website uses cookies to ensure you get the best experience on our website.


Privacy Policy