dashlet.class.inc.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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 (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. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  446. $sClass = $oQuery->GetClass();
  447. $sClassAlias = $oQuery->GetClassAlias();
  448. // Check groupby... it can be wrong at this stage
  449. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  450. {
  451. $this->sGroupByAttCode = $aMatches[1];
  452. $this->sFunction = $aMatches[2];
  453. }
  454. else
  455. {
  456. $this->sGroupByAttCode = $sGroupBy;
  457. $this->sFunction = null;
  458. }
  459. if ($this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  460. {
  461. $sAttLabel = $this->oModelReflection->GetLabel($sClass, $this->sGroupByAttCode);
  462. if (!is_null($this->sFunction))
  463. {
  464. switch($this->sFunction)
  465. {
  466. case 'hour':
  467. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Hour', $sAttLabel);
  468. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%H')"; // 0 -> 23
  469. break;
  470. case 'month':
  471. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Month', $sAttLabel);
  472. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%Y-%m')"; // yyyy-mm
  473. break;
  474. case 'day_of_week':
  475. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfWeek', $sAttLabel);
  476. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%w')";
  477. break;
  478. case 'day_of_month':
  479. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfMonth', $sAttLabel);
  480. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%Y-%m-%d')"; // mm-dd
  481. break;
  482. default:
  483. $this->sGroupByLabel = 'Unknown group by function '.$this->sFunction;
  484. $this->sGroupByExpr = $sClassAlias.'.'.$this->sGroupByAttCode;
  485. }
  486. }
  487. else
  488. {
  489. $this->sGroupByExpr = $sClassAlias.'.'.$this->sGroupByAttCode;
  490. $this->sGroupByLabel = $sAttLabel;
  491. }
  492. }
  493. else
  494. {
  495. $this->sGroupByAttCode = null;
  496. }
  497. }
  498. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  499. {
  500. $sTitle = $this->aProperties['title'];
  501. $sQuery = $this->aProperties['query'];
  502. $sGroupBy = $this->aProperties['group_by'];
  503. $sStyle = $this->aProperties['style'];
  504. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  505. $oFilter = DBObjectSearch::FromOQL($sQuery);
  506. $sClass = $oFilter->GetClass();
  507. $sClassAlias = $oFilter->GetClassAlias();
  508. if (!$this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  509. {
  510. $oPage->add('<p>'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'</p>');
  511. }
  512. else
  513. {
  514. switch($sStyle)
  515. {
  516. case 'bars':
  517. $sType = 'open_flash_chart';
  518. $aExtraParams = array(
  519. 'chart_type' => 'bars',
  520. 'chart_title' => $sTitle,
  521. 'group_by' => $this->sGroupByExpr,
  522. 'group_by_label' => $this->sGroupByLabel,
  523. );
  524. $sHtmlTitle = ''; // done in the itop block
  525. break;
  526. case 'pie':
  527. $sType = 'open_flash_chart';
  528. $aExtraParams = array(
  529. 'chart_type' => 'pie',
  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 'table':
  537. default:
  538. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  539. $sType = 'count';
  540. $aExtraParams = array(
  541. 'group_by' => $this->sGroupByExpr,
  542. 'group_by_label' => $this->sGroupByLabel,
  543. );
  544. break;
  545. }
  546. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  547. if ($sHtmlTitle != '')
  548. {
  549. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  550. }
  551. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  552. $oBlock = new DisplayBlock($oFilter, $sType);
  553. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  554. $oPage->add('</div>');
  555. }
  556. }
  557. protected function MakeSimulatedData()
  558. {
  559. $sQuery = $this->aProperties['query'];
  560. $sGroupBy = $this->aProperties['group_by'];
  561. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  562. $sClass = $oQuery->GetClass();
  563. $aDisplayValues = array();
  564. if ($this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  565. {
  566. $aAttributeTypes = $this->oModelReflection->ListAttributes($sClass);
  567. $sAttributeType = $aAttributeTypes[$this->sGroupByAttCode];
  568. if (is_subclass_of($sAttributeType, 'AttributeDateTime') || $sAttributeType == 'AttributeDateTime')
  569. {
  570. // Note: an alternative to this somewhat hardcoded way of doing things would be to implement...
  571. //$oExpr = Expression::FromOQL($this->sGroupByExpr);
  572. //$aTranslationData = array($oQuery->GetClassAlias() => array($this->sGroupByAttCode => new ScalarExpression(date('Y-m-d H:i:s', $iTime))));
  573. //$sRawValue = CMDBSource::QueryToScalar('SELECT '.$oExpr->Translate($aTranslationData)->Render());
  574. //$sValueLabel = $oExpr->MakeValueLabel(oFilter, $sRawValue, $sRawValue);
  575. // Anyhow, this requires :
  576. // - an update to the prototype of MakeValueLabel() so that it takes ModelReflection parameters
  577. // - propose clever date/times samples
  578. $aValues = array();
  579. switch($this->sFunction)
  580. {
  581. case 'hour':
  582. $aValues = array(8, 9, 15, 18);
  583. break;
  584. case 'month':
  585. $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'));
  586. break;
  587. case 'day_of_week':
  588. $aValues = array(Dict::S('DayOfWeek-Monday'), Dict::S('DayOfWeek-Wednesday'), Dict::S('DayOfWeek-Thursday'), Dict::S('DayOfWeek-Friday'));
  589. break;
  590. case 'day_of_month':
  591. $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');
  592. break;
  593. }
  594. foreach ($aValues as $sValue)
  595. {
  596. $aDisplayValues[] = array('label' => $sValue, 'count' => (int)rand(1, 15));
  597. }
  598. }
  599. elseif (is_subclass_of($sAttributeType, 'AttributeEnum') || $sAttributeType == 'AttributeEnum')
  600. {
  601. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $this->sGroupByAttCode);
  602. if ($aAllowed) // null for non enums
  603. {
  604. foreach ($aAllowed as $sValue => $sValueLabel)
  605. {
  606. $iCount = (int) rand(2, 100);
  607. $aDisplayValues[] = array(
  608. 'label' => $sValueLabel,
  609. 'count' => $iCount
  610. );
  611. }
  612. }
  613. }
  614. else
  615. {
  616. $aDisplayValues[] = array('label' => 'a', 'count' => 123);
  617. $aDisplayValues[] = array('label' => 'b', 'count' => 321);
  618. $aDisplayValues[] = array('label' => 'c', 'count' => 456);
  619. }
  620. }
  621. return $aDisplayValues;
  622. }
  623. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  624. {
  625. $oPage->add('<div class="dashlet-content">');
  626. $oPage->add('error!');
  627. $oPage->add('</div>');
  628. }
  629. protected function GetGroupByOptions($sOql)
  630. {
  631. $oQuery = $this->oModelReflection->GetQuery($sOql);
  632. $sClass = $oQuery->GetClass();
  633. $aGroupBy = array();
  634. foreach($this->oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType)
  635. {
  636. if ($sAttType == 'AttributeLinkedSet') continue;
  637. if (is_subclass_of($sAttType, 'AttributeLinkedSet')) continue;
  638. if ($sAttType == 'AttributeFriendlyName') continue;
  639. if (is_subclass_of($sAttType, 'AttributeFriendlyName')) continue;
  640. if ($sAttType == 'AttributeExternalField') continue;
  641. if (is_subclass_of($sAttType, 'AttributeExternalField')) continue;
  642. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  643. $aGroupBy[$sAttCode] = $sLabel;
  644. if (is_subclass_of($sAttType, 'AttributeDateTime') || $sAttType == 'AttributeDateTime')
  645. {
  646. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  647. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  648. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  649. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  650. }
  651. }
  652. asort($aGroupBy);
  653. return $aGroupBy;
  654. }
  655. public function GetPropertiesFields(DesignerForm $oForm)
  656. {
  657. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  658. $oForm->AddField($oField);
  659. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  660. $oField->SetMandatory();
  661. $oForm->AddField($oField);
  662. try
  663. {
  664. // Group by field: build the list of possible values (attribute codes + ...)
  665. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  666. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  667. $oField->SetMandatory();
  668. $oField->SetAllowedValues($aGroupBy);
  669. }
  670. catch(Exception $e)
  671. {
  672. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  673. $oField->SetReadOnly();
  674. }
  675. $oForm->AddField($oField);
  676. $aStyles = array(
  677. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  678. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  679. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  680. );
  681. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  682. $oField->SetMandatory();
  683. $oField->SetAllowedValues($aStyles);
  684. $oForm->AddField($oField);
  685. }
  686. public function Update($aValues, $aUpdatedFields)
  687. {
  688. if (in_array('query', $aUpdatedFields))
  689. {
  690. try
  691. {
  692. $sCurrQuery = $aValues['query'];
  693. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  694. $sCurrClass = $oCurrSearch->GetClass();
  695. $sPrevQuery = $this->aProperties['query'];
  696. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  697. $sPrevClass = $oPrevSearch->GetClass();
  698. if ($sCurrClass != $sPrevClass)
  699. {
  700. $this->bFormRedrawNeeded = true;
  701. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  702. $this->aProperties['group_by'] = '';
  703. }
  704. }
  705. catch(Exception $e)
  706. {
  707. $this->bFormRedrawNeeded = true;
  708. }
  709. }
  710. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  711. if (in_array('style', $aUpdatedFields))
  712. {
  713. switch($aValues['style'])
  714. {
  715. // Style changed, mutate to the specified type of chart
  716. case 'pie':
  717. $oDashlet = new DashletGroupByPie($this->oModelReflection, $this->sId);
  718. break;
  719. case 'bars':
  720. $oDashlet = new DashletGroupByBars($this->oModelReflection, $this->sId);
  721. break;
  722. case 'table':
  723. $oDashlet = new DashletGroupByTable($this->oModelReflection, $this->sId);
  724. break;
  725. }
  726. $oDashlet->FromParams($aValues);
  727. $oDashlet->bRedrawNeeded = true;
  728. $oDashlet->bFormRedrawNeeded = true;
  729. }
  730. return $oDashlet;
  731. }
  732. static public function GetInfo()
  733. {
  734. // Note: no need to translate, should never be visible to the end-user!
  735. return array(
  736. 'label' => 'Objects grouped by...',
  737. 'icon' => 'images/dashlet-object-grouped.png',
  738. 'description' => 'Grouped objects dashlet (abstract)',
  739. );
  740. }
  741. static public function CanCreateFromOQL()
  742. {
  743. return true;
  744. }
  745. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  746. {
  747. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  748. $oForm->AddField($oField);
  749. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  750. $oField->SetMandatory();
  751. $oForm->AddField($oField);
  752. if (!is_null($sOQL))
  753. {
  754. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  755. $aGroupBy = $this->GetGroupByOptions($sOQL);
  756. $oField->SetAllowedValues($aGroupBy);
  757. }
  758. else
  759. {
  760. // Creating a form for reading parameters!
  761. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  762. }
  763. $oField->SetMandatory();
  764. $oForm->AddField($oField);
  765. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  766. $oField->SetMandatory();
  767. $oForm->AddField($oField);
  768. }
  769. }
  770. class DashletGroupByPie extends DashletGroupBy
  771. {
  772. public function __construct($oModelReflection, $sId)
  773. {
  774. parent::__construct($oModelReflection, $sId);
  775. $this->aProperties['style'] = 'pie';
  776. }
  777. static public function GetInfo()
  778. {
  779. return array(
  780. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  781. 'icon' => 'images/dashlet-pie-chart.png',
  782. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  783. );
  784. }
  785. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  786. {
  787. $sTitle = $this->aProperties['title'];
  788. $aDisplayValues = $this->MakeSimulatedData();
  789. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  790. $oChart = new open_flash_chart();
  791. $aGroupBy = array();
  792. $aLabels = array();
  793. foreach($aDisplayValues as $iRow => $aDisplayData)
  794. {
  795. $aLabels[$iRow] = $aDisplayData['label'];
  796. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  797. }
  798. $oChartElement = new pie();
  799. $oChartElement->set_start_angle( 35 );
  800. $oChartElement->set_animate( true );
  801. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  802. $oChartElement->set_colours( array('#FF8A00', '#909980', '#2C2B33', '#CCC08D', '#596664') );
  803. $aData = array();
  804. foreach($aGroupBy as $iRow => $iCount)
  805. {
  806. $sFlashLabel = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  807. $PieValue = new pie_value($iCount, $sFlashLabel);
  808. $aData[] = $PieValue;
  809. }
  810. $oChartElement->set_values($aData);
  811. $oChart->x_axis = null;
  812. if (!empty($sTitle))
  813. {
  814. // The title has been given in an url, and urlencoded...
  815. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  816. // Example: é (C3A9 becomes %E9)
  817. // As a consequence, json_encode (called within open-flash-chart.php)
  818. // was returning 'null' and the graph was not displayed at all
  819. // To make sure that the graph is displayed AND to get a correct title
  820. // (at least for european characters) let's transform back into utf-8 !
  821. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  822. // If the title is a dictionnary entry, fetch it
  823. $sTitle = $this->oModelReflection->DictString($sTitle);
  824. $oTitle = new title($sTitle);
  825. $oChart->set_title($oTitle);
  826. }
  827. $oChart->set_bg_colour('#FFFFFF');
  828. $oChart->add_element($oChartElement);
  829. $sData = $oChart->toPrettyString();
  830. $sData = json_encode($sData);
  831. $oPage->add_script(
  832. <<< EOF
  833. function ofc_get_data_dashlet_{$this->sId}()
  834. {
  835. return $sData;
  836. }
  837. EOF
  838. );
  839. $oPage->add('<div class="dashlet-content">');
  840. $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");
  841. $oPage->add('</div>');
  842. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  843. $oPage->add_ready_script(
  844. <<<EOF
  845. swfobject.embedSWF( "../images/open-flash-chart.swf",
  846. "dashlet_chart_{$this->sId}",
  847. "100%", "300","9.0.0",
  848. "expressInstall.swf",
  849. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  850. {'wmode': 'transparent'}
  851. );
  852. EOF
  853. );
  854. }
  855. }
  856. class DashletGroupByBars extends DashletGroupBy
  857. {
  858. public function __construct($oModelReflection, $sId)
  859. {
  860. parent::__construct($oModelReflection, $sId);
  861. $this->aProperties['style'] = 'bars';
  862. }
  863. static public function GetInfo()
  864. {
  865. return array(
  866. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  867. 'icon' => 'images/dashlet-bar-chart.png',
  868. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  869. );
  870. }
  871. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  872. {
  873. $sTitle = $this->aProperties['title'];
  874. $aDisplayValues = $this->MakeSimulatedData();
  875. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  876. $oChart = new open_flash_chart();
  877. $aGroupBy = array();
  878. $aLabels = array();
  879. foreach($aDisplayValues as $iRow => $aDisplayData)
  880. {
  881. $aLabels[$iRow] = $aDisplayData['label'];
  882. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  883. }
  884. $oChartElement = new bar_glass();
  885. $aData = array();
  886. $aChartLabels = array();
  887. $maxValue = 0;
  888. foreach($aGroupBy as $iRow => $iCount)
  889. {
  890. $oBarValue = new bar_value($iCount);
  891. $aData[] = $oBarValue;
  892. if ($iCount > $maxValue) $maxValue = $iCount;
  893. $aChartLabels[] = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  894. }
  895. $oYAxis = new y_axis();
  896. $aMagicValues = array(1,2,5,10);
  897. $iMultiplier = 1;
  898. $index = 0;
  899. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  900. while($maxValue > $iTop)
  901. {
  902. $index++;
  903. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  904. if (($index % count($aMagicValues)) == 0)
  905. {
  906. $iMultiplier = $iMultiplier * 10;
  907. }
  908. }
  909. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  910. $oYAxis->set_range(0, $iTop, $iMultiplier);
  911. $oChart->set_y_axis( $oYAxis );
  912. $oChartElement->set_values( $aData );
  913. $oXAxis = new x_axis();
  914. $oXLabels = new x_axis_labels();
  915. // set them vertical
  916. $oXLabels->set_vertical();
  917. // set the label text
  918. $oXLabels->set_labels($aChartLabels);
  919. // Add the X Axis Labels to the X Axis
  920. $oXAxis->set_labels( $oXLabels );
  921. $oChart->set_x_axis( $oXAxis );
  922. if (!empty($sTitle))
  923. {
  924. // The title has been given in an url, and urlencoded...
  925. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  926. // Example: é (C3A9 becomes %E9)
  927. // As a consequence, json_encode (called within open-flash-chart.php)
  928. // was returning 'null' and the graph was not displayed at all
  929. // To make sure that the graph is displayed AND to get a correct title
  930. // (at least for european characters) let's transform back into utf-8 !
  931. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  932. // If the title is a dictionnary entry, fetch it
  933. $sTitle = $this->oModelReflection->DictString($sTitle);
  934. $oTitle = new title($sTitle);
  935. $oChart->set_title($oTitle);
  936. }
  937. $oChart->set_bg_colour('#FFFFFF');
  938. $oChart->add_element($oChartElement);
  939. $sData = $oChart->toPrettyString();
  940. $sData = json_encode($sData);
  941. $oPage->add_script(
  942. <<< EOF
  943. function ofc_get_data_dashlet_{$this->sId}()
  944. {
  945. return $sData;
  946. }
  947. EOF
  948. );
  949. $oPage->add('<div class="dashlet-content">');
  950. $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");
  951. $oPage->add('</div>');
  952. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  953. $oPage->add_ready_script(
  954. <<<EOF
  955. swfobject.embedSWF( "../images/open-flash-chart.swf",
  956. "dashlet_chart_{$this->sId}",
  957. "100%", "300","9.0.0",
  958. "expressInstall.swf",
  959. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  960. {'wmode': 'transparent'}
  961. );
  962. EOF
  963. );
  964. }
  965. }
  966. class DashletGroupByTable extends DashletGroupBy
  967. {
  968. public function __construct($oModelReflection, $sId)
  969. {
  970. parent::__construct($oModelReflection, $sId);
  971. $this->aProperties['style'] = 'table';
  972. }
  973. static public function GetInfo()
  974. {
  975. return array(
  976. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  977. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  978. 'icon' => 'images/dashlet-groupby-table.png',
  979. );
  980. }
  981. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  982. {
  983. $sTitle = $this->aProperties['title'];
  984. $aDisplayValues = $this->MakeSimulatedData();
  985. $iTotal = 0;
  986. foreach($aDisplayValues as $iRow => $aDisplayData)
  987. {
  988. $iTotal += $aDisplayData['count'];
  989. }
  990. $oPage->add('<div class="dashlet-content">');
  991. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  992. $oPage->add('<div id="'.$sBlockId.'" class="display_block">');
  993. $oPage->add('<p>'.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'</p>');
  994. $oPage->add('<table class="listResults">');
  995. $oPage->add('<thead>');
  996. $oPage->add('<tr>');
  997. $oPage->add('<th class="header" title="">'.$this->sGroupByLabel.'</th>');
  998. $oPage->add('<th class="header" title="'.Dict::S('UI:GroupBy:Count+').'">'.Dict::S('UI:GroupBy:Count').'</th>');
  999. $oPage->add('</tr>');
  1000. $oPage->add('</thead>');
  1001. $oPage->add('<tbody>');
  1002. foreach($aDisplayValues as $aDisplayData)
  1003. {
  1004. $oPage->add('<tr class="even">');
  1005. $oPage->add('<td class=""><span title="Active">'.$aDisplayData['label'].'</span></td>');
  1006. $oPage->add('<td class=""><a>'.$aDisplayData['count'].'</a></td>');
  1007. $oPage->add('</tr>');
  1008. }
  1009. $oPage->add('</tbody>');
  1010. $oPage->add('</table>');
  1011. $oPage->add('</div>');
  1012. $oPage->add('</div>');
  1013. }
  1014. }
  1015. class DashletHeaderStatic extends Dashlet
  1016. {
  1017. public function __construct($oModelReflection, $sId)
  1018. {
  1019. parent::__construct($oModelReflection, $sId);
  1020. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  1021. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1022. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1023. }
  1024. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1025. {
  1026. $sTitle = $this->aProperties['title'];
  1027. $sIcon = $this->aProperties['icon'];
  1028. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1029. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1030. $oPage->add('<div class="dashlet-content">');
  1031. $oPage->add('<div class="main_header">');
  1032. $oPage->add('<img src="'.$sIconPath.'">');
  1033. $oPage->add('<h1>'.$this->oModelReflection->DictString($sTitle).'</h1>');
  1034. $oPage->add('</div>');
  1035. $oPage->add('</div>');
  1036. }
  1037. public function GetPropertiesFields(DesignerForm $oForm)
  1038. {
  1039. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  1040. $oForm->AddField($oField);
  1041. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  1042. $oForm->AddField($oField);
  1043. }
  1044. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1045. {
  1046. if ($sProperty == 'icon')
  1047. {
  1048. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1049. return $oIconField->ValueFromDOMNode($oDOMNode);
  1050. }
  1051. else
  1052. {
  1053. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1054. }
  1055. }
  1056. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1057. {
  1058. if ($sProperty == 'icon')
  1059. {
  1060. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1061. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1062. }
  1063. else
  1064. {
  1065. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1066. }
  1067. }
  1068. static public function GetInfo()
  1069. {
  1070. return array(
  1071. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  1072. 'icon' => 'images/dashlet-header.png',
  1073. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  1074. );
  1075. }
  1076. }
  1077. class DashletHeaderDynamic extends Dashlet
  1078. {
  1079. public function __construct($oModelReflection, $sId)
  1080. {
  1081. parent::__construct($oModelReflection, $sId);
  1082. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  1083. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1084. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1085. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  1086. $this->aProperties['query'] = 'SELECT Contact';
  1087. $this->aProperties['group_by'] = 'status';
  1088. $this->aProperties['values'] = array('active', 'inactive');
  1089. }
  1090. protected function GetValues()
  1091. {
  1092. $sQuery = $this->aProperties['query'];
  1093. $sGroupBy = $this->aProperties['group_by'];
  1094. $aValues = $this->aProperties['values'];
  1095. if (empty($aValues))
  1096. {
  1097. $aValues = array();
  1098. }
  1099. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1100. $sClass = $oQuery->GetClass();
  1101. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  1102. {
  1103. if (count($aValues) == 0)
  1104. {
  1105. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  1106. if (is_array($aAllowed))
  1107. {
  1108. $aValues = array_keys($aAllowed);
  1109. }
  1110. }
  1111. }
  1112. return $aValues;
  1113. }
  1114. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1115. {
  1116. $sTitle = $this->aProperties['title'];
  1117. $sIcon = $this->aProperties['icon'];
  1118. $sSubtitle = $this->aProperties['subtitle'];
  1119. $sQuery = $this->aProperties['query'];
  1120. $sGroupBy = $this->aProperties['group_by'];
  1121. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1122. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1123. $aValues = $this->GetValues();
  1124. if (count($aValues) > 0)
  1125. {
  1126. // Stats grouped by <group_by>
  1127. $sCSV = implode(',', $aValues);
  1128. $aExtraParams = array(
  1129. 'title[block]' => $sTitle,
  1130. 'label[block]' => $sSubtitle,
  1131. 'status[block]' => $sGroupBy,
  1132. 'status_codes[block]' => $sCSV,
  1133. 'context_filter' => 1,
  1134. );
  1135. }
  1136. else
  1137. {
  1138. // Simple stats
  1139. $aExtraParams = array(
  1140. 'title[block]' => $sTitle,
  1141. 'label[block]' => $sSubtitle,
  1142. 'context_filter' => 1,
  1143. );
  1144. }
  1145. $oPage->add('<div class="dashlet-content">');
  1146. $oPage->add('<div class="main_header">');
  1147. $oPage->add('<img src="'.$sIconPath.'">');
  1148. $oFilter = DBObjectSearch::FromOQL($sQuery);
  1149. $oBlock = new DisplayBlock($oFilter, 'summary');
  1150. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1151. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1152. $oPage->add('</div>');
  1153. $oPage->add('</div>');
  1154. }
  1155. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1156. {
  1157. $sTitle = $this->aProperties['title'];
  1158. $sIcon = $this->aProperties['icon'];
  1159. $sSubtitle = $this->aProperties['subtitle'];
  1160. $sQuery = $this->aProperties['query'];
  1161. $sGroupBy = $this->aProperties['group_by'];
  1162. $aValues = $this->aProperties['values'];
  1163. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1164. $sClass = $oQuery->GetClass();
  1165. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1166. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1167. $oPage->add('<div class="dashlet-content">');
  1168. $oPage->add('<div class="main_header">');
  1169. $oPage->add('<img src="'.$sIconPath.'">');
  1170. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1171. $iTotal = 0;
  1172. $aValues = $this->GetValues();
  1173. if (count($aValues) > 0)
  1174. {
  1175. // Stats grouped by <group_by>
  1176. }
  1177. else
  1178. {
  1179. // Simple stats
  1180. }
  1181. $oPage->add('<div class="display_block" id="'.$sBlockId.'">');
  1182. $oPage->add('<div class="summary-details">');
  1183. $oPage->add('<table><tbody>');
  1184. $oPage->add('<tr>');
  1185. foreach ($aValues as $sValue)
  1186. {
  1187. $sValueLabel = $this->oModelReflection->GetValueLabel($sClass, $sGroupBy, $sValue);
  1188. $oPage->add(' <th>'.$sValueLabel.'</th>');
  1189. }
  1190. $oPage->add('</tr>');
  1191. $oPage->add('<tr>');
  1192. foreach ($aValues as $sValue)
  1193. {
  1194. $iCount = (int) rand(2, 100);
  1195. $iTotal += $iCount;
  1196. $oPage->add(' <td>'.$iCount.'</td>');
  1197. }
  1198. $oPage->add('</tr>');
  1199. $oPage->add('</tbody></table>');
  1200. $oPage->add('</div>');
  1201. $sTitle = $this->oModelReflection->DictString($sTitle);
  1202. $sSubtitle = $this->oModelReflection->DictFormat($sSubtitle, $iTotal);
  1203. // $sSubtitle = "original: $sSubtitle, S:".$this->oModelReflection->DictString($sSubtitle).", Format: '".$this->oModelReflection->DictFormat($sSubtitle, $iTotal)."'";
  1204. $oPage->add('<h1>'.$sTitle.'</h1>');
  1205. $oPage->add('<a class="summary">'.$sSubtitle.'</a>');
  1206. $oPage->add('</div>');
  1207. $oPage->add('</div>');
  1208. $oPage->add('</div>');
  1209. }
  1210. public function GetPropertiesFields(DesignerForm $oForm)
  1211. {
  1212. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  1213. $oForm->AddField($oField);
  1214. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  1215. $oForm->AddField($oField);
  1216. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  1217. $oForm->AddField($oField);
  1218. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  1219. $oField->SetMandatory();
  1220. $oForm->AddField($oField);
  1221. try
  1222. {
  1223. // Group by field: build the list of possible values (attribute codes + ...)
  1224. $oQuery = $this->oModelReflection->GetQuery($this->aProperties['query']);
  1225. $sClass = $oQuery->GetClass();
  1226. $aGroupBy = array();
  1227. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  1228. {
  1229. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  1230. {
  1231. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  1232. }
  1233. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  1234. $aGroupBy[$sAttCode] = $sLabel;
  1235. }
  1236. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1237. $oField->SetMandatory();
  1238. $oField->SetAllowedValues($aGroupBy);
  1239. }
  1240. catch(Exception $e)
  1241. {
  1242. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1243. $oField->SetReadOnly();
  1244. }
  1245. $oForm->AddField($oField);
  1246. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  1247. $oField->MultipleSelection(true);
  1248. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  1249. {
  1250. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  1251. $oField->SetAllowedValues($aValues);
  1252. }
  1253. else
  1254. {
  1255. $oField->SetReadOnly();
  1256. }
  1257. $oForm->AddField($oField);
  1258. }
  1259. public function Update($aValues, $aUpdatedFields)
  1260. {
  1261. if (in_array('query', $aUpdatedFields))
  1262. {
  1263. try
  1264. {
  1265. $sCurrQuery = $aValues['query'];
  1266. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  1267. $sCurrClass = $oCurrSearch->GetClass();
  1268. $sPrevQuery = $this->aProperties['query'];
  1269. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  1270. $sPrevClass = $oPrevSearch->GetClass();
  1271. if ($sCurrClass != $sPrevClass)
  1272. {
  1273. $this->bFormRedrawNeeded = true;
  1274. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  1275. $this->aProperties['group_by'] = '';
  1276. $this->aProperties['values'] = array();
  1277. }
  1278. }
  1279. catch(Exception $e)
  1280. {
  1281. $this->bFormRedrawNeeded = true;
  1282. }
  1283. }
  1284. if (in_array('group_by', $aUpdatedFields))
  1285. {
  1286. $this->bFormRedrawNeeded = true;
  1287. $this->aProperties['values'] = array();
  1288. }
  1289. return parent::Update($aValues, $aUpdatedFields);
  1290. }
  1291. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1292. {
  1293. if ($sProperty == 'icon')
  1294. {
  1295. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1296. return $oIconField->ValueFromDOMNode($oDOMNode);
  1297. }
  1298. else
  1299. {
  1300. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1301. }
  1302. }
  1303. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1304. {
  1305. if ($sProperty == 'icon')
  1306. {
  1307. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1308. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1309. }
  1310. else
  1311. {
  1312. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1313. }
  1314. }
  1315. static public function GetInfo()
  1316. {
  1317. return array(
  1318. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  1319. 'icon' => 'images/dashlet-header-stats.png',
  1320. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  1321. );
  1322. }
  1323. }
  1324. class DashletBadge extends Dashlet
  1325. {
  1326. public function __construct($oModelReflection, $sId)
  1327. {
  1328. parent::__construct($oModelReflection, $sId);
  1329. $this->aProperties['class'] = 'Contact';
  1330. $this->aCSSClasses[] = 'dashlet-inline';
  1331. $this->aCSSClasses[] = 'dashlet-badge';
  1332. }
  1333. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1334. {
  1335. $sClass = $this->aProperties['class'];
  1336. $oPage->add('<div class="dashlet-content">');
  1337. $oFilter = new DBObjectSearch($sClass);
  1338. $oBlock = new DisplayBlock($oFilter, 'actions');
  1339. $aExtraParams = array(
  1340. 'context_filter' => 1,
  1341. );
  1342. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1343. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1344. $oPage->add('</div>');
  1345. }
  1346. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1347. {
  1348. $sClass = $this->aProperties['class'];
  1349. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1350. $sClassLabel = $this->oModelReflection->GetName($sClass);
  1351. $oPage->add('<div class="dashlet-content">');
  1352. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  1353. $oPage->add('<p>');
  1354. $oPage->add(' <a class="actions"><img src="'.$sIconUrl.'" style="vertical-align:middle;float;left;margin-right:10px;border:0;">'.$sClassLabel.': 947</a>');
  1355. $oPage->add('</p>');
  1356. $oPage->add('<p>');
  1357. $oPage->add(' <a>'.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'</a>');
  1358. $oPage->add(' <br/>');
  1359. $oPage->add(' <a>Search for Server objects</a>');
  1360. $oPage->add('</p>');
  1361. $oPage->add('</div>');
  1362. $oPage->add('</div>');
  1363. }
  1364. static protected $aClassList = null;
  1365. public function GetPropertiesFields(DesignerForm $oForm)
  1366. {
  1367. if (is_null(self::$aClassList))
  1368. {
  1369. // Cache the ordered list of classes (ordered on the label)
  1370. // (has a significant impact when editing a page with lots of badges)
  1371. //
  1372. $aClasses = array();
  1373. foreach($this->oModelReflection->GetClasses('bizmodel', true /*exclude links*/) as $sClass)
  1374. {
  1375. $aClasses[$sClass] = $this->oModelReflection->GetName($sClass);
  1376. }
  1377. asort($aClasses);
  1378. self::$aClassList = array();
  1379. foreach($aClasses as $sClass => $sLabel)
  1380. {
  1381. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1382. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  1383. if ($sIconUrl == '')
  1384. {
  1385. // The icon does not exist, let's use a transparent one of the same size.
  1386. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  1387. }
  1388. self::$aClassList[] = array('value' => $sClass, 'label' => $sLabel, 'icon' => $sIconUrl);
  1389. }
  1390. }
  1391. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  1392. $oField->SetAllowedValues(self::$aClassList);
  1393. $oForm->AddField($oField);
  1394. }
  1395. static public function GetInfo()
  1396. {
  1397. return array(
  1398. 'label' => Dict::S('UI:DashletBadge:Label'),
  1399. 'icon' => 'images/dashlet-badge.png',
  1400. 'description' => Dict::S('UI:DashletBadge:Description'),
  1401. );
  1402. }
  1403. }
  1404. ?>