123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650 |
- <?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 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;
- }
- }
- 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())
- {
- $oPage->add('<div style="text-align:center; line-height:5em" class="dashlet-content"><span>'.$this->aProperties['text'].'</span></div>');
- }
- 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 style="text-align:center" class="dashlet-content">');
- // C'est quoi ce paramètre "menu" ?
- $sXML = '<itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql" parameters="menu:'.$sShowMenu.'">'.$sQuery.'</itopblock>';
- $aParams = array();
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock = DisplayBlock::FromTemplate($sXML);
- $oBlock->Display($oPage, $sBlockId, $aParams);
- $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-object-list.png',
- 'description' => 'Object list dashlet',
- );
- }
- }
- 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
- {
- switch($sStyle)
- {
- case 'bars':
- $sXML = '<itopblock BlockClass="DisplayBlock" type="open_flash_chart" parameters="chart_type:bars;chart_title:'.$sTitle.';group_by:'.$sGroupBy.'" asynchronous="false" encoding="text/oql">'.$sQuery.'</itopblock>';
- $sHtmlTitle = ''; // done in the itop block
- break;
- case 'pie':
- $sXML = '<itopblock BlockClass="DisplayBlock" type="open_flash_chart" parameters="chart_type:pie;chart_title:'.$sTitle.';group_by:'.$sGroupBy.'" asynchronous="false" encoding="text/oql">'.$sQuery.'</itopblock>';
- $sHtmlTitle = ''; // done in the itop block
- break;
- case 'table':
- default:
- $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
- $sXML = '<itopblock BlockClass="DisplayBlock" type="count" parameters="group_by:'.$sGroupBy.'" asynchronous="false" encoding="text/oql">'.$sQuery.'</itopblock>';
- break;
- }
-
- $oPage->add('<div style="text-align:center" class="dashlet-content">');
- if ($sHtmlTitle != '')
- {
- $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
- }
- $aParams = array();
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock = DisplayBlock::FromTemplate($sXML);
- $oBlock->Display($oPage, $sBlockId, $aParams);
- $oPage->add('</div>');
- // TEST Group By as SQL!
- //$oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
- //$sSql = MetaModel::MakeSelectQuery($oSearch);
- //$sHtmlSql = htmlentities($sSql, ENT_QUOTES, 'UTF-8');
- //$oPage->p($sHtmlSql);
- }
- }
- 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
- 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);
- $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',
- );
- }
- }
- 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 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-group-by-table.png',
- 'description' => 'List (Grouped by a field)',
- );
- }
- }
- class DashletHeader extends Dashlet
- {
- public function __construct($sId)
- {
- parent::__construct($sId);
- $this->aProperties['title'] = 'Hardcoded header of contacts';
- $this->aProperties['subtitle'] = 'Contacts';
- $this->aProperties['class'] = 'Contact';
- }
-
- public function Render($oPage, $bEditMode = false, $aExtraParams = array())
- {
- $sTitle = $this->aProperties['title'];
- $sSubtitle = $this->aProperties['subtitle'];
- $sClass = $this->aProperties['class'];
- $sTitleReady = str_replace(':', '_', $sTitle);
- $sSubtitleReady = str_replace(':', '_', $sSubtitle);
- $sStatusAttCode = MetaModel::GetStateAttributeCode($sClass);
- if (($sStatusAttCode == '') && MetaModel::IsValidAttCode($sClass, 'status'))
- {
- // Based on an enum
- $sStatusAttCode = 'status';
- $aStates = array_keys(MetaModel::GetAllowedValues_att($sClass, $sStatusAttCode));
- }
- else
- {
- // Based on a state variable
- $aStates = array_keys(MetaModel::EnumStates($sClass));
- }
-
- if ($sStatusAttCode == '')
- {
- // Simple stats
- $sXML = '<itopblock BlockClass="DisplayBlock" type="summary" asynchronous="false" encoding="text/oql" parameters="title[block]:'.$sTitleReady.';context_filter:1;label[block]:'.$sSubtitleReady.'">SELECT '.$sClass.'</itopblock>';
- }
- else
- {
- // Stats grouped by "status"
- $sStatusList = implode(',', $aStates);
- //$oPage->p('State: '.$sStatusAttCode.' states='.$sStatusList);
- $sXML = '<itopblock BlockClass="DisplayBlock" type="summary" asynchronous="false" encoding="text/oql" parameters="title[block]:'.$sTitleReady.';context_filter:1;label[block]:'.$sSubtitleReady.';status[block]:status;status_codes[block]:'.$sStatusList.'">SELECT '.$sClass.'</itopblock>';
- }
- $oPage->add('<div style="text-align:center" class="dashlet-content">');
- $oPage->add('<div class="main_header">');
- $aParams = array();
- $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
- $oBlock = DisplayBlock::FromTemplate($sXML);
- $oBlock->Display($oPage, $sBlockId, $aParams);
- $oPage->add('</div>');
- $oPage->add('</div>');
- }
- public function GetPropertiesFields(DesignerForm $oForm)
- {
- $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
- $oForm->AddField($oField);
- $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
- $oForm->AddField($oField);
- $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Header with stats',
- '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 style="text-align:center" class="dashlet-content">');
- $sXml = "<itopblock BlockClass=\"DisplayBlock\" type=\"actions\" asynchronous=\"false\" encoding=\"text/oql\" parameters=\"context_filter:1\">SELECT $sClass</itopblock>";
- $oBlock = DisplayBlock::FromTemplate($sXml);
- $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('class', 'Class', $this->aProperties['class']);
- $oForm->AddField($oField);
- }
-
- static public function GetInfo()
- {
- return array(
- 'label' => 'Badge',
- 'icon' => 'images/dashlet-badge.png',
- 'description' => 'Object Icon with new/search',
- );
- }
- }
|