dashlet.class.inc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. $oPage->add('<div style="text-align:center; line-height:5em" class="dashlet-content"><span>'.$this->aProperties['text'].'</span></div>');
  227. }
  228. public function GetPropertiesFields(DesignerForm $oForm)
  229. {
  230. $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
  231. $oForm->AddField($oField);
  232. }
  233. static public function GetInfo()
  234. {
  235. return array(
  236. 'label' => 'Hello World',
  237. 'icon' => 'images/dashlet-text.png',
  238. 'description' => 'Hello World test Dashlet',
  239. );
  240. }
  241. }
  242. class DashletObjectList extends Dashlet
  243. {
  244. public function __construct($sId)
  245. {
  246. parent::__construct($sId);
  247. $this->aProperties['title'] = 'Hardcoded list of "my requests"';
  248. $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
  249. $this->aProperties['menu'] = false;
  250. }
  251. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  252. {
  253. $sTitle = $this->aProperties['title'];
  254. $sQuery = $this->aProperties['query'];
  255. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  256. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  257. $oFilter = DBObjectSearch::FromOQL($sQuery);
  258. $oBlock = new DisplayBlock($oFilter, 'list');
  259. $aExtraParams = array(
  260. 'menu' => $sShowMenu,
  261. );
  262. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  263. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  264. $oPage->add('</div>');
  265. }
  266. public function GetPropertiesFields(DesignerForm $oForm)
  267. {
  268. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  269. $oForm->AddField($oField);
  270. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  271. $oForm->AddField($oField);
  272. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  273. $oForm->AddField($oField);
  274. }
  275. static public function GetInfo()
  276. {
  277. return array(
  278. 'label' => 'Object list',
  279. 'icon' => 'images/dashlet-object-list.png',
  280. 'description' => 'Object list dashlet',
  281. );
  282. }
  283. static public function CanCreateFromOQL()
  284. {
  285. return true;
  286. }
  287. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  288. {
  289. $oField = new DesignerTextField('title', 'Title', '');
  290. $oForm->AddField($oField);
  291. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  292. $oForm->AddField($oField);
  293. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  294. $oForm->AddField($oField);
  295. }
  296. }
  297. abstract class DashletGroupBy extends Dashlet
  298. {
  299. public function __construct($sId)
  300. {
  301. parent::__construct($sId);
  302. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  303. $this->aProperties['query'] = 'SELECT Contact';
  304. $this->aProperties['group_by'] = 'location_name';
  305. $this->aProperties['style'] = 'table';
  306. }
  307. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  308. {
  309. $sTitle = $this->aProperties['title'];
  310. $sQuery = $this->aProperties['query'];
  311. $sGroupBy = $this->aProperties['group_by'];
  312. $sStyle = $this->aProperties['style'];
  313. if ($sQuery == '')
  314. {
  315. $oPage->add('<p>Please enter a valid OQL query</p>');
  316. }
  317. elseif ($sGroupBy == '')
  318. {
  319. $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
  320. }
  321. else
  322. {
  323. $oFilter = DBObjectSearch::FromOQL($sQuery);
  324. $sClassAlias = $oFilter->GetClassAlias();
  325. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  326. {
  327. $sAttCode = $aMatches[1];
  328. $sFunction = $aMatches[2];
  329. switch($sFunction)
  330. {
  331. case 'hour':
  332. $sGroupByLabel = 'Hour of '.$sAttCode. ' (0-23)';
  333. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 31
  334. break;
  335. case 'month':
  336. $sGroupByLabel = 'Month of '.$sAttCode. ' (1 - 12)';
  337. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%m')"; // 0 -> 31
  338. break;
  339. case 'day_of_week':
  340. $sGroupByLabel = 'Day of week for '.$sAttCode. ' (sunday to saturday)';
  341. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
  342. break;
  343. case 'day_of_month':
  344. $sGroupByLabel = 'Day of month for'.$sAttCode;
  345. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%e')"; // 0 -> 31
  346. break;
  347. default:
  348. $sGroupByLabel = 'Unknown group by function '.$sFunction;
  349. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  350. }
  351. }
  352. else
  353. {
  354. $sAttCode = $sGroupBy;
  355. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  356. $sGroupByLabel = MetaModel::GetLabel($oFilter->GetClass(), $sAttCode);
  357. }
  358. switch($sStyle)
  359. {
  360. case 'bars':
  361. $sType = 'open_flash_chart';
  362. $aExtraParams = array(
  363. 'chart_type' => 'bars',
  364. 'chart_title' => $sTitle,
  365. 'group_by' => $sGroupByExpr,
  366. 'group_by_label' => $sGroupByLabel,
  367. );
  368. $sHtmlTitle = ''; // done in the itop block
  369. break;
  370. case 'pie':
  371. $sType = 'open_flash_chart';
  372. $aExtraParams = array(
  373. 'chart_type' => 'pie',
  374. 'chart_title' => $sTitle,
  375. 'group_by' => $sGroupByExpr,
  376. 'group_by_label' => $sGroupByLabel,
  377. );
  378. $sHtmlTitle = ''; // done in the itop block
  379. break;
  380. case 'table':
  381. default:
  382. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  383. $sType = 'count';
  384. $aExtraParams = array(
  385. 'group_by' => $sGroupByExpr,
  386. 'group_by_label' => $sGroupByLabel,
  387. );
  388. break;
  389. }
  390. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  391. if ($sHtmlTitle != '')
  392. {
  393. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  394. }
  395. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  396. $oBlock = new DisplayBlock($oFilter, $sType);
  397. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  398. $oPage->add('</div>');
  399. }
  400. }
  401. public function GetPropertiesFields(DesignerForm $oForm)
  402. {
  403. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  404. $oForm->AddField($oField);
  405. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  406. $oForm->AddField($oField);
  407. // Group by field: build the list of possible values (attribute codes + ...)
  408. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  409. $sClass = $oSearch->GetClass();
  410. $aGroupBy = array();
  411. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  412. {
  413. if (!$oAttDef->IsScalar()) continue; // skip link sets
  414. $sLabel = $oAttDef->GetLabel();
  415. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  416. {
  417. $sLabel = $oAttDef->GetLabel().' (strict)';
  418. }
  419. $aGroupBy[$sAttCode] = $sLabel;
  420. if ($oAttDef instanceof AttributeDateTime)
  421. {
  422. $aGroupBy[$sAttCode.':hour'] = $oAttDef->GetLabel().' (hour)';
  423. $aGroupBy[$sAttCode.':month'] = $oAttDef->GetLabel().' (month)';
  424. $aGroupBy[$sAttCode.':day_of_week'] = $oAttDef->GetLabel().' (day of week)';
  425. $aGroupBy[$sAttCode.':day_of_month'] = $oAttDef->GetLabel().' (day of month)';
  426. }
  427. }
  428. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  429. $oField->SetAllowedValues($aGroupBy);
  430. $oForm->AddField($oField);
  431. $aStyles = array(
  432. 'pie' => 'Pie chart',
  433. 'bars' => 'Bar chart',
  434. 'table' => 'Table',
  435. );
  436. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  437. $oField->SetAllowedValues($aStyles);
  438. $oForm->AddField($oField);
  439. }
  440. public function Update($aValues, $aUpdatedFields)
  441. {
  442. if (in_array('query', $aUpdatedFields))
  443. {
  444. $sCurrQuery = $aValues['query'];
  445. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  446. $sCurrClass = $oCurrSearch->GetClass();
  447. $sPrevQuery = $this->aProperties['query'];
  448. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  449. $sPrevClass = $oPrevSearch->GetClass();
  450. if ($sCurrClass != $sPrevClass)
  451. {
  452. $this->bFormRedrawNeeded = true;
  453. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  454. $this->aProperties['group_by'] = '';
  455. }
  456. }
  457. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  458. if (in_array('style', $aUpdatedFields))
  459. {
  460. switch($aValues['style'])
  461. {
  462. // Style changed, mutate to the specified type of chart
  463. case 'pie':
  464. $oDashlet = new DashletGroupByPie($this->sId);
  465. break;
  466. case 'bars':
  467. $oDashlet = new DashletGroupByBars($this->sId);
  468. break;
  469. case 'table':
  470. $oDashlet = new DashletGroupByTable($this->sId);
  471. break;
  472. }
  473. $oDashlet->FromParams($aValues);
  474. $oDashlet->bRedrawNeeded = true;
  475. $oDashlet->bFormRedrawNeeded = true;
  476. }
  477. return $oDashlet;
  478. }
  479. static public function GetInfo()
  480. {
  481. return array(
  482. 'label' => 'Objects grouped by...',
  483. 'icon' => 'images/dashlet-object-grouped.png',
  484. 'description' => 'Grouped objects dashlet',
  485. );
  486. }
  487. static public function CanCreateFromOQL()
  488. {
  489. return true;
  490. }
  491. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  492. {
  493. $oField = new DesignerTextField('title', 'Title', '');
  494. $oForm->AddField($oField);
  495. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  496. $oForm->AddField($oField);
  497. // Group by field: build the list of possible values (attribute codes + ...)
  498. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  499. $sClass = $oSearch->GetClass();
  500. $aGroupBy = array();
  501. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  502. {
  503. if (!$oAttDef->IsScalar()) continue; // skip link sets
  504. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
  505. $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
  506. if ($oAttDef instanceof AttributeDateTime)
  507. {
  508. //date_format(start_date, '%d')
  509. $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
  510. }
  511. }
  512. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  513. $oField->SetAllowedValues($aGroupBy);
  514. $oForm->AddField($oField);
  515. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  516. $oForm->AddField($oField);
  517. }
  518. }
  519. class DashletGroupByPie extends DashletGroupBy
  520. {
  521. public function __construct($sId)
  522. {
  523. parent::__construct($sId);
  524. $this->aProperties['style'] = 'pie';
  525. }
  526. static public function GetInfo()
  527. {
  528. return array(
  529. 'label' => 'Pie Chart',
  530. 'icon' => 'images/dashlet-pie-chart.png',
  531. 'description' => 'Pie Chart',
  532. );
  533. }
  534. }
  535. class DashletGroupByBars extends DashletGroupBy
  536. {
  537. public function __construct($sId)
  538. {
  539. parent::__construct($sId);
  540. $this->aProperties['style'] = 'bars';
  541. }
  542. static public function GetInfo()
  543. {
  544. return array(
  545. 'label' => 'Bar Chart',
  546. 'icon' => 'images/dashlet-bar-chart.png',
  547. 'description' => 'Bar Chart',
  548. );
  549. }
  550. }
  551. class DashletGroupByTable extends DashletGroupBy
  552. {
  553. public function __construct($sId)
  554. {
  555. parent::__construct($sId);
  556. $this->aProperties['style'] = 'table';
  557. }
  558. static public function GetInfo()
  559. {
  560. return array(
  561. 'label' => 'Group By (table)',
  562. 'icon' => 'images/dashlet-group-by-table.png',
  563. 'description' => 'List (Grouped by a field)',
  564. );
  565. }
  566. }
  567. class DashletHeader extends Dashlet
  568. {
  569. public function __construct($sId)
  570. {
  571. parent::__construct($sId);
  572. $this->aProperties['title'] = 'Hardcoded header of contacts';
  573. $this->aProperties['subtitle'] = 'Contacts';
  574. $this->aProperties['class'] = 'Contact';
  575. }
  576. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  577. {
  578. $sTitle = $this->aProperties['title'];
  579. $sSubtitle = $this->aProperties['subtitle'];
  580. $sClass = $this->aProperties['class'];
  581. $sTitleReady = str_replace(':', '_', $sTitle);
  582. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  583. $sStatusAttCode = MetaModel::GetStateAttributeCode($sClass);
  584. if (($sStatusAttCode == '') && MetaModel::IsValidAttCode($sClass, 'status'))
  585. {
  586. // Based on an enum
  587. $sStatusAttCode = 'status';
  588. $aStates = array_keys(MetaModel::GetAllowedValues_att($sClass, $sStatusAttCode));
  589. }
  590. else
  591. {
  592. // Based on a state variable
  593. $aStates = array_keys(MetaModel::EnumStates($sClass));
  594. }
  595. if ($sStatusAttCode == '')
  596. {
  597. // Simple stats
  598. $aExtraParams = array(
  599. 'title[block]' => $sTitleReady,
  600. 'label[block]' => $sSubtitleReady,
  601. 'context_filter' => 1,
  602. );
  603. }
  604. else
  605. {
  606. // Stats grouped by "status"
  607. $sStatusList = implode(',', $aStates);
  608. $aExtraParams = array(
  609. 'title[block]' => $sTitleReady,
  610. 'label[block]' => $sSubtitleReady,
  611. 'status[block]' => 'status',
  612. 'status_codes[block]' => $sStatusList,
  613. 'context_filter' => 1,
  614. );
  615. }
  616. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  617. $oPage->add('<div class="main_header">');
  618. $oFilter = new DBObjectSearch($sClass);
  619. $oBlock = new DisplayBlock($oFilter, 'summary');
  620. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  621. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  622. $oPage->add('</div>');
  623. $oPage->add('</div>');
  624. }
  625. public function GetPropertiesFields(DesignerForm $oForm)
  626. {
  627. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  628. $oForm->AddField($oField);
  629. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  630. $oForm->AddField($oField);
  631. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  632. $oForm->AddField($oField);
  633. }
  634. static public function GetInfo()
  635. {
  636. return array(
  637. 'label' => 'Header with stats',
  638. 'icon' => 'images/dashlet-header-stats.png',
  639. 'description' => 'Header with stats (grouped by...)',
  640. );
  641. }
  642. }
  643. class DashletBadge extends Dashlet
  644. {
  645. public function __construct($sId)
  646. {
  647. parent::__construct($sId);
  648. $this->aProperties['class'] = 'Contact';
  649. $this->aCSSClasses[] = 'dashlet-inline';
  650. $this->aCSSClasses[] = 'dashlet-badge';
  651. }
  652. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  653. {
  654. $sClass = $this->aProperties['class'];
  655. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  656. $oFilter = new DBObjectSearch($sClass);
  657. $oBlock = new DisplayBlock($oFilter, 'actions');
  658. $aExtraParams = array(
  659. 'context_filter' => 1,
  660. );
  661. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  662. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  663. $oPage->add('</div>');
  664. }
  665. public function GetPropertiesFields(DesignerForm $oForm)
  666. {
  667. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  668. $oForm->AddField($oField);
  669. }
  670. static public function GetInfo()
  671. {
  672. return array(
  673. 'label' => 'Badge',
  674. 'icon' => 'images/dashlet-badge.png',
  675. 'description' => 'Object Icon with new/search',
  676. );
  677. }
  678. }
  679. class DashletProto extends Dashlet
  680. {
  681. public function __construct($sId)
  682. {
  683. parent::__construct($sId);
  684. $this->aProperties['class'] = 'Foo';
  685. }
  686. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  687. {
  688. $sClass = $this->aProperties['class'];
  689. $oFilter = DBObjectSearch::FromOQL('SELECT FunctionalCI AS fci');
  690. $sGroupBy1 = 'status';
  691. //$sGroupBy2 = 'org_id_friendlyname';
  692. $sGroupBy2 = 'org_id';
  693. $sHtmlTitle = "Hardcoded on $sGroupBy1 and $sGroupBy2...";
  694. $sAlias = $oFilter->GetClassAlias();
  695. $oGroupByExp1 = new FieldExpression($sGroupBy1, $sAlias);
  696. $sGroupByLabel1 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy1);
  697. $oGroupByExp2 = new FieldExpression($sGroupBy2, $sAlias);
  698. $sGroupByLabel2 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy2);
  699. $aGroupBy = array();
  700. $aGroupBy['grouped_by_1'] = $oGroupByExp1;
  701. $aGroupBy['grouped_by_2'] = $oGroupByExp2;
  702. $sSql = MetaModel::MakeGroupByQuery($oFilter, array(), $aGroupBy);
  703. $aRes = CMDBSource::QueryToArray($sSql);
  704. $iTotalCount = 0;
  705. $aData = array();
  706. $oAppContext = new ApplicationContext();
  707. $sParams = $oAppContext->GetForLink();
  708. foreach ($aRes as $aRow)
  709. {
  710. $iCount = $aRow['_itop_count_'];
  711. $iTotalCount += $iCount;
  712. $sValue1 = $aRow['grouped_by_1'];
  713. $sValue2 = $aRow['grouped_by_2'];
  714. $sDisplayValue1 = $aGroupBy['grouped_by_1']->MakeValueLabel($oFilter, $sValue1, $sValue1); // default to the raw value
  715. $sDisplayValue2 = $aGroupBy['grouped_by_2']->MakeValueLabel($oFilter, $sValue2, $sValue2); // default to the raw value
  716. // Build the search for this subset
  717. $oSubsetSearch = clone $oFilter;
  718. $oCondition = new BinaryExpression($oGroupByExp1, '=', new ScalarExpression($sValue1));
  719. $oSubsetSearch->AddConditionExpression($oCondition);
  720. $oCondition = new BinaryExpression($oGroupByExp2, '=', new ScalarExpression($sValue2));
  721. $oSubsetSearch->AddConditionExpression($oCondition);
  722. $sFilter = urlencode($oSubsetSearch->serialize());
  723. $aData[] = array (
  724. 'group1' => $sDisplayValue1,
  725. 'group2' => $sDisplayValue2,
  726. 'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"
  727. ); // TO DO: add the context information
  728. }
  729. $aAttribs =array(
  730. 'group1' => array('label' => $sGroupByLabel1, 'description' => ''),
  731. 'group2' => array('label' => $sGroupByLabel2, 'description' => ''),
  732. 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
  733. );
  734. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  735. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  736. $oPage->p(Dict::Format('UI:Pagination:HeaderNoSelection', $iTotalCount));
  737. $oPage->table($aAttribs, $aData);
  738. $oPage->add('</div>');
  739. }
  740. public function GetPropertiesFields(DesignerForm $oForm)
  741. {
  742. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  743. $oForm->AddField($oField);
  744. }
  745. static public function GetInfo()
  746. {
  747. return array(
  748. 'label' => 'Test3D',
  749. 'icon' => 'images/xxxxxx.png',
  750. 'description' => 'Group by on two dimensions',
  751. );
  752. }
  753. }