dashlet.class.inc.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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. $oTitle->set_style("{font-size: 16px; font-family: Tahoma; font-weight: bold; text-align: center;}");
  827. }
  828. $oChart->set_bg_colour('#FFFFFF');
  829. $oChart->add_element($oChartElement);
  830. $sData = $oChart->toPrettyString();
  831. $sData = json_encode($sData);
  832. $oPage->add_script(
  833. <<< EOF
  834. function ofc_get_data_dashlet_{$this->sId}()
  835. {
  836. return $sData;
  837. }
  838. EOF
  839. );
  840. $oPage->add('<div class="dashlet-content">');
  841. $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");
  842. $oPage->add('</div>');
  843. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  844. $oPage->add_ready_script(
  845. <<<EOF
  846. swfobject.embedSWF( "../images/open-flash-chart.swf",
  847. "dashlet_chart_{$this->sId}",
  848. "100%", "300","9.0.0",
  849. "expressInstall.swf",
  850. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  851. {'wmode': 'transparent'}
  852. );
  853. EOF
  854. );
  855. }
  856. }
  857. class DashletGroupByBars extends DashletGroupBy
  858. {
  859. public function __construct($oModelReflection, $sId)
  860. {
  861. parent::__construct($oModelReflection, $sId);
  862. $this->aProperties['style'] = 'bars';
  863. }
  864. static public function GetInfo()
  865. {
  866. return array(
  867. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  868. 'icon' => 'images/dashlet-bar-chart.png',
  869. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  870. );
  871. }
  872. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  873. {
  874. $sTitle = $this->aProperties['title'];
  875. $aDisplayValues = $this->MakeSimulatedData();
  876. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  877. $oChart = new open_flash_chart();
  878. $aGroupBy = array();
  879. $aLabels = array();
  880. foreach($aDisplayValues as $iRow => $aDisplayData)
  881. {
  882. $aLabels[$iRow] = $aDisplayData['label'];
  883. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  884. }
  885. $oChartElement = new bar_glass();
  886. $aData = array();
  887. $aChartLabels = array();
  888. $maxValue = 0;
  889. foreach($aGroupBy as $iRow => $iCount)
  890. {
  891. $oBarValue = new bar_value($iCount);
  892. $aData[] = $oBarValue;
  893. if ($iCount > $maxValue) $maxValue = $iCount;
  894. $aChartLabels[] = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  895. }
  896. $oYAxis = new y_axis();
  897. $aMagicValues = array(1,2,5,10);
  898. $iMultiplier = 1;
  899. $index = 0;
  900. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  901. while($maxValue > $iTop)
  902. {
  903. $index++;
  904. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  905. if (($index % count($aMagicValues)) == 0)
  906. {
  907. $iMultiplier = $iMultiplier * 10;
  908. }
  909. }
  910. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  911. $oYAxis->set_range(0, $iTop, $iMultiplier);
  912. $oChart->set_y_axis( $oYAxis );
  913. $oChartElement->set_values( $aData );
  914. $oXAxis = new x_axis();
  915. $oXLabels = new x_axis_labels();
  916. // set them vertical
  917. $oXLabels->set_vertical();
  918. // set the label text
  919. $oXLabels->set_labels($aChartLabels);
  920. // Add the X Axis Labels to the X Axis
  921. $oXAxis->set_labels( $oXLabels );
  922. $oChart->set_x_axis( $oXAxis );
  923. if (!empty($sTitle))
  924. {
  925. // The title has been given in an url, and urlencoded...
  926. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  927. // Example: é (C3A9 becomes %E9)
  928. // As a consequence, json_encode (called within open-flash-chart.php)
  929. // was returning 'null' and the graph was not displayed at all
  930. // To make sure that the graph is displayed AND to get a correct title
  931. // (at least for european characters) let's transform back into utf-8 !
  932. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  933. // If the title is a dictionnary entry, fetch it
  934. $sTitle = $this->oModelReflection->DictString($sTitle);
  935. $oTitle = new title($sTitle);
  936. $oChart->set_title($oTitle);
  937. $oTitle->set_style("{font-size: 16px; font-family: Tahoma; font-weight: bold; text-align: center;}");
  938. }
  939. $oChart->set_bg_colour('#FFFFFF');
  940. $oChart->add_element($oChartElement);
  941. $sData = $oChart->toPrettyString();
  942. $sData = json_encode($sData);
  943. $oPage->add_script(
  944. <<< EOF
  945. function ofc_get_data_dashlet_{$this->sId}()
  946. {
  947. return $sData;
  948. }
  949. EOF
  950. );
  951. $oPage->add('<div class="dashlet-content">');
  952. $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");
  953. $oPage->add('</div>');
  954. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  955. $oPage->add_ready_script(
  956. <<<EOF
  957. swfobject.embedSWF( "../images/open-flash-chart.swf",
  958. "dashlet_chart_{$this->sId}",
  959. "100%", "300","9.0.0",
  960. "expressInstall.swf",
  961. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  962. {'wmode': 'transparent'}
  963. );
  964. EOF
  965. );
  966. }
  967. }
  968. class DashletGroupByTable extends DashletGroupBy
  969. {
  970. public function __construct($oModelReflection, $sId)
  971. {
  972. parent::__construct($oModelReflection, $sId);
  973. $this->aProperties['style'] = 'table';
  974. }
  975. static public function GetInfo()
  976. {
  977. return array(
  978. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  979. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  980. 'icon' => 'images/dashlet-groupby-table.png',
  981. );
  982. }
  983. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  984. {
  985. $sTitle = $this->aProperties['title'];
  986. $aDisplayValues = $this->MakeSimulatedData();
  987. $iTotal = 0;
  988. foreach($aDisplayValues as $iRow => $aDisplayData)
  989. {
  990. $iTotal += $aDisplayData['count'];
  991. }
  992. $oPage->add('<div class="dashlet-content">');
  993. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  994. $oPage->add('<div id="'.$sBlockId.'" class="display_block">');
  995. $oPage->add('<p>'.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'</p>');
  996. $oPage->add('<table class="listResults">');
  997. $oPage->add('<thead>');
  998. $oPage->add('<tr>');
  999. $oPage->add('<th class="header" title="">'.$this->sGroupByLabel.'</th>');
  1000. $oPage->add('<th class="header" title="'.Dict::S('UI:GroupBy:Count+').'">'.Dict::S('UI:GroupBy:Count').'</th>');
  1001. $oPage->add('</tr>');
  1002. $oPage->add('</thead>');
  1003. $oPage->add('<tbody>');
  1004. foreach($aDisplayValues as $aDisplayData)
  1005. {
  1006. $oPage->add('<tr class="even">');
  1007. $oPage->add('<td class=""><span title="Active">'.$aDisplayData['label'].'</span></td>');
  1008. $oPage->add('<td class=""><a>'.$aDisplayData['count'].'</a></td>');
  1009. $oPage->add('</tr>');
  1010. }
  1011. $oPage->add('</tbody>');
  1012. $oPage->add('</table>');
  1013. $oPage->add('</div>');
  1014. $oPage->add('</div>');
  1015. }
  1016. }
  1017. class DashletHeaderStatic extends Dashlet
  1018. {
  1019. public function __construct($oModelReflection, $sId)
  1020. {
  1021. parent::__construct($oModelReflection, $sId);
  1022. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  1023. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1024. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1025. }
  1026. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1027. {
  1028. $sTitle = $this->aProperties['title'];
  1029. $sIcon = $this->aProperties['icon'];
  1030. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1031. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1032. $oPage->add('<div class="dashlet-content">');
  1033. $oPage->add('<div class="main_header">');
  1034. $oPage->add('<img src="'.$sIconPath.'">');
  1035. $oPage->add('<h1>'.$this->oModelReflection->DictString($sTitle).'</h1>');
  1036. $oPage->add('</div>');
  1037. $oPage->add('</div>');
  1038. }
  1039. public function GetPropertiesFields(DesignerForm $oForm)
  1040. {
  1041. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  1042. $oForm->AddField($oField);
  1043. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  1044. $oForm->AddField($oField);
  1045. }
  1046. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1047. {
  1048. if ($sProperty == 'icon')
  1049. {
  1050. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1051. return $oIconField->ValueFromDOMNode($oDOMNode);
  1052. }
  1053. else
  1054. {
  1055. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1056. }
  1057. }
  1058. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1059. {
  1060. if ($sProperty == 'icon')
  1061. {
  1062. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1063. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1064. }
  1065. else
  1066. {
  1067. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1068. }
  1069. }
  1070. static public function GetInfo()
  1071. {
  1072. return array(
  1073. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  1074. 'icon' => 'images/dashlet-header.png',
  1075. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  1076. );
  1077. }
  1078. }
  1079. class DashletHeaderDynamic extends Dashlet
  1080. {
  1081. public function __construct($oModelReflection, $sId)
  1082. {
  1083. parent::__construct($oModelReflection, $sId);
  1084. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  1085. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1086. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1087. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  1088. $this->aProperties['query'] = 'SELECT Contact';
  1089. $this->aProperties['group_by'] = 'status';
  1090. $this->aProperties['values'] = array('active', 'inactive');
  1091. }
  1092. protected function GetValues()
  1093. {
  1094. $sQuery = $this->aProperties['query'];
  1095. $sGroupBy = $this->aProperties['group_by'];
  1096. $aValues = $this->aProperties['values'];
  1097. if (empty($aValues))
  1098. {
  1099. $aValues = array();
  1100. }
  1101. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1102. $sClass = $oQuery->GetClass();
  1103. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  1104. {
  1105. if (count($aValues) == 0)
  1106. {
  1107. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  1108. if (is_array($aAllowed))
  1109. {
  1110. $aValues = array_keys($aAllowed);
  1111. }
  1112. }
  1113. }
  1114. return $aValues;
  1115. }
  1116. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1117. {
  1118. $sTitle = $this->aProperties['title'];
  1119. $sIcon = $this->aProperties['icon'];
  1120. $sSubtitle = $this->aProperties['subtitle'];
  1121. $sQuery = $this->aProperties['query'];
  1122. $sGroupBy = $this->aProperties['group_by'];
  1123. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1124. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1125. $aValues = $this->GetValues();
  1126. if (count($aValues) > 0)
  1127. {
  1128. // Stats grouped by <group_by>
  1129. $sCSV = implode(',', $aValues);
  1130. $aExtraParams = array(
  1131. 'title[block]' => $sTitle,
  1132. 'label[block]' => $sSubtitle,
  1133. 'status[block]' => $sGroupBy,
  1134. 'status_codes[block]' => $sCSV,
  1135. 'context_filter' => 1,
  1136. );
  1137. }
  1138. else
  1139. {
  1140. // Simple stats
  1141. $aExtraParams = array(
  1142. 'title[block]' => $sTitle,
  1143. 'label[block]' => $sSubtitle,
  1144. 'context_filter' => 1,
  1145. );
  1146. }
  1147. $oPage->add('<div class="dashlet-content">');
  1148. $oPage->add('<div class="main_header">');
  1149. $oPage->add('<img src="'.$sIconPath.'">');
  1150. $oFilter = DBObjectSearch::FromOQL($sQuery);
  1151. $oBlock = new DisplayBlock($oFilter, 'summary');
  1152. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1153. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1154. $oPage->add('</div>');
  1155. $oPage->add('</div>');
  1156. }
  1157. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1158. {
  1159. $sTitle = $this->aProperties['title'];
  1160. $sIcon = $this->aProperties['icon'];
  1161. $sSubtitle = $this->aProperties['subtitle'];
  1162. $sQuery = $this->aProperties['query'];
  1163. $sGroupBy = $this->aProperties['group_by'];
  1164. $aValues = $this->aProperties['values'];
  1165. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1166. $sClass = $oQuery->GetClass();
  1167. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1168. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1169. $oPage->add('<div class="dashlet-content">');
  1170. $oPage->add('<div class="main_header">');
  1171. $oPage->add('<img src="'.$sIconPath.'">');
  1172. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1173. $iTotal = 0;
  1174. $aValues = $this->GetValues();
  1175. if (count($aValues) > 0)
  1176. {
  1177. // Stats grouped by <group_by>
  1178. }
  1179. else
  1180. {
  1181. // Simple stats
  1182. }
  1183. $oPage->add('<div class="display_block" id="'.$sBlockId.'">');
  1184. $oPage->add('<div class="summary-details">');
  1185. $oPage->add('<table><tbody>');
  1186. $oPage->add('<tr>');
  1187. foreach ($aValues as $sValue)
  1188. {
  1189. $sValueLabel = $this->oModelReflection->GetValueLabel($sClass, $sGroupBy, $sValue);
  1190. $oPage->add(' <th>'.$sValueLabel.'</th>');
  1191. }
  1192. $oPage->add('</tr>');
  1193. $oPage->add('<tr>');
  1194. foreach ($aValues as $sValue)
  1195. {
  1196. $iCount = (int) rand(2, 100);
  1197. $iTotal += $iCount;
  1198. $oPage->add(' <td>'.$iCount.'</td>');
  1199. }
  1200. $oPage->add('</tr>');
  1201. $oPage->add('</tbody></table>');
  1202. $oPage->add('</div>');
  1203. $sTitle = $this->oModelReflection->DictString($sTitle);
  1204. $sSubtitle = $this->oModelReflection->DictFormat($sSubtitle, $iTotal);
  1205. // $sSubtitle = "original: $sSubtitle, S:".$this->oModelReflection->DictString($sSubtitle).", Format: '".$this->oModelReflection->DictFormat($sSubtitle, $iTotal)."'";
  1206. $oPage->add('<h1>'.$sTitle.'</h1>');
  1207. $oPage->add('<a class="summary">'.$sSubtitle.'</a>');
  1208. $oPage->add('</div>');
  1209. $oPage->add('</div>');
  1210. $oPage->add('</div>');
  1211. }
  1212. public function GetPropertiesFields(DesignerForm $oForm)
  1213. {
  1214. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  1215. $oForm->AddField($oField);
  1216. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  1217. $oForm->AddField($oField);
  1218. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  1219. $oForm->AddField($oField);
  1220. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  1221. $oField->SetMandatory();
  1222. $oForm->AddField($oField);
  1223. try
  1224. {
  1225. // Group by field: build the list of possible values (attribute codes + ...)
  1226. $oQuery = $this->oModelReflection->GetQuery($this->aProperties['query']);
  1227. $sClass = $oQuery->GetClass();
  1228. $aGroupBy = array();
  1229. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  1230. {
  1231. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  1232. {
  1233. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  1234. }
  1235. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  1236. $aGroupBy[$sAttCode] = $sLabel;
  1237. }
  1238. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1239. $oField->SetMandatory();
  1240. $oField->SetAllowedValues($aGroupBy);
  1241. }
  1242. catch(Exception $e)
  1243. {
  1244. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1245. $oField->SetReadOnly();
  1246. }
  1247. $oForm->AddField($oField);
  1248. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  1249. $oField->MultipleSelection(true);
  1250. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  1251. {
  1252. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  1253. $oField->SetAllowedValues($aValues);
  1254. }
  1255. else
  1256. {
  1257. $oField->SetReadOnly();
  1258. }
  1259. $oForm->AddField($oField);
  1260. }
  1261. public function Update($aValues, $aUpdatedFields)
  1262. {
  1263. if (in_array('query', $aUpdatedFields))
  1264. {
  1265. try
  1266. {
  1267. $sCurrQuery = $aValues['query'];
  1268. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  1269. $sCurrClass = $oCurrSearch->GetClass();
  1270. $sPrevQuery = $this->aProperties['query'];
  1271. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  1272. $sPrevClass = $oPrevSearch->GetClass();
  1273. if ($sCurrClass != $sPrevClass)
  1274. {
  1275. $this->bFormRedrawNeeded = true;
  1276. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  1277. $this->aProperties['group_by'] = '';
  1278. $this->aProperties['values'] = array();
  1279. }
  1280. }
  1281. catch(Exception $e)
  1282. {
  1283. $this->bFormRedrawNeeded = true;
  1284. }
  1285. }
  1286. if (in_array('group_by', $aUpdatedFields))
  1287. {
  1288. $this->bFormRedrawNeeded = true;
  1289. $this->aProperties['values'] = array();
  1290. }
  1291. return parent::Update($aValues, $aUpdatedFields);
  1292. }
  1293. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1294. {
  1295. if ($sProperty == 'icon')
  1296. {
  1297. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1298. return $oIconField->ValueFromDOMNode($oDOMNode);
  1299. }
  1300. else
  1301. {
  1302. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1303. }
  1304. }
  1305. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1306. {
  1307. if ($sProperty == 'icon')
  1308. {
  1309. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1310. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1311. }
  1312. else
  1313. {
  1314. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1315. }
  1316. }
  1317. static public function GetInfo()
  1318. {
  1319. return array(
  1320. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  1321. 'icon' => 'images/dashlet-header-stats.png',
  1322. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  1323. );
  1324. }
  1325. }
  1326. class DashletBadge extends Dashlet
  1327. {
  1328. public function __construct($oModelReflection, $sId)
  1329. {
  1330. parent::__construct($oModelReflection, $sId);
  1331. $this->aProperties['class'] = 'Contact';
  1332. $this->aCSSClasses[] = 'dashlet-inline';
  1333. $this->aCSSClasses[] = 'dashlet-badge';
  1334. }
  1335. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1336. {
  1337. $sClass = $this->aProperties['class'];
  1338. $oPage->add('<div class="dashlet-content">');
  1339. $oFilter = new DBObjectSearch($sClass);
  1340. $oBlock = new DisplayBlock($oFilter, 'actions');
  1341. $aExtraParams = array(
  1342. 'context_filter' => 1,
  1343. );
  1344. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1345. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1346. $oPage->add('</div>');
  1347. }
  1348. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1349. {
  1350. $sClass = $this->aProperties['class'];
  1351. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1352. $sClassLabel = $this->oModelReflection->GetName($sClass);
  1353. $oPage->add('<div class="dashlet-content">');
  1354. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  1355. $oPage->add('<p>');
  1356. $oPage->add(' <a class="actions"><img src="'.$sIconUrl.'" style="vertical-align:middle;float;left;margin-right:10px;border:0;">'.$sClassLabel.': 947</a>');
  1357. $oPage->add('</p>');
  1358. $oPage->add('<p>');
  1359. $oPage->add(' <a>'.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'</a>');
  1360. $oPage->add(' <br/>');
  1361. $oPage->add(' <a>Search for Server objects</a>');
  1362. $oPage->add('</p>');
  1363. $oPage->add('</div>');
  1364. $oPage->add('</div>');
  1365. }
  1366. static protected $aClassList = null;
  1367. public function GetPropertiesFields(DesignerForm $oForm)
  1368. {
  1369. if (is_null(self::$aClassList))
  1370. {
  1371. // Cache the ordered list of classes (ordered on the label)
  1372. // (has a significant impact when editing a page with lots of badges)
  1373. //
  1374. $aClasses = array();
  1375. foreach($this->oModelReflection->GetClasses('bizmodel', true /*exclude links*/) as $sClass)
  1376. {
  1377. $aClasses[$sClass] = $this->oModelReflection->GetName($sClass);
  1378. }
  1379. asort($aClasses);
  1380. self::$aClassList = array();
  1381. foreach($aClasses as $sClass => $sLabel)
  1382. {
  1383. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1384. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  1385. if ($sIconUrl == '')
  1386. {
  1387. // The icon does not exist, let's use a transparent one of the same size.
  1388. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  1389. }
  1390. self::$aClassList[] = array('value' => $sClass, 'label' => $sLabel, 'icon' => $sIconUrl);
  1391. }
  1392. }
  1393. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  1394. $oField->SetAllowedValues(self::$aClassList);
  1395. $oForm->AddField($oField);
  1396. }
  1397. static public function GetInfo()
  1398. {
  1399. return array(
  1400. 'label' => Dict::S('UI:DashletBadge:Label'),
  1401. 'icon' => 'images/dashlet-badge.png',
  1402. 'description' => Dict::S('UI:DashletBadge:Description'),
  1403. );
  1404. }
  1405. }
  1406. ?>