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-2012 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ abstract class Dashlet { protected $sId; protected $bRedrawNeeded; protected $bFormRedrawNeeded; protected $aProperties; // array of {property => value} protected $aCSSClasses; public function __construct($sId) { $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 ($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; } public function FromDOMNode($oDOMNode) { foreach ($this->aProperties as $sProperty => $value) { $this->oDOMNode = $oDOMNode->getElementsByTagName($sProperty)->item(0); if ($this->oDOMNode != null) { $newvalue = $this->Str2Prop($sProperty, $this->oDOMNode->textContent); $this->aProperties[$sProperty] = $newvalue; } } } public function ToDOMNode($oDOMNode) { foreach ($this->aProperties as $sProperty => $value) { $sXmlValue = $this->Prop2Str($value); $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty, $sXmlValue); $oDOMNode->appendChild($oPropNode); } } 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]; } } } 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 { $sAttLabel = MetaModel::GetLabel($sClass, $sAttCode); if (!is_null($sFunction)) { $sFunction = $aMatches[2]; switch($sFunction) { case 'hour': $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Hour', $sAttLabel); $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 23 break; case 'month': $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Month', $sAttLabel); $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%m')"; // 0 -> 31 break; case 'day_of_week': $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfWeek', $sAttLabel); $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')"; break; case 'day_of_month': $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfMonth', $sAttLabel); $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%e')"; // 0 -> 31 break; default: $sGroupByLabel = 'Unknown group by function '.$sFunction; $sGroupByExpr = $sClassAlias.'.'.$sAttCode; } } else { $sGroupByExpr = $sClassAlias.'.'.$sAttCode; $sGroupByLabel = $sAttLabel; } switch($sStyle) { case 'bars': $sType = 'open_flash_chart'; $aExtraParams = array( 'chart_type' => 'bars', 'chart_title' => $sTitle, 'group_by' => $sGroupByExpr, 'group_by_label' => $sGroupByLabel, ); $sHtmlTitle = ''; // done in the itop block break; case 'pie': $sType = 'open_flash_chart'; $aExtraParams = array( 'chart_type' => 'pie', 'chart_title' => $sTitle, 'group_by' => $sGroupByExpr, 'group_by_label' => $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' => $sGroupByExpr, 'group_by_label' => $sGroupByLabel, ); break; } $oPage->add('