dashlet.class.inc.php 24 KB

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