2014-01-03 17:18:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FML;
|
|
|
|
|
2014-01-19 19:30:21 +01:00
|
|
|
use FML\Elements\Dico;
|
2014-01-21 20:30:40 +01:00
|
|
|
use FML\Script\Script;
|
2014-01-19 19:30:21 +01:00
|
|
|
use FML\Stylesheet\Stylesheet;
|
2014-01-21 20:30:40 +01:00
|
|
|
use FML\Types\Renderable;
|
2014-04-27 14:44:40 +02:00
|
|
|
use FML\Types\ScriptFeatureable;
|
2014-01-03 17:18:14 +01:00
|
|
|
|
|
|
|
/**
|
2014-01-12 00:51:46 +01:00
|
|
|
* Class representing a ManiaLink
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-05-20 15:44:45 +02:00
|
|
|
* @author steeffeen <mail@steeffeen.com>
|
2014-04-13 18:21:40 +02:00
|
|
|
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
|
2014-05-14 23:24:00 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
2014-01-19 19:30:21 +01:00
|
|
|
class ManiaLink {
|
2014-01-21 20:30:40 +01:00
|
|
|
/*
|
2014-01-19 19:30:21 +01:00
|
|
|
* Constants
|
|
|
|
*/
|
2014-05-14 23:24:00 +02:00
|
|
|
const BACKGROUND_0 = '0';
|
|
|
|
const BACKGROUND_1 = '1';
|
|
|
|
const BACKGROUND_STARS = 'stars';
|
2014-01-19 19:30:21 +01:00
|
|
|
const BACKGROUND_STATIONS = 'stations';
|
2014-05-14 23:24:00 +02:00
|
|
|
const BACKGROUND_TITLE = 'title';
|
|
|
|
|
2014-01-21 20:30:40 +01:00
|
|
|
/*
|
2014-06-21 03:18:21 +02:00
|
|
|
* Protected properties
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
protected $encoding = 'utf-8';
|
|
|
|
protected $tagName = 'manialink';
|
2014-06-21 03:18:21 +02:00
|
|
|
protected $maniaLinkId = null;
|
2014-01-03 17:18:14 +01:00
|
|
|
protected $version = 1;
|
2014-06-21 03:18:21 +02:00
|
|
|
protected $background = null;
|
2014-04-27 14:44:40 +02:00
|
|
|
protected $navigable3d = 1;
|
2014-07-03 22:34:47 +02:00
|
|
|
protected $name = null;
|
2014-01-03 17:18:14 +01:00
|
|
|
protected $timeout = 0;
|
2014-06-21 03:18:21 +02:00
|
|
|
/** @var Renderable[] $children */
|
2014-01-03 17:18:14 +01:00
|
|
|
protected $children = array();
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Dico $dico */
|
2014-01-19 19:30:21 +01:00
|
|
|
protected $dico = null;
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Stylesheet $stylesheet */
|
2014-01-19 19:30:21 +01:00
|
|
|
protected $stylesheet = null;
|
2014-05-14 23:24:00 +02:00
|
|
|
/** @var Script $script */
|
2014-01-03 17:18:14 +01:00
|
|
|
protected $script = null;
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Create a new ManiaLink object
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $maniaLinkId (optional) ManiaLink id
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-19 19:30:21 +01:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public static function create($maniaLinkId = null) {
|
|
|
|
return new static($maniaLinkId);
|
2014-01-19 19:30:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Construct a new ManiaLink object
|
2014-01-19 19:30:21 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $maniaLinkId (optional) ManiaLink id
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public function __construct($maniaLinkId = null) {
|
|
|
|
if (!is_null($maniaLinkId)) {
|
|
|
|
$this->setId($maniaLinkId);
|
2014-01-19 19:30:21 +01:00
|
|
|
}
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set XML encoding
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $encoding XML encoding
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function setXmlEncoding($encoding) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$this->encoding = (string)$encoding;
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set ManiaLink id
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $maniaLinkId ManiaLink id
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
2014-06-21 03:18:21 +02:00
|
|
|
public function setId($maniaLinkId) {
|
|
|
|
$this->maniaLinkId = (string)$maniaLinkId;
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-02-16 13:59:28 +01:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Get ManiaLink id
|
2014-04-27 14:44:40 +02:00
|
|
|
*
|
2014-02-16 13:59:28 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId() {
|
2014-06-21 03:18:21 +02:00
|
|
|
return $this->maniaLinkId;
|
2014-02-16 13:59:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 17:18:14 +01:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set background
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param string $background Background value
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function setBackground($background) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$this->background = (string)$background;
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set navigable3d
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-01-12 00:51:46 +01:00
|
|
|
* @param bool $navigable3d Whether the manialink should be 3d navigable
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function setNavigable3d($navigable3d) {
|
|
|
|
$this->navigable3d = ($navigable3d ? 1 : 0);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:34:47 +02:00
|
|
|
/**
|
|
|
|
* Set the ManiaLink Name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$this->name = (string)$name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:18:14 +01:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Set timeout
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param int $timeout Timeout duration
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function setTimeout($timeout) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$this->timeout = (int)$timeout;
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Add an element to the ManiaLink
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param Renderable $child Child element to add
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function add(Renderable $child) {
|
2014-01-12 14:47:17 +01:00
|
|
|
if (!in_array($child, $this->children, true)) {
|
|
|
|
array_push($this->children, $child);
|
|
|
|
}
|
2014-01-03 17:18:14 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Remove all elements from the ManiaLink
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function removeChildren() {
|
|
|
|
$this->children = array();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-19 19:30:21 +01:00
|
|
|
/**
|
|
|
|
* Set the Dictionary of the ManiaLink
|
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param Dico $dico Dictionary for the ManiaLink
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-19 19:30:21 +01:00
|
|
|
*/
|
|
|
|
public function setDico(Dico $dico) {
|
|
|
|
$this->dico = $dico;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Get the Dictionary of the ManiaLink
|
2014-01-19 19:30:21 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param bool $createIfEmpty (optional) Whether the Dico object should be created if it's not set
|
2014-01-19 19:30:21 +01:00
|
|
|
* @return \FML\Elements\Dico
|
|
|
|
*/
|
|
|
|
public function getDico($createIfEmpty = true) {
|
|
|
|
if (!$this->dico && $createIfEmpty) {
|
2014-04-13 18:21:40 +02:00
|
|
|
$this->setDico(new Dico());
|
2014-01-19 19:30:21 +01:00
|
|
|
}
|
|
|
|
return $this->dico;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Stylesheet of the ManiaLink
|
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param Stylesheet $stylesheet Stylesheet for the ManiaLink
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-19 19:30:21 +01:00
|
|
|
*/
|
|
|
|
public function setStylesheet(Stylesheet $stylesheet) {
|
|
|
|
$this->stylesheet = $stylesheet;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Stylesheet of the ManiaLink
|
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param bool $createIfEmpty (optional) Whether the Stylesheet object should be created if it's not set
|
2014-01-19 19:30:21 +01:00
|
|
|
* @return \FML\Stylesheet\Stylesheet
|
|
|
|
*/
|
|
|
|
public function getStylesheet($createIfEmpty = true) {
|
|
|
|
if (!$this->stylesheet && $createIfEmpty) {
|
2014-04-13 18:21:40 +02:00
|
|
|
$this->setStylesheet(new Stylesheet());
|
2014-01-19 19:30:21 +01:00
|
|
|
}
|
|
|
|
return $this->stylesheet;
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:18:14 +01:00
|
|
|
/**
|
2014-01-05 16:04:55 +01:00
|
|
|
* Set the Script of the ManiaLink
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param Script $script Script for the ManiaLink
|
2014-07-03 22:34:47 +02:00
|
|
|
* @return static
|
2014-01-03 17:18:14 +01:00
|
|
|
*/
|
|
|
|
public function setScript(Script $script) {
|
|
|
|
$this->script = $script;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:04:55 +01:00
|
|
|
/**
|
|
|
|
* Get the current Script of the ManiaLink
|
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param bool $createIfEmpty (optional) Whether the Script object should be created if it's not set
|
2014-01-05 16:04:55 +01:00
|
|
|
* @return \FML\Script\Script
|
|
|
|
*/
|
|
|
|
public function getScript($createIfEmpty = true) {
|
|
|
|
if (!$this->script && $createIfEmpty) {
|
2014-04-13 18:21:40 +02:00
|
|
|
$this->setScript(new Script());
|
2014-01-05 16:04:55 +01:00
|
|
|
}
|
|
|
|
return $this->script;
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:18:14 +01:00
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Render the XML document
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
2014-06-21 03:18:21 +02:00
|
|
|
* @param bool $echo (optional) If the XML text should be echoed and the Content-Type header should be set
|
|
|
|
* @param \DOMDocument $domDocument (optional) DOMDocument for which the XML element should be created
|
2014-01-03 17:18:14 +01:00
|
|
|
* @return \DOMDocument
|
|
|
|
*/
|
|
|
|
public function render($echo = false, $domDocument = null) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$isChild = (bool)$domDocument;
|
2014-01-03 17:18:14 +01:00
|
|
|
if (!$isChild) {
|
2014-05-14 23:24:00 +02:00
|
|
|
$domDocument = new \DOMDocument('1.0', $this->encoding);
|
2014-01-19 19:30:21 +01:00
|
|
|
$domDocument->xmlStandalone = true;
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-01-12 00:51:46 +01:00
|
|
|
$maniaLink = $domDocument->createElement($this->tagName);
|
2014-01-03 17:18:14 +01:00
|
|
|
if (!$isChild) {
|
2014-01-12 00:51:46 +01:00
|
|
|
$domDocument->appendChild($maniaLink);
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-06-21 03:18:21 +02:00
|
|
|
if (strlen($this->maniaLinkId) > 0) {
|
|
|
|
$maniaLink->setAttribute('id', $this->maniaLinkId);
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-01-19 19:30:21 +01:00
|
|
|
if ($this->version) {
|
2014-01-12 00:51:46 +01:00
|
|
|
$maniaLink->setAttribute('version', $this->version);
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-01-21 20:30:40 +01:00
|
|
|
if (strlen($this->background) > 0) {
|
2014-01-12 00:51:46 +01:00
|
|
|
$maniaLink->setAttribute('background', $this->background);
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-04-27 14:44:40 +02:00
|
|
|
if (!$this->navigable3d) {
|
2014-01-12 00:51:46 +01:00
|
|
|
$maniaLink->setAttribute('navigable3d', $this->navigable3d);
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-07-03 22:34:47 +02:00
|
|
|
if ($this->name) {
|
|
|
|
$maniaLink->setAttribute('name', $this->name);
|
|
|
|
}
|
2014-01-19 19:30:21 +01:00
|
|
|
if ($this->timeout) {
|
2014-01-03 17:18:14 +01:00
|
|
|
$timeoutXml = $domDocument->createElement('timeout', $this->timeout);
|
2014-01-12 00:51:46 +01:00
|
|
|
$maniaLink->appendChild($timeoutXml);
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2014-01-19 19:30:21 +01:00
|
|
|
if ($this->dico) {
|
|
|
|
$dicoXml = $this->dico->render($domDocument);
|
|
|
|
$maniaLink->appendChild($dicoXml);
|
|
|
|
}
|
2014-04-27 14:44:40 +02:00
|
|
|
$scriptFeatures = array();
|
|
|
|
foreach ($this->children as $child) {
|
|
|
|
$childXml = $child->render($domDocument, $this->getScript());
|
|
|
|
$maniaLink->appendChild($childXml);
|
|
|
|
if ($child instanceof ScriptFeatureable) {
|
|
|
|
$scriptFeatures = array_merge($scriptFeatures, $child->getScriptFeatures());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($scriptFeatures) {
|
|
|
|
$this->getScript()->loadFeatures($scriptFeatures);
|
|
|
|
}
|
2014-01-19 19:30:21 +01:00
|
|
|
if ($this->stylesheet) {
|
|
|
|
$stylesheetXml = $this->stylesheet->render($domDocument);
|
|
|
|
$maniaLink->appendChild($stylesheetXml);
|
|
|
|
}
|
2014-04-27 16:31:46 +02:00
|
|
|
if ($this->script) {
|
|
|
|
if ($this->script->needsRendering()) {
|
|
|
|
$scriptXml = $this->script->render($domDocument);
|
|
|
|
$maniaLink->appendChild($scriptXml);
|
|
|
|
}
|
|
|
|
$this->script->resetGenericScriptLabels();
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
|
|
|
if ($isChild) {
|
2014-01-12 00:51:46 +01:00
|
|
|
return $maniaLink;
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
|
|
|
if ($echo) {
|
2014-01-19 19:30:21 +01:00
|
|
|
header('Content-Type: application/xml; charset=utf-8;');
|
2014-01-03 17:18:14 +01:00
|
|
|
echo $domDocument->saveXML();
|
|
|
|
}
|
|
|
|
return $domDocument;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-21 03:18:21 +02:00
|
|
|
* Get string representation
|
2014-01-03 17:18:14 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
2014-06-21 03:18:21 +02:00
|
|
|
return $this->render()->saveXML();
|
2014-01-03 17:18:14 +01:00
|
|
|
}
|
2013-11-25 00:02:07 +01:00
|
|
|
}
|