require_once(APPROOT.'application/dashboardlayout.class.inc.php'); require_once(APPROOT.'application/dashlet.class.inc.php'); /** * A user editable dashboard page * * @copyright Copyright (C) 2010-2012 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ abstract class Dashboard { protected $sTitle; protected $sLayoutClass; protected $aWidgetsData; protected $oDOMNode; protected $sId; protected $aCells; public function __construct($sId) { $this->sLayoutClass = null; $this->aCells = array(); $this->oDOMNode = null; $this->sId = $sId; } public function FromXml($sXml) { $this->aCells = array(); // reset the content of the dashboard set_error_handler(array('Dashboard', 'ErrorHandler')); $oDoc = new DOMDocument(); $oDoc->loadXML($sXml); restore_error_handler(); $this->oDOMNode = $oDoc->getElementsByTagName('dashboard')->item(0); $oLayoutNode = $this->oDOMNode->getElementsByTagName('layout')->item(0); $this->sLayoutClass = $oLayoutNode->textContent; $oTitleNode = $this->oDOMNode->getElementsByTagName('title')->item(0); $this->sTitle = $oTitleNode->textContent; $oCellsNode = $this->oDOMNode->getElementsByTagName('cells')->item(0); $oCellsList = $oCellsNode->getElementsByTagName('cell'); $aCellOrder = array(); $iCellRank = 0; foreach($oCellsList as $oCellNode) { $aDashletList = array(); $oCellRank = $oCellNode->getElementsByTagName('rank')->item(0); if ($oCellRank) { $iCellRank = (float)$oCellRank->textContent; } $oDashletsNode = $oCellNode->getElementsByTagName('dashlets')->item(0); $oDashletList = $oDashletsNode->getElementsByTagName('dashlet'); $iRank = 0; $aDashletOrder = array(); foreach($oDashletList as $oDomNode) { $sDashletClass = $oDomNode->getAttribute('xsi:type'); $oRank = $oDomNode->getElementsByTagName('rank')->item(0); if ($oRank) { $iRank = (float)$oRank->textContent; } $sId = $oDomNode->getAttribute('id'); $oNewDashlet = new $sDashletClass($sId); $oNewDashlet->FromDOMNode($oDomNode); $aDashletOrder[] = array('rank' => $iRank, 'dashlet' => $oNewDashlet); } usort($aDashletOrder, array(get_class($this), 'SortOnRank')); $aDashletList = array(); foreach($aDashletOrder as $aItem) { $aDashletList[] = $aItem['dashlet']; } $aCellOrder[] = array('rank' => $iCellRank, 'dashlets' => $aDashletList); } usort($aCellOrder, array(get_class($this), 'SortOnRank')); foreach($aCellOrder as $aItem) { $this->aCells[] = $aItem['dashlets']; } } static function SortOnRank($aItem1, $aItem2) { return ($aItem1['rank'] > $aItem2['rank']) ? +1 : -1; } /** * Error handler to turn XML loading warnings into exceptions */ public static function ErrorHandler($errno, $errstr, $errfile, $errline) { if ($errno == E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0)) { throw new DOMException($errstr); } else { return false; } } public function ToXml() { $oDoc = new DOMDocument(); $oDoc->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS) $oDoc->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect $oMainNode = $oDoc->createElement('dashboard'); $oMainNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance"); $oDoc->appendChild($oMainNode); $oNode = $oDoc->createElement('layout', $this->sLayoutClass); $oMainNode->appendChild($oNode); $oNode = $oDoc->createElement('title', $this->sTitle); $oMainNode->appendChild($oNode); $oCellsNode = $oDoc->createElement('cells'); $oMainNode->appendChild($oCellsNode); $iCellRank = 0; foreach ($this->aCells as $aCell) { $oCellNode = $oDoc->createElement('cell'); $oCellNode->setAttribute('id', $iCellRank); $oCellsNode->appendChild($oCellNode); $oCellRank = $oDoc->createElement('rank', $iCellRank); $oCellNode->appendChild($oCellRank); $iCellRank++; $iDashletRank = 0; $oDashletsNode = $oDoc->createElement('dashlets'); $oCellNode->appendChild($oDashletsNode); foreach ($aCell as $oDashlet) { $oNode = $oDoc->createElement('dashlet'); $oDashletsNode->appendChild($oNode); $oNode->setAttribute('id', $oDashlet->GetID()); $oNode->setAttribute('xsi:type', get_class($oDashlet)); $oDashletRank = $oDoc->createElement('rank', $iDashletRank); $oNode->appendChild($oDashletRank); $iDashletRank++; $oDashlet->ToDOMNode($oNode); } } $sXml = $oDoc->saveXML(); return $sXml; } public function FromParams($aParams) { $this->sLayoutClass = $aParams['layout_class']; $this->sTitle = $aParams['title']; foreach($aParams['cells'] as $aCell) { $aCellDashlets = array(); foreach($aCell as $aDashletParams) { $sDashletClass = $aDashletParams['dashlet_class']; $sId = $aDashletParams['dashlet_id']; $oNewDashlet = new $sDashletClass($sId); $oForm = $oNewDashlet->GetForm(); $oForm->SetParamsContainer($sId); $oForm->SetPrefix(''); $aValues = $oForm->ReadParams(); $oNewDashlet->FromParams($aValues); $aCellDashlets[] = $oNewDashlet; } $this->aCells[] = $aCellDashlets; } } public function Save() { } public function GetLayout() { return $this->sLayoutClass; } public function SetLayout($sLayoutClass) { $this->sLayoutClass = $sLayoutClass; } public function GetTitle() { return $this->sTitle; } public function SetTitle($sTitle) { $this->sTitle = $sTitle; } public function AddDashlet($oDashlet) { $sId = $this->GetNewDashletId(); $oDashlet->SetId($sId); $this->aCells[] = array($oDashlet); } public function Render($oPage, $bEditMode = false, $aExtraParams = array()) { $oPage->add('