dashlet.class.inc.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. <?php
  2. // Copyright (C) 2012-2013 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->ListAttributes($sClass) as $sAttCode => $sAttType)
  473. {
  474. if ($sAttType == 'AttributeLinkedSet') continue;
  475. if (is_subclass_of($sAttType, 'AttributeLinkedSet')) continue;
  476. if ($sAttType == 'AttributeFriendlyName') continue;
  477. if (is_subclass_of($sAttType, 'AttributeFriendlyName')) continue;
  478. if ($sAttType == 'AttributeExternalField') continue;
  479. if (is_subclass_of($sAttType, 'AttributeExternalField')) continue;
  480. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  481. $aGroupBy[$sAttCode] = $sLabel;
  482. if (is_subclass_of($sAttType, 'AttributeDateTime') || $sAttType == 'AttributeDateTime')
  483. {
  484. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  485. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  486. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  487. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  488. }
  489. }
  490. asort($aGroupBy);
  491. return $aGroupBy;
  492. }
  493. public function GetPropertiesFields(DesignerForm $oForm)
  494. {
  495. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  496. $oForm->AddField($oField);
  497. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  498. $oField->SetMandatory();
  499. $oForm->AddField($oField);
  500. try
  501. {
  502. // Group by field: build the list of possible values (attribute codes + ...)
  503. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  504. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  505. $oField->SetMandatory();
  506. $oField->SetAllowedValues($aGroupBy);
  507. }
  508. catch(Exception $e)
  509. {
  510. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  511. $oField->SetReadOnly();
  512. }
  513. $oForm->AddField($oField);
  514. $aStyles = array(
  515. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  516. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  517. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  518. );
  519. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  520. $oField->SetMandatory();
  521. $oField->SetAllowedValues($aStyles);
  522. $oForm->AddField($oField);
  523. }
  524. public function Update($aValues, $aUpdatedFields)
  525. {
  526. if (in_array('query', $aUpdatedFields))
  527. {
  528. try
  529. {
  530. $sCurrQuery = $aValues['query'];
  531. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  532. $sCurrClass = $oCurrSearch->GetClass();
  533. $sPrevQuery = $this->aProperties['query'];
  534. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  535. $sPrevClass = $oPrevSearch->GetClass();
  536. if ($sCurrClass != $sPrevClass)
  537. {
  538. $this->bFormRedrawNeeded = true;
  539. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  540. $this->aProperties['group_by'] = '';
  541. }
  542. }
  543. catch(Exception $e)
  544. {
  545. $this->bFormRedrawNeeded = true;
  546. }
  547. }
  548. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  549. if (in_array('style', $aUpdatedFields))
  550. {
  551. switch($aValues['style'])
  552. {
  553. // Style changed, mutate to the specified type of chart
  554. case 'pie':
  555. $oDashlet = new DashletGroupByPie($this->sId);
  556. break;
  557. case 'bars':
  558. $oDashlet = new DashletGroupByBars($this->sId);
  559. break;
  560. case 'table':
  561. $oDashlet = new DashletGroupByTable($this->sId);
  562. break;
  563. }
  564. $oDashlet->FromParams($aValues);
  565. $oDashlet->bRedrawNeeded = true;
  566. $oDashlet->bFormRedrawNeeded = true;
  567. }
  568. return $oDashlet;
  569. }
  570. static public function GetInfo()
  571. {
  572. // Note: no need to translate, should never be visible to the end-user!
  573. return array(
  574. 'label' => 'Objects grouped by...',
  575. 'icon' => 'images/dashlet-object-grouped.png',
  576. 'description' => 'Grouped objects dashlet (abstract)',
  577. );
  578. }
  579. static public function CanCreateFromOQL()
  580. {
  581. return true;
  582. }
  583. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  584. {
  585. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  586. $oForm->AddField($oField);
  587. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  588. $oField->SetMandatory();
  589. $oForm->AddField($oField);
  590. if (!is_null($sOQL))
  591. {
  592. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  593. $aGroupBy = $this->GetGroupByOptions($sOQL);
  594. $oField->SetAllowedValues($aGroupBy);
  595. }
  596. else
  597. {
  598. // Creating a form for reading parameters!
  599. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  600. }
  601. $oField->SetMandatory();
  602. $oForm->AddField($oField);
  603. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  604. $oField->SetMandatory();
  605. $oForm->AddField($oField);
  606. }
  607. }
  608. class DashletGroupByPie extends DashletGroupBy
  609. {
  610. public function __construct($oModelReflection, $sId)
  611. {
  612. parent::__construct($oModelReflection, $sId);
  613. $this->aProperties['style'] = 'pie';
  614. }
  615. static public function GetInfo()
  616. {
  617. return array(
  618. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  619. 'icon' => 'images/dashlet-pie-chart.png',
  620. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  621. );
  622. }
  623. }
  624. class DashletGroupByBars extends DashletGroupBy
  625. {
  626. public function __construct($oModelReflection, $sId)
  627. {
  628. parent::__construct($oModelReflection, $sId);
  629. $this->aProperties['style'] = 'bars';
  630. }
  631. static public function GetInfo()
  632. {
  633. return array(
  634. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  635. 'icon' => 'images/dashlet-bar-chart.png',
  636. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  637. );
  638. }
  639. }
  640. class DashletGroupByTable extends DashletGroupBy
  641. {
  642. public function __construct($oModelReflection, $sId)
  643. {
  644. parent::__construct($oModelReflection, $sId);
  645. $this->aProperties['style'] = 'table';
  646. }
  647. static public function GetInfo()
  648. {
  649. return array(
  650. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  651. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  652. 'icon' => 'images/dashlet-groupby-table.png',
  653. );
  654. }
  655. }
  656. class DashletHeaderStatic extends Dashlet
  657. {
  658. public function __construct($oModelReflection, $sId)
  659. {
  660. parent::__construct($oModelReflection, $sId);
  661. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  662. $sIcon = $this->oModelReflection->GetClassIcon('Contact', false);
  663. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIcon);
  664. $this->aProperties['icon'] = $sIcon;
  665. }
  666. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  667. {
  668. $sTitle = $this->aProperties['title'];
  669. $sIcon = $this->aProperties['icon'];
  670. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  671. $oPage->add('<div class="dashlet-content">');
  672. $oPage->add('<div class="main_header">');
  673. $oPage->add('<img src="'.$sIconPath.'">');
  674. $oPage->add('<h1>'.Dict::S($sTitle).'</h1>');
  675. $oPage->add('</div>');
  676. $oPage->add('</div>');
  677. }
  678. public function GetPropertiesFields(DesignerForm $oForm)
  679. {
  680. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  681. $oForm->AddField($oField);
  682. $oField = new DesignerIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  683. $aAllIcons = self::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
  684. ksort($aAllIcons);
  685. $aValues = array();
  686. foreach($aAllIcons as $sFilePath)
  687. {
  688. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  689. }
  690. $oField->SetAllowedValues($aValues);
  691. $oForm->AddField($oField);
  692. }
  693. static public function GetInfo()
  694. {
  695. return array(
  696. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  697. 'icon' => 'images/dashlet-header.png',
  698. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  699. );
  700. }
  701. static public function FindIcons($sBaseDir, $sDir = '')
  702. {
  703. $aResult = array();
  704. // Populate automatically the list of icon files
  705. if ($hDir = @opendir($sBaseDir.'/'.$sDir))
  706. {
  707. while (($sFile = readdir($hDir)) !== false)
  708. {
  709. $aMatches = array();
  710. if (($sFile != '.') && ($sFile != '..') && ($sFile != 'lifecycle') && is_dir($sBaseDir.'/'.$sDir.'/'.$sFile))
  711. {
  712. $sDirSubPath = ($sDir == '') ? $sFile : $sDir.'/'.$sFile;
  713. $aResult = array_merge($aResult, self::FindIcons($sBaseDir, $sDirSubPath));
  714. }
  715. if (preg_match("/\.(png|jpg|jpeg|gif)$/i", $sFile, $aMatches)) // png, jp(e)g and gif are considered valid
  716. {
  717. $aResult[$sFile.'_'.$sDir] = $sDir.'/'.$sFile;
  718. }
  719. }
  720. closedir($hDir);
  721. }
  722. return $aResult;
  723. }
  724. }
  725. class DashletHeaderDynamic extends Dashlet
  726. {
  727. public function __construct($oModelReflection, $sId)
  728. {
  729. parent::__construct($oModelReflection, $sId);
  730. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  731. $sIcon = $this->oModelReflection->GetClassIcon('Contact', false);
  732. $sIcon = str_replace(utils::GetAbsoluteUrlModulesRoot(), '', $sIcon);
  733. $this->aProperties['icon'] = $sIcon;
  734. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  735. $this->aProperties['query'] = 'SELECT Contact';
  736. $this->aProperties['group_by'] = 'status';
  737. $this->aProperties['values'] = array('active', 'inactive');
  738. }
  739. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  740. {
  741. $sTitle = $this->aProperties['title'];
  742. $sIcon = $this->aProperties['icon'];
  743. $sSubtitle = $this->aProperties['subtitle'];
  744. $sQuery = $this->aProperties['query'];
  745. $sGroupBy = $this->aProperties['group_by'];
  746. $aValues = $this->aProperties['values'];
  747. $oFilter = DBObjectSearch::FromOQL($sQuery);
  748. $sClass = $oFilter->GetClass();
  749. $sIconPath = utils::GetAbsoluteUrlModulesRoot().$sIcon;
  750. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  751. {
  752. if (count($aValues) == 0)
  753. {
  754. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  755. if (is_array($aAllowed))
  756. {
  757. $aValues = array_keys($aAllowed);
  758. }
  759. }
  760. }
  761. if (count($aValues) > 0)
  762. {
  763. // Stats grouped by <group_by>
  764. $sCSV = implode(',', $aValues);
  765. $aExtraParams = array(
  766. 'title[block]' => $sTitle,
  767. 'label[block]' => $sSubtitle,
  768. 'status[block]' => $sGroupBy,
  769. 'status_codes[block]' => $sCSV,
  770. 'context_filter' => 1,
  771. );
  772. }
  773. else
  774. {
  775. // Simple stats
  776. $aExtraParams = array(
  777. 'title[block]' => $sTitle,
  778. 'label[block]' => $sSubtitle,
  779. 'context_filter' => 1,
  780. );
  781. }
  782. $oPage->add('<div class="dashlet-content">');
  783. $oPage->add('<div class="main_header">');
  784. $oPage->add('<img src="'.$sIconPath.'">');
  785. $oBlock = new DisplayBlock($oFilter, 'summary');
  786. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  787. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  788. $oPage->add('</div>');
  789. $oPage->add('</div>');
  790. }
  791. public function GetPropertiesFields(DesignerForm $oForm)
  792. {
  793. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  794. $oForm->AddField($oField);
  795. $oField = new DesignerIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  796. $aAllIcons = DashletHeaderStatic::FindIcons(APPROOT.'env-'.utils::GetCurrentEnvironment());
  797. ksort($aAllIcons);
  798. $aValues = array();
  799. foreach($aAllIcons as $sFilePath)
  800. {
  801. $aValues[] = array('value' => $sFilePath, 'label' => basename($sFilePath), 'icon' => utils::GetAbsoluteUrlModulesRoot().$sFilePath);
  802. }
  803. $oField->SetAllowedValues($aValues);
  804. $oForm->AddField($oField);
  805. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  806. $oForm->AddField($oField);
  807. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  808. $oField->SetMandatory();
  809. $oForm->AddField($oField);
  810. try
  811. {
  812. // Group by field: build the list of possible values (attribute codes + ...)
  813. $oSearch = DBObjectSearch::FromOQL($this->aProperties['query']);
  814. $sClass = $oSearch->GetClass();
  815. $aGroupBy = array();
  816. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  817. {
  818. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  819. {
  820. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  821. }
  822. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  823. $aGroupBy[$sAttCode] = $sLabel;
  824. }
  825. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  826. $oField->SetMandatory();
  827. $oField->SetAllowedValues($aGroupBy);
  828. }
  829. catch(Exception $e)
  830. {
  831. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  832. $oField->SetReadOnly();
  833. }
  834. $oForm->AddField($oField);
  835. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  836. $oField->MultipleSelection(true);
  837. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  838. {
  839. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  840. $oField->SetAllowedValues($aValues);
  841. }
  842. else
  843. {
  844. $oField->SetReadOnly();
  845. }
  846. $oForm->AddField($oField);
  847. }
  848. public function Update($aValues, $aUpdatedFields)
  849. {
  850. if (in_array('query', $aUpdatedFields))
  851. {
  852. try
  853. {
  854. $sCurrQuery = $aValues['query'];
  855. $oCurrSearch = DBObjectSearch::FromOQL($sCurrQuery);
  856. $sCurrClass = $oCurrSearch->GetClass();
  857. $sPrevQuery = $this->aProperties['query'];
  858. $oPrevSearch = DBObjectSearch::FromOQL($sPrevQuery);
  859. $sPrevClass = $oPrevSearch->GetClass();
  860. if ($sCurrClass != $sPrevClass)
  861. {
  862. $this->bFormRedrawNeeded = true;
  863. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  864. $this->aProperties['group_by'] = '';
  865. $this->aProperties['values'] = array();
  866. }
  867. }
  868. catch(Exception $e)
  869. {
  870. $this->bFormRedrawNeeded = true;
  871. }
  872. }
  873. if (in_array('group_by', $aUpdatedFields))
  874. {
  875. $this->bFormRedrawNeeded = true;
  876. $this->aProperties['values'] = array();
  877. }
  878. return parent::Update($aValues, $aUpdatedFields);
  879. }
  880. static public function GetInfo()
  881. {
  882. return array(
  883. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  884. 'icon' => 'images/dashlet-header-stats.png',
  885. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  886. );
  887. }
  888. }
  889. class DashletBadge extends Dashlet
  890. {
  891. public function __construct($oModelReflection, $sId)
  892. {
  893. parent::__construct($oModelReflection, $sId);
  894. $this->aProperties['class'] = 'Contact';
  895. $this->aCSSClasses[] = 'dashlet-inline';
  896. $this->aCSSClasses[] = 'dashlet-badge';
  897. }
  898. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  899. {
  900. $sClass = $this->aProperties['class'];
  901. $oPage->add('<div class="dashlet-content">');
  902. $oFilter = new DBObjectSearch($sClass);
  903. $oBlock = new DisplayBlock($oFilter, 'actions');
  904. $aExtraParams = array(
  905. 'context_filter' => 1,
  906. );
  907. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  908. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  909. $oPage->add('</div>');
  910. if ($bEditMode)
  911. {
  912. // Since the container div is not rendered the same way in edit mode, add the 'inline' style to it
  913. $oPage->add_ready_script("$('#dashlet_".$this->sId."').addClass('dashlet-inline');");
  914. }
  915. }
  916. public function GetPropertiesFields(DesignerForm $oForm)
  917. {
  918. $oClassesSet = new ValueSetEnumClasses('bizmodel', array());
  919. $aClasses = $oClassesSet->GetValues(array());
  920. $aLinkClasses = array();
  921. foreach($this->oModelReflection->GetClasses('bizmodel') as $sClass)
  922. {
  923. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeLinkedSetIndirect') as $sAttCode => $sAttType)
  924. {
  925. $sLinkedClass = $this->oModelReflection->GetAttributeProperty($sClass, $sAttCode, 'linked_class');
  926. if ($sLinkedClass != null)
  927. {
  928. $aLinkClasses[$sLinkedClass] = true;
  929. }
  930. }
  931. }
  932. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  933. ksort($aClasses);
  934. $aValues = array();
  935. foreach($aClasses as $sClass => $sClass)
  936. {
  937. if (!array_key_exists($sClass, $aLinkClasses))
  938. {
  939. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  940. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  941. if (($sIconUrl == '') || !file_exists($sIconFilePath))
  942. {
  943. // The icon does not exist, leet's use a transparent one of the same size.
  944. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  945. }
  946. $aValues[] = array('value' => $sClass, 'label' => $this->oModelReflection->GetName($sClass), 'icon' => $sIconUrl);
  947. }
  948. }
  949. $oField->SetAllowedValues($aValues);
  950. $oForm->AddField($oField);
  951. }
  952. static public function GetInfo()
  953. {
  954. return array(
  955. 'label' => Dict::S('UI:DashletBadge:Label'),
  956. 'icon' => 'images/dashlet-badge.png',
  957. 'description' => Dict::S('UI:DashletBadge:Description'),
  958. );
  959. }
  960. }
  961. ?>