dashlet.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. // Copyright (C) 2012 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. require_once(APPROOT.'application/forms.class.inc.php');
  17. /**
  18. * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard)
  19. *
  20. */
  21. abstract class Dashlet
  22. {
  23. protected $sId;
  24. protected $bRedrawNeeded;
  25. protected $bFormRedrawNeeded;
  26. protected $aProperties; // array of {property => value}
  27. public function __construct($sId)
  28. {
  29. $this->sId = $sId;
  30. $this->bRedrawNeeded = true; // By default: redraw each time a property changes
  31. $this->bFormRedrawNeeded = false; // By default: no need to redraw the form (independent fields)
  32. $this->aProperties = array(); // By default: there is no property
  33. }
  34. // Assuming that a property has the type of its default value, set in the constructor
  35. //
  36. public function Str2Prop($sProperty, $sValue)
  37. {
  38. $refValue = $this->aProperties[$sProperty];
  39. $sRefType = gettype($refValue);
  40. if ($sRefType == 'boolean')
  41. {
  42. $ret = ($sValue == 'true');
  43. }
  44. else
  45. {
  46. $ret = $sValue;
  47. settype($ret, $sRefType);
  48. }
  49. return $ret;
  50. }
  51. public function Prop2Str($value)
  52. {
  53. if (gettype($value) == 'boolean')
  54. {
  55. $sRet = $value ? 'true' : 'false';
  56. }
  57. else
  58. {
  59. $sRet = (string) $value;
  60. }
  61. return $sRet;
  62. }
  63. public function FromDOMNode($oDOMNode)
  64. {
  65. foreach ($this->aProperties as $sProperty => $value)
  66. {
  67. $this->oDOMNode = $oDOMNode->getElementsByTagName($sProperty)->item(0);
  68. if ($this->oDOMNode != null)
  69. {
  70. $newvalue = $this->Str2Prop($sProperty, $this->oDOMNode->textContent);
  71. $this->aProperties[$sProperty] = $newvalue;
  72. }
  73. }
  74. }
  75. public function ToDOMNode($oDOMNode)
  76. {
  77. foreach ($this->aProperties as $sProperty => $value)
  78. {
  79. $sXmlValue = $this->Prop2Str($value);
  80. $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty, $sXmlValue);
  81. $oDOMNode->appendChild($oPropNode);
  82. }
  83. }
  84. public function FromXml($sXml)
  85. {
  86. $oDomDoc = new DOMDocument('1.0', 'UTF-8');
  87. $oDomDoc->loadXml($sXml);
  88. $this->FromDOMNode($oDomDoc->firstChild);
  89. }
  90. public function FromParams($aParams)
  91. {
  92. foreach ($this->aProperties as $sProperty => $value)
  93. {
  94. if (array_key_exists($sProperty, $aParams))
  95. {
  96. $this->aProperties[$sProperty] = $aParams[$sProperty];
  97. }
  98. }
  99. }
  100. public function DoRender($oPage, $bEditMode = false, $aExtraParams = array())
  101. {
  102. if ($bEditMode)
  103. {
  104. $sId = $this->GetID();
  105. $oPage->add('<div class="dashlet" id="dashlet_'.$sId.'">');
  106. }
  107. else
  108. {
  109. $oPage->add('<div class="dashlet">');
  110. }
  111. $this->Render($oPage, $bEditMode, $aExtraParams);
  112. $oPage->add('</div>');
  113. if ($bEditMode)
  114. {
  115. $sClass = get_class($this);
  116. $oPage->add_ready_script(
  117. <<<EOF
  118. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  119. EOF
  120. );
  121. }
  122. }
  123. public function GetID()
  124. {
  125. return $this->sId;
  126. }
  127. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  128. abstract public function GetPropertiesFields(DesignerForm $oForm);
  129. public function ToXml(DOMNode $oContainerNode)
  130. {
  131. }
  132. public function Update($aValues, $aUpdatedFields)
  133. {
  134. foreach($aUpdatedFields as $sProp)
  135. {
  136. if (array_key_exists($sProp, $this->aProperties))
  137. {
  138. $this->aProperties[$sProp] = $aValues[$sProp];
  139. }
  140. }
  141. return $this;
  142. }
  143. public function IsRedrawNeeded()
  144. {
  145. return $this->bRedrawNeeded;
  146. }
  147. public function IsFormRedrawNeeded()
  148. {
  149. return $this->bFormRedrawNeeded;
  150. }
  151. static public function GetInfo()
  152. {
  153. return array(
  154. 'label' => '',
  155. 'icon' => '',
  156. 'description' => '',
  157. );
  158. }
  159. public function GetForm()
  160. {
  161. $oForm = new DesignerForm();
  162. $oForm->SetPrefix("dashlet_". $this->GetID());
  163. $oForm->SetParamsContainer('params');
  164. $this->GetPropertiesFields($oForm);
  165. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  166. $oForm->AddField($oDashletClassField);
  167. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  168. $oForm->AddField($oDashletIdField);
  169. return $oForm;
  170. }
  171. static public function IsVisible()
  172. {
  173. return true;
  174. }
  175. }
  176. class DashletEmptyCell extends Dashlet
  177. {
  178. public function __construct($sId)
  179. {
  180. parent::__construct($sId);
  181. }
  182. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  183. {
  184. $oPage->add('&nbsp;');
  185. }
  186. public function GetPropertiesFields(DesignerForm $oForm)
  187. {
  188. }
  189. static public function GetInfo()
  190. {
  191. return array(
  192. 'label' => 'Empty Cell',
  193. 'icon' => 'images/dashlet-text.png',
  194. 'description' => 'Empty Cell Dashlet Placeholder',
  195. );
  196. }
  197. static public function IsVisible()
  198. {
  199. return false;
  200. }
  201. }
  202. class DashletHelloWorld extends Dashlet
  203. {
  204. public function __construct($sId)
  205. {
  206. parent::__construct($sId);
  207. $this->aProperties['text'] = 'Hello World';
  208. }
  209. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  210. {
  211. $oPage->add('<div style="text-align:center; line-height:5em" class="dashlet-content"><span>'.$this->aProperties['text'].'</span></div>');
  212. }
  213. public function GetPropertiesFields(DesignerForm $oForm)
  214. {
  215. $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
  216. $oForm->AddField($oField);
  217. }
  218. static public function GetInfo()
  219. {
  220. return array(
  221. 'label' => 'Hello World',
  222. 'icon' => 'images/dashlet-text.png',
  223. 'description' => 'Hello World test Dashlet',
  224. );
  225. }
  226. }
  227. class DashletObjectList extends Dashlet
  228. {
  229. public function __construct($sId)
  230. {
  231. parent::__construct($sId);
  232. $this->aProperties['title'] = 'Hardcoded list of "my requests"';
  233. $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
  234. $this->aProperties['menu'] = false;
  235. }
  236. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  237. {
  238. $sTitle = $this->aProperties['title'];
  239. $sQuery = $this->aProperties['query'];
  240. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  241. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  242. // C'est quoi ce paramètre "menu" ?
  243. $sXML = '<itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql" parameters="menu:'.$sShowMenu.'">'.$sQuery.'</itopblock>';
  244. $aParams = array();
  245. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  246. $oBlock = DisplayBlock::FromTemplate($sXML);
  247. $oBlock->Display($oPage, $sBlockId, $aParams);
  248. $oPage->add('</div>');
  249. }
  250. public function GetPropertiesFields(DesignerForm $oForm)
  251. {
  252. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  253. $oForm->AddField($oField);
  254. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  255. $oForm->AddField($oField);
  256. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  257. $oForm->AddField($oField);
  258. }
  259. static public function GetInfo()
  260. {
  261. return array(
  262. 'label' => 'Object list',
  263. 'icon' => 'images/dashlet-object-list.png',
  264. 'description' => 'Object list dashlet',
  265. );
  266. }
  267. }
  268. abstract class DashletGroupBy extends Dashlet
  269. {
  270. public function __construct($sId)
  271. {
  272. parent::__construct($sId);
  273. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  274. $this->aProperties['query'] = 'SELECT Contact';
  275. $this->aProperties['group_by'] = 'location_name';
  276. $this->aProperties['style'] = 'table';
  277. }
  278. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  279. {
  280. $sTitle = $this->aProperties['title'];
  281. $sQuery = $this->aProperties['query'];
  282. $sGroupBy = $this->aProperties['group_by'];
  283. $sStyle = $this->aProperties['style'];
  284. if ($sQuery == '')
  285. {
  286. $oPage->add('<p>Please enter a valid OQL query</p>');
  287. }
  288. elseif ($sGroupBy == '')
  289. {
  290. $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
  291. }
  292. else
  293. {
  294. switch($sStyle)
  295. {
  296. case 'bars':
  297. $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>';
  298. $sHtmlTitle = ''; // done in the itop block
  299. break;
  300. case 'pie':
  301. $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>';
  302. $sHtmlTitle = ''; // done in the itop block
  303. break;
  304. case 'table':
  305. default:
  306. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  307. $sXML = '<itopblock BlockClass="DisplayBlock" type="count" parameters="group_by:'.$sGroupBy.'" asynchronous="false" encoding="text/oql">'.$sQuery.'</itopblock>';
  308. break;
  309. }
  310. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  311. if ($sHtmlTitle != '')
  312. {
  313. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  314. }
  315. $aParams = array();
  316. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  317. $oBlock = DisplayBlock::FromTemplate($sXML);
  318. $oBlock->Display($oPage, $sBlockId, $aParams);
  319. $oPage->add('</div>');
  320. // TEST Group By as SQL!
  321. //$oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  322. //$sSql = MetaModel::MakeSelectQuery($oSearch);
  323. //$sHtmlSql = htmlentities($sSql, ENT_QUOTES, 'UTF-8');
  324. //$oPage->p($sHtmlSql);
  325. }
  326. }
  327. public function GetPropertiesFields(DesignerForm $oForm)
  328. {
  329. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  330. $oForm->AddField($oField);
  331. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  332. $oForm->AddField($oField);
  333. // Group by field: build the list of possible values (attribute codes + ...)
  334. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  335. $sClass = $oSearch->GetClass();
  336. $aGroupBy = array();
  337. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  338. {
  339. if (!$oAttDef->IsScalar()) continue; // skip link sets
  340. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
  341. $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
  342. if ($oAttDef instanceof AttributeDateTime)
  343. {
  344. //date_format(start_date, '%d')
  345. $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
  346. }
  347. }
  348. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  349. $oField->SetAllowedValues($aGroupBy);
  350. $oForm->AddField($oField);
  351. $aStyles = array(
  352. 'pie' => 'Pie chart',
  353. 'bars' => 'Bar chart',
  354. 'table' => 'Table',
  355. );
  356. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  357. $oField->SetAllowedValues($aStyles);
  358. $oForm->AddField($oField);
  359. }
  360. public function Update($aValues, $aUpdatedFields)
  361. {
  362. if (in_array('query', $aUpdatedFields))
  363. {
  364. $sCurrQuery = $aValues['query'];
  365. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  366. $sCurrClass = $oCurrSearch->GetClass();
  367. $sPrevQuery = $this->aProperties['query'];
  368. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  369. $sPrevClass = $oPrevSearch->GetClass();
  370. if ($sCurrClass != $sPrevClass)
  371. {
  372. $this->bFormRedrawNeeded = true;
  373. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  374. $this->aProperties['group_by'] = '';
  375. }
  376. }
  377. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  378. if (in_array('style', $aUpdatedFields))
  379. {
  380. switch($aValues['style'])
  381. {
  382. // Style changed, mutate to the specified type of chart
  383. case 'pie':
  384. $oDashlet = new DashletGroupByPie($this->sId);
  385. break;
  386. case 'bars':
  387. $oDashlet = new DashletGroupByBars($this->sId);
  388. break;
  389. case 'table':
  390. $oDashlet = new DashletGroupByTable($this->sId);
  391. break;
  392. }
  393. $oDashlet->FromParams($aValues);
  394. $oDashlet->bRedrawNeeded = true;
  395. $oDashlet->bFormRedrawNeeded = true;
  396. }
  397. return $oDashlet;
  398. }
  399. static public function GetInfo()
  400. {
  401. return array(
  402. 'label' => 'Objects grouped by...',
  403. 'icon' => 'images/dashlet-object-grouped.png',
  404. 'description' => 'Grouped objects dashlet',
  405. );
  406. }
  407. }
  408. class DashletGroupByPie extends DashletGroupBy
  409. {
  410. public function __construct($sId)
  411. {
  412. parent::__construct($sId);
  413. $this->aProperties['style'] = 'pie';
  414. }
  415. static public function GetInfo()
  416. {
  417. return array(
  418. 'label' => 'Pie Chart',
  419. 'icon' => 'images/dashlet-pie-chart.png',
  420. 'description' => 'Pie Chart',
  421. );
  422. }
  423. }
  424. class DashletGroupByBars extends DashletGroupBy
  425. {
  426. public function __construct($sId)
  427. {
  428. parent::__construct($sId);
  429. $this->aProperties['style'] = 'bars';
  430. }
  431. static public function GetInfo()
  432. {
  433. return array(
  434. 'label' => 'Bar Chart',
  435. 'icon' => 'images/dashlet-bar-chart.png',
  436. 'description' => 'Bar Chart',
  437. );
  438. }
  439. }
  440. class DashletGroupByTable extends DashletGroupBy
  441. {
  442. public function __construct($sId)
  443. {
  444. parent::__construct($sId);
  445. $this->aProperties['style'] = 'table';
  446. }
  447. static public function GetInfo()
  448. {
  449. return array(
  450. 'label' => 'Group By (table)',
  451. 'icon' => 'images/dashlet-group-by-table.png',
  452. 'description' => 'List (Grouped by a field)',
  453. );
  454. }
  455. }
  456. class DashletHeader extends Dashlet
  457. {
  458. public function __construct($sId)
  459. {
  460. parent::__construct($sId);
  461. $this->aProperties['title'] = 'Hardcoded header of contacts';
  462. $this->aProperties['subtitle'] = 'Contacts';
  463. $this->aProperties['class'] = 'Contact';
  464. }
  465. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  466. {
  467. $sTitle = $this->aProperties['title'];
  468. $sSubtitle = $this->aProperties['subtitle'];
  469. $sClass = $this->aProperties['class'];
  470. $sTitleReady = str_replace(':', '_', $sTitle);
  471. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  472. $sStatusAttCode = MetaModel::GetStateAttributeCode($sClass);
  473. if (($sStatusAttCode == '') && MetaModel::IsValidAttCode($sClass, 'status'))
  474. {
  475. // Based on an enum
  476. $sStatusAttCode = 'status';
  477. $aStates = array_keys(MetaModel::GetAllowedValues_att($sClass, $sStatusAttCode));
  478. }
  479. else
  480. {
  481. // Based on a state variable
  482. $aStates = array_keys(MetaModel::EnumStates($sClass));
  483. }
  484. if ($sStatusAttCode == '')
  485. {
  486. // Simple stats
  487. $sXML = '<itopblock BlockClass="DisplayBlock" type="summary" asynchronous="false" encoding="text/oql" parameters="title[block]:'.$sTitleReady.';context_filter:1;label[block]:'.$sSubtitleReady.'">SELECT '.$sClass.'</itopblock>';
  488. }
  489. else
  490. {
  491. // Stats grouped by "status"
  492. $sStatusList = implode(',', $aStates);
  493. //$oPage->p('State: '.$sStatusAttCode.' states='.$sStatusList);
  494. $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>';
  495. }
  496. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  497. $aParams = array();
  498. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  499. $oBlock = DisplayBlock::FromTemplate($sXML);
  500. $oBlock->Display($oPage, $sBlockId, $aParams);
  501. $oPage->add('</div>');
  502. }
  503. public function GetPropertiesFields(DesignerForm $oForm)
  504. {
  505. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  506. $oForm->AddField($oField);
  507. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  508. $oForm->AddField($oField);
  509. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  510. $oForm->AddField($oField);
  511. }
  512. static public function GetInfo()
  513. {
  514. return array(
  515. 'label' => 'Header with stats',
  516. 'icon' => 'images/dashlet-header-stats.png',
  517. 'description' => 'Header with stats (grouped by...)',
  518. );
  519. }
  520. }