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 );
}
Now let's look at how the file works:
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
This makes sure Mambo is calling the file. Very important for security.
require_once($mosConfig_absolute_path."/administrator/components/com_hello_world/class.hello_world.php");
require_once( $mainframe->getPath( 'admin_html' ) );
Calls in the 2 files you will need. class.hello_world.php and admin.hello_world.html.php
switch ($act) {
default:
$task = "showText";
break;
}
Sets a switch so that if you have an $act (action) set, it will redefine it as a $task. You will generally use $act for the main functions such as displaying the text list in this example. $task is used for things such as saving, deleting, etc.
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;
}
The switch that controls your tasks. This switch tells which $task to run which function.
function save( $option ) {
Our first function. This one will save an item we create or edit.
global $database;
Sets $database as a global variable.
$row = new mosHello_world( $database );
This creates a new variable named $row which will store the information to insert into the database. It creates a new instance of your mosHello_world class from your class.hello_world.php file.
if (!$row->bind( $_POST )) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
exit();
}
If $row doesn't have any values, display the error and go back a window. Unless you are doing something wierd with entering urls on your own, this will never be used :).
if (!$row->store()) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
exit();
}
If it can't write to the database, report the error and go back. This will normally only occur if you are having a problem with your database.
mosRedirect( "index2.php?option=$option", "Saved" );
If all goes well, redirect to your main option window and display the message "Saved".
function edit( $option, $uid ) {
Much of this has been covered, so we will only cover the new parts.
$row->load( $uid );
After you have your $row defined and set to the mosHello_world class, load the row that contains the $uid value. $uid is set to the value of the $id of the item we want to edit.
HTML_hello_world::edit( $option, $row );
Sends the $row data to admin.hello_world.html.php to be displayed.
function delete( $option, $cid ) {
Again, I will only cover the concepts not already covered.
if (!is_array( $cid ) || count( $cid ) < 1) {
echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>n";
exit;
}
This checks to make sure that there has been at least one item selected to be deleted.
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";
}
This checks to make sure there is a value for $cid (an array that holds the $ids of the items to be deleted). If there is a value, it creates a comma delimited list of the ids and stores it in $cids. It then deletes all entries with matching ids. If there is an error deleting, it will go back a screen.
function showText($option) {
This will be the main output function. This is what will create our list to display the text entries. All lines have been explained already.