dashlet.class.inc.php 47 KB

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