dashlet.class.inc.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 ($sRefType == 'boolean')
  49. {
  50. $ret = ($sValue == 'true');
  51. }
  52. elseif ($sRefType == 'array')
  53. {
  54. $ret = explode(',', $sValue);
  55. }
  56. else
  57. {
  58. $ret = $sValue;
  59. settype($ret, $sRefType);
  60. }
  61. return $ret;
  62. }
  63. public function Prop2Str($value)
  64. {
  65. $sType = gettype($value);
  66. if ($sType == 'boolean')
  67. {
  68. $sRet = $value ? 'true' : 'false';
  69. }
  70. elseif ($sType == 'array')
  71. {
  72. $sRet = implode(',', $value);
  73. }
  74. else
  75. {
  76. $sRet = (string) $value;
  77. }
  78. return $sRet;
  79. }
  80. public function FromDOMNode($oDOMNode)
  81. {
  82. foreach ($this->aProperties as $sProperty => $value)
  83. {
  84. $oPropNode = $oDOMNode->getElementsByTagName($sProperty)->item(0);
  85. if ($oPropNode != null)
  86. {
  87. $this->aProperties[$sProperty] = $this->PropertyFromDOMNode($oPropNode, $sProperty);
  88. }
  89. }
  90. }
  91. public function ToDOMNode($oDOMNode)
  92. {
  93. foreach ($this->aProperties as $sProperty => $value)
  94. {
  95. $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty);
  96. $oDOMNode->appendChild($oPropNode);
  97. $this->PropertyToDOMNode($oPropNode, $sProperty, $value);
  98. }
  99. }
  100. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  101. {
  102. $res = $this->Str2Prop($sProperty, $oDOMNode->textContent);
  103. return $res;
  104. }
  105. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  106. {
  107. $sXmlValue = $this->Prop2Str($value);
  108. $oTextNode = $oDOMNode->ownerDocument->createTextNode($sXmlValue);
  109. $oDOMNode->appendChild($oTextNode);
  110. }
  111. public function FromXml($sXml)
  112. {
  113. $oDomDoc = new DOMDocument('1.0', 'UTF-8');
  114. $oDomDoc->loadXml($sXml);
  115. $this->FromDOMNode($oDomDoc->firstChild);
  116. }
  117. public function FromParams($aParams)
  118. {
  119. foreach ($this->aProperties as $sProperty => $value)
  120. {
  121. if (array_key_exists($sProperty, $aParams))
  122. {
  123. $this->aProperties[$sProperty] = $aParams[$sProperty];
  124. }
  125. }
  126. }
  127. public function DoRender($oPage, $bEditMode = false, $bEnclosingDiv = true, $aExtraParams = array())
  128. {
  129. $sCSSClasses = implode(' ', $this->aCSSClasses);
  130. $sId = $this->GetID();
  131. if ($bEnclosingDiv)
  132. {
  133. if ($bEditMode)
  134. {
  135. $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
  136. }
  137. else
  138. {
  139. $oPage->add('<div class="'.$sCSSClasses.'">');
  140. }
  141. }
  142. else
  143. {
  144. foreach ($this->aCSSClasses as $sCSSClass)
  145. {
  146. $oPage->add_ready_script("$('#dashlet_".$sId."').addClass('$sCSSClass');");
  147. }
  148. }
  149. try
  150. {
  151. if (get_class($this->oModelReflection) == 'ModelReflectionRuntime')
  152. {
  153. $this->Render($oPage, $bEditMode, $aExtraParams);
  154. }
  155. else
  156. {
  157. $this->RenderNoData($oPage, $bEditMode, $aExtraParams);
  158. }
  159. }
  160. catch(UnknownClassOqlException $e)
  161. {
  162. // Maybe the class is part of a non-installed module, fail silently
  163. // Except in Edit mode
  164. if ($bEditMode)
  165. {
  166. $oPage->add('<div class="dashlet-content">');
  167. $oPage->add('<h2>'.$e->GetUserFriendlyDescription().'</h2>');
  168. $oPage->add('</div>');
  169. }
  170. }
  171. catch(OqlException $e)
  172. {
  173. $oPage->add('<div class="dashlet-content">');
  174. $oPage->p($e->GetUserFriendlyDescription());
  175. $oPage->add('</div>');
  176. }
  177. catch(Exception $e)
  178. {
  179. $oPage->add('<div class="dashlet-content">');
  180. $oPage->p($e->getMessage());
  181. $oPage->add('</div>');
  182. }
  183. if ($bEnclosingDiv)
  184. {
  185. $oPage->add('</div>');
  186. }
  187. if ($bEditMode)
  188. {
  189. $sClass = get_class($this);
  190. $oPage->add_ready_script(
  191. <<<EOF
  192. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  193. EOF
  194. );
  195. }
  196. }
  197. public function SetID($sId)
  198. {
  199. $this->sId = $sId;
  200. }
  201. public function GetID()
  202. {
  203. return $this->sId;
  204. }
  205. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  206. /* Rendering without the real data */
  207. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  208. {
  209. $this->Render($oPage, $bEditMode, $aExtraParams);
  210. }
  211. abstract public function GetPropertiesFields(DesignerForm $oForm);
  212. public function ToXml(DOMNode $oContainerNode)
  213. {
  214. }
  215. public function Update($aValues, $aUpdatedFields)
  216. {
  217. foreach($aUpdatedFields as $sProp)
  218. {
  219. if (array_key_exists($sProp, $this->aProperties))
  220. {
  221. $this->aProperties[$sProp] = $aValues[$sProp];
  222. }
  223. }
  224. return $this;
  225. }
  226. public function IsRedrawNeeded()
  227. {
  228. return $this->bRedrawNeeded;
  229. }
  230. public function IsFormRedrawNeeded()
  231. {
  232. return $this->bFormRedrawNeeded;
  233. }
  234. static public function GetInfo()
  235. {
  236. return array(
  237. 'label' => '',
  238. 'icon' => '',
  239. 'description' => '',
  240. );
  241. }
  242. public function GetForm()
  243. {
  244. $oForm = new DesignerForm();
  245. $oForm->SetPrefix("dashlet_". $this->GetID());
  246. $oForm->SetParamsContainer('params');
  247. $this->GetPropertiesFields($oForm);
  248. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  249. $oForm->AddField($oDashletClassField);
  250. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  251. $oForm->AddField($oDashletIdField);
  252. return $oForm;
  253. }
  254. static public function IsVisible()
  255. {
  256. return true;
  257. }
  258. static public function CanCreateFromOQL()
  259. {
  260. return false;
  261. }
  262. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  263. {
  264. // Default: do nothing since it's not supported
  265. }
  266. }
  267. class DashletEmptyCell extends Dashlet
  268. {
  269. public function __construct($oModelReflection, $sId)
  270. {
  271. parent::__construct($oModelReflection, $sId);
  272. }
  273. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  274. {
  275. $oPage->add('&nbsp;');
  276. }
  277. public function GetPropertiesFields(DesignerForm $oForm)
  278. {
  279. }
  280. static public function GetInfo()
  281. {
  282. return array(
  283. 'label' => 'Empty Cell',
  284. 'icon' => 'images/dashlet-text.png',
  285. 'description' => 'Empty Cell Dashlet Placeholder',
  286. );
  287. }
  288. static public function IsVisible()
  289. {
  290. return false;
  291. }
  292. }
  293. class DashletPlainText extends Dashlet
  294. {
  295. public function __construct($oModelReflection, $sId)
  296. {
  297. parent::__construct($oModelReflection, $sId);
  298. $this->aProperties['text'] = Dict::S('UI:DashletPlainText:Prop-Text:Default');
  299. }
  300. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  301. {
  302. $sText = htmlentities($this->aProperties['text'], ENT_QUOTES, 'UTF-8');
  303. $sId = 'plaintext_'.($bEditMode? 'edit_' : '').$this->sId;
  304. $oPage->add('<div id="'.$sId.'" class="dashlet-content">'.$sText.'</div>');
  305. }
  306. public function GetPropertiesFields(DesignerForm $oForm)
  307. {
  308. $oField = new DesignerLongTextField('text', Dict::S('UI:DashletPlainText:Prop-Text'), $this->aProperties['text']);
  309. $oField->SetMandatory();
  310. $oForm->AddField($oField);
  311. }
  312. static public function GetInfo()
  313. {
  314. return array(
  315. 'label' => Dict::S('UI:DashletPlainText:Label'),
  316. 'icon' => 'images/dashlet-text.png',
  317. 'description' => Dict::S('UI:DashletPlainText:Description'),
  318. );
  319. }
  320. }
  321. class DashletObjectList extends Dashlet
  322. {
  323. public function __construct($oModelReflection, $sId)
  324. {
  325. parent::__construct($oModelReflection, $sId);
  326. $this->aProperties['title'] = '';
  327. $this->aProperties['query'] = 'SELECT Contact';
  328. $this->aProperties['menu'] = false;
  329. }
  330. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  331. {
  332. $sTitle = $this->aProperties['title'];
  333. $sQuery = $this->aProperties['query'];
  334. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  335. $oPage->add('<div class="dashlet-content">');
  336. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  337. if ($sHtmlTitle != '')
  338. {
  339. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  340. }
  341. $oFilter = DBObjectSearch::FromOQL($sQuery);
  342. $oBlock = new DisplayBlock($oFilter, 'list');
  343. $aExtraParams = array(
  344. 'menu' => $sShowMenu,
  345. 'table_id' => 'Dashlet'.$this->sId,
  346. );
  347. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  348. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  349. $oPage->add('</div>');
  350. }
  351. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  352. {
  353. $sTitle = $this->aProperties['title'];
  354. $sQuery = $this->aProperties['query'];
  355. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  356. $oPage->add('<div class="dashlet-content">');
  357. $sHtmlTitle = htmlentities($this->oModelReflection->DictString($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  358. if ($sHtmlTitle != '')
  359. {
  360. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  361. }
  362. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  363. $sClass = $oQuery->GetClass();
  364. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  365. $oPage->p(Dict::S('UI:NoObjectToDisplay'));
  366. $oPage->p('<a href="">'.Dict::Format('UI:ClickToCreateNew', $this->oModelReflection->GetName($sClass)).'</a>');
  367. $oPage->add('</div>');
  368. $oPage->add('</div>');
  369. }
  370. public function GetPropertiesFields(DesignerForm $oForm)
  371. {
  372. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), $this->aProperties['title']);
  373. $oForm->AddField($oField);
  374. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $this->aProperties['query']);
  375. $oField->SetMandatory();
  376. $oForm->AddField($oField);
  377. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  378. $oForm->AddField($oField);
  379. }
  380. static public function GetInfo()
  381. {
  382. return array(
  383. 'label' => Dict::S('UI:DashletObjectList:Label'),
  384. 'icon' => 'images/dashlet-list.png',
  385. 'description' => Dict::S('UI:DashletObjectList:Description'),
  386. );
  387. }
  388. static public function CanCreateFromOQL()
  389. {
  390. return true;
  391. }
  392. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  393. {
  394. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), '');
  395. $oForm->AddField($oField);
  396. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $sOQL);
  397. $oField->SetMandatory();
  398. $oForm->AddField($oField);
  399. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  400. $oForm->AddField($oField);
  401. }
  402. }
  403. abstract class DashletGroupBy extends Dashlet
  404. {
  405. public function __construct($oModelReflection, $sId)
  406. {
  407. parent::__construct($oModelReflection, $sId);
  408. $this->aProperties['title'] = '';
  409. $this->aProperties['query'] = 'SELECT Contact';
  410. $this->aProperties['group_by'] = 'status';
  411. $this->aProperties['style'] = 'table';
  412. }
  413. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  414. {
  415. $sTitle = $this->aProperties['title'];
  416. $sQuery = $this->aProperties['query'];
  417. $sGroupBy = $this->aProperties['group_by'];
  418. $sStyle = $this->aProperties['style'];
  419. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  420. $oFilter = DBObjectSearch::FromOQL($sQuery);
  421. $sClass = $oFilter->GetClass();
  422. $sClassAlias = $oFilter->GetClassAlias();
  423. // Check groupby... it can be wrong at this stage
  424. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  425. {
  426. $sAttCode = $aMatches[1];
  427. $sFunction = $aMatches[2];
  428. }
  429. else
  430. {
  431. $sAttCode = $sGroupBy;
  432. $sFunction = null;
  433. }
  434. if (!$this->oModelReflection->IsValidAttCode($sClass, $sAttCode))
  435. {
  436. $oPage->add('<p>'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'</p>');
  437. }
  438. else
  439. {
  440. $sAttLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  441. if (!is_null($sFunction))
  442. {
  443. $sFunction = $aMatches[2];
  444. switch($sFunction)
  445. {
  446. case 'hour':
  447. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Hour', $sAttLabel);
  448. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%H')"; // 0 -> 23
  449. break;
  450. case 'month':
  451. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Month', $sAttLabel);
  452. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%Y-%m')"; // yyyy-mm
  453. break;
  454. case 'day_of_week':
  455. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfWeek', $sAttLabel);
  456. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%w')";
  457. break;
  458. case 'day_of_month':
  459. $sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfMonth', $sAttLabel);
  460. $sGroupByExpr = "DATE_FORMAT($sClassAlias.$sAttCode, '%Y-%m-%d')"; // mm-dd
  461. break;
  462. default:
  463. $sGroupByLabel = 'Unknown group by function '.$sFunction;
  464. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  465. }
  466. }
  467. else
  468. {
  469. $sGroupByExpr = $sClassAlias.'.'.$sAttCode;
  470. $sGroupByLabel = $sAttLabel;
  471. }
  472. switch($sStyle)
  473. {
  474. case 'bars':
  475. $sType = 'open_flash_chart';
  476. $aExtraParams = array(
  477. 'chart_type' => 'bars',
  478. 'chart_title' => $sTitle,
  479. 'group_by' => $sGroupByExpr,
  480. 'group_by_label' => $sGroupByLabel,
  481. );
  482. $sHtmlTitle = ''; // done in the itop block
  483. break;
  484. case 'pie':
  485. $sType = 'open_flash_chart';
  486. $aExtraParams = array(
  487. 'chart_type' => 'pie',
  488. 'chart_title' => $sTitle,
  489. 'group_by' => $sGroupByExpr,
  490. 'group_by_label' => $sGroupByLabel,
  491. );
  492. $sHtmlTitle = ''; // done in the itop block
  493. break;
  494. case 'table':
  495. default:
  496. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  497. $sType = 'count';
  498. $aExtraParams = array(
  499. 'group_by' => $sGroupByExpr,
  500. 'group_by_label' => $sGroupByLabel,
  501. );
  502. break;
  503. }
  504. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  505. if ($sHtmlTitle != '')
  506. {
  507. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  508. }
  509. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  510. $oBlock = new DisplayBlock($oFilter, $sType);
  511. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  512. $oPage->add('</div>');
  513. }
  514. }
  515. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  516. {
  517. $sTitle = $this->aProperties['title'];
  518. $sQuery = $this->aProperties['query'];
  519. $sGroupBy = $this->aProperties['group_by'];
  520. $sStyle = $this->aProperties['style'];
  521. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  522. $sClass = $oQuery->GetClass();
  523. $aDisplayValues = array();
  524. $sAttLabel = '';
  525. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  526. {
  527. $sAttLabel = $this->oModelReflection->GetLabel($sClass, $sGroupBy);
  528. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  529. if ($aAllowed) // null for non enums
  530. {
  531. $iTotal = 0;
  532. foreach ($aAllowed as $sValue => $sValueLabel)
  533. {
  534. $iCount = (int) rand(2, 100);
  535. $iTotal += $iCount;
  536. $aDisplayValues[] = array(
  537. 'label' => $sValueLabel,
  538. 'count' => $iCount
  539. );
  540. }
  541. }
  542. }
  543. else
  544. {
  545. $oPage->add('<div class="dashlet-content">Sorry, grouping by date/time cannot be viewed in the designer</div>');
  546. }
  547. $oPage->add('<div class="dashlet-content">');
  548. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  549. $oPage->add('<div id="'.$sBlockId.'" class="display_block">');
  550. $oPage->add('<p>'.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'</p>');
  551. $oPage->add('<table class="listResults">');
  552. $oPage->add('<thead>');
  553. $oPage->add('<tr>');
  554. $oPage->add('<th class="header" title="">'.$sAttLabel.'</th>');
  555. $oPage->add('<th class="header" title="'.Dict::S('UI:GroupBy:Count+').'">'.Dict::S('UI:GroupBy:Count').'</th>');
  556. $oPage->add('</tr>');
  557. $oPage->add('</thead>');
  558. $oPage->add('<tbody>');
  559. foreach($aDisplayValues as $aDisplayData)
  560. {
  561. $oPage->add('<tr class="even">');
  562. $oPage->add('<td class=""><span title="Active">'.$aDisplayData['label'].'</span></td>');
  563. $oPage->add('<td class=""><a href="">'.$aDisplayData['count'].'</a></td>');
  564. $oPage->add('</tr>');
  565. }
  566. $oPage->add('</tbody>');
  567. $oPage->add('</table>');
  568. $oPage->add('</div>');
  569. $oPage->add('</div>');
  570. }
  571. protected function GetGroupByOptions($sOql)
  572. {
  573. $oQuery = $this->oModelReflection->GetQuery($sOql);
  574. $sClass = $oQuery->GetClass();
  575. $aGroupBy = array();
  576. foreach($this->oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType)
  577. {
  578. if ($sAttType == 'AttributeLinkedSet') continue;
  579. if (is_subclass_of($sAttType, 'AttributeLinkedSet')) continue;
  580. if ($sAttType == 'AttributeFriendlyName') continue;
  581. if (is_subclass_of($sAttType, 'AttributeFriendlyName')) continue;
  582. if ($sAttType == 'AttributeExternalField') continue;
  583. if (is_subclass_of($sAttType, 'AttributeExternalField')) continue;
  584. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  585. $aGroupBy[$sAttCode] = $sLabel;
  586. if (is_subclass_of($sAttType, 'AttributeDateTime') || $sAttType == 'AttributeDateTime')
  587. {
  588. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  589. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  590. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  591. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  592. }
  593. }
  594. asort($aGroupBy);
  595. return $aGroupBy;
  596. }
  597. public function GetPropertiesFields(DesignerForm $oForm)
  598. {
  599. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  600. $oForm->AddField($oField);
  601. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  602. $oField->SetMandatory();
  603. $oForm->AddField($oField);
  604. try
  605. {
  606. // Group by field: build the list of possible values (attribute codes + ...)
  607. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  608. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  609. $oField->SetMandatory();
  610. $oField->SetAllowedValues($aGroupBy);
  611. }
  612. catch(Exception $e)
  613. {
  614. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  615. $oField->SetReadOnly();
  616. }
  617. $oForm->AddField($oField);
  618. $aStyles = array(
  619. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  620. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  621. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  622. );
  623. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  624. $oField->SetMandatory();
  625. $oField->SetAllowedValues($aStyles);
  626. $oForm->AddField($oField);
  627. }
  628. public function Update($aValues, $aUpdatedFields)
  629. {
  630. if (in_array('query', $aUpdatedFields))
  631. {
  632. try
  633. {
  634. $sCurrQuery = $aValues['query'];
  635. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  636. $sCurrClass = $oCurrSearch->GetClass();
  637. $sPrevQuery = $this->aProperties['query'];
  638. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  639. $sPrevClass = $oPrevSearch->GetClass();
  640. if ($sCurrClass != $sPrevClass)
  641. {
  642. $this->bFormRedrawNeeded = true;
  643. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  644. $this->aProperties['group_by'] = '';
  645. }
  646. }
  647. catch(Exception $e)
  648. {
  649. $this->bFormRedrawNeeded = true;
  650. }
  651. }
  652. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  653. if (in_array('style', $aUpdatedFields))
  654. {
  655. switch($aValues['style'])
  656. {
  657. // Style changed, mutate to the specified type of chart
  658. case 'pie':
  659. $oDashlet = new DashletGroupByPie($this->oModelReflection, $this->sId);
  660. break;
  661. case 'bars':
  662. $oDashlet = new DashletGroupByBars($this->oModelReflection, $this->sId);
  663. break;
  664. case 'table':
  665. $oDashlet = new DashletGroupByTable($this->oModelReflection, $this->sId);
  666. break;
  667. }
  668. $oDashlet->FromParams($aValues);
  669. $oDashlet->bRedrawNeeded = true;
  670. $oDashlet->bFormRedrawNeeded = true;
  671. }
  672. return $oDashlet;
  673. }
  674. static public function GetInfo()
  675. {
  676. // Note: no need to translate, should never be visible to the end-user!
  677. return array(
  678. 'label' => 'Objects grouped by...',
  679. 'icon' => 'images/dashlet-object-grouped.png',
  680. 'description' => 'Grouped objects dashlet (abstract)',
  681. );
  682. }
  683. static public function CanCreateFromOQL()
  684. {
  685. return true;
  686. }
  687. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  688. {
  689. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  690. $oForm->AddField($oField);
  691. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  692. $oField->SetMandatory();
  693. $oForm->AddField($oField);
  694. if (!is_null($sOQL))
  695. {
  696. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  697. $aGroupBy = $this->GetGroupByOptions($sOQL);
  698. $oField->SetAllowedValues($aGroupBy);
  699. }
  700. else
  701. {
  702. // Creating a form for reading parameters!
  703. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  704. }
  705. $oField->SetMandatory();
  706. $oForm->AddField($oField);
  707. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  708. $oField->SetMandatory();
  709. $oForm->AddField($oField);
  710. }
  711. }
  712. class DashletGroupByPie extends DashletGroupBy
  713. {
  714. public function __construct($oModelReflection, $sId)
  715. {
  716. parent::__construct($oModelReflection, $sId);
  717. $this->aProperties['style'] = 'pie';
  718. }
  719. static public function GetInfo()
  720. {
  721. return array(
  722. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  723. 'icon' => 'images/dashlet-pie-chart.png',
  724. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  725. );
  726. }
  727. }
  728. class DashletGroupByBars extends DashletGroupBy
  729. {
  730. public function __construct($oModelReflection, $sId)
  731. {
  732. parent::__construct($oModelReflection, $sId);
  733. $this->aProperties['style'] = 'bars';
  734. }
  735. static public function GetInfo()
  736. {
  737. return array(
  738. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  739. 'icon' => 'images/dashlet-bar-chart.png',
  740. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  741. );
  742. }
  743. }
  744. class DashletGroupByTable extends DashletGroupBy
  745. {
  746. public function __construct($oModelReflection, $sId)
  747. {
  748. parent::__construct($oModelReflection, $sId);
  749. $this->aProperties['style'] = 'table';
  750. }
  751. static public function GetInfo()
  752. {
  753. return array(
  754. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  755. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  756. 'icon' => 'images/dashlet-groupby-table.png',
  757. );
  758. }
  759. }
  760. class DashletHeaderStatic extends Dashlet
  761. {
  762. public function __construct($oModelReflection, $sId)
  763. {
  764. parent::__construct($oModelReflection, $sId);
  765. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  766. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  767. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  768. }
  769. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  770. {
  771. $sTitle = $this->aProperties['title'];
  772. $sIcon = $this->aProperties['icon'];
  773. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  774. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  775. $oPage->add('<div class="dashlet-content">');
  776. $oPage->add('<div class="main_header">');
  777. $oPage->add('<img src="'.$sIconPath.'">');
  778. $oPage->add('<h1>'.$this->oModelReflection->DictString($sTitle).'</h1>');
  779. $oPage->add('</div>');
  780. $oPage->add('</div>');
  781. }
  782. public function GetPropertiesFields(DesignerForm $oForm)
  783. {
  784. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  785. $oForm->AddField($oField);
  786. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  787. $oForm->AddField($oField);
  788. }
  789. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  790. {
  791. if ($sProperty == 'icon')
  792. {
  793. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  794. return $oIconField->ValueFromDOMNode($oDOMNode);
  795. }
  796. else
  797. {
  798. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  799. }
  800. }
  801. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  802. {
  803. if ($sProperty == 'icon')
  804. {
  805. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  806. $oIconField->ValueToDOMNode($oDOMNode, $value);
  807. }
  808. else
  809. {
  810. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  811. }
  812. }
  813. static public function GetInfo()
  814. {
  815. return array(
  816. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  817. 'icon' => 'images/dashlet-header.png',
  818. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  819. );
  820. }
  821. }
  822. class DashletHeaderDynamic extends Dashlet
  823. {
  824. public function __construct($oModelReflection, $sId)
  825. {
  826. parent::__construct($oModelReflection, $sId);
  827. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  828. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  829. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  830. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  831. $this->aProperties['query'] = 'SELECT Contact';
  832. $this->aProperties['group_by'] = 'status';
  833. $this->aProperties['values'] = array('active', 'inactive');
  834. }
  835. protected function GetValues()
  836. {
  837. $sQuery = $this->aProperties['query'];
  838. $sGroupBy = $this->aProperties['group_by'];
  839. $aValues = $this->aProperties['values'];
  840. if (empty($aValues))
  841. {
  842. $aValues = array();
  843. }
  844. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  845. $sClass = $oQuery->GetClass();
  846. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  847. {
  848. if (count($aValues) == 0)
  849. {
  850. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  851. if (is_array($aAllowed))
  852. {
  853. $aValues = array_keys($aAllowed);
  854. }
  855. }
  856. }
  857. return $aValues;
  858. }
  859. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  860. {
  861. $sTitle = $this->aProperties['title'];
  862. $sIcon = $this->aProperties['icon'];
  863. $sSubtitle = $this->aProperties['subtitle'];
  864. $sQuery = $this->aProperties['query'];
  865. $sGroupBy = $this->aProperties['group_by'];
  866. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  867. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  868. $aValues = $this->GetValues();
  869. if (count($aValues) > 0)
  870. {
  871. // Stats grouped by <group_by>
  872. $sCSV = implode(',', $aValues);
  873. $aExtraParams = array(
  874. 'title[block]' => $sTitle,
  875. 'label[block]' => $sSubtitle,
  876. 'status[block]' => $sGroupBy,
  877. 'status_codes[block]' => $sCSV,
  878. 'context_filter' => 1,
  879. );
  880. }
  881. else
  882. {
  883. // Simple stats
  884. $aExtraParams = array(
  885. 'title[block]' => $sTitle,
  886. 'label[block]' => $sSubtitle,
  887. 'context_filter' => 1,
  888. );
  889. }
  890. $oPage->add('<div class="dashlet-content">');
  891. $oPage->add('<div class="main_header">');
  892. $oPage->add('<img src="'.$sIconPath.'">');
  893. $oFilter = DBObjectSearch::FromOQL($sQuery);
  894. $oBlock = new DisplayBlock($oFilter, 'summary');
  895. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  896. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  897. $oPage->add('</div>');
  898. $oPage->add('</div>');
  899. }
  900. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  901. {
  902. $sTitle = $this->aProperties['title'];
  903. $sIcon = $this->aProperties['icon'];
  904. $sSubtitle = $this->aProperties['subtitle'];
  905. $sQuery = $this->aProperties['query'];
  906. $sGroupBy = $this->aProperties['group_by'];
  907. $aValues = $this->aProperties['values'];
  908. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  909. $sClass = $oQuery->GetClass();
  910. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  911. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  912. $oPage->add('<div class="dashlet-content">');
  913. $oPage->add('<div class="main_header">');
  914. $oPage->add('<img src="'.$sIconPath.'">');
  915. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  916. $iTotal = 0;
  917. $aValues = $this->GetValues();
  918. if (count($aValues) > 0)
  919. {
  920. // Stats grouped by <group_by>
  921. }
  922. else
  923. {
  924. // Simple stats
  925. }
  926. $oPage->add('<div class="display_block" id="'.$sBlockId.'">');
  927. $oPage->add('<div class="summary-details">');
  928. $oPage->add('<table><tbody>');
  929. $oPage->add('<tr>');
  930. foreach ($aValues as $sValue)
  931. {
  932. $sValueLabel = $this->oModelReflection->GetValueLabel($sClass, $sGroupBy, $sValue);
  933. $oPage->add(' <th>'.$sValueLabel.'</th>');
  934. }
  935. $oPage->add('</tr>');
  936. $oPage->add('<tr>');
  937. foreach ($aValues as $sValue)
  938. {
  939. $iCount = (int) rand(2, 100);
  940. $iTotal += $iCount;
  941. $oPage->add(' <td>'.$iCount.'</td>');
  942. }
  943. $oPage->add('</tr>');
  944. $oPage->add('</tbody></table>');
  945. $oPage->add('</div>');
  946. $sTitle = $this->oModelReflection->DictString($sTitle);
  947. $sSubtitle = $this->oModelReflection->DictFormat($sSubtitle, $iTotal);
  948. // $sSubtitle = "original: $sSubtitle, S:".$this->oModelReflection->DictString($sSubtitle).", Format: '".$this->oModelReflection->DictFormat($sSubtitle, $iTotal)."'";
  949. $oPage->add('<h1>'.$sTitle.'</h1>');
  950. $oPage->add('<a href="" class="summary">'.$sSubtitle.'</a>');
  951. $oPage->add('</div>');
  952. $oPage->add('</div>');
  953. $oPage->add('</div>');
  954. }
  955. public function GetPropertiesFields(DesignerForm $oForm)
  956. {
  957. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  958. $oForm->AddField($oField);
  959. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  960. $oForm->AddField($oField);
  961. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  962. $oForm->AddField($oField);
  963. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  964. $oField->SetMandatory();
  965. $oForm->AddField($oField);
  966. try
  967. {
  968. // Group by field: build the list of possible values (attribute codes + ...)
  969. $oQuery = $this->oModelReflection->GetQuery($this->aProperties['query']);
  970. $sClass = $oQuery->GetClass();
  971. $aGroupBy = array();
  972. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  973. {
  974. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  975. {
  976. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  977. }
  978. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  979. $aGroupBy[$sAttCode] = $sLabel;
  980. }
  981. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  982. $oField->SetMandatory();
  983. $oField->SetAllowedValues($aGroupBy);
  984. }
  985. catch(Exception $e)
  986. {
  987. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  988. $oField->SetReadOnly();
  989. }
  990. $oForm->AddField($oField);
  991. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  992. $oField->MultipleSelection(true);
  993. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  994. {
  995. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  996. $oField->SetAllowedValues($aValues);
  997. }
  998. else
  999. {
  1000. $oField->SetReadOnly();
  1001. }
  1002. $oForm->AddField($oField);
  1003. }
  1004. public function Update($aValues, $aUpdatedFields)
  1005. {
  1006. if (in_array('query', $aUpdatedFields))
  1007. {
  1008. try
  1009. {
  1010. $sCurrQuery = $aValues['query'];
  1011. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  1012. $sCurrClass = $oCurrSearch->GetClass();
  1013. $sPrevQuery = $this->aProperties['query'];
  1014. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  1015. $sPrevClass = $oPrevSearch->GetClass();
  1016. if ($sCurrClass != $sPrevClass)
  1017. {
  1018. $this->bFormRedrawNeeded = true;
  1019. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  1020. $this->aProperties['group_by'] = '';
  1021. $this->aProperties['values'] = array();
  1022. }
  1023. }
  1024. catch(Exception $e)
  1025. {
  1026. $this->bFormRedrawNeeded = true;
  1027. }
  1028. }
  1029. if (in_array('group_by', $aUpdatedFields))
  1030. {
  1031. $this->bFormRedrawNeeded = true;
  1032. $this->aProperties['values'] = array();
  1033. }
  1034. return parent::Update($aValues, $aUpdatedFields);
  1035. }
  1036. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1037. {
  1038. if ($sProperty == 'icon')
  1039. {
  1040. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1041. return $oIconField->ValueFromDOMNode($oDOMNode);
  1042. }
  1043. else
  1044. {
  1045. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1046. }
  1047. }
  1048. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1049. {
  1050. if ($sProperty == 'icon')
  1051. {
  1052. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1053. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1054. }
  1055. else
  1056. {
  1057. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1058. }
  1059. }
  1060. static public function GetInfo()
  1061. {
  1062. return array(
  1063. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  1064. 'icon' => 'images/dashlet-header-stats.png',
  1065. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  1066. );
  1067. }
  1068. }
  1069. class DashletBadge extends Dashlet
  1070. {
  1071. public function __construct($oModelReflection, $sId)
  1072. {
  1073. parent::__construct($oModelReflection, $sId);
  1074. $this->aProperties['class'] = 'Contact';
  1075. $this->aCSSClasses[] = 'dashlet-inline';
  1076. $this->aCSSClasses[] = 'dashlet-badge';
  1077. }
  1078. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1079. {
  1080. $sClass = $this->aProperties['class'];
  1081. $oPage->add('<div class="dashlet-content">');
  1082. $oFilter = new DBObjectSearch($sClass);
  1083. $oBlock = new DisplayBlock($oFilter, 'actions');
  1084. $aExtraParams = array(
  1085. 'context_filter' => 1,
  1086. );
  1087. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1088. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1089. $oPage->add('</div>');
  1090. }
  1091. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1092. {
  1093. $sClass = $this->aProperties['class'];
  1094. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1095. $sClassLabel = $this->oModelReflection->GetName($sClass);
  1096. $oPage->add('<div class="dashlet-content">');
  1097. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  1098. $oPage->add('<p>');
  1099. $oPage->add(' <a class="actions" href=""><img src="'.$sIconUrl.'" style="vertical-align:middle;float;left;margin-right:10px;border:0;">'.$sClassLabel.': 947</a>');
  1100. $oPage->add('</p>');
  1101. $oPage->add('<p>');
  1102. $oPage->add(' <a href="">'.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'</a>');
  1103. $oPage->add(' <br/>');
  1104. $oPage->add(' <a href="http://localhost/trunk/pages/UI.php?operation=search_form&amp;class=Server&amp;c[menu]=WelcomeMenuPage">Search for Server objects</a>');
  1105. $oPage->add('</p>');
  1106. $oPage->add('</div>');
  1107. $oPage->add('</div>');
  1108. }
  1109. static protected $aClassList = null;
  1110. public function GetPropertiesFields(DesignerForm $oForm)
  1111. {
  1112. if (is_null(self::$aClassList))
  1113. {
  1114. // Cache the ordered list of classes (ordered on the label)
  1115. // (has a significant impact when editing a page with lots of badges)
  1116. //
  1117. $aClasses = array();
  1118. foreach($this->oModelReflection->GetClasses('bizmodel', true /*exclude links*/) as $sClass)
  1119. {
  1120. $aClasses[$sClass] = $this->oModelReflection->GetName($sClass);
  1121. }
  1122. asort($aClasses);
  1123. self::$aClassList = array();
  1124. foreach($aClasses as $sClass => $sLabel)
  1125. {
  1126. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1127. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  1128. if ($sIconUrl == '')
  1129. {
  1130. // The icon does not exist, let's use a transparent one of the same size.
  1131. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  1132. }
  1133. self::$aClassList[] = array('value' => $sClass, 'label' => $sLabel, 'icon' => $sIconUrl);
  1134. }
  1135. }
  1136. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  1137. $oField->SetAllowedValues(self::$aClassList);
  1138. $oForm->AddField($oField);
  1139. }
  1140. static public function GetInfo()
  1141. {
  1142. return array(
  1143. 'label' => Dict::S('UI:DashletBadge:Label'),
  1144. 'icon' => 'images/dashlet-badge.png',
  1145. 'description' => Dict::S('UI:DashletBadge:Description'),
  1146. );
  1147. }
  1148. }
  1149. ?>