dashlet.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 $aCSSClasses;
  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->aCSSClasses = array('dashlet');
  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 = $oDOMNode->ownerDocument->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. $this->aProperties[$sProperty] = $aParams[$sProperty];
  99. }
  100. }
  101. }
  102. public function DoRender($oPage, $bEditMode = false, $aExtraParams = array())
  103. {
  104. $sCSSClasses = implode(' ', $this->aCSSClasses);
  105. if ($bEditMode)
  106. {
  107. $sId = $this->GetID();
  108. $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
  109. }
  110. else
  111. {
  112. $oPage->add('<div class="'.$sCSSClasses.'">');
  113. }
  114. $this->Render($oPage, $bEditMode, $aExtraParams);
  115. $oPage->add('</div>');
  116. if ($bEditMode)
  117. {
  118. $sClass = get_class($this);
  119. $oPage->add_ready_script(
  120. <<<EOF
  121. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  122. EOF
  123. );
  124. }
  125. }
  126. public function SetID($sId)
  127. {
  128. $this->sId = $sId;
  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. return $this;
  149. }
  150. public function IsRedrawNeeded()
  151. {
  152. return $this->bRedrawNeeded;
  153. }
  154. public function IsFormRedrawNeeded()
  155. {
  156. return $this->bFormRedrawNeeded;
  157. }
  158. static public function GetInfo()
  159. {
  160. return array(
  161. 'label' => '',
  162. 'icon' => '',
  163. 'description' => '',
  164. );
  165. }
  166. public function GetForm()
  167. {
  168. $oForm = new DesignerForm();
  169. $oForm->SetPrefix("dashlet_". $this->GetID());
  170. $oForm->SetParamsContainer('params');
  171. $this->GetPropertiesFields($oForm);
  172. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  173. $oForm->AddField($oDashletClassField);
  174. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  175. $oForm->AddField($oDashletIdField);
  176. return $oForm;
  177. }
  178. static public function IsVisible()
  179. {
  180. return true;
  181. }
  182. static public function CanCreateFromOQL()
  183. {
  184. return false;
  185. }
  186. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  187. {
  188. // Default: do nothing since it's not supported
  189. }
  190. }
  191. class DashletEmptyCell extends Dashlet
  192. {
  193. public function __construct($sId)
  194. {
  195. parent::__construct($sId);
  196. }
  197. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  198. {
  199. $oPage->add('&nbsp;');
  200. }
  201. public function GetPropertiesFields(DesignerForm $oForm)
  202. {
  203. }
  204. static public function GetInfo()
  205. {
  206. return array(
  207. 'label' => 'Empty Cell',
  208. 'icon' => 'images/dashlet-text.png',
  209. 'description' => 'Empty Cell Dashlet Placeholder',
  210. );
  211. }
  212. static public function IsVisible()
  213. {
  214. return false;
  215. }
  216. }
  217. class DashletHelloWorld extends Dashlet
  218. {
  219. public function __construct($sId)
  220. {
  221. parent::__construct($sId);
  222. $this->aProperties['text'] = 'Hello World';
  223. }
  224. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  225. {
  226. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  227. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  228. $oPage->add_ready_script("$('#chart_{$sId}').pie_chart();");
  229. }
  230. public function GetPropertiesFields(DesignerForm $oForm)
  231. {
  232. $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
  233. $oForm->AddField($oField);
  234. }
  235. static public function GetInfo()
  236. {
  237. return array(
  238. 'label' => 'Hello World',
  239. 'icon' => 'images/dashlet-text.png',
  240. 'description' => 'Hello World test Dashlet',
  241. );
  242. }
  243. }
  244. class DashletObjectList extends Dashlet
  245. {
  246. public function __construct($sId)
  247. {
  248. parent::__construct($sId);
  249. $this->aProperties['title'] = 'Hardcoded list of "my requests"';
  250. $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
  251. $this->aProperties['menu'] = false;
  252. }
  253. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  254. {
  255. $sTitle = $this->aProperties['title'];
  256. $sQuery = $this->aProperties['query'];
  257. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  258. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  259. $oFilter = DBObjectSearch::FromOQL($sQuery);
  260. $oBlock = new DisplayBlock($oFilter, 'list');
  261. $aExtraParams = array(
  262. 'menu' => $sShowMenu,
  263. );
  264. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  265. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  266. $oPage->add('</div>');
  267. }
  268. public function GetPropertiesFields(DesignerForm $oForm)
  269. {
  270. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  271. $oForm->AddField($oField);
  272. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  273. $oForm->AddField($oField);
  274. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  275. $oForm->AddField($oField);
  276. }
  277. static public function GetInfo()
  278. {
  279. return array(
  280. 'label' => 'Object list',
  281. 'icon' => 'images/dashlet-list.png',
  282. 'description' => 'Object list dashlet',
  283. );
  284. }
  285. static public function CanCreateFromOQL()
  286. {
  287. return true;
  288. }
  289. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  290. {
  291. $oField = new DesignerTextField('title', 'Title', '');
  292. $oForm->AddField($oField);
  293. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  294. $oForm->AddField($oField);
  295. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  296. $oForm->AddField($oField);
  297. }
  298. }
  299. abstract class DashletGroupBy extends Dashlet
  300. {
  301. public function __construct($sId)
  302. {
  303. parent::__construct($sId);
  304. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  305. $this->aProperties['query'] = 'SELECT Contact';
  306. $this->aProperties['group_by'] = 'location_name';
  307. $this->aProperties['style'] = 'table';
  308. }
  309. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  310. {
  311. $sTitle = $this->aProperties['title'];
  312. $sQuery = $this->aProperties['query'];
  313. $sGroupBy = $this->aProperties['group_by'];
  314. $sStyle = $this->aProperties['style'];
  315. if ($sQuery == '')
  316. {
  317. $oPage->add('<p>Please enter a valid OQL query</p>');
  318. }
  319. elseif ($sGroupBy == '')
  320. {
  321. $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
  322. }
  323. else
  324. {
  325. $oFilter = DBObjectSearch::FromOQL($sQuery);
  326. $sClassAlias = $oFilter->GetClassAlias();
  327. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  328. {
  329. $sAttCode = $aMatches[1];
  330. $sFunction = $aMatches[2];
  331. switch($sFunction)
  332. {
  333. case 'hour':
  334. $sGroupByLabel = 'Hour of '.$sAttCode. ' (0-23)';
  335. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 31
  336. break;
  337. case 'month':
  338. $sGroupByLabel = 'Month of '.$sAttCode. ' (1 - 12)';
  339. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%m')"; // 0 -> 31
  340. break;
  341. case 'day_of_week':
  342. $sGroupByLabel = 'Day of week for '.$sAttCode. ' (sunday to saturday)';
  343. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
  344. break;
  345. case 'day_of_month':
  346. $sGroupByLabel = 'Day of month for'.$sAttCode;
  347. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%e')"; // 0 -> 31
  348. break;
  349. default:
  350. $sGroupByLabel = 'Unknown group by function '.$sFunction;
  351. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  352. }
  353. }
  354. else
  355. {
  356. $sAttCode = $sGroupBy;
  357. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  358. $sGroupByLabel = MetaModel::GetLabel($oFilter->GetClass(), $sAttCode);
  359. }
  360. switch($sStyle)
  361. {
  362. case 'bars':
  363. $sType = 'open_flash_chart';
  364. $aExtraParams = array(
  365. 'chart_type' => 'bars',
  366. 'chart_title' => $sTitle,
  367. 'group_by' => $sGroupByExpr,
  368. 'group_by_label' => $sGroupByLabel,
  369. );
  370. $sHtmlTitle = ''; // done in the itop block
  371. break;
  372. case 'pie':
  373. $sType = 'open_flash_chart';
  374. $aExtraParams = array(
  375. 'chart_type' => 'pie',
  376. 'chart_title' => $sTitle,
  377. 'group_by' => $sGroupByExpr,
  378. 'group_by_label' => $sGroupByLabel,
  379. );
  380. $sHtmlTitle = ''; // done in the itop block
  381. break;
  382. case 'table':
  383. default:
  384. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  385. $sType = 'count';
  386. $aExtraParams = array(
  387. 'group_by' => $sGroupByExpr,
  388. 'group_by_label' => $sGroupByLabel,
  389. );
  390. break;
  391. }
  392. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  393. if ($sHtmlTitle != '')
  394. {
  395. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  396. }
  397. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  398. $oBlock = new DisplayBlock($oFilter, $sType);
  399. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  400. $oPage->add('</div>');
  401. }
  402. }
  403. public function GetPropertiesFields(DesignerForm $oForm)
  404. {
  405. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  406. $oForm->AddField($oField);
  407. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  408. $oForm->AddField($oField);
  409. // Group by field: build the list of possible values (attribute codes + ...)
  410. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  411. $sClass = $oSearch->GetClass();
  412. $aGroupBy = array();
  413. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  414. {
  415. if (!$oAttDef->IsScalar()) continue; // skip link sets
  416. $sLabel = $oAttDef->GetLabel();
  417. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  418. {
  419. $sLabel = $oAttDef->GetLabel().' (strict)';
  420. }
  421. $aGroupBy[$sAttCode] = $sLabel;
  422. if ($oAttDef instanceof AttributeDateTime)
  423. {
  424. $aGroupBy[$sAttCode.':hour'] = $oAttDef->GetLabel().' (hour)';
  425. $aGroupBy[$sAttCode.':month'] = $oAttDef->GetLabel().' (month)';
  426. $aGroupBy[$sAttCode.':day_of_week'] = $oAttDef->GetLabel().' (day of week)';
  427. $aGroupBy[$sAttCode.':day_of_month'] = $oAttDef->GetLabel().' (day of month)';
  428. }
  429. }
  430. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  431. $oField->SetAllowedValues($aGroupBy);
  432. $oForm->AddField($oField);
  433. $aStyles = array(
  434. 'pie' => 'Pie chart',
  435. 'bars' => 'Bar chart',
  436. 'table' => 'Table',
  437. );
  438. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  439. $oField->SetAllowedValues($aStyles);
  440. $oForm->AddField($oField);
  441. }
  442. public function Update($aValues, $aUpdatedFields)
  443. {
  444. if (in_array('query', $aUpdatedFields))
  445. {
  446. $sCurrQuery = $aValues['query'];
  447. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  448. $sCurrClass = $oCurrSearch->GetClass();
  449. $sPrevQuery = $this->aProperties['query'];
  450. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  451. $sPrevClass = $oPrevSearch->GetClass();
  452. if ($sCurrClass != $sPrevClass)
  453. {
  454. $this->bFormRedrawNeeded = true;
  455. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  456. $this->aProperties['group_by'] = '';
  457. }
  458. }
  459. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  460. if (in_array('style', $aUpdatedFields))
  461. {
  462. switch($aValues['style'])
  463. {
  464. // Style changed, mutate to the specified type of chart
  465. case 'pie':
  466. $oDashlet = new DashletGroupByPie($this->sId);
  467. break;
  468. case 'bars':
  469. $oDashlet = new DashletGroupByBars($this->sId);
  470. break;
  471. case 'table':
  472. $oDashlet = new DashletGroupByTable($this->sId);
  473. break;
  474. }
  475. $oDashlet->FromParams($aValues);
  476. $oDashlet->bRedrawNeeded = true;
  477. $oDashlet->bFormRedrawNeeded = true;
  478. }
  479. return $oDashlet;
  480. }
  481. static public function GetInfo()
  482. {
  483. return array(
  484. 'label' => 'Objects grouped by...',
  485. 'icon' => 'images/dashlet-object-grouped.png',
  486. 'description' => 'Grouped objects dashlet',
  487. );
  488. }
  489. static public function CanCreateFromOQL()
  490. {
  491. return true;
  492. }
  493. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  494. {
  495. $oField = new DesignerTextField('title', 'Title', '');
  496. $oForm->AddField($oField);
  497. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  498. $oForm->AddField($oField);
  499. // Group by field: build the list of possible values (attribute codes + ...)
  500. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  501. $sClass = $oSearch->GetClass();
  502. $aGroupBy = array();
  503. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  504. {
  505. if (!$oAttDef->IsScalar()) continue; // skip link sets
  506. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
  507. $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
  508. if ($oAttDef instanceof AttributeDateTime)
  509. {
  510. //date_format(start_date, '%d')
  511. $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
  512. }
  513. }
  514. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  515. $oField->SetAllowedValues($aGroupBy);
  516. $oForm->AddField($oField);
  517. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  518. $oForm->AddField($oField);
  519. }
  520. }
  521. class DashletGroupByPie extends DashletGroupBy
  522. {
  523. public function __construct($sId)
  524. {
  525. parent::__construct($sId);
  526. $this->aProperties['style'] = 'pie';
  527. }
  528. static public function GetInfo()
  529. {
  530. return array(
  531. 'label' => 'Pie Chart',
  532. 'icon' => 'images/dashlet-pie-chart.png',
  533. 'description' => 'Pie Chart',
  534. );
  535. }
  536. }
  537. class DashletGroupByPie2 extends DashletGroupByPie
  538. {
  539. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  540. {
  541. $sTitle = addslashes($this->aProperties['title']);
  542. $sQuery = $this->aProperties['query'];
  543. $sGroupBy = $this->aProperties['group_by'];
  544. $oSearch = DBObjectSearch::FromOQL($sQuery);
  545. $sClassAlias = $oSearch->GetClassAlias();
  546. $aQueryParams = array();
  547. $aGroupBy = array();
  548. $oGroupByExp = Expression::FromOQL($sClassAlias.'.'.$sGroupBy);
  549. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  550. $sSql = MetaModel::MakeGroupByQuery($oSearch, $aQueryParams, $aGroupBy);
  551. $aRes = CMDBSource::QueryToArray($sSql);
  552. $aGroupBy = array();
  553. $aLabels = array();
  554. $iTotalCount = 0;
  555. foreach ($aRes as $aRow)
  556. {
  557. $sValue = $aRow['grouped_by_1'];
  558. $aLabels[] = ($sValue == '') ? 'Empty (%%.%%)' : $sValue.' (%%.%%)'; //TODO: localize
  559. $aGroupBy[] = (int) $aRow['_itop_count_'];
  560. $iTotalCount += $aRow['_itop_count_'];
  561. }
  562. $aURLs = array();
  563. $sContext = ''; //TODO get the context ??
  564. foreach($aGroupBy as $sValue => $iValue)
  565. {
  566. // Build the search for this subset
  567. $oSubsetSearch = clone $oSearch;
  568. $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($sValue));
  569. $oSubsetSearch->AddConditionExpression($oCondition);
  570. $aURLs[] = 'http://www.combodo.com/itop'; //utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html{$sContext}&filter=".addslashes($oSubsetSearch->serialize());
  571. }
  572. $sJSValues = json_encode($aGroupBy);
  573. $sJSHrefs = json_encode($aURLs);
  574. $sJSLabels = json_encode($aLabels);
  575. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  576. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  577. $oPage->add_ready_script("$('#chart_{$sId}').pie_chart({chart_label: '$sTitle', values: $sJSValues, labels: $sJSLabels, hrefs: $sJSHrefs });");
  578. }
  579. static public function GetInfo()
  580. {
  581. return array(
  582. 'label' => 'Pie (Raphael)',
  583. 'icon' => 'images/dashlet-pie-chart.png',
  584. 'description' => 'Pure JS Pie Chart',
  585. );
  586. }
  587. }
  588. class DashletGroupByBars extends DashletGroupBy
  589. {
  590. public function __construct($sId)
  591. {
  592. parent::__construct($sId);
  593. $this->aProperties['style'] = 'bars';
  594. }
  595. static public function GetInfo()
  596. {
  597. return array(
  598. 'label' => 'Bar Chart',
  599. 'icon' => 'images/dashlet-bar-chart.png',
  600. 'description' => 'Bar Chart',
  601. );
  602. }
  603. }
  604. class DashletGroupByTable extends DashletGroupBy
  605. {
  606. public function __construct($sId)
  607. {
  608. parent::__construct($sId);
  609. $this->aProperties['style'] = 'table';
  610. }
  611. static public function GetInfo()
  612. {
  613. return array(
  614. 'label' => 'Group By (table)',
  615. 'icon' => 'images/dashlet-groupby-table.png',
  616. 'description' => 'List (Grouped by a field)',
  617. );
  618. }
  619. }
  620. class DashletHeader extends Dashlet
  621. {
  622. public function __construct($sId)
  623. {
  624. parent::__construct($sId);
  625. $this->aProperties['title'] = 'Hardcoded header of contacts';
  626. $this->aProperties['subtitle'] = 'Contacts';
  627. $this->aProperties['class'] = 'Contact';
  628. }
  629. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  630. {
  631. $sTitle = $this->aProperties['title'];
  632. $sSubtitle = $this->aProperties['subtitle'];
  633. $sClass = $this->aProperties['class'];
  634. $sTitleReady = str_replace(':', '_', $sTitle);
  635. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  636. $sStatusAttCode = MetaModel::GetStateAttributeCode($sClass);
  637. if (($sStatusAttCode == '') && MetaModel::IsValidAttCode($sClass, 'status'))
  638. {
  639. // Based on an enum
  640. $sStatusAttCode = 'status';
  641. $aStates = array_keys(MetaModel::GetAllowedValues_att($sClass, $sStatusAttCode));
  642. }
  643. else
  644. {
  645. // Based on a state variable
  646. $aStates = array_keys(MetaModel::EnumStates($sClass));
  647. }
  648. if ($sStatusAttCode == '')
  649. {
  650. // Simple stats
  651. $aExtraParams = array(
  652. 'title[block]' => $sTitleReady,
  653. 'label[block]' => $sSubtitleReady,
  654. 'context_filter' => 1,
  655. );
  656. }
  657. else
  658. {
  659. // Stats grouped by "status"
  660. $sStatusList = implode(',', $aStates);
  661. $aExtraParams = array(
  662. 'title[block]' => $sTitleReady,
  663. 'label[block]' => $sSubtitleReady,
  664. 'status[block]' => 'status',
  665. 'status_codes[block]' => $sStatusList,
  666. 'context_filter' => 1,
  667. );
  668. }
  669. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  670. $oPage->add('<div class="main_header">');
  671. $oFilter = new DBObjectSearch($sClass);
  672. $oBlock = new DisplayBlock($oFilter, 'summary');
  673. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  674. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  675. $oPage->add('</div>');
  676. $oPage->add('</div>');
  677. }
  678. public function GetPropertiesFields(DesignerForm $oForm)
  679. {
  680. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  681. $oForm->AddField($oField);
  682. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  683. $oForm->AddField($oField);
  684. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  685. $oForm->AddField($oField);
  686. }
  687. static public function GetInfo()
  688. {
  689. return array(
  690. 'label' => 'Header with stats',
  691. 'icon' => 'images/dashlet-header-stats.png',
  692. 'description' => 'Header with stats (grouped by...)',
  693. );
  694. }
  695. }
  696. class DashletBadge extends Dashlet
  697. {
  698. public function __construct($sId)
  699. {
  700. parent::__construct($sId);
  701. $this->aProperties['class'] = 'Contact';
  702. $this->aCSSClasses[] = 'dashlet-inline';
  703. $this->aCSSClasses[] = 'dashlet-badge';
  704. }
  705. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  706. {
  707. $sClass = $this->aProperties['class'];
  708. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  709. $oFilter = new DBObjectSearch($sClass);
  710. $oBlock = new DisplayBlock($oFilter, 'actions');
  711. $aExtraParams = array(
  712. 'context_filter' => 1,
  713. );
  714. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  715. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  716. $oPage->add('</div>');
  717. }
  718. public function GetPropertiesFields(DesignerForm $oForm)
  719. {
  720. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  721. $oForm->AddField($oField);
  722. }
  723. static public function GetInfo()
  724. {
  725. return array(
  726. 'label' => 'Badge',
  727. 'icon' => 'images/dashlet-badge.png',
  728. 'description' => 'Object Icon with new/search',
  729. );
  730. }
  731. }
  732. class DashletProto extends Dashlet
  733. {
  734. public function __construct($sId)
  735. {
  736. parent::__construct($sId);
  737. $this->aProperties['class'] = 'Foo';
  738. }
  739. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  740. {
  741. $sClass = $this->aProperties['class'];
  742. $oFilter = DBObjectSearch::FromOQL('SELECT FunctionalCI AS fci');
  743. $sGroupBy1 = 'status';
  744. //$sGroupBy2 = 'org_id_friendlyname';
  745. $sGroupBy2 = 'org_id';
  746. $sHtmlTitle = "Hardcoded on $sGroupBy1 and $sGroupBy2...";
  747. $sAlias = $oFilter->GetClassAlias();
  748. $oGroupByExp1 = new FieldExpression($sGroupBy1, $sAlias);
  749. $sGroupByLabel1 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy1);
  750. $oGroupByExp2 = new FieldExpression($sGroupBy2, $sAlias);
  751. $sGroupByLabel2 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy2);
  752. $aGroupBy = array();
  753. $aGroupBy['grouped_by_1'] = $oGroupByExp1;
  754. $aGroupBy['grouped_by_2'] = $oGroupByExp2;
  755. $sSql = MetaModel::MakeGroupByQuery($oFilter, array(), $aGroupBy);
  756. $aRes = CMDBSource::QueryToArray($sSql);
  757. $iTotalCount = 0;
  758. $aData = array();
  759. $oAppContext = new ApplicationContext();
  760. $sParams = $oAppContext->GetForLink();
  761. foreach ($aRes as $aRow)
  762. {
  763. $iCount = $aRow['_itop_count_'];
  764. $iTotalCount += $iCount;
  765. $sValue1 = $aRow['grouped_by_1'];
  766. $sValue2 = $aRow['grouped_by_2'];
  767. $sDisplayValue1 = $aGroupBy['grouped_by_1']->MakeValueLabel($oFilter, $sValue1, $sValue1); // default to the raw value
  768. $sDisplayValue2 = $aGroupBy['grouped_by_2']->MakeValueLabel($oFilter, $sValue2, $sValue2); // default to the raw value
  769. // Build the search for this subset
  770. $oSubsetSearch = clone $oFilter;
  771. $oCondition = new BinaryExpression($oGroupByExp1, '=', new ScalarExpression($sValue1));
  772. $oSubsetSearch->AddConditionExpression($oCondition);
  773. $oCondition = new BinaryExpression($oGroupByExp2, '=', new ScalarExpression($sValue2));
  774. $oSubsetSearch->AddConditionExpression($oCondition);
  775. $sFilter = urlencode($oSubsetSearch->serialize());
  776. $aData[] = array (
  777. 'group1' => $sDisplayValue1,
  778. 'group2' => $sDisplayValue2,
  779. 'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"
  780. ); // TO DO: add the context information
  781. }
  782. $aAttribs =array(
  783. 'group1' => array('label' => $sGroupByLabel1, 'description' => ''),
  784. 'group2' => array('label' => $sGroupByLabel2, 'description' => ''),
  785. 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
  786. );
  787. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  788. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  789. $oPage->p(Dict::Format('UI:Pagination:HeaderNoSelection', $iTotalCount));
  790. $oPage->table($aAttribs, $aData);
  791. $oPage->add('</div>');
  792. }
  793. public function GetPropertiesFields(DesignerForm $oForm)
  794. {
  795. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  796. $oForm->AddField($oField);
  797. }
  798. static public function GetInfo()
  799. {
  800. return array(
  801. 'label' => 'Test3D',
  802. 'icon' => 'images/dashlet-groupby2-table.png',
  803. 'description' => 'Group by on two dimensions',
  804. );
  805. }
  806. }
  807. class DashletHeatMap extends Dashlet
  808. {
  809. public function __construct($sId)
  810. {
  811. parent::__construct($sId);
  812. $this->aProperties['class'] = 'Contact';
  813. $this->aProperties['title'] = 'Test';
  814. }
  815. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  816. {
  817. $sTitle = addslashes($this->aProperties['title']);
  818. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  819. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  820. $oPage->add_ready_script("$('#chart_{$sId}').heatmap_chart({chart_label: '$sTitle'});");
  821. }
  822. public function GetPropertiesFields(DesignerForm $oForm)
  823. {
  824. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  825. $oForm->AddField($oField);
  826. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  827. $oForm->AddField($oField);
  828. }
  829. static public function GetInfo()
  830. {
  831. return array(
  832. 'label' => 'Heatmap (Raphael)',
  833. 'icon' => 'images/dashlet-heatmap.png',
  834. 'description' => 'Pure JS Heat Map Chart',
  835. );
  836. }
  837. }