dashlet.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. }
  142. public function IsRedrawNeeded()
  143. {
  144. return $this->bRedrawNeeded;
  145. }
  146. public function IsFormRedrawNeeded()
  147. {
  148. return $this->bFormRedrawNeeded;
  149. }
  150. static public function GetInfo()
  151. {
  152. return array(
  153. 'label' => '',
  154. 'icon' => '',
  155. 'description' => '',
  156. );
  157. }
  158. public function GetForm()
  159. {
  160. $oForm = new DesignerForm();
  161. $oForm->SetPrefix("dashlet_". $this->GetID());
  162. $oForm->SetParamsContainer('params');
  163. $this->GetPropertiesFields($oForm);
  164. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  165. $oForm->AddField($oDashletClassField);
  166. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  167. $oForm->AddField($oDashletIdField);
  168. return $oForm;
  169. }
  170. static public function IsVisible()
  171. {
  172. return true;
  173. }
  174. }
  175. class DashletEmptyCell extends Dashlet
  176. {
  177. public function __construct($sId)
  178. {
  179. parent::__construct($sId);
  180. }
  181. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  182. {
  183. $oPage->add('&nbsp;');
  184. }
  185. public function GetPropertiesFields(DesignerForm $oForm)
  186. {
  187. }
  188. static public function GetInfo()
  189. {
  190. return array(
  191. 'label' => 'Empty Cell',
  192. 'icon' => 'images/dashlet-text.png',
  193. 'description' => 'Empty Cell Dashlet Placeholder',
  194. );
  195. }
  196. static public function IsVisible()
  197. {
  198. return false;
  199. }
  200. }
  201. class DashletHelloWorld extends Dashlet
  202. {
  203. public function __construct($sId)
  204. {
  205. parent::__construct($sId);
  206. $this->aProperties['text'] = 'Hello World';
  207. }
  208. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  209. {
  210. $oPage->add('<div style="text-align:center; line-height:5em" class="dashlet-content"><span>'.$this->aProperties['text'].'</span></div>');
  211. }
  212. public function GetPropertiesFields(DesignerForm $oForm)
  213. {
  214. $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
  215. $oForm->AddField($oField);
  216. }
  217. static public function GetInfo()
  218. {
  219. return array(
  220. 'label' => 'Hello World',
  221. 'icon' => 'images/dashlet-text.png',
  222. 'description' => 'Hello World test Dashlet',
  223. );
  224. }
  225. }
  226. class DashletFakeBarChart extends Dashlet
  227. {
  228. public function __construct($sId)
  229. {
  230. parent::__construct($sId);
  231. }
  232. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  233. {
  234. $oPage->add('<div style="text-align:center" class="dashlet-content"><div>Fake Bar Chart</div><divp><img src="../images/fake-bar-chart.png"/></div></div>');
  235. }
  236. public function GetPropertiesFields(DesignerForm $oForm, $oDashlet = null)
  237. {
  238. }
  239. public function ToXml(DOMNode $oContainerNode)
  240. {
  241. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('fake_bar_chart', 'test');
  242. $oContainerNode->appendChild($oNewNodeNode);
  243. }
  244. static public function GetInfo()
  245. {
  246. return array(
  247. 'label' => 'Bar Chart',
  248. 'icon' => 'images/dashlet-bar-chart.png',
  249. 'description' => 'Fake Bar Chart (for testing)',
  250. );
  251. }
  252. }
  253. class DashletFakePieChart extends Dashlet
  254. {
  255. public function __construct($sId)
  256. {
  257. parent::__construct($sId);
  258. }
  259. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  260. {
  261. $oPage->add('<div style="text-align:center" class="dashlet-content"><div>Fake Pie Chart</div><div><img src="../images/fake-pie-chart.png"/></div></div>');
  262. }
  263. public function GetPropertiesFields(DesignerForm $oForm, $oDashlet = null)
  264. {
  265. }
  266. public function ToXml(DOMNode $oContainerNode)
  267. {
  268. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('fake_pie_chart', 'test');
  269. $oContainerNode->appendChild($oNewNodeNode);
  270. }
  271. static public function GetInfo()
  272. {
  273. return array(
  274. 'label' => 'Pie Chart',
  275. 'icon' => 'images/dashlet-pie-chart.png',
  276. 'description' => 'Fake Pie Chart (for testing)',
  277. );
  278. }
  279. }
  280. class DashletObjectList extends Dashlet
  281. {
  282. public function __construct($sId)
  283. {
  284. parent::__construct($sId);
  285. $this->aProperties['title'] = 'Hardcoded list of "my requests"';
  286. $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
  287. $this->aProperties['menu'] = false;
  288. }
  289. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  290. {
  291. $sTitle = $this->aProperties['title'];
  292. $sQuery = $this->aProperties['query'];
  293. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  294. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  295. // C'est quoi ce paramètre "menu" ?
  296. $sXML = '<itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql" parameters="menu:'.$sShowMenu.'">'.$sQuery.'</itopblock>';
  297. $aParams = array();
  298. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  299. $oBlock = DisplayBlock::FromTemplate($sXML);
  300. $oBlock->Display($oPage, $sBlockId, $aParams);
  301. $oPage->add('</div>');
  302. }
  303. public function GetPropertiesFields(DesignerForm $oForm)
  304. {
  305. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  306. $oForm->AddField($oField);
  307. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  308. $oForm->AddField($oField);
  309. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  310. $oForm->AddField($oField);
  311. }
  312. static public function GetInfo()
  313. {
  314. return array(
  315. 'label' => 'Object list',
  316. 'icon' => 'images/dashlet-object-list.png',
  317. 'description' => 'Object list dashlet',
  318. );
  319. }
  320. }
  321. class DashletGroupBy extends Dashlet
  322. {
  323. public function __construct($sId)
  324. {
  325. parent::__construct($sId);
  326. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  327. $this->aProperties['query'] = 'SELECT Contact';
  328. $this->aProperties['group_by'] = 'location_name';
  329. $this->aProperties['style'] = 'table';
  330. }
  331. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  332. {
  333. $sTitle = $this->aProperties['title'];
  334. $sQuery = $this->aProperties['query'];
  335. $sGroupBy = $this->aProperties['group_by'];
  336. $sStyle = $this->aProperties['style'];
  337. if ($sQuery == '')
  338. {
  339. $oPage->add('<p>Please enter a valid OQL query</p>');
  340. }
  341. elseif ($sGroupBy == '')
  342. {
  343. $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
  344. }
  345. else
  346. {
  347. switch($sStyle)
  348. {
  349. case 'bars':
  350. $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>';
  351. $sHtmlTitle = ''; // done in the itop block
  352. break;
  353. case 'pie':
  354. $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>';
  355. $sHtmlTitle = ''; // done in the itop block
  356. break;
  357. case 'table':
  358. default:
  359. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  360. $sXML = '<itopblock BlockClass="DisplayBlock" type="count" parameters="group_by:'.$sGroupBy.'" asynchronous="false" encoding="text/oql">'.$sQuery.'</itopblock>';
  361. break;
  362. }
  363. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  364. if ($sHtmlTitle != '')
  365. {
  366. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  367. }
  368. $aParams = array();
  369. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  370. $oBlock = DisplayBlock::FromTemplate($sXML);
  371. $oBlock->Display($oPage, $sBlockId, $aParams);
  372. $oPage->add('</div>');
  373. // TEST Group By as SQL!
  374. //$oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  375. //$sSql = MetaModel::MakeSelectQuery($oSearch);
  376. //$sHtmlSql = htmlentities($sSql, ENT_QUOTES, 'UTF-8');
  377. //$oPage->p($sHtmlSql);
  378. }
  379. }
  380. public function GetPropertiesFields(DesignerForm $oForm)
  381. {
  382. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  383. $oForm->AddField($oField);
  384. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  385. $oForm->AddField($oField);
  386. // Group by field: build the list of possible values (attribute codes + ...)
  387. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  388. $sClass = $oSearch->GetClass();
  389. $aGroupBy = array();
  390. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  391. {
  392. if (!$oAttDef->IsScalar()) continue; // skip link sets
  393. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
  394. $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
  395. if ($oAttDef instanceof AttributeDateTime)
  396. {
  397. //date_format(start_date, '%d')
  398. $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
  399. }
  400. }
  401. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  402. $oField->SetAllowedValues($aGroupBy);
  403. $oForm->AddField($oField);
  404. $aStyles = array(
  405. 'pie' => 'Pie chart',
  406. 'bars' => 'Bar chart',
  407. 'table' => 'Table',
  408. );
  409. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  410. $oField->SetAllowedValues($aStyles);
  411. $oForm->AddField($oField);
  412. }
  413. public function Update($aValues, $aUpdatedFields)
  414. {
  415. if (in_array('query', $aUpdatedFields))
  416. {
  417. $sCurrQuery = $aValues['query'];
  418. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  419. $sCurrClass = $oCurrSearch->GetClass();
  420. $sPrevQuery = $this->aProperties['query'];
  421. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  422. $sPrevClass = $oPrevSearch->GetClass();
  423. if ($sCurrClass != $sPrevClass)
  424. {
  425. $this->bFormRedrawNeeded = true;
  426. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  427. $this->aProperties['group_by'] = '';
  428. }
  429. }
  430. parent::Update($aValues, $aUpdatedFields);
  431. }
  432. static public function GetInfo()
  433. {
  434. return array(
  435. 'label' => 'Objects grouped by...',
  436. 'icon' => 'images/dashlet-object-grouped.png',
  437. 'description' => 'Grouped objects dashlet',
  438. );
  439. }
  440. }
  441. class DashletHeader extends Dashlet
  442. {
  443. public function __construct($sId)
  444. {
  445. parent::__construct($sId);
  446. $this->aProperties['title'] = 'Hardcoded header of contacts';
  447. $this->aProperties['subtitle'] = 'Contacts';
  448. $this->aProperties['class'] = 'Contact';
  449. }
  450. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  451. {
  452. $sTitle = $this->aProperties['title'];
  453. $sSubtitle = $this->aProperties['subtitle'];
  454. $sClass = $this->aProperties['class'];
  455. $sTitleReady = str_replace(':', '_', $sTitle);
  456. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  457. $sStatusAttCode = MetaModel::GetStateAttributeCode($sClass);
  458. if (($sStatusAttCode == '') && MetaModel::IsValidAttCode($sClass, 'status'))
  459. {
  460. // Based on an enum
  461. $sStatusAttCode = 'status';
  462. $aStates = array_keys(MetaModel::GetAllowedValues_att($sClass, $sStatusAttCode));
  463. }
  464. else
  465. {
  466. // Based on a state variable
  467. $aStates = array_keys(MetaModel::EnumStates($sClass));
  468. }
  469. if ($sStatusAttCode == '')
  470. {
  471. // Simple stats
  472. $sXML = '<itopblock BlockClass="DisplayBlock" type="summary" asynchronous="false" encoding="text/oql" parameters="title[block]:'.$sTitleReady.';context_filter:1;label[block]:'.$sSubtitleReady.'">SELECT '.$sClass.'</itopblock>';
  473. }
  474. else
  475. {
  476. // Stats grouped by "status"
  477. $sStatusList = implode(',', $aStates);
  478. //$oPage->p('State: '.$sStatusAttCode.' states='.$sStatusList);
  479. $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>';
  480. }
  481. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  482. $aParams = array();
  483. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  484. $oBlock = DisplayBlock::FromTemplate($sXML);
  485. $oBlock->Display($oPage, $sBlockId, $aParams);
  486. $oPage->add('</div>');
  487. }
  488. public function GetPropertiesFields(DesignerForm $oForm)
  489. {
  490. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  491. $oForm->AddField($oField);
  492. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  493. $oForm->AddField($oField);
  494. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  495. $oForm->AddField($oField);
  496. }
  497. static public function GetInfo()
  498. {
  499. return array(
  500. 'label' => 'Header with stats',
  501. 'icon' => 'images/dashlet-header-stats.png',
  502. 'description' => 'Header with stats (grouped by...)',
  503. );
  504. }
  505. }