<?php
/*
Widgets.php, provides the base set for widgets
Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For any questions, comments or whatever, you may mail me at: hide@address.com
*/
/**
* @brief Interface for widgets.
*
* A widget is an object which is used to show information, in whatever sense,
* to the user.
*/
abstract class Widget
{
/**
* Constructor. Does the basic initialization every widget needs.
* All subclasses should call this constructor from their constructor.
*
* @param parent Parent container this widget should be added to.
* @param id The ID of this widget.
*/
public function __construct(Container $parent, $id = '')
{
$this->reparent($parent);
$this->setID($id);
$this->javaScript = array();
$this->cssClass = '';
$this->cssStyle = '';
}
/**
* Give the widget a new parent.
*
* @note You shouldn't have to use this function yourself, but if you
* do, use with care: The original parent will _not_ loose this widget
* as a child.
*
* @param parent The new parent for this widget.
*/
public function reparent(Container $parent)
{
$this->parent = $parent;
$parent->addWidget($this);
$this->form = $parent->form();
}
/**
* Returns the parent widget.
*
* @return The Container widget which was set as parent for this widget.
*
* @since 1.1
*/
public function parent()
{
return $this->parent;
}
/**
* Sets the widget's ID.
*
* @param id The new ID for the widget.
*/
public function setID($id)
{
$this->id = $id;
}
/**
* Returns the widget's ID.
*
* @return The ID of this widget.
*/
public function id()
{
return $this->id;
}
/**
* Returns the parent form.
*
* @return The nearest (grand)parent which is a form. Can be @p null if
* the widget has no form as (grand)parent.
*/
public function form()
{
return $this->form;
}
/**
* Adds JavaScript code to the widget.
*
* @param event The event on which to execute the JavaScript, like
* 'onload'.
* @param javaScript The JavaScript code to add to the widget.
*
*/
public function addJavaScript($event, $javaScript)
{
if(isset($this->javaScript[$event]))
{
$this->javaScript[$event] .= "; $javaScript";
}
else
{
$this->javaScript[$event] = $javaScript;
}
}
/**
* Sets the CSS class the widget should use.
*
* @param cssClass The CSS class name.
*
* @note Not all widgets may actually use or support the class you set.
*/
public function setCssClass($cssClass)
{
$this->cssClass = $cssClass;
}
/**
* Sets a custom CSS style on the widget. This function discards any
* previously set style properties of the widget.
*
* @param cssStyle Custom CSS properties to assign to this widget.
*
* @note Not all widgets may actually use or support the style you set.
*/
public function setCssStyle($cssStyle)
{
$this->cssStyle = $cssStyle;
}
/**
* Adds custom CSS style properties to the widget. This function preserves any
* previously set style properties.
*
* @param cssStyle Custom CSS properties to add to this widget. Already
* set properties are preserved.
*
* @note Not all widgets may actually use or support the style you set.
*/
public function addCssStyle($cssStyle)
{
if($this->cssStyle != '')
{
$this->cssStyle .= '; ';
}
$this->cssStyle .= $cssStyle;
}
/**
* Show the widget.
*/
abstract public function show();
/**
* Parent in the widget tree.
*/
protected $parent;
/**
* Widget's ID.
*/
protected $id;
/**
* Parent form.
*/
protected $form;
/**
* JavaScript code assigned to this widget.
*/
protected $javaScript;
/**
* A CSS class assigned to this widget.
*/
protected $cssClass;
/**
* Custom CSS properties assigned to this widget.
*/
protected $cssStyle;
/**
* Returns a list of properties which a subclass inherits from the base
* class, like CSS properties, JavaScript properties, ID, etc...
*/
protected function parentProperties()
{
if($this->id != '')
{
$parentProperties = " id=\"{$this->id}\"";
}
else
{
$parentProperties = '';
}
if($this->cssClass != '')
{
$parentProperties .= " class=\"{$this->cssClass}\"";
}
if($this->cssStyle != '')
{
$parentProperties .= " style=\"{$this->cssStyle}\"";
}
foreach($this->javaScript as $event => $javaScript)
{
$parentProperties .= " $event=\"$javaScript\"";
}
return $parentProperties;
}
}
/**
* @brief A widget that can contain other widgets.
*/
class Container extends Widget
{
/**
* Constructor.
*
* @param parent The parent widget this container should be added to.
* @param id The ID of this container.
*/
function __construct(Container $parent, $id = '')
{
parent::__construct($parent, $id);
$this->children = array();
}
/**
* Adds a widget to the children list.
*
* @note You shouldn't have to call this function directly as the
* constructor of child widgets will already call this function for you.
*
* @param widget The widget to add to the children list.
*/
public function addWidget(Widget $widget)
{
$this->children[] = $widget;
}
/**
* Shows this widget and all child widgets.
*/
public function show()
{
foreach($this->children as $child)
{
$child->show();
}
}
/**
* Reparents all children to another container. Be careful if you try
* reparent your children to their children, this can result in
* infinite loops.
*
* @param container The container to reparent the children to.
*/
protected function reparentChildrenTo(Container $container)
{
$flag = false;
foreach($this->children as $child)
{
if($child === $container)
{
$flag = true;
}
else
{
$child->reparent($container);
}
}
$this->children = array();
if($flag == true)
{
$this->children[] = $container;
}
}
/**
* Registered children of the class.
*/
protected $children;
}
/**
* @brief A container with no parent.
*
* This widget is generally used to create a top-level widget.
*/
class RootContainer extends Container
{
/**
* Constructor.
*
* Creates a new parent-less container.
*
* @param id The ID of this container.
*/
public function __construct($id = '')
{
$this->children = array();
$this->cssClass = '';
$this->cssStyle = '';
$this->form = null;
$this->id = $id;
$this->javaScript = array();
$this->parent = null;
}
protected $children;
protected $cssClass;
protected $cssStyle;
protected $id;
protected $form;
protected $javaScript;
protected $parent;
}
/**
* @brief Raw widget.
*
* This is the simplest form of a widget. It just inserts the raw text when the
* widget is shown.
*
* You can use this widget to insert raw Aukyla XML data. Only use this widget
* if you really know what you are doing though.
*/
class RawWidget extends Widget
{
/**
* Constructor.
*
* @param parent The parent widget this widget should be added to.
* @param text The raw text data that should be displayed.
*/
public function __construct(Container $parent, $text = '')
{
parent::__construct($parent);
self::setText($text);
}
/**
* Sets the text on this widget.
*
* @param text The new text to be shown on the widget.
*/
public function setText($text)
{
$this->text = $text;
}
/**
* Displays the set text.
*/
public function show()
{
Output::write($this->text);
}
/**
* The text shown in this widget.
*/
protected $text;
}
/**
* @brief Raw container widget.
*
* This is a combination of RawWidget and Container.
*
* You can use this widget to insert raw Aukyla XML data which contains other
* widgets. Only use this widget if you really know what you are doing though.
*/
class RawContainer extends Container
{
/**
* Constructor.
*
* @param parent The parent widget this widget should be added to.
* @param text The raw text data that should be displayed.
*/
public function __construct(Container $parent, $text = '')
{
parent::__construct($parent);
self::setText($text);
}
/**
* Sets the text on this widget.
*
* @param text The new text to be shown on the widget.
*/
public function setText($text)
{
$this->text = $text;
}
/**
* Displays the set text.
*/
public function show()
{
Output::write($this->text);
parent::show();
}
/**
* The text shown in this widget.
*/
protected $text;
}
/**
* @brief Simple text label.
*
* This widget can be used to insert a simple text label. A label can contain
* any other widgets though.
*/
class Label extends Container
{
/**
* Constructor.
*
* @param parent The parent widget this text should be added to.
* @param text The text that should be displayed.
*/
public function __construct(Container $parent, $text = '')
{
parent::__construct($parent);
self::setText($text);
}
/**
* Sets the text on this widget.
*
* @param text The new text to be shown on the widget.
*/
public function setText($text)
{
$this->text = $text;
}
/**
* Displays the set text.
*/
public function show()
{
Output::write("<label".$this->parentProperties().">{$this->text}");
parent::show();
Output::write("</label>");
}
/**
* The text shown in this widget.
*/
protected $text;
}
?>