dashlet.class.inc.php 16 KB

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