1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159 |
- <?php
- // Copyright (C) 2012 Combodo SARL
- //
- // 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; version 3 of the License.
- //
- // 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, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- require_once(APPROOT.'application/forms.class.inc.php');
- /**
- * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard)
- *
- */
- 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');
- }
- else
- {
- $ret = $sValue;
- settype($ret, $sRefType);
- }
- return $ret;
- }
- public function Prop2Str($value)
- {
- if (gettype($value) == 'boolean')
- {
- $sRet = $value ? 'true' : 'false';
- }
- 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, $aExtraParams = array())
- {
- $sCSSClasses = implode(' ', $this->aCSSClasses);
- if ($bEditMode)
- {
- $sId = $this->GetID();
- $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
- }
- else
- {
- $oPage->add('<div class="'.$sCSSClasses.'">');
- }
- $this->Render($oPage, $bEditMode, $aExtraParams);
- $oPage->add('</div>');
- if ($bEditMode)
- {
- $sClass = get_class($this);
- $oPage->add_ready_script(
- <<<EOF
- $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
- EOF
- );
- }
- }
-
- public function SetID($sId)
- {
- $this->sId = $sId;
- }
-
- public function GetID()
- {
- return $this->sId;
- }
-
- abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
-
- abstract public function GetPropertiesFields(DesignerForm $oForm);
-
- public function ToXml(DOMNode $oContainerNode)
- {
-
- }
- public function Update($aValues, $aUpdatedFields)
- {
- foreach($aUpdatedFields as $sProp)
- {
- if (array_key_exists($sProp, $this->aProperties))
- {
- $this->aProperties[$sProp] = $aValues[$sProp];
- }
- }
- return $this;
- }
-
- public function IsRedrawNeeded()
- {
- return $this->bRedrawNeeded;
- }
-
- public function IsFormRedrawNeeded()
- {
- return $this->bFormRedrawNeeded;
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => '',
- 'icon' => '',
- 'description' => '',
- );
- }
-
- public function GetForm()
- {
- $oForm = new DesignerForm();
- $oForm->SetPrefix("dashlet_". $this->GetID());
- $oForm->SetParamsContainer('params');
-
- $this->GetPropertiesFields($oForm);
-
- $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
- $oForm->AddField($oDashletClassField);
-
- $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
- $oForm->AddField($oDashletIdField);
-
- return $oForm;
- }
-
- static public function IsVisible()
- {
- return true;
- }
-
- static public function CanCreateFromOQL()
- {
- return false;
- }
-
- public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
- {
- // Default: do nothing since it's not supported
- }
- }
- class DashletEmptyCell extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $oPage->add(' ');
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Empty Cell',
- 'icon' => 'images/dashlet-text.png',
- 'description' => 'Empty Cell Dashlet Placeholder',
- );
- }
-
- static public function IsVisible()
- {
- return false;
- }
- }
- class DashletHelloWorld extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['text'] = 'Hello World';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
- $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
- $oPage->add_ready_script("$('#chart_{$sId}').pie_chart();");
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Hello World',
- 'icon' => 'images/dashlet-text.png',
- 'description' => 'Hello World test Dashlet',
- );
- }
- }
- class DashletObjectList extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['title'] = 'Hardcoded list of "my requests"';
- $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
- $this->aProperties['menu'] = false;
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = $this->aProperties['title'];
- $sQuery = $this->aProperties['query'];
- $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
- $oPage->add('<div class="dashlet-content">');
- $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
- if ($sHtmlTitle != '')
- {
- $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
- }
- $oFilter = DBObjectSearch::FromOQL($sQuery);
- $oBlock = new DisplayBlock($oFilter, 'list');
- $aExtraParams = array(
- 'menu' => $sShowMenu,
- );
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock->Display($oPage, $sBlockId, $aExtraParams);
- $oPage->add('</div>');
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
- $oForm->AddField($oField);
- $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
- $oForm->AddField($oField);
- $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
- $oForm->AddField($oField);
- }
- static public function GetInfo()
- {
- return array(
- 'label' => 'Object list',
- 'icon' => 'images/dashlet-list.png',
- 'description' => 'Object list dashlet',
- );
- }
-
- static public function CanCreateFromOQL()
- {
- return true;
- }
-
- public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
- {
- $oField = new DesignerTextField('title', 'Title', '');
- $oForm->AddField($oField);
- $oField = new DesignerHiddenField('query', 'Query', $sOQL);
- $oForm->AddField($oField);
- $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
- $oForm->AddField($oField);
- }
- }
- abstract class DashletGroupBy extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
- $this->aProperties['query'] = 'SELECT Contact';
- $this->aProperties['group_by'] = 'location_name';
- $this->aProperties['style'] = 'table';
- }
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = $this->aProperties['title'];
- $sQuery = $this->aProperties['query'];
- $sGroupBy = $this->aProperties['group_by'];
- $sStyle = $this->aProperties['style'];
- if ($sQuery == '')
- {
- $oPage->add('<p>Please enter a valid OQL query</p>');
- }
- elseif ($sGroupBy == '')
- {
- $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
- }
- else
- {
- $oFilter = DBObjectSearch::FromOQL($sQuery);
- $sClassAlias = $oFilter->GetClassAlias();
- if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
- {
- $sAttCode = $aMatches[1];
- $sFunction = $aMatches[2];
- switch($sFunction)
- {
- case 'hour':
- $sGroupByLabel = 'Hour of '.$sAttCode. ' (0-23)';
- $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 31
- break;
- case 'month':
- $sGroupByLabel = 'Month of '.$sAttCode. ' (1 - 12)';
- $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%m')"; // 0 -> 31
- break;
- case 'day_of_week':
- $sGroupByLabel = 'Day of week for '.$sAttCode. ' (sunday to saturday)';
- $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
- break;
- case 'day_of_month':
- $sGroupByLabel = 'Day of month for'.$sAttCode;
- $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%e')"; // 0 -> 31
- break;
- default:
- $sGroupByLabel = 'Unknown group by function '.$sFunction;
- $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
- }
- }
- else
- {
- $sAttCode = $sGroupBy;
- $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
- $sGroupByLabel = MetaModel::GetLabel($oFilter->GetClass(), $sAttCode);
- }
- 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('<div style="text-align:center" class="dashlet-content">');
- if ($sHtmlTitle != '')
- {
- $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
- }
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock = new DisplayBlock($oFilter, $sType);
- $oBlock->Display($oPage, $sBlockId, $aExtraParams);
- $oPage->add('</div>');
- }
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
- $oForm->AddField($oField);
- $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
- $oForm->AddField($oField);
- // Group by field: build the list of possible values (attribute codes + ...)
- $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
- $sClass = $oSearch->GetClass();
- $aGroupBy = array();
- foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
- {
- if (!$oAttDef->IsScalar()) continue; // skip link sets
- $sLabel = $oAttDef->GetLabel();
- if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
- {
- $sLabel = $oAttDef->GetLabel().' (strict)';
- }
- $aGroupBy[$sAttCode] = $sLabel;
- if ($oAttDef instanceof AttributeDateTime)
- {
- $aGroupBy[$sAttCode.':hour'] = $oAttDef->GetLabel().' (hour)';
- $aGroupBy[$sAttCode.':month'] = $oAttDef->GetLabel().' (month)';
- $aGroupBy[$sAttCode.':day_of_week'] = $oAttDef->GetLabel().' (day of week)';
- $aGroupBy[$sAttCode.':day_of_month'] = $oAttDef->GetLabel().' (day of month)';
- }
- }
- $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
- $oField->SetAllowedValues($aGroupBy);
- $oForm->AddField($oField);
- $aStyles = array(
- 'pie' => 'Pie chart',
- 'bars' => 'Bar chart',
- 'table' => 'Table',
- );
-
- $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
- $oField->SetAllowedValues($aStyles);
- $oForm->AddField($oField);
- }
-
- public function Update($aValues, $aUpdatedFields)
- {
- if (in_array('query', $aUpdatedFields))
- {
- $sCurrQuery = $aValues['query'];
- $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
- $sCurrClass = $oCurrSearch->GetClass();
- $sPrevQuery = $this->aProperties['query'];
- $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
- $sPrevClass = $oPrevSearch->GetClass();
- if ($sCurrClass != $sPrevClass)
- {
- $this->bFormRedrawNeeded = true;
- // wrong but not necessary - unset($aUpdatedFields['group_by']);
- $this->aProperties['group_by'] = '';
- }
- }
- $oDashlet = parent::Update($aValues, $aUpdatedFields);
-
- if (in_array('style', $aUpdatedFields))
- {
- switch($aValues['style'])
- {
- // Style changed, mutate to the specified type of chart
- case 'pie':
- $oDashlet = new DashletGroupByPie($this->sId);
- break;
-
- case 'bars':
- $oDashlet = new DashletGroupByBars($this->sId);
- break;
-
- case 'table':
- $oDashlet = new DashletGroupByTable($this->sId);
- break;
- }
- $oDashlet->FromParams($aValues);
- $oDashlet->bRedrawNeeded = true;
- $oDashlet->bFormRedrawNeeded = true;
- }
- return $oDashlet;
- }
- static public function GetInfo()
- {
- return array(
- 'label' => 'Objects grouped by...',
- 'icon' => 'images/dashlet-object-grouped.png',
- 'description' => 'Grouped objects dashlet',
- );
- }
-
- static public function CanCreateFromOQL()
- {
- return true;
- }
-
- public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
- {
- $oField = new DesignerTextField('title', 'Title', '');
- $oForm->AddField($oField);
- $oField = new DesignerHiddenField('query', 'Query', $sOQL);
- $oForm->AddField($oField);
- // Group by field: build the list of possible values (attribute codes + ...)
- $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
- $sClass = $oSearch->GetClass();
- $aGroupBy = array();
- foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
- {
- if (!$oAttDef->IsScalar()) continue; // skip link sets
- if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
- $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
- if ($oAttDef instanceof AttributeDateTime)
- {
- //date_format(start_date, '%d')
- $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
- }
- }
- $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
- $oField->SetAllowedValues($aGroupBy);
- $oForm->AddField($oField);
- $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
- $oForm->AddField($oField);
- }
- }
- class DashletGroupByPie extends DashletGroupBy
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['style'] = 'pie';
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Pie Chart',
- 'icon' => 'images/dashlet-pie-chart.png',
- 'description' => 'Pie Chart',
- );
- }
- }
- class DashletGroupByPie2 extends DashletGroupByPie
- {
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = addslashes($this->aProperties['title']);
-
- $sQuery = $this->aProperties['query'];
- $sGroupBy = $this->aProperties['group_by'];
- $oSearch = DBObjectSearch::FromOQL($sQuery);
- $sClassAlias = $oSearch->GetClassAlias();
- $aQueryParams = array();
-
- $aGroupBy = array();
- $oGroupByExp = Expression::FromOQL($sClassAlias.'.'.$sGroupBy);
- $aGroupBy['grouped_by_1'] = $oGroupByExp;
- $sSql = MetaModel::MakeGroupByQuery($oSearch, $aQueryParams, $aGroupBy);
- $aRes = CMDBSource::QueryToArray($sSql);
- $aGroupBy = array();
- $aLabels = array();
- $iTotalCount = 0;
- foreach ($aRes as $aRow)
- {
- $sValue = $aRow['grouped_by_1'];
- $aLabels[] = ($sValue == '') ? 'Empty (%%.%%)' : $sValue.' (%%.%%)'; //TODO: localize
- $aGroupBy[] = (int) $aRow['_itop_count_'];
- $iTotalCount += $aRow['_itop_count_'];
- }
- $aURLs = array();
- $sContext = ''; //TODO get the context ??
- foreach($aGroupBy as $sValue => $iValue)
- {
- // Build the search for this subset
- $oSubsetSearch = clone $oSearch;
- $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($sValue));
- $oSubsetSearch->AddConditionExpression($oCondition);
- $aURLs[] = 'http://www.combodo.com/itop'; //utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html{$sContext}&filter=".addslashes($oSubsetSearch->serialize());
- }
- $sJSValues = json_encode($aGroupBy);
- $sJSHrefs = json_encode($aURLs);
- $sJSLabels = json_encode($aLabels);
-
- $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
- $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
- $oPage->add_ready_script("$('#chart_{$sId}').pie_chart({chart_label: '$sTitle', values: $sJSValues, labels: $sJSLabels, hrefs: $sJSHrefs });");
- }
- static public function GetInfo()
- {
- return array(
- 'label' => 'Pie (Raphael)',
- 'icon' => 'images/dashlet-pie-chart.png',
- 'description' => 'Pure JS Pie Chart',
- );
- }
- }
- class DashletGroupByBars extends DashletGroupBy
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['style'] = 'bars';
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Bar Chart',
- 'icon' => 'images/dashlet-bar-chart.png',
- 'description' => 'Bar Chart',
- );
- }
- }
- class DashletGroupByTable extends DashletGroupBy
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['style'] = 'table';
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Group By (table)',
- 'icon' => 'images/dashlet-groupby-table.png',
- 'description' => 'List (Grouped by a field)',
- );
- }
- }
- class DashletHeaderStatic extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['title'] = 'Contacts';
- $this->aProperties['icon'] = 'itop-config-mgmt-1.0.0/images/contact.png';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = $this->aProperties['title'];
- $sIcon = $this->aProperties['icon'];
- $sTitleReady = str_replace(':', '_', $sTitle);
- $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
- $oPage->add('<div class="dashlet-content">');
- $oPage->add('<div class="main_header">');
- $oPage->add('<img src="'.$sIconPath.'">');
- $oPage->add('<h1>'.Dict::S($sTitleReady).'</h1>');
- $oPage->add('</div>');
- $oPage->add('</div>');
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
- $oForm->AddField($oField);
-
- $oField = new DesignerIconSelectionField('icon', 'Icon', $this->aProperties['icon']);
- $aAllIcons = self::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
- ksort($aAllIcons);
- $aValues = array();
- foreach($aAllIcons as $sFilePath)
- {
- $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
- }
- $oField->SetAllowedValues($aValues);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Header',
- 'icon' => 'images/dashlet-header.png',
- 'description' => 'Header with stats (grouped by...)',
- );
- }
-
- static public function FindIcons($sBaseDir, $sDir = '')
- {
- $aResult = array();
- // Populate automatically the list of icon files
- if ($hDir = @opendir($sBaseDir.'/'.$sDir))
- {
- while (($sFile = readdir($hDir)) !== false)
- {
- $aMatches = array();
- if (($sFile != '.') && ($sFile != '..') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
- {
- $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
- $aResult = array_merge($aResult, self::FindIcons($sBaseDir, $sDirSubPath));
- }
- if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
- {
- $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
- }
- }
- closedir($hDir);
- }
- return $aResult;
- }
- }
- class DashletHeaderDynamic extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['title'] = 'Contacts';
- $this->aProperties['icon'] = 'itop-config-mgmt-1.0.0/images/contact.png';
- $this->aProperties['subtitle'] = 'Contacts';
- $this->aProperties['query'] = 'SELECT Contact';
- $this->aProperties['group_by'] = 'status';
- $this->aProperties['values'] = 'active,inactive,terminated';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = $this->aProperties['title'];
- $sIcon = $this->aProperties['icon'];
- $sSubtitle = $this->aProperties['subtitle'];
- $sQuery = $this->aProperties['query'];
- $sGroupBy = $this->aProperties['group_by'];
- $sValues = $this->aProperties['values'];
- $oFilter = DBObjectSearch::FromOQL($sQuery);
- $sClass = $oFilter->GetClass();
- $sTitleReady = str_replace(':', '_', $sTitle);
- $sSubtitleReady = str_replace(':', '_', $sSubtitle);
- $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
- $aValues = null;
- if (MetaModel::IsValidAttCode($sClass, $sGroupBy))
- {
- if ($sValues == '')
- {
- $aAllowed = MetaModel::GetAllowedValues_att($sClass, $sGroupBy);
- if (is_array($aAllowed))
- {
- $aValues = array_keys($aAllowed);
- }
- }
- else
- {
- $aValues = explode(',', $sValues);
- }
- }
- if (is_array($aValues))
- {
- // Stats grouped by <group_by>
- $aCSV = implode(',', $aValues);
- $aExtraParams = array(
- 'title[block]' => $sTitleReady,
- 'label[block]' => $sSubtitleReady,
- 'status[block]' => $sGroupBy,
- 'status_codes[block]' => $aCSV,
- 'context_filter' => 1,
- );
- }
- else
- {
- // Simple stats
- $aExtraParams = array(
- 'title[block]' => $sTitleReady,
- 'label[block]' => $sSubtitleReady,
- 'context_filter' => 1,
- );
- }
- $oPage->add('<div class="dashlet-content">');
- $oPage->add('<div class="main_header">');
- $oPage->add('<img src="'.$sIconPath.'">');
- $oBlock = new DisplayBlock($oFilter, 'summary');
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock->Display($oPage, $sBlockId, $aExtraParams);
- $oPage->add('</div>');
- $oPage->add('</div>');
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
- $oForm->AddField($oField);
- $oField = new DesignerIconSelectionField('icon', 'Icon', $this->aProperties['icon']);
- $aAllIcons = DashletHeaderStatic::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
- ksort($aAllIcons);
- $aValues = array();
- foreach($aAllIcons as $sFilePath)
- {
- $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
- }
- $oField->SetAllowedValues($aValues);
- $oForm->AddField($oField);
- $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
- $oForm->AddField($oField);
- $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
- $oForm->AddField($oField);
- // Group by field: build the list of possible values (attribute codes + ...)
- $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
- $sClass = $oSearch->GetClass();
- $aGroupBy = array();
- foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
- {
- if (!$oAttDef->IsScalar()) continue; // skip link sets
- $sLabel = $oAttDef->GetLabel();
- if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
- {
- $sLabel = $oAttDef->GetLabel().' (strict)';
- }
- $aGroupBy[$sAttCode] = $sLabel;
- }
- $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
- $oField->SetAllowedValues($aGroupBy);
- $oForm->AddField($oField);
- $oField = new DesignerTextField('values', 'Values (CSV list)', $this->aProperties['values']);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Header with statistics',
- 'icon' => 'images/dashlet-header-stats.png',
- 'description' => 'Header with stats (grouped by...)',
- );
- }
- }
- class DashletBadge extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['class'] = 'Contact';
- $this->aCSSClasses[] = 'dashlet-inline';
- $this->aCSSClasses[] = 'dashlet-badge';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sClass = $this->aProperties['class'];
- $oPage->add('<div class="dashlet-content">');
- $oFilter = new DBObjectSearch($sClass);
- $oBlock = new DisplayBlock($oFilter, 'actions');
- $aExtraParams = array(
- 'context_filter' => 1,
- );
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock->Display($oPage, $sBlockId, $aExtraParams);
- $oPage->add('</div>');
- if ($bEditMode)
- {
- // Since the container div is not rendered the same way in edit mode, add the 'inline' style to it
- $oPage->add_ready_script("$('#dashlet_".$this->sId."').addClass('dashlet-inline');");
- }
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oClassesSet = new ValueSetEnumClasses('bizmodel', array());
- $aClasses = $oClassesSet->GetValues(array());
-
- $aLinkClasses = array();
-
- foreach(MetaModel::GetClasses('bizmodel') as $sClass)
- {
- foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
- {
- if ($oAttDef instanceof AttributeLinkedSetIndirect)
- {
- $aLinkClasses[$oAttDef->GetLinkedClass()] = true;
- }
- }
- }
-
-
- $oField = new DesignerIconSelectionField('class', 'Class', $this->aProperties['class']);
- ksort($aClasses);
- $aValues = array();
- foreach($aClasses as $sClass => $sClass)
- {
- if (!array_key_exists($sClass, $aLinkClasses))
- {
- $sIconUrl = MetaModel::GetClassIcon($sClass, false);
- $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
- if (($sIconUrl == '') || !file_exists($sIconFilePath))
- {
- // The icon does not exist, leet's use a transparent one of the same size.
- $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
- }
- $aValues[] = array('value' => $sClass, 'label' => $sClass, 'icon' => $sIconUrl);
- }
- }
- $oField->SetAllowedValues($aValues);
-
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Badge',
- 'icon' => 'images/dashlet-badge.png',
- 'description' => 'Object Icon with new/search',
- );
- }
- }
- class DashletProto extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['class'] = 'Foo';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sClass = $this->aProperties['class'];
- $oFilter = DBObjectSearch::FromOQL('SELECT FunctionalCI AS fci');
- $sGroupBy1 = 'status';
- //$sGroupBy2 = 'org_id_friendlyname';
- $sGroupBy2 = 'org_id';
- $sHtmlTitle = "Hardcoded on $sGroupBy1 and $sGroupBy2...";
- $sAlias = $oFilter->GetClassAlias();
- $oGroupByExp1 = new FieldExpression($sGroupBy1, $sAlias);
- $sGroupByLabel1 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy1);
-
- $oGroupByExp2 = new FieldExpression($sGroupBy2, $sAlias);
- $sGroupByLabel2 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy2);
-
- $aGroupBy = array();
- $aGroupBy['grouped_by_1'] = $oGroupByExp1;
- $aGroupBy['grouped_by_2'] = $oGroupByExp2;
- $sSql = MetaModel::MakeGroupByQuery($oFilter, array(), $aGroupBy);
- $aRes = CMDBSource::QueryToArray($sSql);
-
- $iTotalCount = 0;
- $aData = array();
- $oAppContext = new ApplicationContext();
- $sParams = $oAppContext->GetForLink();
- foreach ($aRes as $aRow)
- {
- $iCount = $aRow['_itop_count_'];
- $iTotalCount += $iCount;
- $sValue1 = $aRow['grouped_by_1'];
- $sValue2 = $aRow['grouped_by_2'];
- $sDisplayValue1 = $aGroupBy['grouped_by_1']->MakeValueLabel($oFilter, $sValue1, $sValue1); // default to the raw value
- $sDisplayValue2 = $aGroupBy['grouped_by_2']->MakeValueLabel($oFilter, $sValue2, $sValue2); // default to the raw value
- // Build the search for this subset
- $oSubsetSearch = clone $oFilter;
- $oCondition = new BinaryExpression($oGroupByExp1, '=', new ScalarExpression($sValue1));
- $oSubsetSearch->AddConditionExpression($oCondition);
- $oCondition = new BinaryExpression($oGroupByExp2, '=', new ScalarExpression($sValue2));
- $oSubsetSearch->AddConditionExpression($oCondition);
-
- $sFilter = urlencode($oSubsetSearch->serialize());
- $aData[] = array (
- 'group1' => $sDisplayValue1,
- 'group2' => $sDisplayValue2,
- 'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"
- ); // TO DO: add the context information
- }
- $aAttribs =array(
- 'group1' => array('label' => $sGroupByLabel1, 'description' => ''),
- 'group2' => array('label' => $sGroupByLabel2, 'description' => ''),
- 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
- );
- $oPage->add('<div style="text-align:center" class="dashlet-content">');
- $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
- $oPage->p(Dict::Format('UI:Pagination:HeaderNoSelection', $iTotalCount));
- $oPage->table($aAttribs, $aData);
- $oPage->add('</div>');
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Test3D',
- 'icon' => 'images/dashlet-groupby2-table.png',
- 'description' => 'Group by on two dimensions',
- );
- }
- }
- class DashletHeatMap extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['class'] = 'Contact';
- $this->aProperties['title'] = 'Test';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = addslashes($this->aProperties['title']);
-
- $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
- $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
- $oPage->add_ready_script("$('#chart_{$sId}').heatmap_chart({chart_label: '$sTitle'});");
- }
-
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
- $oForm->AddField($oField);
-
- $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Heatmap (Raphael)',
- 'icon' => 'images/dashlet-heatmap.png',
- 'description' => 'Pure JS Heat Map Chart',
- );
- }
- }
|