dashlet.class.inc.php 47 KB

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