dashlet.class.inc.php 30 KB

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