<?php
/**
* Copyright (C) 2010 Kai Dorschner
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* @author Kai Dorschner <the-hide@address.com>
* @copyright Copyright 2010, Kai Dorschner
* @license http://www.gnu.org/licenses/gpl.html GPLv3
* @package mocovi
*/
/**
* Require Singleton.
*/
class_exists('Singleton') or require $GLOBALS['library'].'autoload/Singleton.php';
/**
* Represents the layout and structure of one page.
*
* Each element is affected by the model. It serves the structure and all
* necessary texts and elements without defining how the result has to be
* shown.
*
* The model is strict XML.
*
* <b>Time Controlling:</b>
* You can modify each element by setting a
* "<b>{@link control::$options showDateFrom}</b>" and/or
* "<b>{@link control::$options showDateTo}</b>" attribute, which contains a
* specified datetime format. With this powerful feature you can enable or
* disable elements time controlled. The timeformat is:
* <b>YYYYMMDD-HHMinMinSS</b>
*
* - Y: Year
* - M: Month
* - D: Day
* - H: Hour
* - Min: Minute
* - S: Second
*
* By omitting fragments of the timestamp the system will insert automatically
* a zero at this place. Here some examples:
*
* The timestamp "200803" means that something changes on first day of march 2008
* at midnight (00:00 AM).
*
* The timestamp "20090131-12" means that something changes on the last day of
* january 2009 at 12:00 AM.
*
* Keep in mind that we use the <b>24h time-format</b>.
*
* <code>
* <controlname showFromDate="200803" showToDate="20090301-235959" />
* </code>
*
* @package mocovi
* @todo http://de.wikipedia.org/wiki/Vererbung_(Programmierung)#Akquisition
*/
class Model extends Singleton
{
/**
* DomNode contains the page model.
*
* @var DomNode
* @access protected
*/
protected $model;
/**
* Constructor creates DOM and loads the page model. (Singleton!)
*
* This is the first checkpoint, the DOM is getting filled and optimized
* for system, not for humans. If you want to make it human readable, you
* have to change the "formatOutput" variable from "false" to "true" and
* and leave the preserveWhiteSpace as it is.
* All XML-includes and -references are getting parsed.
*
* @param DomDocument $xml Contains the page model
* @return void
* @access protected
*/
protected function __construct(DOMNodeList $model)
{
if($model->length > 1 || $model->item(0)->nodeName != 'root')
{
$root = $model->item(0)->ownerDocument->createElement('root');
$parent = $model->item(0)->parentNode;
$length = $model->length;
for($i = 0; $i < $length; $i++)
$root->appendChild($model->item(0));
$parent->appendChild($root);
$model = $parent->childNodes;
}
$this->model = $model;
}
/**
* Loads all controls of the page model
*
* This function parses recursively the whole model DOM, loads all
* controls in an array and returns it.
*
* @param object $model Contains the page model
* @return array Contains the page model
* @access public
*/
public function getControls(DomNodeList $model = null)
{
$controlArray = array();
if(!isset($model))
$model = $this->model;
foreach($model as $sourceNode)
if(in_array($sourceNode->nodeType, array(XML_ELEMENT_NODE, XML_TEXT_NODE)))
{
$control = controlFactory::create($sourceNode);
$key = $this->arrayInsert($controlArray, $control); // returns the key of insertion
}
return $controlArray;
}
/**
* Fills an array and returns the key where the value has been saved.
*
* @param array $array Contains the array where the value has to be written
* @param string $value Contains the value which will be written in the array
* @return integer Returns the last filled key
* @access private
*/
private function arrayInsert(Array &$array, $value)
{
$array[$key = count($array)] = $value;
return $key;
}
}