dashlet.class.inc.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. <?php
  2. // Copyright (C) 2012-2017 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-2017 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 (gettype($sValue) == $sRefType)
  49. {
  50. // Do not change anything in that case!
  51. $ret = $sValue;
  52. }
  53. elseif ($sRefType == 'boolean')
  54. {
  55. $ret = ($sValue == 'true');
  56. }
  57. elseif ($sRefType == 'array')
  58. {
  59. $ret = explode(',', $sValue);
  60. }
  61. else
  62. {
  63. $ret = $sValue;
  64. settype($ret, $sRefType);
  65. }
  66. return $ret;
  67. }
  68. public function Prop2Str($value)
  69. {
  70. $sType = gettype($value);
  71. if ($sType == 'boolean')
  72. {
  73. $sRet = $value ? 'true' : 'false';
  74. }
  75. elseif ($sType == 'array')
  76. {
  77. $sRet = implode(',', $value);
  78. }
  79. else
  80. {
  81. $sRet = (string) $value;
  82. }
  83. return $sRet;
  84. }
  85. protected function OnUpdate()
  86. {
  87. }
  88. public function FromDOMNode($oDOMNode)
  89. {
  90. foreach ($this->aProperties as $sProperty => $value)
  91. {
  92. $oPropNode = $oDOMNode->getElementsByTagName($sProperty)->item(0);
  93. if ($oPropNode != null)
  94. {
  95. $this->aProperties[$sProperty] = $this->PropertyFromDOMNode($oPropNode, $sProperty);
  96. }
  97. }
  98. $this->OnUpdate();
  99. }
  100. public function ToDOMNode($oDOMNode)
  101. {
  102. foreach ($this->aProperties as $sProperty => $value)
  103. {
  104. $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty);
  105. $oDOMNode->appendChild($oPropNode);
  106. $this->PropertyToDOMNode($oPropNode, $sProperty, $value);
  107. }
  108. }
  109. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  110. {
  111. $res = $this->Str2Prop($sProperty, $oDOMNode->textContent);
  112. return $res;
  113. }
  114. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  115. {
  116. $sXmlValue = $this->Prop2Str($value);
  117. $oTextNode = $oDOMNode->ownerDocument->createTextNode($sXmlValue);
  118. $oDOMNode->appendChild($oTextNode);
  119. }
  120. public function FromXml($sXml)
  121. {
  122. $oDomDoc = new DOMDocument('1.0', 'UTF-8');
  123. $oDomDoc->loadXml($sXml);
  124. $this->FromDOMNode($oDomDoc->firstChild);
  125. }
  126. public function FromParams($aParams)
  127. {
  128. foreach ($this->aProperties as $sProperty => $value)
  129. {
  130. if (array_key_exists($sProperty, $aParams))
  131. {
  132. $this->aProperties[$sProperty] = $aParams[$sProperty];
  133. }
  134. }
  135. $this->OnUpdate();
  136. }
  137. public function DoRender($oPage, $bEditMode = false, $bEnclosingDiv = true, $aExtraParams = array())
  138. {
  139. $sCSSClasses = implode(' ', $this->aCSSClasses);
  140. $sId = $this->GetID();
  141. if ($bEnclosingDiv)
  142. {
  143. if ($bEditMode)
  144. {
  145. $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
  146. }
  147. else
  148. {
  149. $oPage->add('<div class="'.$sCSSClasses.'">');
  150. }
  151. }
  152. else
  153. {
  154. foreach ($this->aCSSClasses as $sCSSClass)
  155. {
  156. $oPage->add_ready_script("$('#dashlet_".$sId."').addClass('$sCSSClass');");
  157. }
  158. }
  159. try
  160. {
  161. if (get_class($this->oModelReflection) == 'ModelReflectionRuntime')
  162. {
  163. $this->Render($oPage, $bEditMode, $aExtraParams);
  164. }
  165. else
  166. {
  167. $this->RenderNoData($oPage, $bEditMode, $aExtraParams);
  168. }
  169. }
  170. catch(UnknownClassOqlException $e)
  171. {
  172. // Maybe the class is part of a non-installed module, fail silently
  173. // Except in Edit mode
  174. if ($bEditMode)
  175. {
  176. $oPage->add('<div class="dashlet-content">');
  177. $oPage->add('<h2>'.$e->GetUserFriendlyDescription().'</h2>');
  178. $oPage->add('</div>');
  179. }
  180. }
  181. catch(OqlException $e)
  182. {
  183. $oPage->add('<div class="dashlet-content">');
  184. $oPage->p($e->GetUserFriendlyDescription());
  185. $oPage->add('</div>');
  186. }
  187. catch(Exception $e)
  188. {
  189. $oPage->add('<div class="dashlet-content">');
  190. $oPage->p($e->getMessage());
  191. $oPage->add('</div>');
  192. }
  193. if ($bEnclosingDiv)
  194. {
  195. $oPage->add('</div>');
  196. }
  197. if ($bEditMode)
  198. {
  199. $sClass = get_class($this);
  200. $oPage->add_ready_script(
  201. <<<EOF
  202. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  203. EOF
  204. );
  205. }
  206. }
  207. public function SetID($sId)
  208. {
  209. $this->sId = $sId;
  210. }
  211. public function GetID()
  212. {
  213. return $this->sId;
  214. }
  215. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  216. /* Rendering without the real data */
  217. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  218. {
  219. $this->Render($oPage, $bEditMode, $aExtraParams);
  220. }
  221. abstract public function GetPropertiesFields(DesignerForm $oForm);
  222. public function ToXml(DOMNode $oContainerNode)
  223. {
  224. }
  225. public function Update($aValues, $aUpdatedFields)
  226. {
  227. foreach($aUpdatedFields as $sProp)
  228. {
  229. if (array_key_exists($sProp, $this->aProperties))
  230. {
  231. $this->aProperties[$sProp] = $this->Str2Prop($sProp, $aValues[$sProp]);
  232. }
  233. }
  234. $this->OnUpdate();
  235. return $this;
  236. }
  237. public function IsRedrawNeeded()
  238. {
  239. return $this->bRedrawNeeded;
  240. }
  241. public function IsFormRedrawNeeded()
  242. {
  243. return $this->bFormRedrawNeeded;
  244. }
  245. static public function GetInfo()
  246. {
  247. return array(
  248. 'label' => '',
  249. 'icon' => '',
  250. 'description' => '',
  251. );
  252. }
  253. public function GetForm()
  254. {
  255. $oForm = new DesignerForm();
  256. $oForm->SetPrefix("dashlet_". $this->GetID());
  257. $oForm->SetParamsContainer('params');
  258. $this->GetPropertiesFields($oForm);
  259. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  260. $oForm->AddField($oDashletClassField);
  261. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  262. $oForm->AddField($oDashletIdField);
  263. return $oForm;
  264. }
  265. static public function IsVisible()
  266. {
  267. return true;
  268. }
  269. static public function CanCreateFromOQL()
  270. {
  271. return false;
  272. }
  273. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  274. {
  275. // Default: do nothing since it's not supported
  276. }
  277. }
  278. class DashletEmptyCell extends Dashlet
  279. {
  280. public function __construct($oModelReflection, $sId)
  281. {
  282. parent::__construct($oModelReflection, $sId);
  283. }
  284. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  285. {
  286. $oPage->add('&nbsp;');
  287. }
  288. public function GetPropertiesFields(DesignerForm $oForm)
  289. {
  290. }
  291. static public function GetInfo()
  292. {
  293. return array(
  294. 'label' => 'Empty Cell',
  295. 'icon' => 'images/dashlet-text.png',
  296. 'description' => 'Empty Cell Dashlet Placeholder',
  297. );
  298. }
  299. static public function IsVisible()
  300. {
  301. return false;
  302. }
  303. }
  304. class DashletPlainText extends Dashlet
  305. {
  306. public function __construct($oModelReflection, $sId)
  307. {
  308. parent::__construct($oModelReflection, $sId);
  309. $this->aProperties['text'] = Dict::S('UI:DashletPlainText:Prop-Text:Default');
  310. }
  311. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  312. {
  313. $sText = htmlentities($this->aProperties['text'], ENT_QUOTES, 'UTF-8');
  314. $sText = str_replace(array("\r\n", "\n", "\r"), "<br/>", $sText);
  315. $sId = 'plaintext_'.($bEditMode? 'edit_' : '').$this->sId;
  316. $oPage->add('<div id="'.$sId.'" class="dashlet-content">'.$sText.'</div>');
  317. }
  318. public function GetPropertiesFields(DesignerForm $oForm)
  319. {
  320. $oField = new DesignerLongTextField('text', Dict::S('UI:DashletPlainText:Prop-Text'), $this->aProperties['text']);
  321. $oField->SetMandatory();
  322. $oForm->AddField($oField);
  323. }
  324. static public function GetInfo()
  325. {
  326. return array(
  327. 'label' => Dict::S('UI:DashletPlainText:Label'),
  328. 'icon' => 'images/dashlet-text.png',
  329. 'description' => Dict::S('UI:DashletPlainText:Description'),
  330. );
  331. }
  332. }
  333. class DashletObjectList extends Dashlet
  334. {
  335. public function __construct($oModelReflection, $sId)
  336. {
  337. parent::__construct($oModelReflection, $sId);
  338. $this->aProperties['title'] = '';
  339. $this->aProperties['query'] = 'SELECT Contact';
  340. $this->aProperties['menu'] = false;
  341. }
  342. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  343. {
  344. $sTitle = $this->aProperties['title'];
  345. $sQuery = $this->aProperties['query'];
  346. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  347. $oPage->add('<div class="dashlet-content">');
  348. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  349. if ($sHtmlTitle != '')
  350. {
  351. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  352. }
  353. $oFilter = DBObjectSearch::FromOQL($sQuery);
  354. $oBlock = new DisplayBlock($oFilter, 'list');
  355. $aExtraParams = array(
  356. 'menu' => $sShowMenu,
  357. 'table_id' => 'Dashlet'.$this->sId,
  358. );
  359. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  360. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  361. $oPage->add('</div>');
  362. }
  363. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  364. {
  365. $sTitle = $this->aProperties['title'];
  366. $sQuery = $this->aProperties['query'];
  367. $bShowMenu = $this->aProperties['menu'];
  368. $oPage->add('<div class="dashlet-content">');
  369. $sHtmlTitle = htmlentities($this->oModelReflection->DictString($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  370. if ($sHtmlTitle != '')
  371. {
  372. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  373. }
  374. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  375. $sClass = $oQuery->GetClass();
  376. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  377. $oPage->p(Dict::S('UI:NoObjectToDisplay'));
  378. if ($bShowMenu)
  379. {
  380. $oPage->p('<a>'.Dict::Format('UI:ClickToCreateNew', $this->oModelReflection->GetName($sClass)).'</a>');
  381. }
  382. $oPage->add('</div>');
  383. $oPage->add('</div>');
  384. }
  385. public function GetPropertiesFields(DesignerForm $oForm)
  386. {
  387. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), $this->aProperties['title']);
  388. $oForm->AddField($oField);
  389. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $this->aProperties['query']);
  390. $oField->SetMandatory();
  391. $oForm->AddField($oField);
  392. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  393. $oForm->AddField($oField);
  394. }
  395. static public function GetInfo()
  396. {
  397. return array(
  398. 'label' => Dict::S('UI:DashletObjectList:Label'),
  399. 'icon' => 'images/dashlet-list.png',
  400. 'description' => Dict::S('UI:DashletObjectList:Description'),
  401. );
  402. }
  403. static public function CanCreateFromOQL()
  404. {
  405. return true;
  406. }
  407. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  408. {
  409. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), '');
  410. $oForm->AddField($oField);
  411. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $sOQL);
  412. $oField->SetMandatory();
  413. $oForm->AddField($oField);
  414. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  415. $oForm->AddField($oField);
  416. }
  417. }
  418. abstract class DashletGroupBy extends Dashlet
  419. {
  420. public function __construct($oModelReflection, $sId)
  421. {
  422. parent::__construct($oModelReflection, $sId);
  423. $this->aProperties['title'] = '';
  424. $this->aProperties['query'] = 'SELECT Contact';
  425. $this->aProperties['group_by'] = 'status';
  426. $this->aProperties['style'] = 'table';
  427. }
  428. protected $sGroupByLabel = null;
  429. protected $sGroupByExpr = null;
  430. protected $sGroupByAttCode = null;
  431. protected $sFunction = null;
  432. /**
  433. * Compute Grouping
  434. */
  435. public function OnUpdate()
  436. {
  437. $this->sGroupByExpr = null;
  438. $this->sGroupByLabel = null;
  439. $this->sGroupByAttCode = null;
  440. $this->sFunction = null;
  441. $sQuery = $this->aProperties['query'];
  442. $sGroupBy = $this->aProperties['group_by'];
  443. $sStyle = $this->aProperties['style'];
  444. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  445. try
  446. {
  447. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  448. $sClass = $oQuery->GetClass();
  449. $sClassAlias = $oQuery->GetClassAlias();
  450. }
  451. catch(Exception $e)
  452. {
  453. // Invalid query, let the user edit the dashlet/dashboard anyhow
  454. $sClass = '';
  455. $sClassAlias = '';
  456. }
  457. // Check groupby... it can be wrong at this stage
  458. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  459. {
  460. $this->sGroupByAttCode = $aMatches[1];
  461. $this->sFunction = $aMatches[2];
  462. }
  463. else
  464. {
  465. $this->sGroupByAttCode = $sGroupBy;
  466. $this->sFunction = null;
  467. }
  468. if (($sClass != '') && $this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  469. {
  470. $sAttLabel = $this->oModelReflection->GetLabel($sClass, $this->sGroupByAttCode);
  471. if (!is_null($this->sFunction))
  472. {
  473. switch($this->sFunction)
  474. {
  475. case 'hour':
  476. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Hour', $sAttLabel);
  477. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%H')"; // 0 -> 23
  478. break;
  479. case 'month':
  480. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Month', $sAttLabel);
  481. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%Y-%m')"; // yyyy-mm
  482. break;
  483. case 'day_of_week':
  484. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfWeek', $sAttLabel);
  485. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%w')";
  486. break;
  487. case 'day_of_month':
  488. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfMonth', $sAttLabel);
  489. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%Y-%m-%d')"; // mm-dd
  490. break;
  491. default:
  492. $this->sGroupByLabel = 'Unknown group by function '.$this->sFunction;
  493. $this->sGroupByExpr = $sClassAlias.'.'.$this->sGroupByAttCode;
  494. }
  495. }
  496. else
  497. {
  498. $this->sGroupByExpr = $sClassAlias.'.'.$this->sGroupByAttCode;
  499. $this->sGroupByLabel = $sAttLabel;
  500. }
  501. }
  502. else
  503. {
  504. $this->sGroupByAttCode = null;
  505. }
  506. }
  507. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  508. {
  509. $sTitle = $this->aProperties['title'];
  510. $sQuery = $this->aProperties['query'];
  511. $sGroupBy = $this->aProperties['group_by'];
  512. $sStyle = $this->aProperties['style'];
  513. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  514. $oFilter = DBObjectSearch::FromOQL($sQuery);
  515. $oFilter->SetShowObsoleteData(utils::ShowObsoleteData());
  516. $sClass = $oFilter->GetClass();
  517. $sClassAlias = $oFilter->GetClassAlias();
  518. if (!$this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  519. {
  520. $oPage->add('<p>'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'</p>');
  521. }
  522. else
  523. {
  524. switch($sStyle)
  525. {
  526. case 'bars':
  527. $sType = 'chart';
  528. $aExtraParams = array(
  529. 'chart_type' => 'bars',
  530. 'chart_title' => $sTitle,
  531. 'group_by' => $this->sGroupByExpr,
  532. 'group_by_label' => $this->sGroupByLabel,
  533. );
  534. $sHtmlTitle = ''; // done in the itop block
  535. break;
  536. case 'pie':
  537. $sType = 'chart';
  538. $aExtraParams = array(
  539. 'chart_type' => 'pie',
  540. 'chart_title' => $sTitle,
  541. 'group_by' => $this->sGroupByExpr,
  542. 'group_by_label' => $this->sGroupByLabel,
  543. );
  544. $sHtmlTitle = ''; // done in the itop block
  545. break;
  546. case 'table':
  547. default:
  548. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  549. $sType = 'count';
  550. $aExtraParams = array(
  551. 'group_by' => $this->sGroupByExpr,
  552. 'group_by_label' => $this->sGroupByLabel,
  553. );
  554. break;
  555. }
  556. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  557. if ($sHtmlTitle != '')
  558. {
  559. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  560. }
  561. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  562. $oBlock = new DisplayBlock($oFilter, $sType);
  563. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  564. $oPage->add('</div>');
  565. }
  566. }
  567. protected function MakeSimulatedData()
  568. {
  569. $sQuery = $this->aProperties['query'];
  570. $sGroupBy = $this->aProperties['group_by'];
  571. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  572. $sClass = $oQuery->GetClass();
  573. $aDisplayValues = array();
  574. if ($this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  575. {
  576. $aAttributeTypes = $this->oModelReflection->ListAttributes($sClass);
  577. $sAttributeType = $aAttributeTypes[$this->sGroupByAttCode];
  578. if (is_subclass_of($sAttributeType, 'AttributeDateTime') || $sAttributeType == 'AttributeDateTime')
  579. {
  580. // Note: an alternative to this somewhat hardcoded way of doing things would be to implement...
  581. //$oExpr = Expression::FromOQL($this->sGroupByExpr);
  582. //$aTranslationData = array($oQuery->GetClassAlias() => array($this->sGroupByAttCode => new ScalarExpression(date('Y-m-d H:i:s', $iTime))));
  583. //$sRawValue = CMDBSource::QueryToScalar('SELECT '.$oExpr->Translate($aTranslationData)->Render());
  584. //$sValueLabel = $oExpr->MakeValueLabel(oFilter, $sRawValue, $sRawValue);
  585. // Anyhow, this requires :
  586. // - an update to the prototype of MakeValueLabel() so that it takes ModelReflection parameters
  587. // - propose clever date/times samples
  588. $aValues = array();
  589. switch($this->sFunction)
  590. {
  591. case 'hour':
  592. $aValues = array(8, 9, 15, 18);
  593. break;
  594. case 'month':
  595. $aValues = array('2013 '.Dict::S('Month-11'), '2013 '.Dict::S('Month-12'), '2014 '.Dict::S('Month-01'), '2014 '.Dict::S('Month-02'), '2014 '.Dict::S('Month-03'));
  596. break;
  597. case 'day_of_week':
  598. $aValues = array(Dict::S('DayOfWeek-Monday'), Dict::S('DayOfWeek-Wednesday'), Dict::S('DayOfWeek-Thursday'), Dict::S('DayOfWeek-Friday'));
  599. break;
  600. case 'day_of_month':
  601. $aValues = array(Dict::S('Month-03'). ' 30', Dict::S('Month-03'). ' 31', Dict::S('Month-04'). ' 01', Dict::S('Month-04'). ' 02', Dict::S('Month-04'). ' 03');
  602. break;
  603. }
  604. foreach ($aValues as $sValue)
  605. {
  606. $aDisplayValues[] = array('label' => $sValue, 'count' => (int)rand(1, 15));
  607. }
  608. }
  609. elseif (is_subclass_of($sAttributeType, 'AttributeEnum') || $sAttributeType == 'AttributeEnum')
  610. {
  611. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $this->sGroupByAttCode);
  612. if ($aAllowed) // null for non enums
  613. {
  614. foreach ($aAllowed as $sValue => $sValueLabel)
  615. {
  616. $iCount = (int) rand(2, 100);
  617. $aDisplayValues[] = array(
  618. 'label' => $sValueLabel,
  619. 'count' => $iCount
  620. );
  621. }
  622. }
  623. }
  624. else
  625. {
  626. $aDisplayValues[] = array('label' => 'a', 'count' => 123);
  627. $aDisplayValues[] = array('label' => 'b', 'count' => 321);
  628. $aDisplayValues[] = array('label' => 'c', 'count' => 456);
  629. }
  630. }
  631. return $aDisplayValues;
  632. }
  633. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  634. {
  635. $oPage->add('<div class="dashlet-content">');
  636. $oPage->add('error!');
  637. $oPage->add('</div>');
  638. }
  639. protected function GetGroupByOptions($sOql)
  640. {
  641. $oQuery = $this->oModelReflection->GetQuery($sOql);
  642. $sClass = $oQuery->GetClass();
  643. $aGroupBy = array();
  644. foreach($this->oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType)
  645. {
  646. if ($sAttType == 'AttributeLinkedSet') continue;
  647. if (is_subclass_of($sAttType, 'AttributeLinkedSet')) continue;
  648. if ($sAttType == 'AttributeFriendlyName') continue;
  649. if (is_subclass_of($sAttType, 'AttributeFriendlyName')) continue;
  650. if ($sAttType == 'AttributeExternalField') continue;
  651. if (is_subclass_of($sAttType, 'AttributeExternalField')) continue;
  652. if ($sAttType == 'AttributeOneWayPassword') continue;
  653. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  654. $aGroupBy[$sAttCode] = $sLabel;
  655. if (is_subclass_of($sAttType, 'AttributeDateTime') || $sAttType == 'AttributeDateTime')
  656. {
  657. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  658. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  659. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  660. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  661. }
  662. }
  663. asort($aGroupBy);
  664. return $aGroupBy;
  665. }
  666. public function GetPropertiesFields(DesignerForm $oForm)
  667. {
  668. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  669. $oForm->AddField($oField);
  670. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  671. $oField->SetMandatory();
  672. $oForm->AddField($oField);
  673. try
  674. {
  675. // Group by field: build the list of possible values (attribute codes + ...)
  676. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  677. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  678. $oField->SetMandatory();
  679. $oField->SetAllowedValues($aGroupBy);
  680. }
  681. catch(Exception $e)
  682. {
  683. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  684. $oField->SetReadOnly();
  685. }
  686. $oForm->AddField($oField);
  687. $aStyles = array(
  688. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  689. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  690. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  691. );
  692. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  693. $oField->SetMandatory();
  694. $oField->SetAllowedValues($aStyles);
  695. $oForm->AddField($oField);
  696. }
  697. public function Update($aValues, $aUpdatedFields)
  698. {
  699. if (in_array('query', $aUpdatedFields))
  700. {
  701. try
  702. {
  703. $sCurrQuery = $aValues['query'];
  704. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  705. $sCurrClass = $oCurrSearch->GetClass();
  706. $sPrevQuery = $this->aProperties['query'];
  707. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  708. $sPrevClass = $oPrevSearch->GetClass();
  709. if ($sCurrClass != $sPrevClass)
  710. {
  711. $this->bFormRedrawNeeded = true;
  712. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  713. $this->aProperties['group_by'] = '';
  714. }
  715. }
  716. catch(Exception $e)
  717. {
  718. $this->bFormRedrawNeeded = true;
  719. }
  720. }
  721. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  722. if (in_array('style', $aUpdatedFields))
  723. {
  724. switch($aValues['style'])
  725. {
  726. // Style changed, mutate to the specified type of chart
  727. case 'pie':
  728. $oDashlet = new DashletGroupByPie($this->oModelReflection, $this->sId);
  729. break;
  730. case 'bars':
  731. $oDashlet = new DashletGroupByBars($this->oModelReflection, $this->sId);
  732. break;
  733. case 'table':
  734. $oDashlet = new DashletGroupByTable($this->oModelReflection, $this->sId);
  735. break;
  736. }
  737. $oDashlet->FromParams($aValues);
  738. $oDashlet->bRedrawNeeded = true;
  739. $oDashlet->bFormRedrawNeeded = true;
  740. }
  741. return $oDashlet;
  742. }
  743. static public function GetInfo()
  744. {
  745. // Note: no need to translate, should never be visible to the end-user!
  746. return array(
  747. 'label' => 'Objects grouped by...',
  748. 'icon' => 'images/dashlet-object-grouped.png',
  749. 'description' => 'Grouped objects dashlet (abstract)',
  750. );
  751. }
  752. static public function CanCreateFromOQL()
  753. {
  754. return true;
  755. }
  756. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  757. {
  758. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  759. $oForm->AddField($oField);
  760. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  761. $oField->SetMandatory();
  762. $oForm->AddField($oField);
  763. if (!is_null($sOQL))
  764. {
  765. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  766. $aGroupBy = $this->GetGroupByOptions($sOQL);
  767. $oField->SetAllowedValues($aGroupBy);
  768. }
  769. else
  770. {
  771. // Creating a form for reading parameters!
  772. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  773. }
  774. $oField->SetMandatory();
  775. $oForm->AddField($oField);
  776. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  777. $oField->SetMandatory();
  778. $oForm->AddField($oField);
  779. }
  780. }
  781. class DashletGroupByPie extends DashletGroupBy
  782. {
  783. public function __construct($oModelReflection, $sId)
  784. {
  785. parent::__construct($oModelReflection, $sId);
  786. $this->aProperties['style'] = 'pie';
  787. }
  788. static public function GetInfo()
  789. {
  790. return array(
  791. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  792. 'icon' => 'images/dashlet-pie-chart.png',
  793. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  794. );
  795. }
  796. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  797. {
  798. $sTitle = $this->aProperties['title'];
  799. $aDisplayValues = $this->MakeSimulatedData();
  800. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  801. $oChart = new open_flash_chart();
  802. $aGroupBy = array();
  803. $aLabels = array();
  804. foreach($aDisplayValues as $iRow => $aDisplayData)
  805. {
  806. $aLabels[$iRow] = $aDisplayData['label'];
  807. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  808. }
  809. $oChartElement = new pie();
  810. $oChartElement->set_start_angle( 35 );
  811. $oChartElement->set_animate( true );
  812. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  813. $oChartElement->set_colours( array('#FF8A00', '#909980', '#2C2B33', '#CCC08D', '#596664') );
  814. $aData = array();
  815. foreach($aGroupBy as $iRow => $iCount)
  816. {
  817. $sFlashLabel = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  818. $PieValue = new pie_value($iCount, $sFlashLabel);
  819. $aData[] = $PieValue;
  820. }
  821. $oChartElement->set_values($aData);
  822. $oChart->x_axis = null;
  823. if (!empty($sTitle))
  824. {
  825. // The title has been given in an url, and urlencoded...
  826. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  827. // Example: é (C3A9 becomes %E9)
  828. // As a consequence, json_encode (called within open-flash-chart.php)
  829. // was returning 'null' and the graph was not displayed at all
  830. // To make sure that the graph is displayed AND to get a correct title
  831. // (at least for european characters) let's transform back into utf-8 !
  832. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  833. // If the title is a dictionnary entry, fetch it
  834. $sTitle = $this->oModelReflection->DictString($sTitle);
  835. $oTitle = new title($sTitle);
  836. $oChart->set_title($oTitle);
  837. $oTitle->set_style("{font-size: 16px; font-family: Tahoma; font-weight: bold; text-align: center;}");
  838. }
  839. $oChart->set_bg_colour('#FFFFFF');
  840. $oChart->add_element($oChartElement);
  841. $sData = $oChart->toPrettyString();
  842. $sData = json_encode($sData);
  843. $oPage->add_script(
  844. <<< EOF
  845. function ofc_get_data_dashlet_{$this->sId}()
  846. {
  847. return $sData;
  848. }
  849. EOF
  850. );
  851. $oPage->add('<div class="dashlet-content">');
  852. $oPage->add("<div id=\"dashlet_chart_{$this->sId}\">If the chart does not display, <a href=\"http://get.adobe.com/flash/\" target=\"_blank\">install Flash</a></div>\n");
  853. $oPage->add('</div>');
  854. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  855. $oPage->add_ready_script(
  856. <<<EOF
  857. swfobject.embedSWF( "../images/open-flash-chart.swf",
  858. "dashlet_chart_{$this->sId}",
  859. "100%", "300","9.0.0",
  860. "expressInstall.swf",
  861. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  862. {'wmode': 'transparent'}
  863. );
  864. EOF
  865. );
  866. }
  867. }
  868. class DashletGroupByBars extends DashletGroupBy
  869. {
  870. public function __construct($oModelReflection, $sId)
  871. {
  872. parent::__construct($oModelReflection, $sId);
  873. $this->aProperties['style'] = 'bars';
  874. }
  875. static public function GetInfo()
  876. {
  877. return array(
  878. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  879. 'icon' => 'images/dashlet-bar-chart.png',
  880. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  881. );
  882. }
  883. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  884. {
  885. $sTitle = $this->aProperties['title'];
  886. $aDisplayValues = $this->MakeSimulatedData();
  887. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  888. $oChart = new open_flash_chart();
  889. $aGroupBy = array();
  890. $aLabels = array();
  891. foreach($aDisplayValues as $iRow => $aDisplayData)
  892. {
  893. $aLabels[$iRow] = $aDisplayData['label'];
  894. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  895. }
  896. $oChartElement = new bar_glass();
  897. $aData = array();
  898. $aChartLabels = array();
  899. $maxValue = 0;
  900. foreach($aGroupBy as $iRow => $iCount)
  901. {
  902. $oBarValue = new bar_value($iCount);
  903. $aData[] = $oBarValue;
  904. if ($iCount > $maxValue) $maxValue = $iCount;
  905. $aChartLabels[] = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  906. }
  907. $oYAxis = new y_axis();
  908. $aMagicValues = array(1,2,5,10);
  909. $iMultiplier = 1;
  910. $index = 0;
  911. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  912. while($maxValue > $iTop)
  913. {
  914. $index++;
  915. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  916. if (($index % count($aMagicValues)) == 0)
  917. {
  918. $iMultiplier = $iMultiplier * 10;
  919. }
  920. }
  921. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  922. $oYAxis->set_range(0, $iTop, $iMultiplier);
  923. $oChart->set_y_axis( $oYAxis );
  924. $oChartElement->set_values( $aData );
  925. $oXAxis = new x_axis();
  926. $oXLabels = new x_axis_labels();
  927. // set them vertical
  928. $oXLabels->set_vertical();
  929. // set the label text
  930. $oXLabels->set_labels($aChartLabels);
  931. // Add the X Axis Labels to the X Axis
  932. $oXAxis->set_labels( $oXLabels );
  933. $oChart->set_x_axis( $oXAxis );
  934. if (!empty($sTitle))
  935. {
  936. // The title has been given in an url, and urlencoded...
  937. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  938. // Example: é (C3A9 becomes %E9)
  939. // As a consequence, json_encode (called within open-flash-chart.php)
  940. // was returning 'null' and the graph was not displayed at all
  941. // To make sure that the graph is displayed AND to get a correct title
  942. // (at least for european characters) let's transform back into utf-8 !
  943. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  944. // If the title is a dictionnary entry, fetch it
  945. $sTitle = $this->oModelReflection->DictString($sTitle);
  946. $oTitle = new title($sTitle);
  947. $oChart->set_title($oTitle);
  948. $oTitle->set_style("{font-size: 16px; font-family: Tahoma; font-weight: bold; text-align: center;}");
  949. }
  950. $oChart->set_bg_colour('#FFFFFF');
  951. $oChart->add_element($oChartElement);
  952. $sData = $oChart->toPrettyString();
  953. $sData = json_encode($sData);
  954. $oPage->add_script(
  955. <<< EOF
  956. function ofc_get_data_dashlet_{$this->sId}()
  957. {
  958. return $sData;
  959. }
  960. EOF
  961. );
  962. $oPage->add('<div class="dashlet-content">');
  963. $oPage->add("<div id=\"dashlet_chart_{$this->sId}\">If the chart does not display, <a href=\"http://get.adobe.com/flash/\" target=\"_blank\">install Flash</a></div>\n");
  964. $oPage->add('</div>');
  965. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  966. $oPage->add_ready_script(
  967. <<<EOF
  968. swfobject.embedSWF( "../images/open-flash-chart.swf",
  969. "dashlet_chart_{$this->sId}",
  970. "100%", "300","9.0.0",
  971. "expressInstall.swf",
  972. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  973. {'wmode': 'transparent'}
  974. );
  975. EOF
  976. );
  977. }
  978. }
  979. class DashletGroupByTable extends DashletGroupBy
  980. {
  981. public function __construct($oModelReflection, $sId)
  982. {
  983. parent::__construct($oModelReflection, $sId);
  984. $this->aProperties['style'] = 'table';
  985. }
  986. static public function GetInfo()
  987. {
  988. return array(
  989. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  990. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  991. 'icon' => 'images/dashlet-groupby-table.png',
  992. );
  993. }
  994. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  995. {
  996. $sTitle = $this->aProperties['title'];
  997. $aDisplayValues = $this->MakeSimulatedData();
  998. $iTotal = 0;
  999. foreach($aDisplayValues as $iRow => $aDisplayData)
  1000. {
  1001. $iTotal += $aDisplayData['count'];
  1002. }
  1003. $oPage->add('<div class="dashlet-content">');
  1004. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1005. $oPage->add('<div id="'.$sBlockId.'" class="display_block">');
  1006. $oPage->add('<p>'.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'</p>');
  1007. $oPage->add('<table class="listResults">');
  1008. $oPage->add('<thead>');
  1009. $oPage->add('<tr>');
  1010. $oPage->add('<th class="header" title="">'.$this->sGroupByLabel.'</th>');
  1011. $oPage->add('<th class="header" title="'.Dict::S('UI:GroupBy:Count+').'">'.Dict::S('UI:GroupBy:Count').'</th>');
  1012. $oPage->add('</tr>');
  1013. $oPage->add('</thead>');
  1014. $oPage->add('<tbody>');
  1015. foreach($aDisplayValues as $aDisplayData)
  1016. {
  1017. $oPage->add('<tr class="even">');
  1018. $oPage->add('<td class=""><span title="Active">'.$aDisplayData['label'].'</span></td>');
  1019. $oPage->add('<td class=""><a>'.$aDisplayData['count'].'</a></td>');
  1020. $oPage->add('</tr>');
  1021. }
  1022. $oPage->add('</tbody>');
  1023. $oPage->add('</table>');
  1024. $oPage->add('</div>');
  1025. $oPage->add('</div>');
  1026. }
  1027. }
  1028. class DashletHeaderStatic extends Dashlet
  1029. {
  1030. public function __construct($oModelReflection, $sId)
  1031. {
  1032. parent::__construct($oModelReflection, $sId);
  1033. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  1034. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1035. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1036. }
  1037. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1038. {
  1039. $sTitle = $this->aProperties['title'];
  1040. $sIcon = $this->aProperties['icon'];
  1041. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1042. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1043. $oPage->add('<div class="dashlet-content">');
  1044. $oPage->add('<div class="main_header">');
  1045. $oPage->add('<img src="'.$sIconPath.'">');
  1046. $oPage->add('<h1>'.$this->oModelReflection->DictString($sTitle).'</h1>');
  1047. $oPage->add('</div>');
  1048. $oPage->add('</div>');
  1049. }
  1050. public function GetPropertiesFields(DesignerForm $oForm)
  1051. {
  1052. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  1053. $oForm->AddField($oField);
  1054. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  1055. $oForm->AddField($oField);
  1056. }
  1057. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1058. {
  1059. if ($sProperty == 'icon')
  1060. {
  1061. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1062. return $oIconField->ValueFromDOMNode($oDOMNode);
  1063. }
  1064. else
  1065. {
  1066. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1067. }
  1068. }
  1069. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1070. {
  1071. if ($sProperty == 'icon')
  1072. {
  1073. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1074. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1075. }
  1076. else
  1077. {
  1078. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1079. }
  1080. }
  1081. static public function GetInfo()
  1082. {
  1083. return array(
  1084. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  1085. 'icon' => 'images/dashlet-header.png',
  1086. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  1087. );
  1088. }
  1089. }
  1090. class DashletHeaderDynamic extends Dashlet
  1091. {
  1092. public function __construct($oModelReflection, $sId)
  1093. {
  1094. parent::__construct($oModelReflection, $sId);
  1095. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  1096. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1097. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1098. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  1099. $this->aProperties['query'] = 'SELECT Contact';
  1100. $this->aProperties['group_by'] = 'status';
  1101. $this->aProperties['values'] = array('active', 'inactive');
  1102. }
  1103. protected function GetValues()
  1104. {
  1105. $sQuery = $this->aProperties['query'];
  1106. $sGroupBy = $this->aProperties['group_by'];
  1107. $aValues = $this->aProperties['values'];
  1108. if (empty($aValues))
  1109. {
  1110. $aValues = array();
  1111. }
  1112. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1113. $sClass = $oQuery->GetClass();
  1114. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  1115. {
  1116. if (count($aValues) == 0)
  1117. {
  1118. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  1119. if (is_array($aAllowed))
  1120. {
  1121. $aValues = array_keys($aAllowed);
  1122. }
  1123. }
  1124. }
  1125. return $aValues;
  1126. }
  1127. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1128. {
  1129. $sTitle = $this->aProperties['title'];
  1130. $sIcon = $this->aProperties['icon'];
  1131. $sSubtitle = $this->aProperties['subtitle'];
  1132. $sQuery = $this->aProperties['query'];
  1133. $sGroupBy = $this->aProperties['group_by'];
  1134. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1135. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1136. $aValues = $this->GetValues();
  1137. if (count($aValues) > 0)
  1138. {
  1139. // Stats grouped by <group_by>
  1140. $sCSV = implode(',', $aValues);
  1141. $aExtraParams = array(
  1142. 'title[block]' => $sTitle,
  1143. 'label[block]' => $sSubtitle,
  1144. 'status[block]' => $sGroupBy,
  1145. 'status_codes[block]' => $sCSV,
  1146. 'context_filter' => 1,
  1147. );
  1148. }
  1149. else
  1150. {
  1151. // Simple stats
  1152. $aExtraParams = array(
  1153. 'title[block]' => $sTitle,
  1154. 'label[block]' => $sSubtitle,
  1155. 'context_filter' => 1,
  1156. );
  1157. }
  1158. $oPage->add('<div class="dashlet-content">');
  1159. $oPage->add('<div class="main_header">');
  1160. $oPage->add('<img src="'.$sIconPath.'">');
  1161. $oFilter = DBObjectSearch::FromOQL($sQuery);
  1162. $oBlock = new DisplayBlock($oFilter, 'summary');
  1163. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1164. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1165. $oPage->add('</div>');
  1166. $oPage->add('</div>');
  1167. }
  1168. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1169. {
  1170. $sTitle = $this->aProperties['title'];
  1171. $sIcon = $this->aProperties['icon'];
  1172. $sSubtitle = $this->aProperties['subtitle'];
  1173. $sQuery = $this->aProperties['query'];
  1174. $sGroupBy = $this->aProperties['group_by'];
  1175. $aValues = $this->aProperties['values'];
  1176. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1177. $sClass = $oQuery->GetClass();
  1178. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1179. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1180. $oPage->add('<div class="dashlet-content">');
  1181. $oPage->add('<div class="main_header">');
  1182. $oPage->add('<img src="'.$sIconPath.'">');
  1183. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1184. $iTotal = 0;
  1185. $aValues = $this->GetValues();
  1186. if (count($aValues) > 0)
  1187. {
  1188. // Stats grouped by <group_by>
  1189. }
  1190. else
  1191. {
  1192. // Simple stats
  1193. }
  1194. $oPage->add('<div class="display_block" id="'.$sBlockId.'">');
  1195. $oPage->add('<div class="summary-details">');
  1196. $oPage->add('<table><tbody>');
  1197. $oPage->add('<tr>');
  1198. foreach ($aValues as $sValue)
  1199. {
  1200. $sValueLabel = $this->oModelReflection->GetValueLabel($sClass, $sGroupBy, $sValue);
  1201. $oPage->add(' <th>'.$sValueLabel.'</th>');
  1202. }
  1203. $oPage->add('</tr>');
  1204. $oPage->add('<tr>');
  1205. foreach ($aValues as $sValue)
  1206. {
  1207. $iCount = (int) rand(2, 100);
  1208. $iTotal += $iCount;
  1209. $oPage->add(' <td>'.$iCount.'</td>');
  1210. }
  1211. $oPage->add('</tr>');
  1212. $oPage->add('</tbody></table>');
  1213. $oPage->add('</div>');
  1214. $sTitle = $this->oModelReflection->DictString($sTitle);
  1215. $sSubtitle = $this->oModelReflection->DictFormat($sSubtitle, $iTotal);
  1216. // $sSubtitle = "original: $sSubtitle, S:".$this->oModelReflection->DictString($sSubtitle).", Format: '".$this->oModelReflection->DictFormat($sSubtitle, $iTotal)."'";
  1217. $oPage->add('<h1>'.$sTitle.'</h1>');
  1218. $oPage->add('<a class="summary">'.$sSubtitle.'</a>');
  1219. $oPage->add('</div>');
  1220. $oPage->add('</div>');
  1221. $oPage->add('</div>');
  1222. }
  1223. public function GetPropertiesFields(DesignerForm $oForm)
  1224. {
  1225. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  1226. $oForm->AddField($oField);
  1227. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  1228. $oForm->AddField($oField);
  1229. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  1230. $oForm->AddField($oField);
  1231. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  1232. $oField->SetMandatory();
  1233. $oForm->AddField($oField);
  1234. try
  1235. {
  1236. // Group by field: build the list of possible values (attribute codes + ...)
  1237. $oQuery = $this->oModelReflection->GetQuery($this->aProperties['query']);
  1238. $sClass = $oQuery->GetClass();
  1239. $aGroupBy = array();
  1240. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  1241. {
  1242. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  1243. {
  1244. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  1245. }
  1246. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  1247. $aGroupBy[$sAttCode] = $sLabel;
  1248. }
  1249. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1250. $oField->SetMandatory();
  1251. $oField->SetAllowedValues($aGroupBy);
  1252. }
  1253. catch(Exception $e)
  1254. {
  1255. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1256. $oField->SetReadOnly();
  1257. }
  1258. $oForm->AddField($oField);
  1259. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  1260. $oField->MultipleSelection(true);
  1261. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  1262. {
  1263. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  1264. $oField->SetAllowedValues($aValues);
  1265. }
  1266. else
  1267. {
  1268. $oField->SetReadOnly();
  1269. }
  1270. $oForm->AddField($oField);
  1271. }
  1272. public function Update($aValues, $aUpdatedFields)
  1273. {
  1274. if (in_array('query', $aUpdatedFields))
  1275. {
  1276. try
  1277. {
  1278. $sCurrQuery = $aValues['query'];
  1279. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  1280. $sCurrClass = $oCurrSearch->GetClass();
  1281. $sPrevQuery = $this->aProperties['query'];
  1282. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  1283. $sPrevClass = $oPrevSearch->GetClass();
  1284. if ($sCurrClass != $sPrevClass)
  1285. {
  1286. $this->bFormRedrawNeeded = true;
  1287. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  1288. $this->aProperties['group_by'] = '';
  1289. $this->aProperties['values'] = array();
  1290. }
  1291. }
  1292. catch(Exception $e)
  1293. {
  1294. $this->bFormRedrawNeeded = true;
  1295. }
  1296. }
  1297. if (in_array('group_by', $aUpdatedFields))
  1298. {
  1299. $this->bFormRedrawNeeded = true;
  1300. $this->aProperties['values'] = array();
  1301. }
  1302. return parent::Update($aValues, $aUpdatedFields);
  1303. }
  1304. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1305. {
  1306. if ($sProperty == 'icon')
  1307. {
  1308. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1309. return $oIconField->ValueFromDOMNode($oDOMNode);
  1310. }
  1311. else
  1312. {
  1313. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1314. }
  1315. }
  1316. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1317. {
  1318. if ($sProperty == 'icon')
  1319. {
  1320. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1321. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1322. }
  1323. else
  1324. {
  1325. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1326. }
  1327. }
  1328. static public function GetInfo()
  1329. {
  1330. return array(
  1331. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  1332. 'icon' => 'images/dashlet-header-stats.png',
  1333. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  1334. );
  1335. }
  1336. }
  1337. class DashletBadge extends Dashlet
  1338. {
  1339. public function __construct($oModelReflection, $sId)
  1340. {
  1341. parent::__construct($oModelReflection, $sId);
  1342. $this->aProperties['class'] = 'Contact';
  1343. $this->aCSSClasses[] = 'dashlet-inline';
  1344. $this->aCSSClasses[] = 'dashlet-badge';
  1345. }
  1346. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1347. {
  1348. $sClass = $this->aProperties['class'];
  1349. $oPage->add('<div class="dashlet-content">');
  1350. $oFilter = new DBObjectSearch($sClass);
  1351. $oBlock = new DisplayBlock($oFilter, 'actions');
  1352. $aExtraParams = array(
  1353. 'context_filter' => 1,
  1354. );
  1355. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1356. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1357. $oPage->add('</div>');
  1358. }
  1359. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1360. {
  1361. $sClass = $this->aProperties['class'];
  1362. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1363. $sClassLabel = $this->oModelReflection->GetName($sClass);
  1364. $oPage->add('<div class="dashlet-content">');
  1365. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  1366. $oPage->add('<p>');
  1367. $oPage->add(' <a class="actions"><img src="'.$sIconUrl.'" style="vertical-align:middle;float;left;margin-right:10px;border:0;">'.$sClassLabel.': 947</a>');
  1368. $oPage->add('</p>');
  1369. $oPage->add('<p>');
  1370. $oPage->add(' <a>'.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'</a>');
  1371. $oPage->add(' <br/>');
  1372. $oPage->add(' <a>'.Dict::Format('UI:SearchFor_Class', $sClassLabel).'</a>');
  1373. $oPage->add('</p>');
  1374. $oPage->add('</div>');
  1375. $oPage->add('</div>');
  1376. }
  1377. static protected $aClassList = null;
  1378. public function GetPropertiesFields(DesignerForm $oForm)
  1379. {
  1380. if (is_null(self::$aClassList))
  1381. {
  1382. // Cache the ordered list of classes (ordered on the label)
  1383. // (has a significant impact when editing a page with lots of badges)
  1384. //
  1385. $aClasses = array();
  1386. foreach($this->oModelReflection->GetClasses('bizmodel', true /*exclude links*/) as $sClass)
  1387. {
  1388. $aClasses[$sClass] = $this->oModelReflection->GetName($sClass);
  1389. }
  1390. asort($aClasses);
  1391. self::$aClassList = array();
  1392. foreach($aClasses as $sClass => $sLabel)
  1393. {
  1394. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1395. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  1396. if ($sIconUrl == '')
  1397. {
  1398. // The icon does not exist, let's use a transparent one of the same size.
  1399. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  1400. }
  1401. self::$aClassList[] = array('value' => $sClass, 'label' => $sLabel, 'icon' => $sIconUrl);
  1402. }
  1403. }
  1404. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  1405. $oField->SetAllowedValues(self::$aClassList);
  1406. $oForm->AddField($oField);
  1407. }
  1408. static public function GetInfo()
  1409. {
  1410. return array(
  1411. 'label' => Dict::S('UI:DashletBadge:Label'),
  1412. 'icon' => 'images/dashlet-badge.png',
  1413. 'description' => Dict::S('UI:DashletBadge:Description'),
  1414. );
  1415. }
  1416. }
  1417. ?>