(root)/shared-simplelisteditor.php - Rev 490
Rev 367 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
<?PHP
//! The simple list editor, which lets you add, delete and edit items in an array of simple text items
/**
* Sample code for the simple list editor
*
\code
$dialogue = new SimpleListEditor($this->globalID, $theModuleSection, $this->listOfItems, $theModuleSubsection );
if( $dialogue->changed === true )
{
$this->listOfItems = $dialogue->items;
$this->save();
}
return $dialogue->render();
\endcode
*
*/
class SimpleListEditor
{
var $baseGlobalID; ///< The globalID to work from
var $action; ///< The action you want to do
var $item; ///< Which item do you wish to work on
var $items; ///< The array containing the items to edit
var $changed = false; ///< Wether or not the items are changed
var $title; ///< The type of items you are adding (used as the title of the item in the Add dialogue)
var $description; ///< The tip which is shown below the title in the Add dialogue
function SimpleListEditor
( $baseGlobalID, $action, $items, $item = null )
{
$this->baseGlobalID = $baseGlobalID;
$this->action = $action;
$this->items = $items;
$this->item = $item;
$this->title = i18n
("Item");
$this->description = i18n
("The text contained in your new item. Please note that some constraints might be forced upon it (such as all lower-case and no spaces allowed), but this will be enforced automatically.");
}
function renderAdd
()
{
$dialog = new optionsPanel
("itemEditor");
$dialog->showHeader = false;
$dialog->saveTitle = i18n
("Save item");
$dialog->addOption( $this->title, $this->description, "", "item", "text" );
if( $dialog->submitted )
{
$this->items[] = $dialog->getValue("item");
$this->changed = true;
header("Location: " . globalIDtoURL
( $this->baseGlobalID ) );
}
$renderedContent = renderInformationBox
( i18n
("Add a new item"), $dialog->render(), false );
return $renderedContent;
}
function renderDelete
()
{
$dialog = new confirmDeleteDialog
( $this->items[$this->item], "", $this->baseGlobalID );
if( $dialog->submitted )
{
unset( $this->items[$this->item] );
$this->changed = true;
header("Location: " . globalIDtoURL
( $this->baseGlobalID ) );
}
return $dialog->render();
}
function renderEdit
()
{
$dialog = new optionsPanel
("itemEditor");
$dialog->showHeader = false;
$dialog->saveTitle = i18n
("Save item");
$dialog->addOption( $this->title, $this->description, $this->items[$this->item], "item", "text" );
if( $dialog->submitted )
{
$this->items[$this->item] = $dialog->getValue("item");
$this->changed = true;
header("Location: " . globalIDtoURL
( $this->baseGlobalID ) );
}
$renderedContent = renderInformationBox
( i18n
("Edit item"), $dialog->render(), false );
return $renderedContent;
}
/**
* Renders the list editor into HTML form, including a form element
*
* @return string The list editor as a HTML form - including the form element (with the post url set to currentPage())
*/
function renderForm
()
{
return "<form action=\"" . thisPageURL
() . "\" method=\"POST\">" . $this->render() . "</form>";
}
function render
()
{
switch( $this->action )
{
// action: add
case "add":
$renderedContent = $this->renderAdd();
break;
// action: delete
case "delete":
$renderedContent = $this->renderDelete();
break;
// action: edit
case "edit":
$renderedContent = $this->renderEdit();
break;
// action: none (list)
default:
$addCommand = drawCommand
( i18n
("Add"), i18n
("Add a new item to the list"), globalIDtoURL
( $this->baseGlobalID . "/add" ) );
$renderedContent = "<div class=\"wikirightalign\">$addCommand</div>";
if( is_array( $this->items ) && count( $this->items ) > 0 )
{
foreach( $this->items as $key => $value )
{
$controls = drawCommand
( i18n
("Edit"), i18n
("Edit this item"), globalIDtoURL
( $this->baseGlobalID . "/edit/" . $key ) );
$controls .= drawCommand
( i18n
("Delete"), i18n
("Delete this item"), globalIDtoURL
( $this->baseGlobalID . "/delete/" . $key ) );
$controls = "<div style=\"float: right;\" class=\"item-commands\">$controls</div>";
$renderedContent .= "$controls<div class=\"item\">$value</div>";
}
}
else
$renderedContent .= renderInformationBox
( i18n
("No items to show"), i18n
("The list contains no items. Please add one by clicking on the ##0## command link.", array( $addCommand ) ) );
break;
}
return $renderedContent;
}
}
?>