dashlet.class.inc.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. require_once(APPROOT.'application/forms.class.inc.php');
  19. /**
  20. * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard)
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. abstract class Dashlet
  26. {
  27. protected $oModelReflection;
  28. protected $sId;
  29. protected $bRedrawNeeded;
  30. protected $bFormRedrawNeeded;
  31. protected $aProperties; // array of {property => value}
  32. protected $aCSSClasses;
  33. public function __construct(ModelReflection $oModelReflection, $sId)
  34. {
  35. $this->oModelReflection = $oModelReflection;
  36. $this->sId = $sId;
  37. $this->bRedrawNeeded = true; // By default: redraw each time a property changes
  38. $this->bFormRedrawNeeded = false; // By default: no need to redraw the form (independent fields)
  39. $this->aProperties = array(); // By default: there is no property
  40. $this->aCSSClasses = array('dashlet');
  41. }
  42. // Assuming that a property has the type of its default value, set in the constructor
  43. //
  44. public function Str2Prop($sProperty, $sValue)
  45. {
  46. $refValue = $this->aProperties[$sProperty];
  47. $sRefType = gettype($refValue);
  48. if ($sRefType == 'boolean')
  49. {
  50. $ret = ($sValue == 'true');
  51. }
  52. elseif ($sRefType == 'array')
  53. {
  54. $ret = explode(',', $sValue);
  55. }
  56. else
  57. {
  58. $ret = $sValue;
  59. settype($ret, $sRefType);
  60. }
  61. return $ret;
  62. }
  63. public function Prop2Str($value)
  64. {
  65. $sType = gettype($value);
  66. if ($sType == 'boolean')
  67. {
  68. $sRet = $value ? 'true' : 'false';
  69. }
  70. elseif ($sType == 'array')
  71. {
  72. $sRet = implode(',', $value);
  73. }
  74. else
  75. {
  76. $sRet = (string) $value;
  77. }
  78. return $sRet;
  79. }
  80. public function FromDOMNode($oDOMNode)
  81. {
  82. foreach ($this->aProperties as $sProperty => $value)
  83. {
  84. $this->oDOMNode = $oDOMNode->getElementsByTagName($sProperty)->item(0);
  85. if ($this->oDOMNode != null)
  86. {
  87. $newvalue = $this->Str2Prop($sProperty, $this->oDOMNode->textContent);
  88. $this->aProperties[$sProperty] = $newvalue;
  89. }
  90. }
  91. }
  92. public function ToDOMNode($oDOMNode)
  93. {
  94. foreach ($this->aProperties as $sProperty => $value)
  95. {
  96. $sXmlValue = $this->Prop2Str($value);
  97. $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty, $sXmlValue);
  98. $oDOMNode->appendChild($oPropNode);
  99. }
  100. }
  101. public function FromXml($sXml)
  102. {
  103. $oDomDoc = new DOMDocument('1.0', 'UTF-8');
  104. $oDomDoc->loadXml($sXml);
  105. $this->FromDOMNode($oDomDoc->firstChild);
  106. }
  107. public function FromParams($aParams)
  108. {
  109. foreach ($this->aProperties as $sProperty => $value)
  110. {
  111. if (array_key_exists($sProperty, $aParams))
  112. {
  113. $this->aProperties[$sProperty] = $aParams[$sProperty];
  114. }
  115. }
  116. }
  117. public function DoRender($oPage, $bEditMode = false, $bEnclosingDiv = true, $aExtraParams = array())
  118. {
  119. $sCSSClasses = implode(' ', $this->aCSSClasses);
  120. $sId = $this->GetID();
  121. if ($bEnclosingDiv)
  122. {
  123. if ($bEditMode)
  124. {
  125. $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
  126. }
  127. else
  128. {
  129. $oPage->add('<div class="'.$sCSSClasses.'">');
  130. }
  131. }
  132. try
  133. {
  134. $this->Render($oPage, $bEditMode, $aExtraParams);
  135. }
  136. catch(UnknownClassOqlException $e)
  137. {
  138. // Maybe the class is part of a non-installed module, fail silently
  139. // Except in Edit mode
  140. if ($bEditMode)
  141. {
  142. $oPage->add('<div class="dashlet-content">');
  143. $oPage->add('<h2>'.$e->GetUserFriendlyDescription().'</h2>');
  144. $oPage->add('</div>');
  145. }
  146. }
  147. catch(OqlException $e)
  148. {
  149. $oPage->add('<div class="dashlet-content">');
  150. $oPage->p($e->GetUserFriendlyDescription());
  151. $oPage->add('</div>');
  152. }
  153. catch(Exception $e)
  154. {
  155. $oPage->add('<div class="dashlet-content">');
  156. $oPage->p($e->getMessage());
  157. $oPage->add('</div>');
  158. }
  159. if ($bEnclosingDiv)
  160. {
  161. $oPage->add('</div>');
  162. }
  163. if ($bEditMode)
  164. {
  165. $sClass = get_class($this);
  166. $oPage->add_ready_script(
  167. <<<EOF
  168. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  169. EOF
  170. );
  171. }
  172. }
  173. public function SetID($sId)
  174. {
  175. $this->sId = $sId;
  176. }
  177. public function GetID()
  178. {
  179. return $this->sId;
  180. }
  181. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  182. abstract public function GetPropertiesFields(DesignerForm $oForm);
  183. public function ToXml(DOMNode $oContainerNode)
  184. {
  185. }
  186. public function Update($aValues, $aUpdatedFields)
  187. {
  188. foreach($aUpdatedFields as $sProp)
  189. {
  190. if (array_key_exists($sProp, $this->aProperties))
  191. {
  192. $this->aProperties[$sProp] = $aValues[$sProp];
  193. }
  194. }
  195. return $this;
  196. }
  197. public function IsRedrawNeeded()
  198. {
  199. return $this->bRedrawNeeded;
  200. }
  201. public function IsFormRedrawNeeded()
  202. {
  203. return $this->bFormRedrawNeeded;
  204. }
  205. static public function GetInfo()
  206. {
  207. return array(
  208. 'label' => '',
  209. 'icon' => '',
  210. 'description' => '',
  211. );
  212. }
  213. public function GetForm()
  214. {
  215. $oForm = new DesignerForm();
  216. $oForm->SetPrefix("dashlet_". $this->GetID());
  217. $oForm->SetParamsContainer('params');
  218. $this->GetPropertiesFields($oForm);
  219. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  220. $oForm->AddField($oDashletClassField);
  221. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  222. $oForm->AddField($oDashletIdField);
  223. return $oForm;
  224. }
  225. static public function IsVisible()
  226. {
  227. return true;
  228. }
  229. static public function CanCreateFromOQL()
  230. {
  231. return false;
  232. }
  233. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  234. {
  235. // Default: do nothing since it's not supported
  236. }
  237. }
  238. class DashletEmptyCell extends Dashlet
  239. {
  240. public function __construct($oModelReflection, $sId)
  241. {
  242. parent::__construct($oModelReflection, $sId);
  243. }
  244. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  245. {
  246. $oPage->add('&nbsp;');
  247. }
  248. public function GetPropertiesFields(DesignerForm $oForm)
  249. {
  250. }
  251. static public function GetInfo()
  252. {
  253. return array(
  254. 'label' => 'Empty Cell',
  255. 'icon' => 'images/dashlet-text.png',
  256. 'description' => 'Empty Cell Dashlet Placeholder',
  257. );
  258. }
  259. static public function IsVisible()
  260. {
  261. return false;
  262. }
  263. }
  264. class DashletPlainText extends Dashlet
  265. {
  266. public function __construct($oModelReflection, $sId)
  267. {
  268. parent::__construct($oModelReflection, $sId);
  269. $this->aProperties['text'] = Dict::S('UI:DashletPlainText:Prop-Text:Default');
  270. }
  271. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  272. {
  273. $sText = htmlentities($this->aProperties['text'], ENT_QUOTES, 'UTF-8');
  274. $sId = 'plaintext_'.($bEditMode? 'edit_' : '').$this->sId;
  275. $oPage->add('<div id='.$sId.'" class="dashlet-content">'.$sText.'</div>');
  276. }
  277. public function GetPropertiesFields(DesignerForm $oForm)
  278. {
  279. $oField = new DesignerLongTextField('text', Dict::S('UI:DashletPlainText:Prop-Text'), $this->aProperties['text']);
  280. $oField->SetMandatory();
  281. $oForm->AddField($oField);
  282. }
  283. static public function GetInfo()
  284. {
  285. return array(
  286. 'label' => Dict::S('UI:DashletPlainText:Label'),
  287. 'icon' => 'images/dashlet-text.png',
  288. 'description' => Dict::S('UI:DashletPlainText:Description'),
  289. );
  290. }
  291. }
  292. class DashletObjectList extends Dashlet
  293. {
  294. public function __construct($oModelReflection, $sId)
  295. {
  296. parent::__construct($oModelReflection, $sId);
  297. $this->aProperties['title'] = '';
  298. $this->aProperties['query'] = 'SELECT Contact';
  299. $this->aProperties['menu'] = false;
  300. }
  301. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  302. {
  303. $sTitle = $this->aProperties['title'];
  304. $sQuery = $this->aProperties['query'];
  305. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  306. $oPage->add('<div class="dashlet-content">');
  307. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  308. if ($sHtmlTitle != '')
  309. {
  310. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  311. }
  312. $oFilter = DBObjectSearch::FromOQL($sQuery);
  313. $oBlock = new DisplayBlock($oFilter, 'list');
  314. $aExtraParams = array(
  315. 'menu' => $sShowMenu,
  316. 'table_id' => 'Dashlet'.$this->sId,
  317. );
  318. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  319. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  320. $oPage->add('</div>');
  321. }
  322. public function GetPropertiesFields(DesignerForm $oForm)
  323. {
  324. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), $this->aProperties['title']);
  325. $oForm->AddField($oField);
  326. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $this->aProperties['query']);
  327. $oField->SetMandatory();
  328. $oForm->AddField($oField);
  329. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  330. $oForm->AddField($oField);
  331. }
  332. static public function GetInfo()
  333. {
  334. return array(
  335. 'label' => Dict::S('UI:DashletObjectList:Label'),
  336. 'icon' => 'images/dashlet-list.png',
  337. 'description' => Dict::S('UI:DashletObjectList:Description'),
  338. );
  339. }
  340. static public function CanCreateFromOQL()
  341. {
  342. return true;
  343. }
  344. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  345. {
  346. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), '');
  347. $oForm->AddField($oField);
  348. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $sOQL);
  349. $oField->SetMandatory();
  350. $oForm->AddField($oField);
  351. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  352. $oForm->AddField($oField);
  353. }
  354. }
  355. abstract class DashletGroupBy extends Dashlet
  356. {
  357. public function __construct($oModelReflection, $sId)
  358. {
  359. parent::__construct($oModelReflection, $sId);
  360. $this->aProperties['title'] = '';
  361. $this->aProperties['query'] = 'SELECT Contact';
  362. $this->aProperties['group_by'] = 'status';
  363. $this->aProperties['style'] = 'table';
  364. }
  365. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  366. {
  367. $sTitle = $this->aProperties['title'];
  368. $sQuery = $this->aProperties['query'];
  369. $sGroupBy = $this->aProperties['group_by'];
  370. $sStyle = $this->aProperties['style'];
  371. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  372. $oFilter = DBObjectSearch::FromOQL($sQuery);
  373. $sClass = $oFilter->GetClass();
  374. $sClassAlias = $oFilter->GetClassAlias();
  375. // Check groupby... it can be wrong at this stage
  376. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  377. {
  378. $sAttCode = $aMatches[1];
  379. $sFunction = $aMatches[2];
  380. }
  381. else
  382. {
  383. $sAttCode = $sGroupBy;
  384. $sFunction = null;
  385. }
  386. if (!$this->oModelReflection->IsValidAttCode($sClass, $sAttCode))
  387. {
  388. $oPage->add('<p>'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'</p>');
  389. }
  390. else
  391. {
  392. $sAttLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  393. if (!is_null($sFunction))
  394. {
  395. $sFunction = $aMatches[2];
  396. switch($sFunction)
  397. {
  398. case 'hour':
  399. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Hour', $sAttLabel);
  400. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 23
  401. break;
  402. case 'month':
  403. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Month', $sAttLabel);
  404. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%Y-%m')"; // yyyy-mm
  405. break;
  406. case 'day_of_week':
  407. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfWeek', $sAttLabel);
  408. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
  409. break;
  410. case 'day_of_month':
  411. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfMonth', $sAttLabel);
  412. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%Y-%m-%d')"; // mm-dd
  413. break;
  414. default:
  415. $sGroupByLabel = 'Unknown group by function '.$sFunction;
  416. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  417. }
  418. }
  419. else
  420. {
  421. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  422. $sGroupByLabel = $sAttLabel;
  423. }
  424. switch($sStyle)
  425. {
  426. case 'bars':
  427. $sType = 'open_flash_chart';
  428. $aExtraParams = array(
  429. 'chart_type' => 'bars',
  430. 'chart_title' => $sTitle,
  431. 'group_by' => $sGroupByExpr,
  432. 'group_by_label' => $sGroupByLabel,
  433. );
  434. $sHtmlTitle = ''; // done in the itop block
  435. break;
  436. case 'pie':
  437. $sType = 'open_flash_chart';
  438. $aExtraParams = array(
  439. 'chart_type' => 'pie',
  440. 'chart_title' => $sTitle,
  441. 'group_by' => $sGroupByExpr,
  442. 'group_by_label' => $sGroupByLabel,
  443. );
  444. $sHtmlTitle = ''; // done in the itop block
  445. break;
  446. case 'table':
  447. default:
  448. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  449. $sType = 'count';
  450. $aExtraParams = array(
  451. 'group_by' => $sGroupByExpr,
  452. 'group_by_label' => $sGroupByLabel,
  453. );
  454. break;
  455. }
  456. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  457. if ($sHtmlTitle != '')
  458. {
  459. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  460. }
  461. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  462. $oBlock = new DisplayBlock($oFilter, $sType);
  463. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  464. $oPage->add('</div>');
  465. }
  466. }
  467. protected function GetGroupByOptions($sOql)
  468. {
  469. $oSearch = DBObjectSearch::FromOQL($sOql);
  470. $sClass = $oSearch->GetClass();
  471. $aGroupBy = array();
  472. foreach($this->oModelReflection->ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  473. {
  474. if (!$oAttDef->IsScalar()) continue; // skip link sets
  475. if ($oAttDef instanceof AttributeFriendlyName) continue;
  476. if ($oAttDef instanceof AttributeExternalField) continue;
  477. $sLabel = $oAttDef->GetLabel();
  478. $aGroupBy[$sAttCode] = $sLabel;
  479. if ($oAttDef instanceof AttributeDateTime)
  480. {
  481. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  482. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  483. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  484. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  485. }
  486. }
  487. asort($aGroupBy);
  488. return $aGroupBy;
  489. }
  490. public function GetPropertiesFields(DesignerForm $oForm)
  491. {
  492. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  493. $oForm->AddField($oField);
  494. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  495. $oField->SetMandatory();
  496. $oForm->AddField($oField);
  497. try
  498. {
  499. // Group by field: build the list of possible values (attribute codes + ...)
  500. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  501. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  502. $oField->SetMandatory();
  503. $oField->SetAllowedValues($aGroupBy);
  504. }
  505. catch(Exception $e)
  506. {
  507. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  508. $oField->SetReadOnly();
  509. }
  510. $oForm->AddField($oField);
  511. $aStyles = array(
  512. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  513. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  514. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  515. );
  516. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  517. $oField->SetMandatory();
  518. $oField->SetAllowedValues($aStyles);
  519. $oForm->AddField($oField);
  520. }
  521. public function Update($aValues, $aUpdatedFields)
  522. {
  523. if (in_array('query', $aUpdatedFields))
  524. {
  525. try
  526. {
  527. $sCurrQuery = $aValues['query'];
  528. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  529. $sCurrClass = $oCurrSearch->GetClass();
  530. $sPrevQuery = $this->aProperties['query'];
  531. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  532. $sPrevClass = $oPrevSearch->GetClass();
  533. if ($sCurrClass != $sPrevClass)
  534. {
  535. $this->bFormRedrawNeeded = true;
  536. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  537. $this->aProperties['group_by'] = '';
  538. }
  539. }
  540. catch(Exception $e)
  541. {
  542. $this->bFormRedrawNeeded = true;
  543. }
  544. }
  545. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  546. if (in_array('style', $aUpdatedFields))
  547. {
  548. switch($aValues['style'])
  549. {
  550. // Style changed, mutate to the specified type of chart
  551. case 'pie':
  552. $oDashlet = new DashletGroupByPie($this->sId);
  553. break;
  554. case 'bars':
  555. $oDashlet = new DashletGroupByBars($this->sId);
  556. break;
  557. case 'table':
  558. $oDashlet = new DashletGroupByTable($this->sId);
  559. break;
  560. }
  561. $oDashlet->FromParams($aValues);
  562. $oDashlet->bRedrawNeeded = true;
  563. $oDashlet->bFormRedrawNeeded = true;
  564. }
  565. return $oDashlet;
  566. }
  567. static public function GetInfo()
  568. {
  569. // Note: no need to translate, should never be visible to the end-user!
  570. return array(
  571. 'label' => 'Objects grouped by...',
  572. 'icon' => 'images/dashlet-object-grouped.png',
  573. 'description' => 'Grouped objects dashlet (abstract)',
  574. );
  575. }
  576. static public function CanCreateFromOQL()
  577. {
  578. return true;
  579. }
  580. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  581. {
  582. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  583. $oForm->AddField($oField);
  584. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  585. $oField->SetMandatory();
  586. $oForm->AddField($oField);
  587. if (!is_null($sOQL))
  588. {
  589. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  590. $aGroupBy = $this->GetGroupByOptions($sOQL);
  591. $oField->SetAllowedValues($aGroupBy);
  592. }
  593. else
  594. {
  595. // Creating a form for reading parameters!
  596. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  597. }
  598. $oField->SetMandatory();
  599. $oForm->AddField($oField);
  600. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  601. $oField->SetMandatory();
  602. $oForm->AddField($oField);
  603. }
  604. }
  605. class DashletGroupByPie extends DashletGroupBy
  606. {
  607. public function __construct($oModelReflection, $sId)
  608. {
  609. parent::__construct($oModelReflection, $sId);
  610. $this->aProperties['style'] = 'pie';
  611. }
  612. static public function GetInfo()
  613. {
  614. return array(
  615. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  616. 'icon' => 'images/dashlet-pie-chart.png',
  617. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  618. );
  619. }
  620. }
  621. class DashletGroupByBars extends DashletGroupBy
  622. {
  623. public function __construct($oModelReflection, $sId)
  624. {
  625. parent::__construct($oModelReflection, $sId);
  626. $this->aProperties['style'] = 'bars';
  627. }
  628. static public function GetInfo()
  629. {
  630. return array(
  631. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  632. 'icon' => 'images/dashlet-bar-chart.png',
  633. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  634. );
  635. }
  636. }
  637. class DashletGroupByTable extends DashletGroupBy
  638. {
  639. public function __construct($oModelReflection, $sId)
  640. {
  641. parent::__construct($oModelReflection, $sId);
  642. $this->aProperties['style'] = 'table';
  643. }
  644. static public function GetInfo()
  645. {
  646. return array(
  647. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  648. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  649. 'icon' => 'images/dashlet-groupby-table.png',
  650. );
  651. }
  652. }
  653. class DashletHeaderStatic extends Dashlet
  654. {
  655. public function __construct($oModelReflection, $sId)
  656. {
  657. parent::__construct($oModelReflection, $sId);
  658. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  659. $sIcon = $this->oModelReflection->GetClassIcon('Contact', false);
  660. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIcon);
  661. $this->aProperties['icon'] = $sIcon;
  662. }
  663. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  664. {
  665. $sTitle = $this->aProperties['title'];
  666. $sIcon = $this->aProperties['icon'];
  667. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  668. $oPage->add('<div class="dashlet-content">');
  669. $oPage->add('<div class="main_header">');
  670. $oPage->add('<img src="'.$sIconPath.'">');
  671. $oPage->add('<h1>'.Dict::S($sTitle).'</h1>');
  672. $oPage->add('</div>');
  673. $oPage->add('</div>');
  674. }
  675. public function GetPropertiesFields(DesignerForm $oForm)
  676. {
  677. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  678. $oForm->AddField($oField);
  679. $oField = new DesignerIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  680. $aAllIcons = self::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
  681. ksort($aAllIcons);
  682. $aValues = array();
  683. foreach($aAllIcons as $sFilePath)
  684. {
  685. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  686. }
  687. $oField->SetAllowedValues($aValues);
  688. $oForm->AddField($oField);
  689. }
  690. static public function GetInfo()
  691. {
  692. return array(
  693. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  694. 'icon' => 'images/dashlet-header.png',
  695. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  696. );
  697. }
  698. static public function FindIcons($sBaseDir, $sDir = '')
  699. {
  700. $aResult = array();
  701. // Populate automatically the list of icon files
  702. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  703. {
  704. while (($sFile = readdir($hDir)) !== false)
  705. {
  706. $aMatches = array();
  707. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  708. {
  709. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  710. $aResult = array_merge($aResult, self::FindIcons($sBaseDir, $sDirSubPath));
  711. }
  712. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  713. {
  714. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  715. }
  716. }
  717. closedir($hDir);
  718. }
  719. return $aResult;
  720. }
  721. }
  722. class DashletHeaderDynamic extends Dashlet
  723. {
  724. public function __construct($oModelReflection, $sId)
  725. {
  726. parent::__construct($oModelReflection, $sId);
  727. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  728. $sIcon = $this->oModelReflection->GetClassIcon('Contact', false);
  729. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIcon);
  730. $this->aProperties['icon'] = $sIcon;
  731. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  732. $this->aProperties['query'] = 'SELECT Contact';
  733. $this->aProperties['group_by'] = 'status';
  734. $this->aProperties['values'] = array('active', 'inactive');
  735. }
  736. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  737. {
  738. $sTitle = $this->aProperties['title'];
  739. $sIcon = $this->aProperties['icon'];
  740. $sSubtitle = $this->aProperties['subtitle'];
  741. $sQuery = $this->aProperties['query'];
  742. $sGroupBy = $this->aProperties['group_by'];
  743. $aValues = $this->aProperties['values'];
  744. $oFilter = DBObjectSearch::FromOQL($sQuery);
  745. $sClass = $oFilter->GetClass();
  746. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  747. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  748. {
  749. if (count($aValues) == 0)
  750. {
  751. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  752. if (is_array($aAllowed))
  753. {
  754. $aValues = array_keys($aAllowed);
  755. }
  756. }
  757. }
  758. if (count($aValues) > 0)
  759. {
  760. // Stats grouped by <group_by>
  761. $sCSV = implode(',', $aValues);
  762. $aExtraParams = array(
  763. 'title[block]' => $sTitle,
  764. 'label[block]' => $sSubtitle,
  765. 'status[block]' => $sGroupBy,
  766. 'status_codes[block]' => $sCSV,
  767. 'context_filter' => 1,
  768. );
  769. }
  770. else
  771. {
  772. // Simple stats
  773. $aExtraParams = array(
  774. 'title[block]' => $sTitle,
  775. 'label[block]' => $sSubtitle,
  776. 'context_filter' => 1,
  777. );
  778. }
  779. $oPage->add('<div class="dashlet-content">');
  780. $oPage->add('<div class="main_header">');
  781. $oPage->add('<img src="'.$sIconPath.'">');
  782. $oBlock = new DisplayBlock($oFilter, 'summary');
  783. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  784. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  785. $oPage->add('</div>');
  786. $oPage->add('</div>');
  787. }
  788. public function GetPropertiesFields(DesignerForm $oForm)
  789. {
  790. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  791. $oForm->AddField($oField);
  792. $oField = new DesignerIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  793. $aAllIcons = DashletHeaderStatic::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
  794. ksort($aAllIcons);
  795. $aValues = array();
  796. foreach($aAllIcons as $sFilePath)
  797. {
  798. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  799. }
  800. $oField->SetAllowedValues($aValues);
  801. $oForm->AddField($oField);
  802. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  803. $oForm->AddField($oField);
  804. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  805. $oField->SetMandatory();
  806. $oForm->AddField($oField);
  807. try
  808. {
  809. // Group by field: build the list of possible values (attribute codes + ...)
  810. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  811. $sClass = $oSearch->GetClass();
  812. $aGroupBy = array();
  813. foreach($this->oModelReflection->ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  814. {
  815. if (!$oAttDef instanceof AttributeEnum && (!$oAttDef instanceof AttributeFinalClass || !$this->oModelReflection->HasChildrenClasses($sClass))) continue;
  816. $sLabel = $oAttDef->GetLabel();
  817. $aGroupBy[$sAttCode] = $sLabel;
  818. }
  819. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  820. $oField->SetMandatory();
  821. $oField->SetAllowedValues($aGroupBy);
  822. }
  823. catch(Exception $e)
  824. {
  825. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  826. $oField->SetReadOnly();
  827. }
  828. $oForm->AddField($oField);
  829. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  830. $oField->MultipleSelection(true);
  831. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  832. {
  833. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  834. $oField->SetAllowedValues($aValues);
  835. }
  836. else
  837. {
  838. $oField->SetReadOnly();
  839. }
  840. $oForm->AddField($oField);
  841. }
  842. public function Update($aValues, $aUpdatedFields)
  843. {
  844. if (in_array('query', $aUpdatedFields))
  845. {
  846. try
  847. {
  848. $sCurrQuery = $aValues['query'];
  849. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  850. $sCurrClass = $oCurrSearch->GetClass();
  851. $sPrevQuery = $this->aProperties['query'];
  852. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  853. $sPrevClass = $oPrevSearch->GetClass();
  854. if ($sCurrClass != $sPrevClass)
  855. {
  856. $this->bFormRedrawNeeded = true;
  857. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  858. $this->aProperties['group_by'] = '';
  859. $this->aProperties['values'] = array();
  860. }
  861. }
  862. catch(Exception $e)
  863. {
  864. $this->bFormRedrawNeeded = true;
  865. }
  866. }
  867. if (in_array('group_by', $aUpdatedFields))
  868. {
  869. $this->bFormRedrawNeeded = true;
  870. $this->aProperties['values'] = array();
  871. }
  872. return parent::Update($aValues, $aUpdatedFields);
  873. }
  874. static public function GetInfo()
  875. {
  876. return array(
  877. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  878. 'icon' => 'images/dashlet-header-stats.png',
  879. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  880. );
  881. }
  882. }
  883. class DashletBadge extends Dashlet
  884. {
  885. public function __construct($oModelReflection, $sId)
  886. {
  887. parent::__construct($oModelReflection, $sId);
  888. $this->aProperties['class'] = 'Contact';
  889. $this->aCSSClasses[] = 'dashlet-inline';
  890. $this->aCSSClasses[] = 'dashlet-badge';
  891. }
  892. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  893. {
  894. $sClass = $this->aProperties['class'];
  895. $oPage->add('<div class="dashlet-content">');
  896. $oFilter = new DBObjectSearch($sClass);
  897. $oBlock = new DisplayBlock($oFilter, 'actions');
  898. $aExtraParams = array(
  899. 'context_filter' => 1,
  900. );
  901. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  902. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  903. $oPage->add('</div>');
  904. if ($bEditMode)
  905. {
  906. // Since the container div is not rendered the same way in edit mode, add the 'inline' style to it
  907. $oPage->add_ready_script("$('#dashlet_".$this->sId."').addClass('dashlet-inline');");
  908. }
  909. }
  910. public function GetPropertiesFields(DesignerForm $oForm)
  911. {
  912. $oClassesSet = new ValueSetEnumClasses('bizmodel', array());
  913. $aClasses = $oClassesSet->GetValues(array());
  914. $aLinkClasses = array();
  915. foreach($this->oModelReflection->GetClasses('bizmodel') as $sClass)
  916. {
  917. foreach($this->oModelReflection->ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  918. {
  919. if ($oAttDef instanceof AttributeLinkedSetIndirect)
  920. {
  921. $aLinkClasses[$oAttDef->GetLinkedClass()] = true;
  922. }
  923. }
  924. }
  925. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  926. ksort($aClasses);
  927. $aValues = array();
  928. foreach($aClasses as $sClass => $sClass)
  929. {
  930. if (!array_key_exists($sClass, $aLinkClasses))
  931. {
  932. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  933. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  934. if (($sIconUrl == '') || !file_exists($sIconFilePath))
  935. {
  936. // The icon does not exist, leet's use a transparent one of the same size.
  937. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  938. }
  939. $aValues[] = array('value' => $sClass, 'label' => $this->oModelReflection->GetName($sClass), 'icon' => $sIconUrl);
  940. }
  941. }
  942. $oField->SetAllowedValues($aValues);
  943. $oForm->AddField($oField);
  944. }
  945. static public function GetInfo()
  946. {
  947. return array(
  948. 'label' => Dict::S('UI:DashletBadge:Label'),
  949. 'icon' => 'images/dashlet-badge.png',
  950. 'description' => Dict::S('UI:DashletBadge:Description'),
  951. );
  952. }
  953. }
  954. ?>