按: 本教程非原創,版權歸Keliix06所有.
Hello_world.xml- 組件安裝配置文件
<?xml version="1.0" ?>
<mosinstall type="component">
<name>hello_world</name>
<creationDate>04/15/2004</creationDate>
<author>Doyle Lewis</author>
<copyright>This component in released under the GNU/GPL License</copyright>
<authorEmail>
support@mambo-hosting.com
</authorEmail>
<authorUrl>www.mambo-hosting.com</authorUrl>
<version>1.0</version>
<files>
<filename>hello_world.php</filename>
</files>
<install>
<queries>
<query>DROP TABLE IF EXISTS `mos_hello_world`;</query>
<query>CREATE TABLE `mos_hello_world` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` TEXT NOT NULL,
`published` TINYINT(1) NOT NULL,
PRIMARY KEY (`id`)
)
</query>
</queries>
</install>
<uninstall>
<queries>
<query>DROP TABLE IF EXISTS `mos_hello_world`;</query>
</queries>
</uninstall>
<installfile>
<filename>install.hello_world.php</filename>
</installfile>
<uninstallfile>
<filename>uninstall.hello_world.php</filename>
</uninstallfile>
<administration>
<menu>Hello World</menu>
<submenu>
<menu act="all">Show Text</menu>
</submenu>
<files>
<filename>admin.hello_world.php</filename>
<filename>admin.hello_world.html.php</filename>
<filename>class.hello_world.php</filename>
<filename>toolbar.hello_world.php</filename>
<filename>toolbar.hello_world.html.php</filename>
</files>
</administration>
</mosinstall> 我們來看它是如何工作的:
<?xml version="1.0" ?>
XML的開頭語,為所有XML文件所必需。
<mosinstall type="component">
告訴MAMBO將開始安裝一個組件
<name>hello_world</name>
<creationDate>04/15/2004</creationDate>
<author>Doyle Lewis</author>
<copyright>This component in released under the GNU/GPL License</copyright>
<authorEmail>
support@mambo-hosting.com
</authorEmail>
<authorUrl>www.mambo-hosting.com</authorUrl>
<version>1.0</version>
組件的詳細信息,所有有關組件的信息只能在這加入。 <files>
<filename>hello_world.php</filename>
</files>
所有需要安裝在組件界面需要的文件,將被安裝到components/com_hello_world/目錄下。
<install>
<queries>
<query>DROP TABLE IF EXISTS `mos_hello_world`;</query>
<query>CREATE TABLE `mos_hello_world` (
`id` INT NOT NULL AUTO_INCREMENT,
`text` TEXT NOT NULL,
`published` TINYINT(1) NOT NULL,
PRIMARY KEY (`id`)
)
</query>
</queries>
</install>
安裝組件所需的數據庫查詢語句,本組件中將創建一個有三個字段的表。你可以運行phpMyadmin來得到一所需的查詢語句的,這是一個很簡單易行的辦法。
<uninstall>
<queries>
<query>DROP TABLE IF EXISTS `mos_hello_world`;</query>
</queries>
</uninstall>
卸載組件所需的查詢語句,這里只是簡單的刪除了數據庫表格。
<installfile>
<filename>install.hello_world.php</filename>
</installfile>
<uninstallfile>
<filename>uninstall.hello_world.php</filename>
</uninstallfile>
這幾行代碼是用來說明安裝和卸載組件所用到的文件名。
<administration>
從這行起所有的東東將安裝到管理員目錄下。
<menu>Hello World</menu>
這行將被顯示到管理面板中的“組件”的下拉菜單中。
<submenu>
<menu act="all">Show Text</menu>
</submenu>
這幾行代碼將產生一個二級菜單(相對于剛才產生的Hello World菜單選項),這也將告訴MAMBO你的組件將有些什么功能。
<files>
<filename>admin.hello_world.php</filename>
<filename>admin.hello_world.html.php</filename>
<filename>class.hello_world.php</filename>
<filename>toolbar.hello_world.php</filename>
<filename>toolbar.hello_world.html.php</filename>
</files>
所有將被安裝到administrator/components/com_hello_world/目錄下的文件。
</administration>
</mosinstall>
結束MAMBO組件安裝
hello_world.php-界面顯示文件
<?php
//hello_world Component//
/**
* Content code
* @package hello_world
* @Copyright (C) 2004 Doyle Lewis
* @ All rights reserved
* @ hello_world is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
global $database;
$query = "SELECT * FROM mos_hello_world LIMIT 1";
$database->setQuery( $query );
$rows = $database->loadObjectList();
$row = $rows[0];
echo $row->text;
?>
讓我們來看一下這個文件。
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
這個將驗證是否由MAMBO來調用此文件,防止非法直接運行本文件,對于安全性非常重要。
global $database;
將$database設置為全局變量,你可以在你的函數中使用$database。
$query = "SELECT * FROM mos_hello_world LIMIT 1";
輸出mos_hello_world表中的第一個記錄。
$database->setQuery( $query );
用database類中的數據庫查詢函數調用上述的查詢。
$rows = $database->loadObjectList();
用變量$rows 以數列方式存儲查詢結果。
$row = $rows[0];
用變量 $row 存儲$row中的第一個元素。
echo $row->text;
打印輸出text.
admin.hello_world.php -數據庫查詢和設置HTML輸出
<?php
//hello_world Component//
/**
* Content code
* @package hello_world
* @Copyright (C) 2004 Doyle Lewis
* @ All rights reserved
* @ hello_world is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
require_once($mosConfig_absolute_path."/administrator/components/com_hello_world/class.hello_world.php");
require_once( $mainframe->getPath( 'admin_html' ) );
switch ($act) {
default:
$task = "showText";
break;
}
switch ($task) {
case "save":
save( $option );
break;
case "delete":
delete( $option, $id );
break;
case "new":
$id = '';
edit( $option, $id );
break;
case "edit":
save( $option, $id[0] );
break;
case "showText":
showText( $option );
break;
}
function save( $option ) {
global $database;
$row = new mosHello_world( $database );
if (!$row->bind( $_POST )) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
exit();
}
if (!$row->store()) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
exit();
}
mosRedirect( "index2.php?option=$option", "Saved" );
}
function edit( $option, $uid ) {
global $database;
$row = new mosHello_world( $database );
$row->load( $uid );
HTML_hello_world::edit( $option, $row );
}
function delete( $option, $cid ) {
global $database;
if (!is_array( $cid ) || count( $cid ) < 1) {
echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>n";
exit;
}
if (count( $cid )) {
$cids = implode( ',', $cid );
$database->setQuery( "DELETE FROM mos_hello_world WHERE id IN ($cids)" );
if (!$database->query()) {
echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>n";
}
}
mosRedirect( "index2.php?option=$option" );
}
function showText($option) {
global $database;
# Do the main database query
$database->setQuery( "SELECT * FROM mos_hello_world ORDER BY id" );
$rows = $database->loadObjectList();
if ($database->getErrorNum()) {
echo $database->stderr();
return false;
}
HTML_hello_world::showText( $option, $rows );
}
讓我們來看看這個文件
:
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
這個將驗證是否由MAMBO來調用此文件,防止非法直接運行本文件,對于安全性非常重要。
require_once($mosConfig_absolute_path."/administrator/components/com_hello_world/class.hello_world.php");
require_once( $mainframe->getPath( 'admin_html' ) );
調用以下兩文件 class.hello_world.php and admin.hello_world.html.php
switch ($act) {
default:
$task = "showText";
break;
}
建立一個選擇開關語句這樣假如有一個變量$act 我們將重定義一個新變量$task. 這樣$act 將用來實現顯示文字的函數中,而$task將被用在保存,刪除等。.
switch ($task) {
case "save":
save( $option );
break;
case "delete":
delete( $option, $id );
break;
case "new":
$id = '';
edit( $option, $id );
break;
case "edit":
save( $option, $id[0] );
break;
case "showText":
showText( $option );
break;
}
這個開關語句將根據$task來運行所需的函數。
function save( $option ) {
我們的第一個函數,將保存我們創建或正在編輯的內容。
global $database;
聲明$database為全局變量。
$row = new mosHello_world( $database );
這將定義$row新變量來存儲插入到數據庫中的信息,$row是class class.hello_world.php文件定義的mosHello_world類的一個實例。
if (!$row->bind( $_POST )) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
exit();
}
假如 $row 返回空值,顯示錯誤信息并返回上一個窗口。
if (!$row->store()) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
exit();
}
假如不能完成對數據庫的寫操作,將顯示錯誤信息并返回上一個窗口,這僅僅會在數據庫出現異常時會出現。
mosRedirect( "index2.php?option=$option", "Saved" );
假如一切正常將重定向你的瀏覽器到主選項窗口,并顯示"Saved".
function edit( $option, $uid ) {
很多代碼可參照前面的解釋,這里不再贅述。
$row->load( $uid );
定義了$row后并聲明它為mosHello_world類,裝載$uid相關的數據. $uid是
我們想編輯的內容的$id的值。
HTML_hello_world::edit( $option, $row );
將$row 傳遞到admin.hello_world.html.php 顯示。
.
function delete( $option, $cid ) {
同前,這里不再贅述。
if (!is_array( $cid ) || count( $cid ) < 1) {
echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>n";
exit;
}
這將檢驗是否有刪除對象,如為空將顯示提示信息并返回上一個窗口。
if (count( $cid )) {
$cids = implode( ',', $cid );
$database->setQuery( "DELETE FROM mos_hello_world WHERE id IN ($cids)" );
if (!$database->query()) {
echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>n";
}
這將檢驗$cid (存儲所有欲刪除的對象的$id值的數列)是否為空,如果不為空它將創建一個以逗號為分隔符的所有id的列表并存儲在字符串$cids然后根據相應的id進行刪除操作,如果操作出錯將返回上一個窗口。
function showText($option) {
這是我們的主輸出函數,將創建我們要輸出的文字的列表,解釋參照前面所述。
admin.hello_world.html.php –控制所有的輸出.
<?php
//hello_world Component//
/**
* Content code
* @package hello_world
* @Copyright (C) 2004 Doyle Lewis
* @ All rights reserved
* @ hello_world is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/
// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
require_once($mosConfig_absolute_path."/administrator/components/com_hello_world/class.hello_world.php");
class HTML_hello_world {
function edit( $option, &$row ) {
?>
<script language="javascript" type="text/javascript">
function submitbutton(pressbutton) {
var form = document.adminForm;
if (pressbutton == "cancel") {
submitform( pressbutton );
return;
}
submitform( pressbutton );
}
</script>
<form action="index2.php" method="post" name="adminForm" id="adminForm" class="adminForm">
<table border="0" cellpadding="3" cellspacing="0">
<tr>
<td>Text Output: </td>
<td><input type="text" size="50" maxsize="100" name="text" value="<?php echo $row->text; ?>" /></td>
</tr>
</table>
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="task" value="" />
</form>
<?php } ?>
function showText( $option, &$rows ) {
?>
<script language="javascript" type="text/javascript">
function submitbutton(pressbutton) {
var form = document.adminForm;
if (pressbutton == "cancel") {
submitform( pressbutton );
return;
}
submitform( pressbutton );
}
</script>
<form action="index2.php" method="post" name="adminForm">
<table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">
<tr>
<th width="20"><input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);"
/></th>
<th class="title" width="25%">Text Output</th>
<th width="25%">Published</th>
</tr>
<?php
$k = 0;
for($i=0; $i < count( $rows ); $i++) {
$row = $rows[$i];
?>
<tr class="<?php echo "row$k"; ?>">
<td><input type="checkbox" id="cb<?php echo $i;?>" name="id[]" value="<?php echo $row->id; ?>"
onclick="isChecked(this.checked);" /></td>
<td><a onclick="return listItemTask('cb<?php echo $i;?>','edit')"><?php echo $row->text; ?></a></td>
<td align="center">
<?php
if ($row->published == "1") {
echo "<img src="images/tick.png" border="0" />";
} else {
echo "<img src="images/publish_x.png" border="0" />";
}
?>
</td>
<?php $k = 1 - $k; ?>
</tr>
<?php } ?>
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
</form>
<?php }
} ?>
我們來看一下這個文件
class HTML_hello_world {
聲明一個新類:HTML_hello_world
function edit( $option, &$row ) {
聲明edit函數, 將顯示用來創建新內容并編輯已有的內容的表單。
<script language="javascript" type="text/javascript">
function submitbutton(pressbutton) {
var form = document.adminForm;
if (pressbutton == "cancel") {
submitform( pressbutton );
return;
}
submitform( pressbutton );
}
</script>
這將檢驗你是否按下了任何工具欄的按鈕,你可以加入任何表單驗證代碼在這里。
<form action="index2.php" method="post" name="adminForm" id="adminForm" class="adminForm">
你可以將這段代碼拷貝到任何組件中,它必須包括這些標簽。
<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
<input type="hidden" name="option" value="<?php echo $option; ?>" />
<input type="hidden" name="task" value="" />
保存或取消操作所必須的option和task字段。
function showText( $option, &$rows ) {
將顯示所有的文字部分。.
<th width="20"><input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);"
/></th>
這將快速全選所有的選擇框。
<?php
$k = 0;
for($i=0; $i < count( $rows ); $i++) {
$row = $rows[$i];
$k用來產生行的替換圖效果,for 語句將保證遍列所有的結果。
<td><input type="checkbox" id="cb<?php echo $i;?>" name="id[]" value="<?php echo $row->id; ?>"
onclick="isChecked(this.checked);" /></td>
這將選上本行的選擇框。
<td><a onclick="return listItemTask('cb<?php echo $i;?>','edit)"><?php echo $row->text; ?></a></td>
將產生編輯此項的鏈接。
<?php
if ($row->published == "1") {
echo "<img src="images/tick.png" border="0" />";
} else {
echo "<img src="images/publish_x.png" border="0" />";
}
?>
假如此行被設置為發行,你將看到一個綠的叉,否則將看到一個小紅叉。
<?php $k = 1 - $k; ?>
將 $k設為 1和它自身的差值,假如$k是 0,它就等于1,假如它等于1它就被設為0.
<input type="hidden" name="boxchecked" value="0" />
這行代碼很重要不然所有的選擇框將不能正常工作。
class.hello_world.php -數據庫類文件.
<?php
//hello_world Component//
/**
* Content code
* @package hello_world
* @Copyright (C) 2004 Doyle Lewis
* @ All rights reserved
* @ hello_world is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
class mosHello_world extends mosDBTable {
// INT(11) AUTO_INCREMENT
var $id=null;
// TEXT
var $text=null;
// TINYINT(1)
var $published=null;
function mosHello_world( &$db ) {
$this->mosDBTable( 'mos_hello_world', 'id', $db );
}
}
我們來看一下這個文件
class mosHello_world extends mosDBTable {
聲明類mosHello_world為mosDBTable的派生類,你可以將mosHello_world改成你自己的類名稱。
// INT(11) AUTO_INCREMENT
var $id=null;
注釋行只是給出了變量的一些信息,變量名必須和你數據庫的相應字段相符合并將它們設為空值。
function mosHello_world( &$db ) {
$this->mosDBTable( 'mos_hello_world', 'id', $db );
}
調用父類的構造函數,你可以調用$row=new mosHello_world($database)來得到想要的結果。
install.hello_world.php – 安裝文件.
<?php
function com_install() {
echo "Thank you for using this component. Please contact me at
support@mambo-hosting.com
with any questions";
}
?>
在文件中調用函數com_install(),不然會導致異常。 uninstall.hello_world.php – 卸載文件.
<?
function com_uninstall() {
echo "Thank you for using this component. Please contact me at
support@mambo-hosting.com
with any questions";
}
?>
在文件中調用函數com_uninstall(),不然會導致異常。 toolbar.hello_world.php -設置工具欄
<?php
//hello_world Component//
/**
* Content code
* @package hello_world
* @Copyright (C) 2004 Doyle Lewis
* @ All rights reserved
* @ hello_world is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
require_once( $mainframe->getPath( 'toolbar_html' ) );
if($task) {
switch($task) {
case 'new':
case 'edit':
$act = "edit";
break;
}
}
if ($act) {
switch ( $act ) {
case 'edit':
menuHello_world::EDIT_MENU();
break;
case 'text':
menuHello_world::TEXT_MENU();
break;
}
}
?>
我們來看一下這個文件
require_once( $mainframe->getPath( 'toolbar_html' ) );
和在admin.hello_world.php中包括admin.hello_world.html.php一樣
if($task) {
switch($task) {
case 'new':
case 'edit':
$act = "edit";
break;
}
}
"new" 和"edit" 將應用相同的工具欄。
case 'edit':
menuHello_world::EDIT_MENU();
break;
告訴toolbar.hello_world.html.php 該調用哪個函數。
toolbar.hello_world.html.php -控制工具欄的輸出
<?php
//hello_world Component//
/**
* Content code
* @package hello_world
* @Copyright (C) 2004 Doyle Lewis
* @ All rights reserved
* @ hello_world is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
class menuHello_world {
function TEXT_MENU() {
mosMenuBar::startTable();
mosMenuBar::publish('publish');
mosMenuBar::unpublish('unpublish');
mosMenuBar::divider();
mosMenuBar::addNew('new');
mosMenuBar::editList('edit', 'Edit');
mosMenuBar::deleteList( ' ', 'delete', 'Remove' );
mosMenuBar::endTable();
}
function EDIT_MENU() {
mosMenuBar::startTable();
mosMenuBar::back();
mosMenuBar::save('save');
mosMenuBar::spacer();
mosMenuBar::endTable();
}
}
?>
class menuHello_world {
Sets the toolbar class
function TEXT_MENU() {
mosMenuBar::startTable();
mosMenuBar::publish('publish');
mosMenuBar::unpublish('unpublish');
mosMenuBar::divider();
mosMenuBar::addNew('new');
mosMenuBar::editList('edit', 'Edit');
mosMenuBar::deleteList( ' ', 'delete', 'Remove' );
mosMenuBar::endTable();
}
告訴mosMenuBar 在工具欄中輸出什么內容,括號中小寫的內容將告訴admin.hello_world.php
執行哪個任務。
希望諸位看官眼睛還沒有花:-) 這只是對組件的一個很簡單的介紹,但是如果你打印本教程的話還是有16頁之長:-)* 希望本教程能幫助你設計出更強大的MAMBO組件(沒準會收入MAMBO5.0版中哦)
*這里其實有17頁,因為我重新排了一下版。
原文代碼在我的機器中安裝有許多問題, 大家可下載我修改后的代碼對比源碼, 僅供新手學習使用(共同學習.... )