Opera - najszybsza przeglądarka!
21.11.2009 10:42:41
PL EN
News | Articles | Files | Skin ⇓

How to create extensions?

Let's start

Create a new folder in plugins directory. Its name may contain only letters and digits. You may place other characters except / but it's inadvisable.

Information file

Go to the directory and create plugin.ini file. It must have information about the extension and its name translated to different languages.

Here is an example plugin.ini file:

[main]
config=1
install=php
version=0.1
credits=COMPMaster 2009

[names]
en=Issue system
pl=System zgłoszeń

[menu]
link=1

First answer the following questions:

  • Will your extension work without installation or must it be installed?
  • Will it have a module in admin panel?
  • Do you want to add a link to menu?

Option config informs that the extension has a module in admin panel. Delete this line if it needn't be configured in admin panel.

Option install tells about installation method. Currently the only method is php. Delete this option if your extension will work without installation.

Next parameter is extension's version. In case of incomplete addons it's recommended to start from 0, e.g. 0.1. The number must be decimal.

Variable credits should contain your name or nickname. If you have your own website, place its URL in www parameter. Remember about http://.

Next group contains translations of extension's name. If there is no text for used language, folder's name will be shown on extensions list.

Installation file

If you decided your add-on needs to make changes inside database or files, create a new file - setup.php. It must have at least 2 functions:

  • Install() - installs your extension
  • Uninstall() - uninstalls it

This is an example setup.php:

<?php
function Install()
{
	global $db;
	/* add tables, rows, make configuration file, etc. */
}
function Uninstall()
{
	global $db;
	/* delete tables, rows, cleanup, etc. */
}

There is no need to cover database queries by transaction because it's done automatically by extensions module. If you want to add example rows into tables, use prepared statements in PDO.

See installation files in other extensions for F3Site 2009 or newer.

Startup file

Create default.php file. It will be included if variable co in the URL equals your extension's directory name. Then you decide, what to do next. Example:

<?php
if(iCMS!=1) exit;

#Site title
$content->title = 'Chat';

#Directory containing templates for edit
$content->dir = './plugins/chat/';

#Directory with compiled templates
$content->cache = './cache/chat/';

#Template name without extension
#If its name equals directory name, below line is not required
$content->file = 'chat';

#If configuration file exists, include it
#Else treat the extension as not installed
if(file_exists('./cfg/chat.php'))
{
	require './cfg/chat.php';
}
else
{
	$content->message('Chat is NOT installed!');
}

Configuration file's name may be different. It's important to not collide with another modules. If your extension has no settings file, omit that fragment of code. If you don't know basics of PDO class for managing database, read official documentation. PDO object is stored in $db variable.

What are templates?

Most XHTML code is situated in templates - separate files that do not contain logics of the application. It lets you edit the appearance easier.

How to assign data into templates?

$content->data = array(
	'title' => 'Room number 5.3',
	'date'  => genDate('2009-06-30'),
	'users' => array('Admin', 'Bill', 'Guest01')
);

Now create the template - e.g. chat.html - and type:

<h2>{title} - {date}</h2>
<div class="box">

<!-- START users -->
<div>{ITEM}</div>
<!-- STOP -->

You will find more information about template system in another article. Now you can test your extension.

AJAX requests

There is no problem to send AJAX requests to index.php, e.g. index.php?co=chat. Then value of JS constant is true (1). You may also use special front controller request.php. Create new file - js.php. It will be included if you sent AJAX request to request.php?co=chat.

Admin panel

Rules of making pages for admin panel is similar. But how about privileges? Learn, how to register extensions in admmenu table. Its columns:

  • ID - ID of privilege
  • text - privilege title or link in menu
  • file - module, which the link leads to, e.g. chat
  • menu - 1 or 0 - is the link to be visible?
  • rights - 1 or 0 - is this item to be shown while editing user privileges?

Modify setup.php. Add a link and a privilege:

<?php
function Install()
{
	global $db;
	$db->exec('INSERT INTO '.PRE.'admmenu (ID, text, file, menu, rights) VALUES ("CHAT", "Chat", "chat", 1, 1)');
}
function Uninstall()
{
	global $db;
	$db->exec('DELETE FROM '.PRE.'admmenu WHERE ID="CHAT"');
}

Create a new file - adm.php. Example content:

<?php
if(iCMSa!=1 || !Admit('CHAT')) exit;

#Template directories - similar as in default.php
$content->dir = './plugins/chat/';
$content->cache = './cache/chat/';

#Template file
$content->file = 'admin';

#Site title
$content->title = 'Chat options';

You decide, what to do next. Use your invention. Remember: you cannot admit ANY administrator WITHOUT privileges. You can check it with Admit function. If you make advanced extension with lots of templates, it's better to keep them in separate directory.

Add item to SETTINGS menu

You only need to add rows into confmenu table. Fields:

  • ID - path to settings page, e.g. forum&act=configMain
  • name - item title
  • lang - item language, np. pl
  • img - icon next to the link

Insert data for 1 or more language inside Install(). Use prepared statements:

$q = $db->prepare('INSERT INTO '.PRE.'confmenu (ID, name, lang, img) VALUES (?, ?, ?, ?)');
$q -> execute(array('chat', 'Chat', 'en', 'plugins/chat/cfg.png'));
$q -> execute(array('chat', 'Czat', 'pl', 'plugins/chat/cfg.png'));

Function Uninstall() must delete these rows. Just add one query:

$db->exec('DELETE FROM '.PRE.'confmenu WHERE ID="chat"');

WARNING! Delete only rows that belong to YOUR extension. Rely only on unique values, e.g. ID or directory name!

Zakończenie

You know basics of extension system in F3Site 3. It's not everything. Learn how to use common modules, e.g. comments, BBCode, e-mail, categories, private messages... Examine the code in another files. Good luck!

Mark:
Wrote: WebCM - 12.10.2009 o 13:45
Menu
F3Site CMS
Games & tools
Statistics