require_once(APPROOT.'application/forms.class.inc.php'); /** * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard) * * @copyright Copyright (C) 2010-2017 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ abstract class Dashlet { protected $oModelReflection; protected $sId; protected $bRedrawNeeded; protected $bFormRedrawNeeded; protected $aProperties; // array of {property => value} protected $aCSSClasses; public function __construct(ModelReflection $oModelReflection, $sId) { $this->oModelReflection = $oModelReflection; $this->sId = $sId; $this->bRedrawNeeded = true; // By default: redraw each time a property changes $this->bFormRedrawNeeded = false; // By default: no need to redraw the form (independent fields) $this->aProperties = array(); // By default: there is no property $this->aCSSClasses = array('dashlet'); } // Assuming that a property has the type of its default value, set in the constructor // public function Str2Prop($sProperty, $sValue) { $refValue = $this->aProperties[$sProperty]; $sRefType = gettype($refValue); if (gettype($sValue) == $sRefType) { // Do not change anything in that case! $ret = $sValue; } elseif ($sRefType == 'boolean') { $ret = ($sValue == 'true'); } elseif ($sRefType == 'array') { $ret = explode(',', $sValue); } else { $ret = $sValue; settype($ret, $sRefType); } return $ret; } public function Prop2Str($value) { $sType = gettype($value); if ($sType == 'boolean') { $sRet = $value ? 'true' : 'false'; } elseif ($sType == 'array') { $sRet = implode(',', $value); } else { $sRet = (string) $value; } return $sRet; } protected function OnUpdate() { } public function FromDOMNode($oDOMNode) { foreach ($this->aProperties as $sProperty => $value) { $oPropNode = $oDOMNode->getElementsByTagName($sProperty)->item(0); if ($oPropNode != null) { $this->aProperties[$sProperty] = $this->PropertyFromDOMNode($oPropNode, $sProperty); } } $this->OnUpdate(); } public function ToDOMNode($oDOMNode) { foreach ($this->aProperties as $sProperty => $value) { $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty); $oDOMNode->appendChild($oPropNode); $this->PropertyToDOMNode($oPropNode, $sProperty, $value); } } protected function PropertyFromDOMNode($oDOMNode, $sProperty) { $res = $this->Str2Prop($sProperty, $oDOMNode->textContent); return $res; } protected function PropertyToDOMNode($oDOMNode, $sProperty, $value) { $sXmlValue = $this->Prop2Str($value); $oTextNode = $oDOMNode->ownerDocument->createTextNode($sXmlValue); $oDOMNode->appendChild($oTextNode); } public function FromXml($sXml) { $oDomDoc = new DOMDocument('1.0', 'UTF-8'); $oDomDoc->loadXml($sXml); $this->FromDOMNode($oDomDoc->firstChild); } public function FromParams($aParams) { foreach ($this->aProperties as $sProperty => $value) { if (array_key_exists($sProperty, $aParams)) { $this->aProperties[$sProperty] = $aParams[$sProperty]; } } $this->OnUpdate(); } public function DoRender($oPage, $bEditMode = false, $bEnclosingDiv = true, $aExtraParams = array()) { $sCSSClasses = implode(' ', $this->aCSSClasses); $sId = $this->GetID(); if ($bEnclosingDiv) { if ($bEditMode) { $oPage->add('
'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'
'); } else { switch($sStyle) { case 'bars': $sType = 'chart'; $aExtraParams = array( 'chart_type' => 'bars', 'chart_title' => $sTitle, 'group_by' => $this->sGroupByExpr, 'group_by_label' => $this->sGroupByLabel, ); $sHtmlTitle = ''; // done in the itop block break; case 'pie': $sType = 'chart'; $aExtraParams = array( 'chart_type' => 'pie', 'chart_title' => $sTitle, 'group_by' => $this->sGroupByExpr, 'group_by_label' => $this->sGroupByLabel, ); $sHtmlTitle = ''; // done in the itop block break; case 'table': default: $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block $sType = 'count'; $aExtraParams = array( 'group_by' => $this->sGroupByExpr, 'group_by_label' => $this->sGroupByLabel, ); break; } $oPage->add(''.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'
'); $oPage->add(''.$this->sGroupByLabel.' | '); $oPage->add(''.Dict::S('UI:GroupBy:Count').' | '); $oPage->add('
---|---|
'.$aDisplayData['label'].' | '); $oPage->add(''.$aDisplayData['value'].' | '); $oPage->add('
'.$sValueLabel.' | '); } $oPage->add('
---|
'.$iCount.' | '); } $oPage->add('
');
$oPage->add(' '.$sClassLabel.': 947');
$oPage->add('
');
$oPage->add(' '.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'');
$oPage->add('
');
$oPage->add(' '.Dict::Format('UI:SearchFor_Class', $sClassLabel).'');
$oPage->add('