dashlet.class.inc.php 15 KB

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