<?php
/**
* ÐлаÑÑ Toolbar
*
* @package energine
* @author dr.Pavka
* @copyright ColoCall 2006
* @version $Id: Toolbar.class.php,v 1.7 2007/12/17 14:16:14 pavka Exp $
*/
//require_once('core/framework/DBWorker.class.php');
//require_once('core/modules/share/components/Container.class.php');
//require_once('core/modules/share/components/Link.class.php');
//require_once('core/modules/share/components/Button.class.php');
//require_once('core/modules/share/components/Submit.class.php');
//require_once('core/modules/share/components/Separator.class.php');
//require_once('core/modules/share/components/Switcher.class.php');
//require_once('core/modules/share/components/Select.class.php');
/**
* ÐÐ°Ð½ÐµÐ»Ñ Ð¸Ð½ÑÑÑÑменÑов
*
* @package energine
*/
class Toolbar extends Object {
/**
* ÐÐ¼Ñ Ñeга
*/
const TAG_NAME = 'toolbar';
/**
* ÐокÑменÑ
*
* @var DOMDocument
* @access private
*/
private $doc;
/**
* ÐÐ°Ð±Ð¾Ñ ÑлеменÑов ÑпÑавлениÑ
*
* @access private
* @var array
*/
private $controls = array();
/**
* ÐÐ¼Ñ Ð¿Ð°Ð½ÐµÐ»Ð¸ инÑÑÑÑменÑов
*
* @access private
* @var string
*/
private $name;
/**
* ÐÑÑÑ Ðº диÑекÑоÑии ÑодеÑжаÑей ÑиÑÑнки
*
* @access private
* @var string
*/
private $imageDir;
/**
* ÐополниÑелÑнÑе ÑвойÑÑва панели инÑÑÑÑменÑов
*
* @var array
* @access private
*/
private $properties = array();
/**
* ÐÑиÑоединÑÐµÑ Ð¿Ð°Ð½ÐµÐ»Ñ ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ðº компоненÑÑ
*
* @var Component
* @access private
*/
private $component;
/**
* ÐонÑÑÑÑкÑоÑ
*
* @param string $name Ð¸Ð¼Ñ ÑÑлбаÑа
* @param string $imageDir пÑÑÑ Ðº диÑекÑоÑии ÑодеÑжаÑей ÑиÑÑнки
* @param string $module
* @access public
*/
public function __construct($name, $imageDir = false) {
parent::__construct();
$this->name = $name;
$this->doc = new DOMDocument('1.0', 'UTF-8');
$this->imageDir = $imageDir;
}
/**
* ÐÑивÑзÑÐ²Ð°ÐµÑ Ð¿Ð°Ð½ÐµÐ»Ñ ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ðº компоненÑÑ
*
* @param Component
* @return void
* @access public
*/
public function attachToComponent(Component $component) {
$this->component = $component;
}
/**
* ÐозвÑаÑÐ°ÐµÑ ÐºÐ¾Ð¼Ð¿Ð¾Ð½ÐµÐ½Ñ Ðº коÑоÑÐ¾Ð¼Ñ Ð¿ÑивÑзана Ð¿Ð°Ð½ÐµÐ»Ñ ÑпÑавлениÑ
*
* @return Component
* @access public
*/
public function getComponent() {
return $this->component;
}
/**
* ÐÑиÑоединение ÑлеменÑа ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ðº панели
*
* @param Control $control
* @param Control $position еÑли не Ñказан, добавлÑем конÑÑол в ÐºÐ¾Ð½ÐµÑ ÑÑлбаÑа, еÑли Ñказан, Ñо он добавлÑеÑÑÑ Ð² Ñказанное меÑÑо
* @return void
* @access public
*/
public function attachControl(Control $control, Control $position = null) {
$control->setIndex(arrayPush($this->controls, $control));
$control->attach($this);
}
/**
* ÐÑÑоединение ÑлеменÑа ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ð¾Ñ Ð¿Ð°Ð½ÐµÐ»Ð¸
*
* @param Control $control
* @return void
* @access public
*/
public function detachControl(Control $control) {
if (!isset($this->controls[$control->getIndex()])) {
throw new SystemException('ERR_DEV_NO_CONTROL_TO_DETACH', SystemException::ERR_DEVELOPER);
}
unset($this->controls[$control->getIndex()]);
}
/**
* ÐолÑÑение ÑлеменÑа ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ð¿Ð¾ его иденÑиÑикаÑоÑÑ
*
* @param int $id
* @return Control
* @access public
*/
public function getControlByID($id) {
$result = false;
foreach ($this->controls as $control) {
if (method_exists($control, 'getID') && $control->getID() == $id) {
$result = $control;
break;
}
}
return $result;
}
/**
* ÐоÑÑÑоение панели инÑÑÑÑменÑов по XML-опиÑаниÑ
*
* @param SimpleXMLElement $toolbarDescription
* @return boolean
* @access public
*/
public function loadXML(SimpleXMLElement $toolbarDescription) {
if(!empty($toolbarDescription))
foreach ($toolbarDescription->control as $controlDescription) {
if (!isset($controlDescription['type'])) {
throw new SystemException('ERR_DEV_NO_CONTROL_TYPE', SystemException::ERR_DEVELOPER);
}
$controlClassName = ucfirst((string)$controlDescription['type']);
if ($controlClassName == 'Togglebutton') $controlClassName = 'Switcher'; // dirty hack
if (!class_exists($controlClassName, false)) {
//throw new SystemException('ERR_DEV_NO_CONTROL_CLASS', SystemException::ERR_DEVELOPER, $controlClassName);
}
$control = new $controlClassName(
isset($controlDescription['id']) ? (string)$controlDescription['id'] : null
);
$this->attachControl($control);
$control->loadFromXml($controlDescription);
}
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð½Ð°Ð±Ð¾Ñ ÑлеменÑов ÑпÑавлениÑ
*
* @return array
* @access public
*/
public function getControls() {
return $this->controls;
}
/**
* Enter description here...
*
* @param string $name
* @param mixed $value
*/
public function setProperty($name, $value) {
$this->properties[$name] = $value;
}
/**
* Enter description here...
*
* @param string $name
* @return mixed
*/
public function getProperty($name) {
if (isset($this->properties[$name])) {
return $this->properties[$name];
}
}
/**
* ÐоÑÑÑоение панели инÑÑÑÑменÑов
*
* @return DOMNode
* @access public
*/
public function build() {
$result = false;
if (count($this->controls) > 0) {
$toolbarElem = $this->doc->createElement(self::TAG_NAME);
$toolbarElem->setAttribute('name', $this->name);
if (!empty($this->properties)) {
$props = $this->doc->createElement('properties');
foreach ($this->properties as $propName => $propValue) {
$prop = $this->doc->createElement('property');
$prop->setAttribute('name', $propName);
$prop->appendChild($this->doc->createTextNode($propValue));
$props->appendChild($prop);
}
$toolbarElem->appendChild($props);
}
foreach ($this->controls as $control) {
$toolbarElem->appendChild($this->doc->importNode($control->build(), true));
}
$this->doc->appendChild($toolbarElem);
$result = $this->doc->documentElement;
}
return $result;
}
/**
* ÐеÑÐµÐ²Ð¾Ð´Ð¸Ñ Ð²Ñе ÑлеменÑÑ ÑпÑавлениÑ
*
* @return void
* @access public
*/
public function translate() {
foreach ($this->controls as $control) {
$control->translate();
}
}
}