dashlet.class.inc.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. <?php
  2. // Copyright (C) 2012 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. require_once(APPROOT.'application/forms.class.inc.php');
  17. /**
  18. * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard)
  19. *
  20. */
  21. abstract class Dashlet
  22. {
  23. protected $sId;
  24. protected $bRedrawNeeded;
  25. protected $bFormRedrawNeeded;
  26. protected $aProperties; // array of {property => value}
  27. protected $aCSSClasses;
  28. public function __construct($sId)
  29. {
  30. $this->sId = $sId;
  31. $this->bRedrawNeeded = true; // By default: redraw each time a property changes
  32. $this->bFormRedrawNeeded = false; // By default: no need to redraw the form (independent fields)
  33. $this->aProperties = array(); // By default: there is no property
  34. $this->aCSSClasses = array('dashlet');
  35. }
  36. // Assuming that a property has the type of its default value, set in the constructor
  37. //
  38. public function Str2Prop($sProperty, $sValue)
  39. {
  40. $refValue = $this->aProperties[$sProperty];
  41. $sRefType = gettype($refValue);
  42. if ($sRefType == 'boolean')
  43. {
  44. $ret = ($sValue == 'true');
  45. }
  46. else
  47. {
  48. $ret = $sValue;
  49. settype($ret, $sRefType);
  50. }
  51. return $ret;
  52. }
  53. public function Prop2Str($value)
  54. {
  55. if (gettype($value) == 'boolean')
  56. {
  57. $sRet = $value ? 'true' : 'false';
  58. }
  59. else
  60. {
  61. $sRet = (string) $value;
  62. }
  63. return $sRet;
  64. }
  65. public function FromDOMNode($oDOMNode)
  66. {
  67. foreach ($this->aProperties as $sProperty => $value)
  68. {
  69. $this->oDOMNode = $oDOMNode->getElementsByTagName($sProperty)->item(0);
  70. if ($this->oDOMNode != null)
  71. {
  72. $newvalue = $this->Str2Prop($sProperty, $this->oDOMNode->textContent);
  73. $this->aProperties[$sProperty] = $newvalue;
  74. }
  75. }
  76. }
  77. public function ToDOMNode($oDOMNode)
  78. {
  79. foreach ($this->aProperties as $sProperty => $value)
  80. {
  81. $sXmlValue = $this->Prop2Str($value);
  82. $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty, $sXmlValue);
  83. $oDOMNode->appendChild($oPropNode);
  84. }
  85. }
  86. public function FromXml($sXml)
  87. {
  88. $oDomDoc = new DOMDocument('1.0', 'UTF-8');
  89. $oDomDoc->loadXml($sXml);
  90. $this->FromDOMNode($oDomDoc->firstChild);
  91. }
  92. public function FromParams($aParams)
  93. {
  94. foreach ($this->aProperties as $sProperty => $value)
  95. {
  96. if (array_key_exists($sProperty, $aParams))
  97. {
  98. $this->aProperties[$sProperty] = $aParams[$sProperty];
  99. }
  100. }
  101. }
  102. public function DoRender($oPage, $bEditMode = false, $aExtraParams = array())
  103. {
  104. $sCSSClasses = implode(' ', $this->aCSSClasses);
  105. if ($bEditMode)
  106. {
  107. $sId = $this->GetID();
  108. $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
  109. }
  110. else
  111. {
  112. $oPage->add('<div class="'.$sCSSClasses.'">');
  113. }
  114. $this->Render($oPage, $bEditMode, $aExtraParams);
  115. $oPage->add('</div>');
  116. if ($bEditMode)
  117. {
  118. $sClass = get_class($this);
  119. $oPage->add_ready_script(
  120. <<<EOF
  121. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  122. EOF
  123. );
  124. }
  125. }
  126. public function SetID($sId)
  127. {
  128. $this->sId = $sId;
  129. }
  130. public function GetID()
  131. {
  132. return $this->sId;
  133. }
  134. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  135. abstract public function GetPropertiesFields(DesignerForm $oForm);
  136. public function ToXml(DOMNode $oContainerNode)
  137. {
  138. }
  139. public function Update($aValues, $aUpdatedFields)
  140. {
  141. foreach($aUpdatedFields as $sProp)
  142. {
  143. if (array_key_exists($sProp, $this->aProperties))
  144. {
  145. $this->aProperties[$sProp] = $aValues[$sProp];
  146. }
  147. }
  148. return $this;
  149. }
  150. public function IsRedrawNeeded()
  151. {
  152. return $this->bRedrawNeeded;
  153. }
  154. public function IsFormRedrawNeeded()
  155. {
  156. return $this->bFormRedrawNeeded;
  157. }
  158. static public function GetInfo()
  159. {
  160. return array(
  161. 'label' => '',
  162. 'icon' => '',
  163. 'description' => '',
  164. );
  165. }
  166. public function GetForm()
  167. {
  168. $oForm = new DesignerForm();
  169. $oForm->SetPrefix("dashlet_". $this->GetID());
  170. $oForm->SetParamsContainer('params');
  171. $this->GetPropertiesFields($oForm);
  172. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  173. $oForm->AddField($oDashletClassField);
  174. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  175. $oForm->AddField($oDashletIdField);
  176. return $oForm;
  177. }
  178. static public function IsVisible()
  179. {
  180. return true;
  181. }
  182. static public function CanCreateFromOQL()
  183. {
  184. return false;
  185. }
  186. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  187. {
  188. // Default: do nothing since it's not supported
  189. }
  190. }
  191. class DashletEmptyCell extends Dashlet
  192. {
  193. public function __construct($sId)
  194. {
  195. parent::__construct($sId);
  196. }
  197. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  198. {
  199. $oPage->add('&nbsp;');
  200. }
  201. public function GetPropertiesFields(DesignerForm $oForm)
  202. {
  203. }
  204. static public function GetInfo()
  205. {
  206. return array(
  207. 'label' => 'Empty Cell',
  208. 'icon' => 'images/dashlet-text.png',
  209. 'description' => 'Empty Cell Dashlet Placeholder',
  210. );
  211. }
  212. static public function IsVisible()
  213. {
  214. return false;
  215. }
  216. }
  217. class DashletHelloWorld extends Dashlet
  218. {
  219. public function __construct($sId)
  220. {
  221. parent::__construct($sId);
  222. $this->aProperties['text'] = 'Hello World';
  223. }
  224. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  225. {
  226. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  227. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  228. $oPage->add_ready_script("$('#chart_{$sId}').pie_chart();");
  229. }
  230. public function GetPropertiesFields(DesignerForm $oForm)
  231. {
  232. $oField = new DesignerTextField('text', 'Text', $this->aProperties['text']);
  233. $oForm->AddField($oField);
  234. }
  235. static public function GetInfo()
  236. {
  237. return array(
  238. 'label' => 'Hello World',
  239. 'icon' => 'images/dashlet-text.png',
  240. 'description' => 'Hello World test Dashlet',
  241. );
  242. }
  243. }
  244. class DashletObjectList extends Dashlet
  245. {
  246. public function __construct($sId)
  247. {
  248. parent::__construct($sId);
  249. $this->aProperties['title'] = 'Hardcoded list of "my requests"';
  250. $this->aProperties['query'] = 'SELECT UserRequest AS i WHERE i.caller_id = :current_contact_id AND status NOT IN ("closed", "resolved")';
  251. $this->aProperties['menu'] = false;
  252. }
  253. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  254. {
  255. $sTitle = $this->aProperties['title'];
  256. $sQuery = $this->aProperties['query'];
  257. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  258. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  259. $oFilter = DBObjectSearch::FromOQL($sQuery);
  260. $oBlock = new DisplayBlock($oFilter, 'list');
  261. $aExtraParams = array(
  262. 'menu' => $sShowMenu,
  263. );
  264. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  265. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  266. $oPage->add('</div>');
  267. }
  268. public function GetPropertiesFields(DesignerForm $oForm)
  269. {
  270. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  271. $oForm->AddField($oField);
  272. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  273. $oForm->AddField($oField);
  274. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  275. $oForm->AddField($oField);
  276. }
  277. static public function GetInfo()
  278. {
  279. return array(
  280. 'label' => 'Object list',
  281. 'icon' => 'images/dashlet-list.png',
  282. 'description' => 'Object list dashlet',
  283. );
  284. }
  285. static public function CanCreateFromOQL()
  286. {
  287. return true;
  288. }
  289. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  290. {
  291. $oField = new DesignerTextField('title', 'Title', '');
  292. $oForm->AddField($oField);
  293. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  294. $oForm->AddField($oField);
  295. $oField = new DesignerBooleanField('menu', 'Menu', $this->aProperties['menu']);
  296. $oForm->AddField($oField);
  297. }
  298. }
  299. abstract class DashletGroupBy extends Dashlet
  300. {
  301. public function __construct($sId)
  302. {
  303. parent::__construct($sId);
  304. $this->aProperties['title'] = 'Hardcoded list of Contacts grouped by location';
  305. $this->aProperties['query'] = 'SELECT Contact';
  306. $this->aProperties['group_by'] = 'location_name';
  307. $this->aProperties['style'] = 'table';
  308. }
  309. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  310. {
  311. $sTitle = $this->aProperties['title'];
  312. $sQuery = $this->aProperties['query'];
  313. $sGroupBy = $this->aProperties['group_by'];
  314. $sStyle = $this->aProperties['style'];
  315. if ($sQuery == '')
  316. {
  317. $oPage->add('<p>Please enter a valid OQL query</p>');
  318. }
  319. elseif ($sGroupBy == '')
  320. {
  321. $oPage->add('<p>Please select the field on which the objects will be grouped together</p>');
  322. }
  323. else
  324. {
  325. $oFilter = DBObjectSearch::FromOQL($sQuery);
  326. $sClassAlias = $oFilter->GetClassAlias();
  327. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  328. {
  329. $sAttCode = $aMatches[1];
  330. $sFunction = $aMatches[2];
  331. switch($sFunction)
  332. {
  333. case 'hour':
  334. $sGroupByLabel = 'Hour of '.$sAttCode. ' (0-23)';
  335. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 31
  336. break;
  337. case 'month':
  338. $sGroupByLabel = 'Month of '.$sAttCode. ' (1 - 12)';
  339. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%m')"; // 0 -> 31
  340. break;
  341. case 'day_of_week':
  342. $sGroupByLabel = 'Day of week for '.$sAttCode. ' (sunday to saturday)';
  343. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
  344. break;
  345. case 'day_of_month':
  346. $sGroupByLabel = 'Day of month for'.$sAttCode;
  347. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%e')"; // 0 -> 31
  348. break;
  349. default:
  350. $sGroupByLabel = 'Unknown group by function '.$sFunction;
  351. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  352. }
  353. }
  354. else
  355. {
  356. $sAttCode = $sGroupBy;
  357. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  358. $sGroupByLabel = MetaModel::GetLabel($oFilter->GetClass(), $sAttCode);
  359. }
  360. switch($sStyle)
  361. {
  362. case 'bars':
  363. $sType = 'open_flash_chart';
  364. $aExtraParams = array(
  365. 'chart_type' => 'bars',
  366. 'chart_title' => $sTitle,
  367. 'group_by' => $sGroupByExpr,
  368. 'group_by_label' => $sGroupByLabel,
  369. );
  370. $sHtmlTitle = ''; // done in the itop block
  371. break;
  372. case 'pie':
  373. $sType = 'open_flash_chart';
  374. $aExtraParams = array(
  375. 'chart_type' => 'pie',
  376. 'chart_title' => $sTitle,
  377. 'group_by' => $sGroupByExpr,
  378. 'group_by_label' => $sGroupByLabel,
  379. );
  380. $sHtmlTitle = ''; // done in the itop block
  381. break;
  382. case 'table':
  383. default:
  384. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  385. $sType = 'count';
  386. $aExtraParams = array(
  387. 'group_by' => $sGroupByExpr,
  388. 'group_by_label' => $sGroupByLabel,
  389. );
  390. break;
  391. }
  392. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  393. if ($sHtmlTitle != '')
  394. {
  395. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  396. }
  397. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  398. $oBlock = new DisplayBlock($oFilter, $sType);
  399. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  400. $oPage->add('</div>');
  401. }
  402. }
  403. public function GetPropertiesFields(DesignerForm $oForm)
  404. {
  405. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  406. $oForm->AddField($oField);
  407. $oField = new DesignerLongTextField('query', 'Query', $this->aProperties['query']);
  408. $oForm->AddField($oField);
  409. // Group by field: build the list of possible values (attribute codes + ...)
  410. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  411. $sClass = $oSearch->GetClass();
  412. $aGroupBy = array();
  413. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  414. {
  415. if (!$oAttDef->IsScalar()) continue; // skip link sets
  416. $sLabel = $oAttDef->GetLabel();
  417. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  418. {
  419. $sLabel = $oAttDef->GetLabel().' (strict)';
  420. }
  421. $aGroupBy[$sAttCode] = $sLabel;
  422. if ($oAttDef instanceof AttributeDateTime)
  423. {
  424. $aGroupBy[$sAttCode.':hour'] = $oAttDef->GetLabel().' (hour)';
  425. $aGroupBy[$sAttCode.':month'] = $oAttDef->GetLabel().' (month)';
  426. $aGroupBy[$sAttCode.':day_of_week'] = $oAttDef->GetLabel().' (day of week)';
  427. $aGroupBy[$sAttCode.':day_of_month'] = $oAttDef->GetLabel().' (day of month)';
  428. }
  429. }
  430. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  431. $oField->SetAllowedValues($aGroupBy);
  432. $oForm->AddField($oField);
  433. $aStyles = array(
  434. 'pie' => 'Pie chart',
  435. 'bars' => 'Bar chart',
  436. 'table' => 'Table',
  437. );
  438. $oField = new DesignerComboField('style', 'Style', $this->aProperties['style']);
  439. $oField->SetAllowedValues($aStyles);
  440. $oForm->AddField($oField);
  441. }
  442. public function Update($aValues, $aUpdatedFields)
  443. {
  444. if (in_array('query', $aUpdatedFields))
  445. {
  446. $sCurrQuery = $aValues['query'];
  447. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  448. $sCurrClass = $oCurrSearch->GetClass();
  449. $sPrevQuery = $this->aProperties['query'];
  450. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  451. $sPrevClass = $oPrevSearch->GetClass();
  452. if ($sCurrClass != $sPrevClass)
  453. {
  454. $this->bFormRedrawNeeded = true;
  455. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  456. $this->aProperties['group_by'] = '';
  457. }
  458. }
  459. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  460. if (in_array('style', $aUpdatedFields))
  461. {
  462. switch($aValues['style'])
  463. {
  464. // Style changed, mutate to the specified type of chart
  465. case 'pie':
  466. $oDashlet = new DashletGroupByPie($this->sId);
  467. break;
  468. case 'bars':
  469. $oDashlet = new DashletGroupByBars($this->sId);
  470. break;
  471. case 'table':
  472. $oDashlet = new DashletGroupByTable($this->sId);
  473. break;
  474. }
  475. $oDashlet->FromParams($aValues);
  476. $oDashlet->bRedrawNeeded = true;
  477. $oDashlet->bFormRedrawNeeded = true;
  478. }
  479. return $oDashlet;
  480. }
  481. static public function GetInfo()
  482. {
  483. return array(
  484. 'label' => 'Objects grouped by...',
  485. 'icon' => 'images/dashlet-object-grouped.png',
  486. 'description' => 'Grouped objects dashlet',
  487. );
  488. }
  489. static public function CanCreateFromOQL()
  490. {
  491. return true;
  492. }
  493. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL)
  494. {
  495. $oField = new DesignerTextField('title', 'Title', '');
  496. $oForm->AddField($oField);
  497. $oField = new DesignerHiddenField('query', 'Query', $sOQL);
  498. $oForm->AddField($oField);
  499. // Group by field: build the list of possible values (attribute codes + ...)
  500. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  501. $sClass = $oSearch->GetClass();
  502. $aGroupBy = array();
  503. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  504. {
  505. if (!$oAttDef->IsScalar()) continue; // skip link sets
  506. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE)) continue; // skip external keys
  507. $aGroupBy[$sAttCode] = $oAttDef->GetLabel();
  508. if ($oAttDef instanceof AttributeDateTime)
  509. {
  510. //date_format(start_date, '%d')
  511. $aGroupBy['date_of_'.$sAttCode] = 'Day of '.$oAttDef->GetLabel();
  512. }
  513. }
  514. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  515. $oField->SetAllowedValues($aGroupBy);
  516. $oForm->AddField($oField);
  517. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  518. $oForm->AddField($oField);
  519. }
  520. }
  521. class DashletGroupByPie extends DashletGroupBy
  522. {
  523. public function __construct($sId)
  524. {
  525. parent::__construct($sId);
  526. $this->aProperties['style'] = 'pie';
  527. }
  528. static public function GetInfo()
  529. {
  530. return array(
  531. 'label' => 'Pie Chart',
  532. 'icon' => 'images/dashlet-pie-chart.png',
  533. 'description' => 'Pie Chart',
  534. );
  535. }
  536. }
  537. class DashletGroupByPie2 extends DashletGroupByPie
  538. {
  539. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  540. {
  541. $sTitle = addslashes($this->aProperties['title']);
  542. $sQuery = $this->aProperties['query'];
  543. $sGroupBy = $this->aProperties['group_by'];
  544. $oSearch = DBObjectSearch::FromOQL($sQuery);
  545. $sClassAlias = $oSearch->GetClassAlias();
  546. $aQueryParams = array();
  547. $aGroupBy = array();
  548. $oGroupByExp = Expression::FromOQL($sClassAlias.'.'.$sGroupBy);
  549. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  550. $sSql = MetaModel::MakeGroupByQuery($oSearch, $aQueryParams, $aGroupBy);
  551. $aRes = CMDBSource::QueryToArray($sSql);
  552. $aGroupBy = array();
  553. $aLabels = array();
  554. $iTotalCount = 0;
  555. foreach ($aRes as $aRow)
  556. {
  557. $sValue = $aRow['grouped_by_1'];
  558. $aLabels[] = ($sValue == '') ? 'Empty (%%.%%)' : $sValue.' (%%.%%)'; //TODO: localize
  559. $aGroupBy[] = (int) $aRow['_itop_count_'];
  560. $iTotalCount += $aRow['_itop_count_'];
  561. }
  562. $aURLs = array();
  563. $sContext = ''; //TODO get the context ??
  564. foreach($aGroupBy as $sValue => $iValue)
  565. {
  566. // Build the search for this subset
  567. $oSubsetSearch = clone $oSearch;
  568. $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($sValue));
  569. $oSubsetSearch->AddConditionExpression($oCondition);
  570. $aURLs[] = 'http://www.combodo.com/itop'; //utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html{$sContext}&filter=".addslashes($oSubsetSearch->serialize());
  571. }
  572. $sJSValues = json_encode($aGroupBy);
  573. $sJSHrefs = json_encode($aURLs);
  574. $sJSLabels = json_encode($aLabels);
  575. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  576. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  577. $oPage->add_ready_script("$('#chart_{$sId}').pie_chart({chart_label: '$sTitle', values: $sJSValues, labels: $sJSLabels, hrefs: $sJSHrefs });");
  578. }
  579. static public function GetInfo()
  580. {
  581. return array(
  582. 'label' => 'Pie (Raphael)',
  583. 'icon' => 'images/dashlet-pie-chart.png',
  584. 'description' => 'Pure JS Pie Chart',
  585. );
  586. }
  587. }
  588. class DashletGroupByBars extends DashletGroupBy
  589. {
  590. public function __construct($sId)
  591. {
  592. parent::__construct($sId);
  593. $this->aProperties['style'] = 'bars';
  594. }
  595. static public function GetInfo()
  596. {
  597. return array(
  598. 'label' => 'Bar Chart',
  599. 'icon' => 'images/dashlet-bar-chart.png',
  600. 'description' => 'Bar Chart',
  601. );
  602. }
  603. }
  604. class DashletGroupByTable extends DashletGroupBy
  605. {
  606. public function __construct($sId)
  607. {
  608. parent::__construct($sId);
  609. $this->aProperties['style'] = 'table';
  610. }
  611. static public function GetInfo()
  612. {
  613. return array(
  614. 'label' => 'Group By (table)',
  615. 'icon' => 'images/dashlet-groupby-table.png',
  616. 'description' => 'List (Grouped by a field)',
  617. );
  618. }
  619. }
  620. class DashletHeaderStatic extends Dashlet
  621. {
  622. public function __construct($sId)
  623. {
  624. parent::__construct($sId);
  625. $this->aProperties['title'] = 'Contacts';
  626. $this->aProperties['icon'] = 'itop-config-mgmt-1.0.0/images/contact.png';
  627. }
  628. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  629. {
  630. $sTitle = $this->aProperties['title'];
  631. $sIcon = $this->aProperties['icon'];
  632. $sTitleReady = str_replace(':', '_', $sTitle);
  633. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  634. $oPage->add('<div class="dashlet-content">');
  635. $oPage->add('<div class="main_header">');
  636. $oPage->add('<img src="'.$sIconPath.'">');
  637. $oPage->add('<h1>'.Dict::S($sTitleReady).'</h1>');
  638. $oPage->add('</div>');
  639. $oPage->add('</div>');
  640. }
  641. public function GetPropertiesFields(DesignerForm $oForm)
  642. {
  643. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  644. $oForm->AddField($oField);
  645. $oField = new DesignerTextField('icon', 'Icon', $this->aProperties['icon']);
  646. $oForm->AddField($oField);
  647. }
  648. static public function GetInfo()
  649. {
  650. return array(
  651. 'label' => 'Header',
  652. 'icon' => 'images/dashlet-header.png',
  653. 'description' => 'Header with stats (grouped by...)',
  654. );
  655. }
  656. }
  657. class DashletHeaderDynamic extends Dashlet
  658. {
  659. public function __construct($sId)
  660. {
  661. parent::__construct($sId);
  662. $this->aProperties['title'] = 'Contacts';
  663. $this->aProperties['icon'] = 'itop-config-mgmt-1.0.0/images/contact.png';
  664. $this->aProperties['subtitle'] = 'Contacts';
  665. $this->aProperties['query'] = 'SELECT Contact';
  666. $this->aProperties['group_by'] = 'status';
  667. $this->aProperties['values'] = 'active,inactive,terminated';
  668. }
  669. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  670. {
  671. $sTitle = $this->aProperties['title'];
  672. $sIcon = $this->aProperties['icon'];
  673. $sSubtitle = $this->aProperties['subtitle'];
  674. $sQuery = $this->aProperties['query'];
  675. $sGroupBy = $this->aProperties['group_by'];
  676. $sValues = $this->aProperties['values'];
  677. $oFilter = DBObjectSearch::FromOQL($sQuery);
  678. $sClass = $oFilter->GetClass();
  679. $sTitleReady = str_replace(':', '_', $sTitle);
  680. $sSubtitleReady = str_replace(':', '_', $sSubtitle);
  681. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  682. $aValues = null;
  683. if (MetaModel::IsValidAttCode($sClass, $sGroupBy))
  684. {
  685. if ($sValues == '')
  686. {
  687. $aAllowed = MetaModel::GetAllowedValues_att($sClass, $sGroupBy);
  688. if (is_array($aAllowed))
  689. {
  690. $aValues = array_keys($aAllowed);
  691. }
  692. }
  693. else
  694. {
  695. $aValues = explode(',', $sValues);
  696. }
  697. }
  698. if (is_array($aValues))
  699. {
  700. // Stats grouped by <group_by>
  701. $aCSV = implode(',', $aValues);
  702. $aExtraParams = array(
  703. 'title[block]' => $sTitleReady,
  704. 'label[block]' => $sSubtitleReady,
  705. 'status[block]' => $sGroupBy,
  706. 'status_codes[block]' => $aCSV,
  707. 'context_filter' => 1,
  708. );
  709. }
  710. else
  711. {
  712. // Simple stats
  713. $aExtraParams = array(
  714. 'title[block]' => $sTitleReady,
  715. 'label[block]' => $sSubtitleReady,
  716. 'context_filter' => 1,
  717. );
  718. }
  719. $oPage->add('<div class="dashlet-content">');
  720. $oPage->add('<div class="main_header">');
  721. $oPage->add('<img src="'.$sIconPath.'">');
  722. $oBlock = new DisplayBlock($oFilter, 'summary');
  723. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  724. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  725. $oPage->add('</div>');
  726. $oPage->add('</div>');
  727. }
  728. public function GetPropertiesFields(DesignerForm $oForm)
  729. {
  730. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  731. $oForm->AddField($oField);
  732. $oField = new DesignerTextField('icon', 'Icon', $this->aProperties['icon']);
  733. $oForm->AddField($oField);
  734. $oField = new DesignerTextField('subtitle', 'Subtitle', $this->aProperties['subtitle']);
  735. $oForm->AddField($oField);
  736. $oField = new DesignerTextField('query', 'Query', $this->aProperties['query']);
  737. $oForm->AddField($oField);
  738. // Group by field: build the list of possible values (attribute codes + ...)
  739. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  740. $sClass = $oSearch->GetClass();
  741. $aGroupBy = array();
  742. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  743. {
  744. if (!$oAttDef->IsScalar()) continue; // skip link sets
  745. $sLabel = $oAttDef->GetLabel();
  746. if ($oAttDef->IsExternalKey(EXTKEY_ABSOLUTE))
  747. {
  748. $sLabel = $oAttDef->GetLabel().' (strict)';
  749. }
  750. $aGroupBy[$sAttCode] = $sLabel;
  751. }
  752. $oField = new DesignerComboField('group_by', 'Group by', $this->aProperties['group_by']);
  753. $oField->SetAllowedValues($aGroupBy);
  754. $oForm->AddField($oField);
  755. $oField = new DesignerTextField('values', 'Values (CSV list)', $this->aProperties['values']);
  756. $oForm->AddField($oField);
  757. }
  758. static public function GetInfo()
  759. {
  760. return array(
  761. 'label' => 'Header with statistics',
  762. 'icon' => 'images/dashlet-header-stats.png',
  763. 'description' => 'Header with stats (grouped by...)',
  764. );
  765. }
  766. }
  767. class DashletBadge extends Dashlet
  768. {
  769. public function __construct($sId)
  770. {
  771. parent::__construct($sId);
  772. $this->aProperties['class'] = 'Contact';
  773. $this->aCSSClasses[] = 'dashlet-inline';
  774. $this->aCSSClasses[] = 'dashlet-badge';
  775. }
  776. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  777. {
  778. $sClass = $this->aProperties['class'];
  779. $oPage->add('<div class="dashlet-content">');
  780. $oFilter = new DBObjectSearch($sClass);
  781. $oBlock = new DisplayBlock($oFilter, 'actions');
  782. $aExtraParams = array(
  783. 'context_filter' => 1,
  784. );
  785. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  786. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  787. $oPage->add('</div>');
  788. }
  789. public function GetPropertiesFields(DesignerForm $oForm)
  790. {
  791. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  792. $oForm->AddField($oField);
  793. }
  794. static public function GetInfo()
  795. {
  796. return array(
  797. 'label' => 'Badge',
  798. 'icon' => 'images/dashlet-badge.png',
  799. 'description' => 'Object Icon with new/search',
  800. );
  801. }
  802. }
  803. class DashletProto extends Dashlet
  804. {
  805. public function __construct($sId)
  806. {
  807. parent::__construct($sId);
  808. $this->aProperties['class'] = 'Foo';
  809. }
  810. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  811. {
  812. $sClass = $this->aProperties['class'];
  813. $oFilter = DBObjectSearch::FromOQL('SELECT FunctionalCI AS fci');
  814. $sGroupBy1 = 'status';
  815. //$sGroupBy2 = 'org_id_friendlyname';
  816. $sGroupBy2 = 'org_id';
  817. $sHtmlTitle = "Hardcoded on $sGroupBy1 and $sGroupBy2...";
  818. $sAlias = $oFilter->GetClassAlias();
  819. $oGroupByExp1 = new FieldExpression($sGroupBy1, $sAlias);
  820. $sGroupByLabel1 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy1);
  821. $oGroupByExp2 = new FieldExpression($sGroupBy2, $sAlias);
  822. $sGroupByLabel2 = MetaModel::GetLabel($oFilter->GetClass(), $sGroupBy2);
  823. $aGroupBy = array();
  824. $aGroupBy['grouped_by_1'] = $oGroupByExp1;
  825. $aGroupBy['grouped_by_2'] = $oGroupByExp2;
  826. $sSql = MetaModel::MakeGroupByQuery($oFilter, array(), $aGroupBy);
  827. $aRes = CMDBSource::QueryToArray($sSql);
  828. $iTotalCount = 0;
  829. $aData = array();
  830. $oAppContext = new ApplicationContext();
  831. $sParams = $oAppContext->GetForLink();
  832. foreach ($aRes as $aRow)
  833. {
  834. $iCount = $aRow['_itop_count_'];
  835. $iTotalCount += $iCount;
  836. $sValue1 = $aRow['grouped_by_1'];
  837. $sValue2 = $aRow['grouped_by_2'];
  838. $sDisplayValue1 = $aGroupBy['grouped_by_1']->MakeValueLabel($oFilter, $sValue1, $sValue1); // default to the raw value
  839. $sDisplayValue2 = $aGroupBy['grouped_by_2']->MakeValueLabel($oFilter, $sValue2, $sValue2); // default to the raw value
  840. // Build the search for this subset
  841. $oSubsetSearch = clone $oFilter;
  842. $oCondition = new BinaryExpression($oGroupByExp1, '=', new ScalarExpression($sValue1));
  843. $oSubsetSearch->AddConditionExpression($oCondition);
  844. $oCondition = new BinaryExpression($oGroupByExp2, '=', new ScalarExpression($sValue2));
  845. $oSubsetSearch->AddConditionExpression($oCondition);
  846. $sFilter = urlencode($oSubsetSearch->serialize());
  847. $aData[] = array (
  848. 'group1' => $sDisplayValue1,
  849. 'group2' => $sDisplayValue2,
  850. 'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"
  851. ); // TO DO: add the context information
  852. }
  853. $aAttribs =array(
  854. 'group1' => array('label' => $sGroupByLabel1, 'description' => ''),
  855. 'group2' => array('label' => $sGroupByLabel2, 'description' => ''),
  856. 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
  857. );
  858. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  859. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  860. $oPage->p(Dict::Format('UI:Pagination:HeaderNoSelection', $iTotalCount));
  861. $oPage->table($aAttribs, $aData);
  862. $oPage->add('</div>');
  863. }
  864. public function GetPropertiesFields(DesignerForm $oForm)
  865. {
  866. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  867. $oForm->AddField($oField);
  868. }
  869. static public function GetInfo()
  870. {
  871. return array(
  872. 'label' => 'Test3D',
  873. 'icon' => 'images/dashlet-groupby2-table.png',
  874. 'description' => 'Group by on two dimensions',
  875. );
  876. }
  877. }
  878. class DashletHeatMap extends Dashlet
  879. {
  880. public function __construct($sId)
  881. {
  882. parent::__construct($sId);
  883. $this->aProperties['class'] = 'Contact';
  884. $this->aProperties['title'] = 'Test';
  885. }
  886. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  887. {
  888. $sTitle = addslashes($this->aProperties['title']);
  889. $sId = 'chart_'.($bEditMode? 'edit_' : '').$this->sId;
  890. $oPage->add('<div id="chart_'.$sId.'" class="dashlet-content"></div>');
  891. $oPage->add_ready_script("$('#chart_{$sId}').heatmap_chart({chart_label: '$sTitle'});");
  892. }
  893. public function GetPropertiesFields(DesignerForm $oForm)
  894. {
  895. $oField = new DesignerTextField('title', 'Title', $this->aProperties['title']);
  896. $oForm->AddField($oField);
  897. $oField = new DesignerTextField('class', 'Class', $this->aProperties['class']);
  898. $oForm->AddField($oField);
  899. }
  900. static public function GetInfo()
  901. {
  902. return array(
  903. 'label' => 'Heatmap (Raphael)',
  904. 'icon' => 'images/dashlet-heatmap.png',
  905. 'description' => 'Pure JS Heat Map Chart',
  906. );
  907. }
  908. }