ss_blog_claim=3f4ffd837502643f53746c9e0ca5131b

Tips to make a joomla component

01:40 July 14th, 2008 by shadow_of__soul

Hi,

working like a freelance developer you are forced to make several things, using several tools,apis, codes and framework. for a experienced developer, it’s easy to make code for a new platform, if the documentation it’s available and it’s clear.

in the past few days i has been doing a component for joomla, and it’s not hard, but has been some difficult to find some specific information about the framework. for this, i want to use this post explain the principal function and how it work :D

note:

in the bottom you going to find a hello world component like a example of all what i’m talking here.

1. Component Structure

A joomla component it’s really a zip package with files in it, but really, how it’s made?. the structure it’s very simple, inside the zip you going to find a folder, with the same name of the package (normally it’s something like “com_componentname”) and  inside them, a php file with the name of the component (but whiteout the com_), a xml with the name of the component, and the other files are choosen by the developer (you are free to do whatever you want :P).

the most important file what you going to have, it’s the  .xml file, there you going to define the SQL query’s to make in the moment of the installation, all files and structure of the component to install, a custom install/uninstall script to execute and the menu’s in the admin section.

a component, only it’s group of php files working togheter, you need to know what joomla going to call the file “your-component-name.php”, and there you need to define the actions to do, you can receive get and post var, and all the things you can do in a php script :D

of course, the joomla framework it’s done using objects, for that it’s the best to create a class, and inside the methods to call it later.

2. Database

well, at this point you going to unsertand at least, how work a component, for that reason you already start to write your code, checking the example component what you download from the bottom of this page, surely you define the global $database; and when you execute a query, it dont work right? haha, well, here it’s your solution:

for some reason, i dont search why, the global dont return the $database object, for that you need to get it like this:

$database = &JFactory::getDBO();
$this->database= &$database;
$database    = &$database;

of course, you do this inside of a function of your class :D, after this you can start to execute your queries:

$database->setQuery(”SELECT * FROM `#__your_table`”);

and load the data like this:

$dataDatabase = $database->loadObjectList();

foreach($dataDatabase as $value){

echo $value->columnname;

}

as you can see, in my sql query i used this “#_” before the table name, this tipe of prefix going to be replaced for the true table prefix by the setQuery function :) .

now, if you wan to insert, delete or update a row, what i need to do? you going to do something like this:

$database->setQuery(”DELETE FROM `#__table_name` WHERE id=’1′”);

$database->Query();

3.User information

you has been reached the moment, where you need to know some things about the user what it’s visiting the site right?, well, like in the database case, you going to need to get the user object to request this data:

$user =& JFactory::getUser();

and that object contain this variables:

  • id - The unique, numerical user id. Use this when referencing the user record in other database tables.
  • name - The name of the user. (e.g. Vint Cerf)
  • username - The login/screen name of the user. (e.g. shmuffin1979)
  • email - The email address of the user. (e.g. crashoverride@hackers.com)
  • password - The encrypted version of the user’s password
  • password_clear - Set to the user’s password only when it is being changed. Otherwise, remains blank.
  • usertype - The role of the user within Joomla!. (Super Administrator, Editor, etc…)
  • gid - Set to the user’s group id, which corresponds to the usertype.
  • block - Set to ‘1′ when the user is set to ‘blocked’ in Joomla!.
  • registerDate - Set to the date when the user was first registered.
  • lastvisitDate - Set to the date the user last visited the site.
  • guest - If the user is not logged in, this variable will be set to ‘1′. The other variables will be unset or default values.

Conclusion

after use it and create a component (and other several things) for joomla, i think it’s powerful tool to make little and medium project, i think it’s enough flexible to expand it for your needs. and you can get a very good result with the practice, in the beginning it going to be hard, but when you passed, 1-2 days coding, it’s like you are coding any other script :D

like i promise, here it’s the example component (of course, i dont make this, and the source of this data come from the joomla wiki :)  ), also, take the time to read all the comments in the .xml file, it’s very useful :D

i hope my mini-guide would be useful for you, and happy coding :D

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]