
How to build your own XOOPS module XD-0000 How to build your own XOOPS module A collection of tutorials written originally by theCat and translated to english by hsalazar © 2004, The XOOPS Project Document released under the Creative Commons License. Table of contents How to build your own XOOPS module ......................................................................................1 Getting ready........................................................................................................................... 2 The file index.php ................................................................................................................... 4 The queries .............................................................................................................................. 6 The templates..........................................................................................................................8 The blocks.............................................................................................................................. 12 The forms ...............................................................................................................................15 Language files........................................................................................................................ 18 Administration ...................................................................................................................... 21 The search function............................................................................................................... 25 Comments .............................................................................................................................28 Documentation Team Page 1 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 Getting ready Create the necessary directories Create the directory that’ll hold the module’s name. This directory will contain, at least: • The module’s information file (xoops_version.php) • The module’s logo, unless it’s in an images subfolder Unless you’re creating a module without a menu (like, for instance, a block), this directory will contain the file index.php, and this file will be invoked with a link in the Main Menu. The subdirectories They’re all optional, except /language, and act to serve the needs of the module. admin: used if you create a module that needs to be managed blocks: used if you create blocks; you hold here the files that manage those blocks, and you need to associate these files with template/blocks cache: for the files created/updated by your module (that need to be CHMODed) class: if you create any classes images: for your images include: for whatever files including functions you might have language: it’s mandatory to have at least the subdirectory /english sql: if your module uses a database, you put here the script to install/uninstall the module templates: if your module or your blocks use templates In those directories that you’ll use and that don’t contain an index.php file (such as images, class, sql...) you need to include a file index.html containing this single instruction: Documentation Team Page 2 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 <script>history.go(-1);</script> Create your file xoops_version.php // Name of the module $modversion['name'] = _MI_MYMODULE_NAME; // Version of the module $modversion['version'] = 1.0; // Brief description of the module $modversion['description'] = _MI_MYMODULE_DESC; // Author $modversion['author'] = "Author"; // Credits $modversion['credits'] = "Credits"; // If you have a help file $modversion['help'] = " mymodule.html"; // License $modversion['license'] = "GPL see LICENSE"; // If it’s an official module $modversion['official'] = 1; // Path and name of the module’s logo $modversion['image'] = "images/mymodule_slogo.png"; // Name of the directory that holds the module $modversion['dirname'] = " mymodule "; Create your logo • It’s advisable to use the name mymodule_slogo • Use preferently the PNG format (if it’s not possible, use GIF or JPG) Create your language files • modinfo.php Documentation Team Page 3 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 • main.php Create your file mysql.sql This is the script that contains the tables for your module, usually just the structure. Please notice that the tables’ names should NOT include the prefix XOOPS. A module to build upon In this same document you’ll find an empty module to download. It contains the directories and some of the base files, so you can begin easily the creation of your own module; it also contains a blank module logo for you to fill. And if this module has not much of a use, at least you can install it and uninstall it. Cool, huh? The file index.php Unless we’re talking about a particular case, a link in the Main Menu will call this index file. It’s generally better to begin with this file. To begin with, don’t worry too much about the issues of displaying content via templates; instead concentrate on the script’s correct working, and display things using echo statements. <?php // Don’t forget the license // ----------------------------------------------------------------------- // // XOOPS - PHP Content Management System // // <http://www.xoops.org/> // // // etc. // Mandatory inclusion of the header include("header.php"); // next, if necessary // include("include/functions.php"); // $xoopsOption['template_main'] = 'mytemplate_index.html'; Documentation Team Page 4 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 include(XOOPS_ROOT_PATH."/header.php"); // here goes your code // ...... // Don’t forget the footer with this mandatory link. include_once XOOPS_ROOT_PATH.'/footer.php'; ?> Reviewing some rules Write your variables using lowercase, separating words with the underscore (this is a XOOPS naming convention). The use of uppercase characters will be reserved: • for SQL instructions: SELECT, WHERE, ... • for some PHP variables: $_GET, $_POST • for XOOPS variables: XOOPS_ROOT_PATH, XOOPS_URL • for language definitions: _NW_NOSTORY Write your PHP tags complete: <?php // but never <? Some useful instructions // How to include a link in the text $link = '<a href="'.XOOPS_URL.'/modules/mymodule/index.php.''>text of the link </a>'; // How to include a link in an image $link = '<a href="'.XOOPS_URL.'/modules/mymodule/index.php.''>'; $link .= '<img src="'.XOOPS_URL.'/images/image.gif" /> '; $link.= '</a>'; // How to include a functions file include_once XOOPS_ROOT_PATH.'/modules/ mymodule /include/functions.php'; Documentation Team Page 5 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 The queries Always use XOOPS’ database functions: $xoopsDB->prefix(), $xoopsDB->queryF(), etc. • So you benefit from the SQL debug mode. • For future compatibility of XOOPS with other databases. If possible, write your query in a character string ($sql): $sql = 'SELECT * FROM '.$xoopsDB->prefix('mymodule_table1').' WHERE champ1 = '.$variable.' AND champ2 = ‘.$variable2’; Then execute the query: $result = $xoopsDB->queryF($sql) Include always a test to verify its execution: if ( $result == false ) { die( ‘SQL error: ’.$sql .’ ’); } This suggestion presents several advantages in case your query doesn’t execute: • It’ll indicate in which line of code there’s a problem • Your query will be displayed and will let you see if an element is missing (for example, WHERE field1= AND…) • You’ll be able to cut and paste the query to execute it using phpMyAdmin so you can see the result. • A future user will be able to make a precise ‘bug report’ $xoopsDB->queryF() To execute your queries, use $xoopsDB->queryF($sql, $limit, $start), which returns: Documentation Team Page 6 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 • The result of the query in case it is executed, and at least one result • True if it gets executed but with no results • False if there’s an execution error The parameters $limit, $start are optional. Don’t use $xoopsDB->query(), which will likely give your problems when updating or deleting a register. Query SELECT There are several methods to use the results. Some of the more commonly used are: • fetchRow, with list() o when the query returns only one line (for instance, when SELECT COUNT(*) , or a query with a clause WHERE based on the id, ...) list($count) = $xoopsDB->fetchRow($result); // or else list($id, $name, $phone) = $xoopsDB->fetchRow($result); The variables $count, or $id, $name, $phone will contain the result of your query. o When the results can be fed directly into a loop while (list($id, $name) = $xoopsDB->fetchRow($result)) { echo 'Le nom est '.$name.' et le numero id '.$id.' '; } • fetchArray() : o when the query returns a single line $myrow = $xoopsDB->fetchArray($result)) { $variable1 = $myrow['nom_champ1']; Documentation Team Page 7 of 31 Last change: 3/28/2004 12:55 PM How to build your own XOOPS module XD-0000 $variable2 = $myrow['nom_champ2']; } o when the query returns an array $i = 0; while ( $myrow = $xoopsDB->fetchArray($result) ) { $var_array[$i]['elt1'] = $myrow['nom_champ1']; $var_array[$i]['elt2'] = $myrow['nom_champ2']; $i++; } Or else while ($myrow = $xoopsDB->fetchArray($result)) { $var_array['element'][] = array('elt' => $myrow['nom_champ1'], 'elt2' => ($myrow['nom_champ2'])); } Queries in a function Don’t forget the declaration: global $xoopsDB function
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages31 Page
-
File Size-