dashlet.class.inc.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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 class="dashlet-content">');
  259. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  260. if ($sHtmlTitle != '')
  261. {
  262. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  263. }
  264. $oFilter = DBObjectSearch::FromOQL($sQuery);
  265. $oBlock = new DisplayBlock($oFilter, 'list');
  266. $aExtraParams = array(
  267. 'menu' => $sShowMenu,
  268. );
  269. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  270. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  271. $oPage->add('</div>');
  272. }
  273. public function GetPropertiesFields(DesignerForm $oForm)
  274. {
  275. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  276. $oForm->AddField($oField);
  277. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  278. $oForm->AddField($oField);
  279. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  280. $oForm->AddField($oField);
  281. }
  282. static public function GetInfo()
  283. {
  284. return array(
  285. 'label' => 'Object list',
  286. 'icon' => 'images/dashlet-list.png',
  287. 'description' => 'Object list dashlet',
  288. );
  289. }
  290. static public function CanCreateFromOQL()
  291. {
  292. return true;
  293. }
  294. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  295. {
  296. $oField = new DesignerTextField('title', 'Title', '');
  297. $oForm->AddField($oField);
  298. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  299. $oForm->AddField($oField);
  300. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  301. $oForm->AddField($oField);
  302. }
  303. }
  304. abstract class DashletGroupBy extends Dashlet
  305. {
  306. public function __construct($sId)
  307. {
  308. parent::__construct($sId);
  309. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  310. $this->aProperties['query'] = 'SELECT Contact';
  311. $this->aProperties['group_by'] = 'location_name';
  312. $this->aProperties['style'] = 'table';
  313. }
  314. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  315. {
  316. $sTitle = $this->aProperties['title'];
  317. $sQuery = $this->aProperties['query'];
  318. $sGroupBy = $this->aProperties['group_by'];
  319. $sStyle = $this->aProperties['style'];
  320. if ($sQuery == '')
  321. {
  322. $oPage->add('<p>Please enter a valid OQL query</p>');
  323. }
  324. elseif ($sGroupBy == '')
  325. {
  326. $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
  327. }
  328. else
  329. {
  330. $oFilter = DBObjectSearch::FromOQL($sQuery);
  331. $sClassAlias = $oFilter->GetClassAlias();
  332. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  333. {
  334. $sAttCode = $aMatches[1];
  335. $sFunction = $aMatches[2];
  336. switch($sFunction)
  337. {
  338. case 'hour':
  339. $sGroupByLabel = 'Hour of '.$sAttCode. ' (0-23)';
  340. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 31
  341. break;
  342. case 'month':
  343. $sGroupByLabel = 'Month of '.$sAttCode. ' (1 - 12)';
  344. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%m')"; // 0 -> 31
  345. break;
  346. case 'day_of_week':
  347. $sGroupByLabel = 'Day of week for '.$sAttCode. ' (sunday to saturday)';
  348. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
  349. break;
  350. case 'day_of_month':
  351. $sGroupByLabel = 'Day of month for'.$sAttCode;
  352. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%e')"; // 0 -> 31
  353. break;
  354. default:
  355. $sGroupByLabel = 'Unknown group by function '.$sFunction;
  356. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  357. }
  358. }
  359. else
  360. {
  361. $sAttCode = $sGroupBy;
  362. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  363. $sGroupByLabel = MetaModel::GetLabel($oFilter->GetClass(), $sAttCode);
  364. }
  365. switch($sStyle)
  366. {
  367. case 'bars':
  368. $sType = 'open_flash_chart';
  369. $aExtraParams = array(
  370. 'chart_type' => 'bars',
  371. 'chart_title' => $sTitle,
  372. 'group_by' => $sGroupByExpr,
  373. 'group_by_label' => $sGroupByLabel,
  374. );
  375. $sHtmlTitle = ''; // done in the itop block
  376. break;
  377. case 'pie':
  378. $sType = 'open_flash_chart';
  379. $aExtraParams = array(
  380. 'chart_type' => 'pie',
  381. 'chart_title' => $sTitle,
  382. 'group_by' => $sGroupByExpr,
  383. 'group_by_label' => $sGroupByLabel,
  384. );
  385. $sHtmlTitle = ''; // done in the itop block
  386. break;
  387. case 'table':
  388. default:
  389. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  390. $sType = 'count';
  391. $aExtraParams = array(
  392. 'group_by' => $sGroupByExpr,
  393. 'group_by_label' => $sGroupByLabel,
  394. );
  395. break;
  396. }
  397. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  398. if ($sHtmlTitle != '')
  399. {
  400. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  401. }
  402. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  403. $oBlock = new DisplayBlock($oFilter, $sType);
  404. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  405. $oPage->add('</div>');
  406. }
  407. }
  408. public function GetPropertiesFields(DesignerForm $oForm)
  409. {
  410. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  411. $oForm->AddField($oField);
  412. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  413. $oForm->AddField($oField);
  414. // Group by field: build the list of possible values (attribute codes + ...)
  415. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  416. $sClass = $oSearch->GetClass();
  417. $aGroupBy = array();
  418. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  419. {
  420. if (!$oAttDef->IsScalar()) continue; // skip link sets
  421. $sLabel = $oAttDef->GetLabel();
  422. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  423. {
  424. $sLabel = $oAttDef->GetLabel().' (strict)';
  425. }
  426. $aGroupBy[$sAttCode] = $sLabel;
  427. if ($oAttDef instanceof AttributeDateTime)
  428. {
  429. $aGroupBy[$sAttCode.':hour'] = $oAttDef->GetLabel().' (hour)';
  430. $aGroupBy[$sAttCode.':month'] = $oAttDef->GetLabel().' (month)';
  431. $aGroupBy[$sAttCode.':day_of_week'] = $oAttDef->GetLabel().' (day of week)';
  432. $aGroupBy[$sAttCode.':day_of_month'] = $oAttDef->GetLabel().' (day of month)';
  433. }
  434. }
  435. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  436. $oField->SetAllowedValues($aGroupBy);
  437. $oForm->AddField($oField);
  438. $aStyles = array(
  439. 'pie' => 'Pie chart',
  440. 'bars' => 'Bar chart',
  441. 'table' => 'Table',
  442. );
  443. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  444. $oField->SetAllowedValues($aStyles);
  445. $oForm->AddField($oField);
  446. }
  447. public function Update($aValues, $aUpdatedFields)
  448. {
  449. if (in_array('query', $aUpdatedFields))
  450. {
  451. $sCurrQuery = $aValues['query'];
  452. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  453. $sCurrClass = $oCurrSearch->GetClass();
  454. $sPrevQuery = $this->aProperties['query'];
  455. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  456. $sPrevClass = $oPrevSearch->GetClass();
  457. if ($sCurrClass != $sPrevClass)
  458. {
  459. $this->bFormRedrawNeeded = true;
  460. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  461. $this->aProperties['group_by'] = '';
  462. }
  463. }
  464. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  465. if (in_array('style', $aUpdatedFields))
  466. {
  467. switch($aValues['style'])
  468. {
  469. // Style changed, mutate to the specified type of chart
  470. case 'pie':
  471. $oDashlet = new DashletGroupByPie($this->sId);
  472. break;
  473. case 'bars':
  474. $oDashlet = new DashletGroupByBars($this->sId);
  475. break;
  476. case 'table':
  477. $oDashlet = new DashletGroupByTable($this->sId);
  478. break;
  479. }
  480. $oDashlet->FromParams($aValues);
  481. $oDashlet->bRedrawNeeded = true;
  482. $oDashlet->bFormRedrawNeeded = true;
  483. }
  484. return $oDashlet;
  485. }
  486. static public function GetInfo()
  487. {
  488. return array(
  489. 'label' => 'Objects grouped by...',
  490. 'icon' => 'images/dashlet-object-grouped.png',
  491. 'description' => 'Grouped objects dashlet',
  492. );
  493. }
  494. static public function CanCreateFromOQL()
  495. {
  496. return true;
  497. }
  498. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  499. {
  500. $oField = new DesignerTextField('title', 'Title', '');
  501. $oForm->AddField($oField);
  502. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  503. $oForm->AddField($oField);
  504. // Group by field: build the list of possible values (attribute codes + ...)
  505. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  506. $sClass = $oSearch->GetClass();
  507. $aGroupBy = array();
  508. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  509. {
  510. if (!$oAttDef->IsScalar()) continue; // skip link sets
  511. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
  512. $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
  513. if ($oAttDef instanceof AttributeDateTime)
  514. {
  515. //date_format(start_date, '%d')
  516. $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
  517. }
  518. }
  519. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  520. $oField->SetAllowedValues($aGroupBy);
  521. $oForm->AddField($oField);
  522. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  523. $oForm->AddField($oField);
  524. }
  525. }
  526. class DashletGroupByPie extends DashletGroupBy
  527. {
  528. public function __construct($sId)
  529. {
  530. parent::__construct($sId);
  531. $this->aProperties['style'] = 'pie';
  532. }
  533. static public function GetInfo()
  534. {
  535. return array(
  536. 'label' => 'Pie Chart',
  537. 'icon' => 'images/dashlet-pie-chart.png',
  538. 'description' => 'Pie Chart',
  539. );
  540. }
  541. }
  542. class DashletGroupByPie2 extends DashletGroupByPie
  543. {
  544. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  545. {
  546. $sTitle = addslashes($this->aProperties['title']);
  547. $sQuery = $this->aProperties['query'];
  548. $sGroupBy = $this->aProperties['group_by'];
  549. $oSearch = DBObjectSearch::FromOQL($sQuery);
  550. $sClassAlias = $oSearch->GetClassAlias();
  551. $aQueryParams = array();
  552. $aGroupBy = array();
  553. $oGroupByExp = Expression::FromOQL($sClassAlias.'.'.$sGroupBy);
  554. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  555. $sSql = MetaModel::MakeGroupByQuery($oSearch, $aQueryParams, $aGroupBy);
  556. $aRes = CMDBSource::QueryToArray($sSql);
  557. $aGroupBy = array();
  558. $aLabels = array();
  559. $iTotalCount = 0;
  560. foreach ($aRes as $aRow)
  561. {
  562. $sValue = $aRow['grouped_by_1'];
  563. $aLabels[] = ($sValue == '') ? 'Empty (%%.%%)' : $sValue.' (%%.%%)'; //TODO: localize
  564. $aGroupBy[] = (int) $aRow['_itop_count_'];
  565. $iTotalCount += $aRow['_itop_count_'];
  566. }
  567. $aURLs = array();
  568. $sContext = ''; //TODO get the context ??
  569. foreach($aGroupBy as $sValue => $iValue)
  570. {
  571. // Build the search for this subset
  572. $oSubsetSearch = clone $oSearch;
  573. $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($sValue));
  574. $oSubsetSearch->AddConditionExpression($oCondition);
  575. $aURLs[] = 'http://www.combodo.com/itop'; //utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html{$sContext}&filter=".addslashes($oSubsetSearch->serialize());
  576. }
  577. $sJSValues = json_encode($aGroupBy);
  578. $sJSHrefs = json_encode($aURLs);
  579. $sJSLabels = json_encode($aLabels);
  580. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  581. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  582. $oPage->add_ready_script("$('#chart_{$sId}').pie_chart({chart_label: '$sTitle', values: $sJSValues, labels: $sJSLabels, hrefs: $sJSHrefs });");
  583. }
  584. static public function GetInfo()
  585. {
  586. return array(
  587. 'label' => 'Pie (Raphael)',
  588. 'icon' => 'images/dashlet-pie-chart.png',
  589. 'description' => 'Pure JS Pie Chart',
  590. );
  591. }
  592. }
  593. class DashletGroupByBars extends DashletGroupBy
  594. {
  595. public function __construct($sId)
  596. {
  597. parent::__construct($sId);
  598. $this->aProperties['style'] = 'bars';
  599. }
  600. static public function GetInfo()
  601. {
  602. return array(
  603. 'label' => 'Bar Chart',
  604. 'icon' => 'images/dashlet-bar-chart.png',
  605. 'description' => 'Bar Chart',
  606. );
  607. }
  608. }
  609. class DashletGroupByTable extends DashletGroupBy
  610. {
  611. public function __construct($sId)
  612. {
  613. parent::__construct($sId);
  614. $this->aProperties['style'] = 'table';
  615. }
  616. static public function GetInfo()
  617. {
  618. return array(
  619. 'label' => 'Group By (table)',
  620. 'icon' => 'images/dashlet-groupby-table.png',
  621. 'description' => 'List (Grouped by a field)',
  622. );
  623. }
  624. }
  625. class DashletHeaderStatic extends Dashlet
  626. {
  627. public function __construct($sId)
  628. {
  629. parent::__construct($sId);
  630. $this->aProperties['title'] = 'Contacts';
  631. $this->aProperties['icon'] = 'itop-config-mgmt-1.0.0/images/contact.png';
  632. }
  633. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  634. {
  635. $sTitle = $this->aProperties['title'];
  636. $sIcon = $this->aProperties['icon'];
  637. $sTitleReady = str_replace(':', '_', $sTitle);
  638. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  639. $oPage->add('<div class="dashlet-content">');
  640. $oPage->add('<div class="main_header">');
  641. $oPage->add('<img src="'.$sIconPath.'">');
  642. $oPage->add('<h1>'.Dict::S($sTitleReady).'</h1>');
  643. $oPage->add('</div>');
  644. $oPage->add('</div>');
  645. }
  646. public function GetPropertiesFields(DesignerForm $oForm)
  647. {
  648. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  649. $oForm->AddField($oField);
  650. $oField = new DesignerIconSelectionField('icon', 'Icon', $this->aProperties['icon']);
  651. $aAllIcons = self::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
  652. ksort($aAllIcons);
  653. $aValues = array();
  654. foreach($aAllIcons as $sFilePath)
  655. {
  656. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  657. }
  658. $oField->SetAllowedValues($aValues);
  659. $oForm->AddField($oField);
  660. }
  661. static public function GetInfo()
  662. {
  663. return array(
  664. 'label' => 'Header',
  665. 'icon' => 'images/dashlet-header.png',
  666. 'description' => 'Header with stats (grouped by...)',
  667. );
  668. }
  669. static public function FindIcons($sBaseDir, $sDir = '')
  670. {
  671. $aResult = array();
  672. // Populate automatically the list of icon files
  673. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  674. {
  675. while (($sFile = readdir($hDir)) !== false)
  676. {
  677. $aMatches = array();
  678. if (($sFile != '.') && ($sFile != '..') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  679. {
  680. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  681. $aResult = array_merge($aResult, self::FindIcons($sBaseDir, $sDirSubPath));
  682. }
  683. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  684. {
  685. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  686. }
  687. }
  688. closedir($hDir);
  689. }
  690. return $aResult;
  691. }
  692. }
  693. class DashletHeaderDynamic extends Dashlet
  694. {
  695. public function __construct($sId)
  696. {
  697. parent::__construct($sId);
  698. $this->aProperties['title'] = 'Contacts';
  699. $this->aProperties['icon'] = 'itop-config-mgmt-1.0.0/images/contact.png';
  700. $this->aProperties['subtitle'] = 'Contacts';
  701. $this->aProperties['query'] = 'SELECT Contact';
  702. $this->aProperties['group_by'] = 'status';
  703. $this->aProperties['values'] = 'active,inactive,terminated';
  704. }
  705. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  706. {
  707. $sTitle = $this->aProperties['title'];
  708. $sIcon = $this->aProperties['icon'];
  709. $sSubtitle = $this->aProperties['subtitle'];
  710. $sQuery = $this->aProperties['query'];
  711. $sGroupBy = $this->aProperties['group_by'];
  712. $sValues = $this->aProperties['values'];
  713. $oFilter = DBObjectSearch::FromOQL($sQuery);
  714. $sClass = $oFilter->GetClass();
  715. $sTitleReady = str_replace(':', '_', $sTitle);
  716. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  717. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  718. $aValues = null;
  719. if (MetaModel::IsValidAttCode($sClass, $sGroupBy))
  720. {
  721. if ($sValues == '')
  722. {
  723. $aAllowed = MetaModel::GetAllowedValues_att($sClass, $sGroupBy);
  724. if (is_array($aAllowed))
  725. {
  726. $aValues = array_keys($aAllowed);
  727. }
  728. }
  729. else
  730. {
  731. $aValues = explode(',', $sValues);
  732. }
  733. }
  734. if (is_array($aValues))
  735. {
  736. // Stats grouped by <group_by>
  737. $aCSV = implode(',', $aValues);
  738. $aExtraParams = array(
  739. 'title[block]' => $sTitleReady,
  740. 'label[block]' => $sSubtitleReady,
  741. 'status[block]' => $sGroupBy,
  742. 'status_codes[block]' => $aCSV,
  743. 'context_filter' => 1,
  744. );
  745. }
  746. else
  747. {
  748. // Simple stats
  749. $aExtraParams = array(
  750. 'title[block]' => $sTitleReady,
  751. 'label[block]' => $sSubtitleReady,
  752. 'context_filter' => 1,
  753. );
  754. }
  755. $oPage->add('<div class="dashlet-content">');
  756. $oPage->add('<div class="main_header">');
  757. $oPage->add('<img src="'.$sIconPath.'">');
  758. $oBlock = new DisplayBlock($oFilter, 'summary');
  759. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  760. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  761. $oPage->add('</div>');
  762. $oPage->add('</div>');
  763. }
  764. public function GetPropertiesFields(DesignerForm $oForm)
  765. {
  766. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  767. $oForm->AddField($oField);
  768. $oField = new DesignerIconSelectionField('icon', 'Icon', $this->aProperties['icon']);
  769. $aAllIcons = DashletHeaderStatic::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
  770. ksort($aAllIcons);
  771. $aValues = array();
  772. foreach($aAllIcons as $sFilePath)
  773. {
  774. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  775. }
  776. $oField->SetAllowedValues($aValues);
  777. $oForm->AddField($oField);
  778. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  779. $oForm->AddField($oField);
  780. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  781. $oForm->AddField($oField);
  782. // Group by field: build the list of possible values (attribute codes + ...)
  783. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  784. $sClass = $oSearch->GetClass();
  785. $aGroupBy = array();
  786. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  787. {
  788. if (!$oAttDef->IsScalar()) continue; // skip link sets
  789. $sLabel = $oAttDef->GetLabel();
  790. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  791. {
  792. $sLabel = $oAttDef->GetLabel().' (strict)';
  793. }
  794. $aGroupBy[$sAttCode] = $sLabel;
  795. }
  796. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  797. $oField->SetAllowedValues($aGroupBy);
  798. $oForm->AddField($oField);
  799. $oField = new DesignerTextField('values', 'Values (CSV list)', $this->aProperties['values']);
  800. $oForm->AddField($oField);
  801. }
  802. static public function GetInfo()
  803. {
  804. return array(
  805. 'label' => 'Header with statistics',
  806. 'icon' => 'images/dashlet-header-stats.png',
  807. 'description' => 'Header with stats (grouped by...)',
  808. );
  809. }
  810. }
  811. class DashletBadge extends Dashlet
  812. {
  813. public function __construct($sId)
  814. {
  815. parent::__construct($sId);
  816. $this->aProperties['class'] = 'Contact';
  817. $this->aCSSClasses[] = 'dashlet-inline';
  818. $this->aCSSClasses[] = 'dashlet-badge';
  819. }
  820. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  821. {
  822. $sClass = $this->aProperties['class'];
  823. $oPage->add('<div class="dashlet-content">');
  824. $oFilter = new DBObjectSearch($sClass);
  825. $oBlock = new DisplayBlock($oFilter, 'actions');
  826. $aExtraParams = array(
  827. 'context_filter' => 1,
  828. );
  829. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  830. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  831. $oPage->add('</div>');
  832. if ($bEditMode)
  833. {
  834. // Since the container div is not rendered the same way in edit mode, add the 'inline' style to it
  835. $oPage->add_ready_script("$('#dashlet_".$this->sId."').addClass('dashlet-inline');");
  836. }
  837. }
  838. public function GetPropertiesFields(DesignerForm $oForm)
  839. {
  840. $oClassesSet = new ValueSetEnumClasses('bizmodel', array());
  841. $aClasses = $oClassesSet->GetValues(array());
  842. $aLinkClasses = array();
  843. foreach(MetaModel::GetClasses('bizmodel') as $sClass)
  844. {
  845. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  846. {
  847. if ($oAttDef instanceof AttributeLinkedSetIndirect)
  848. {
  849. $aLinkClasses[$oAttDef->GetLinkedClass()] = true;
  850. }
  851. }
  852. }
  853. $oField = new DesignerIconSelectionField('class', 'Class', $this->aProperties['class']);
  854. ksort($aClasses);
  855. $aValues = array();
  856. foreach($aClasses as $sClass => $sClass)
  857. {
  858. if (!array_key_exists($sClass, $aLinkClasses))
  859. {
  860. $sIconUrl = MetaModel::GetClassIcon($sClass, false);
  861. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  862. if (($sIconUrl == '') || !file_exists($sIconFilePath))
  863. {
  864. // The icon does not exist, leet's use a transparent one of the same size.
  865. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  866. }
  867. $aValues[] = array('value' => $sClass, 'label' => $sClass, 'icon' => $sIconUrl);
  868. }
  869. }
  870. $oField->SetAllowedValues($aValues);
  871. $oForm->AddField($oField);
  872. }
  873. static public function GetInfo()
  874. {
  875. return array(
  876. 'label' => 'Badge',
  877. 'icon' => 'images/dashlet-badge.png',
  878. 'description' => 'Object Icon with new/search',
  879. );
  880. }
  881. }
  882. class DashletProto extends Dashlet
  883. {
  884. public function __construct($sId)
  885. {
  886. parent::__construct($sId);
  887. $this->aProperties['class'] = 'Foo';
  888. }
  889. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  890. {
  891. $sClass = $this->aProperties['class'];
  892. $oFilter = DBObjectSearch::FromOQL('SELECT FunctionalCI AS fci');
  893. $sGroupBy1 = 'status';
  894. //$sGroupBy2 = 'org_id_friendlyname';
  895. $sGroupBy2 = 'org_id';
  896. $sHtmlTitle = "Hardcoded on $sGroupBy1 and $sGroupBy2...";
  897. $sAlias = $oFilter->GetClassAlias();
  898. $oGroupByExp1 = new FieldExpression($sGroupBy1, $sAlias);
  899. $sGroupByLabel1 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy1);
  900. $oGroupByExp2 = new FieldExpression($sGroupBy2, $sAlias);
  901. $sGroupByLabel2 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy2);
  902. $aGroupBy = array();
  903. $aGroupBy['grouped_by_1'] = $oGroupByExp1;
  904. $aGroupBy['grouped_by_2'] = $oGroupByExp2;
  905. $sSql = MetaModel::MakeGroupByQuery($oFilter, array(), $aGroupBy);
  906. $aRes = CMDBSource::QueryToArray($sSql);
  907. $iTotalCount = 0;
  908. $aData = array();
  909. $oAppContext = new ApplicationContext();
  910. $sParams = $oAppContext->GetForLink();
  911. foreach ($aRes as $aRow)
  912. {
  913. $iCount = $aRow['_itop_count_'];
  914. $iTotalCount += $iCount;
  915. $sValue1 = $aRow['grouped_by_1'];
  916. $sValue2 = $aRow['grouped_by_2'];
  917. $sDisplayValue1 = $aGroupBy['grouped_by_1']->MakeValueLabel($oFilter, $sValue1, $sValue1); // default to the raw value
  918. $sDisplayValue2 = $aGroupBy['grouped_by_2']->MakeValueLabel($oFilter, $sValue2, $sValue2); // default to the raw value
  919. // Build the search for this subset
  920. $oSubsetSearch = clone $oFilter;
  921. $oCondition = new BinaryExpression($oGroupByExp1, '=', new ScalarExpression($sValue1));
  922. $oSubsetSearch->AddConditionExpression($oCondition);
  923. $oCondition = new BinaryExpression($oGroupByExp2, '=', new ScalarExpression($sValue2));
  924. $oSubsetSearch->AddConditionExpression($oCondition);
  925. $sFilter = urlencode($oSubsetSearch->serialize());
  926. $aData[] = array (
  927. 'group1' => $sDisplayValue1,
  928. 'group2' => $sDisplayValue2,
  929. 'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"
  930. ); // TO DO: add the context information
  931. }
  932. $aAttribs =array(
  933. 'group1' => array('label' => $sGroupByLabel1, 'description' => ''),
  934. 'group2' => array('label' => $sGroupByLabel2, 'description' => ''),
  935. 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
  936. );
  937. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  938. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  939. $oPage->p(Dict::Format('UI:Pagination:HeaderNoSelection', $iTotalCount));
  940. $oPage->table($aAttribs, $aData);
  941. $oPage->add('</div>');
  942. }
  943. public function GetPropertiesFields(DesignerForm $oForm)
  944. {
  945. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  946. $oForm->AddField($oField);
  947. }
  948. static public function GetInfo()
  949. {
  950. return array(
  951. 'label' => 'Test3D',
  952. 'icon' => 'images/dashlet-groupby2-table.png',
  953. 'description' => 'Group by on two dimensions',
  954. );
  955. }
  956. }
  957. class DashletHeatMap extends Dashlet
  958. {
  959. public function __construct($sId)
  960. {
  961. parent::__construct($sId);
  962. $this->aProperties['class'] = 'Contact';
  963. $this->aProperties['title'] = 'Test';
  964. }
  965. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  966. {
  967. $sTitle = addslashes($this->aProperties['title']);
  968. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  969. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  970. $oPage->add_ready_script("$('#chart_{$sId}').heatmap_chart({chart_label: '$sTitle'});");
  971. }
  972. public function GetPropertiesFields(DesignerForm $oForm)
  973. {
  974. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  975. $oForm->AddField($oField);
  976. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  977. $oForm->AddField($oField);
  978. }
  979. static public function GetInfo()
  980. {
  981. return array(
  982. 'label' => 'Heatmap (Raphael)',
  983. 'icon' => 'images/dashlet-heatmap.png',
  984. 'description' => 'Pure JS Heat Map Chart',
  985. );
  986. }
  987. }