dashlet.class.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. public function FromDOMNode($oDOMNode)
  35. {
  36. }
  37. public function FromXml($sXml)
  38. {
  39. }
  40. public function FromParams($aParams)
  41. {
  42. foreach ($this->aProperties as $sProperty => $value)
  43. {
  44. if (array_key_exists($sProperty, $aParams))
  45. {
  46. $this->aProperties[$sProperty] = $aParams[$sProperty];
  47. }
  48. }
  49. }
  50. public function DoRender($oPage, $bEditMode = false, $aExtraParams = array())
  51. {
  52. if ($bEditMode)
  53. {
  54. $sId = $this->GetID();
  55. $oPage->add('<div class="dashlet" id="dashlet_'.$sId.'">');
  56. }
  57. else
  58. {
  59. $oPage->add('<div class="dashlet">');
  60. }
  61. $this->Render($oPage, $bEditMode, $aExtraParams);
  62. $oPage->add('</div>');
  63. if ($bEditMode)
  64. {
  65. $sClass = get_class($this);
  66. $oPage->add_ready_script(
  67. <<<EOF
  68. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  69. EOF
  70. );
  71. }
  72. }
  73. public function GetID()
  74. {
  75. return $this->sId;
  76. }
  77. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  78. abstract public function GetPropertiesFields(DesignerForm $oForm);
  79. public function ToXml(DOMNode $oContainerNode)
  80. {
  81. }
  82. public function Update($aValues, $aUpdatedFields)
  83. {
  84. foreach($aUpdatedFields as $sProp)
  85. {
  86. if (array_key_exists($sProp, $this->aProperties))
  87. {
  88. $this->aProperties[$sProp] = $aValues[$sProp];
  89. }
  90. }
  91. }
  92. public function IsRedrawNeeded()
  93. {
  94. return $this->bRedrawNeeded;
  95. }
  96. public function IsFormRedrawNeeded()
  97. {
  98. return $this->bFormRedrawNeeded;
  99. }
  100. static public function GetInfo()
  101. {
  102. return array(
  103. 'label' => '',
  104. 'icon' => '',
  105. 'description' => '',
  106. );
  107. }
  108. public function GetForm()
  109. {
  110. $oForm = new DesignerForm();
  111. $oForm->SetPrefix("dashlet_". $this->GetID());
  112. $oForm->SetParamsContainer('params');
  113. $this->GetPropertiesFields($oForm);
  114. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  115. $oForm->AddField($oDashletClassField);
  116. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  117. $oForm->AddField($oDashletIdField);
  118. return $oForm;
  119. }
  120. }
  121. class DashletHelloWorld extends Dashlet
  122. {
  123. public function __construct($sId)
  124. {
  125. parent::__construct($sId);
  126. $this->aProperties['text'] = 'Hello World';
  127. }
  128. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  129. {
  130. $oPage->add('<div style="text-align:center; line-height:5em" class="dashlet-content"><span>'.$this->aProperties['text'].'</span></div>');
  131. }
  132. public function GetPropertiesFields(DesignerForm $oForm)
  133. {
  134. $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
  135. $oForm->AddField($oField);
  136. }
  137. static public function GetInfo()
  138. {
  139. return array(
  140. 'label' => 'Hello World',
  141. 'icon' => 'images/dashlet-text.png',
  142. 'description' => 'Hello World test Dashlet',
  143. );
  144. }
  145. }
  146. class DashletFakeBarChart extends Dashlet
  147. {
  148. public function __construct($sId)
  149. {
  150. parent::__construct($sId);
  151. }
  152. public function FromDOMNode($oDOMNode)
  153. {
  154. }
  155. public function FromXml($sXml)
  156. {
  157. }
  158. public function FromParams($aParams)
  159. {
  160. }
  161. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  162. {
  163. $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>');
  164. }
  165. public function GetPropertiesFields(DesignerForm $oForm, $oDashlet = null)
  166. {
  167. }
  168. public function ToXml(DOMNode $oContainerNode)
  169. {
  170. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('fake_bar_chart', 'test');
  171. $oContainerNode->appendChild($oNewNodeNode);
  172. }
  173. static public function GetInfo()
  174. {
  175. return array(
  176. 'label' => 'Bar Chart',
  177. 'icon' => 'images/dashlet-bar-chart.png',
  178. 'description' => 'Fake Bar Chart (for testing)',
  179. );
  180. }
  181. }
  182. class DashletFakePieChart extends Dashlet
  183. {
  184. public function __construct($sId)
  185. {
  186. parent::__construct($sId);
  187. }
  188. public function FromDOMNode($oDOMNode)
  189. {
  190. }
  191. public function FromXml($sXml)
  192. {
  193. }
  194. public function FromParams($aParams)
  195. {
  196. }
  197. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  198. {
  199. $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>');
  200. }
  201. public function GetPropertiesFields(DesignerForm $oForm, $oDashlet = null)
  202. {
  203. }
  204. public function ToXml(DOMNode $oContainerNode)
  205. {
  206. $oNewNodeNode = $oContainerNode->ownerDocument->createElement('fake_pie_chart', 'test');
  207. $oContainerNode->appendChild($oNewNodeNode);
  208. }
  209. static public function GetInfo()
  210. {
  211. return array(
  212. 'label' => 'Pie Chart',
  213. 'icon' => 'images/dashlet-pie-chart.png',
  214. 'description' => 'Fake Pie Chart (for testing)',
  215. );
  216. }
  217. }
  218. class DashletObjectList extends Dashlet
  219. {
  220. public function __construct($sId)
  221. {
  222. parent::__construct($sId);
  223. $this->aProperties['title'] = 'Hardcoded list of "my requests"';
  224. $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
  225. }
  226. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  227. {
  228. $sTitle = $this->aProperties['title'];
  229. $sQuery = $this->aProperties['query'];
  230. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  231. // C'est quoi ce paramètre "menu" ?
  232. $sXML = '<itopblock BlockClass="DisplayBlock" type="list" asynchronous="false" encoding="text/oql" parameters="menu:1">'.$sQuery.'</itopblock>';
  233. $aParams = array();
  234. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  235. $oBlock = DisplayBlock::FromTemplate($sXML);
  236. $oBlock->Display($oPage, $sBlockId, $aParams);
  237. $oPage->add('</div>');
  238. }
  239. public function GetPropertiesFields(DesignerForm $oForm)
  240. {
  241. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  242. $oForm->AddField($oField);
  243. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  244. $oForm->AddField($oField);
  245. }
  246. static public function GetInfo()
  247. {
  248. return array(
  249. 'label' => 'Object list',
  250. 'icon' => 'images/dashlet-object-list.png',
  251. 'description' => 'Object list dashlet',
  252. );
  253. }
  254. }
  255. class DashletGroupBy extends Dashlet
  256. {
  257. public function __construct($sId)
  258. {
  259. parent::__construct($sId);
  260. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  261. $this->aProperties['query'] = 'SELECT Contact';
  262. $this->aProperties['group_by'] = 'location_name';
  263. $this->aProperties['style'] = 'pie';
  264. }
  265. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  266. {
  267. $sTitle = $this->aProperties['title'];
  268. $sQuery = $this->aProperties['query'];
  269. $sGroupBy = $this->aProperties['group_by'];
  270. $sStyle = $this->aProperties['style'];
  271. switch($sStyle)
  272. {
  273. case 'bars':
  274. $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>';
  275. $sHtmlTitle = ''; // done in the itop block
  276. break;
  277. case 'pie':
  278. $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>';
  279. $sHtmlTitle = ''; // done in the itop block
  280. break;
  281. case 'table':
  282. default:
  283. $sHtmlTitle = htmlentities($sTitle, ENT_QUOTES, 'UTF-8'); // done in the itop block
  284. $sXML = '<itopblock BlockClass="DisplayBlock" type="count" parameters="group_by:'.$sGroupBy.'" asynchronous="false" encoding="text/oql">'.$sQuery.'</itopblock>';
  285. break;
  286. }
  287. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  288. if ($sHtmlTitle != '')
  289. {
  290. $oPage->add('<div>'.$sHtmlTitle.'</div>');
  291. }
  292. $aParams = array();
  293. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  294. $oBlock = DisplayBlock::FromTemplate($sXML);
  295. $oBlock->Display($oPage, $sBlockId, $aParams);
  296. $oPage->add('</div>');
  297. }
  298. public function GetPropertiesFields(DesignerForm $oForm)
  299. {
  300. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  301. $oForm->AddField($oField);
  302. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  303. $oForm->AddField($oField);
  304. $oField = new DesignerTextField('group_by', 'Group by', $this->aProperties['group_by']);
  305. $oForm->AddField($oField);
  306. $aStyles = array(
  307. 'pie' => 'Pie chart',
  308. 'bars' => 'Bar chart',
  309. 'table' => 'Table',
  310. );
  311. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  312. $oField->SetAllowedValues($aStyles);
  313. $oForm->AddField($oField);
  314. }
  315. static public function GetInfo()
  316. {
  317. return array(
  318. 'label' => 'Objects grouped by...',
  319. 'icon' => 'images/dashlet-object-grouped.png',
  320. 'description' => 'Grouped objects dashlet',
  321. );
  322. }
  323. }
  324. class DashletHeader extends Dashlet
  325. {
  326. public function __construct($sId)
  327. {
  328. parent::__construct($sId);
  329. $this->aProperties['title'] = 'Hardcoded header of contacts';
  330. $this->aProperties['subtitle'] = 'Contacts';
  331. $this->aProperties['class'] = 'Contact';
  332. }
  333. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  334. {
  335. $sTitle = $this->aProperties['title'];
  336. $sSubtitle = $this->aProperties['subtitle'];
  337. $sClass = $this->aProperties['class'];
  338. $sTitleReady = str_replace(':', '_', $sTitle);
  339. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  340. $sStatusAttCode = MetaModel::GetStateAttributeCode($sClass);
  341. if (($sStatusAttCode == '') && MetaModel::IsValidAttCode($sClass, 'status'))
  342. {
  343. // Based on an enum
  344. $sStatusAttCode = 'status';
  345. $aStates = array_keys(MetaModel::GetAllowedValues_att($sClass, $sStatusAttCode));
  346. }
  347. else
  348. {
  349. // Based on a state variable
  350. $aStates = array_keys(MetaModel::EnumStates($sClass));
  351. }
  352. if ($sStatusAttCode == '')
  353. {
  354. // Simple stats
  355. $sXML = '<itopblock BlockClass="DisplayBlock" type="summary" asynchronous="false" encoding="text/oql" parameters="title[block]:'.$sTitleReady.';context_filter:1;label[block]:'.$sSubtitleReady.'">SELECT '.$sClass.'</itopblock>';
  356. }
  357. else
  358. {
  359. // Stats grouped by "status"
  360. $sStatusList = implode(',', $aStates);
  361. //$oPage->p('State: '.$sStatusAttCode.' states='.$sStatusList);
  362. $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>';
  363. }
  364. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  365. $aParams = array();
  366. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  367. $oBlock = DisplayBlock::FromTemplate($sXML);
  368. $oBlock->Display($oPage, $sBlockId, $aParams);
  369. $oPage->add('</div>');
  370. }
  371. public function GetPropertiesFields(DesignerForm $oForm)
  372. {
  373. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  374. $oForm->AddField($oField);
  375. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  376. $oForm->AddField($oField);
  377. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  378. $oForm->AddField($oField);
  379. }
  380. static public function GetInfo()
  381. {
  382. return array(
  383. 'label' => 'Header with stats',
  384. 'icon' => 'images/dashlet-header-stats.png',
  385. 'description' => 'Header with stats (grouped by...)',
  386. );
  387. }
  388. }