dashlet.class.inc.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. <?php
  2. // Copyright (C) 2012-2017 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-2017 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 (($sClass != '') && $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. $oFilter->SetShowObsoleteData(utils::ShowObsoleteData());
  516. $sClass = $oFilter->GetClass();
  517. $sClassAlias = $oFilter->GetClassAlias();
  518. if (!$this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  519. {
  520. $oPage->add('<p>'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'</p>');
  521. }
  522. else
  523. {
  524. switch($sStyle)
  525. {
  526. case 'bars':
  527. $sType = 'chart';
  528. $aExtraParams = array(
  529. 'chart_type' => 'bars',
  530. 'chart_title' => $sTitle,
  531. 'group_by' => $this->sGroupByExpr,
  532. 'group_by_label' => $this->sGroupByLabel,
  533. );
  534. $sHtmlTitle = ''; // done in the itop block
  535. break;
  536. case 'pie':
  537. $sType = 'chart';
  538. $aExtraParams = array(
  539. 'chart_type' => 'pie',
  540. 'chart_title' => $sTitle,
  541. 'group_by' => $this->sGroupByExpr,
  542. 'group_by_label' => $this->sGroupByLabel,
  543. );
  544. $sHtmlTitle = ''; // done in the itop block
  545. break;
  546. case 'table':
  547. default:
  548. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  549. $sType = 'count';
  550. $aExtraParams = array(
  551. 'group_by' => $this->sGroupByExpr,
  552. 'group_by_label' => $this->sGroupByLabel,
  553. );
  554. break;
  555. }
  556. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  557. if ($sHtmlTitle != '')
  558. {
  559. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  560. }
  561. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  562. $oBlock = new DisplayBlock($oFilter, $sType);
  563. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  564. $oPage->add('</div>');
  565. }
  566. }
  567. protected function MakeSimulatedData()
  568. {
  569. $sQuery = $this->aProperties['query'];
  570. $sGroupBy = $this->aProperties['group_by'];
  571. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  572. $sClass = $oQuery->GetClass();
  573. $aDisplayValues = array();
  574. if ($this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  575. {
  576. $aAttributeTypes = $this->oModelReflection->ListAttributes($sClass);
  577. $sAttributeType = $aAttributeTypes[$this->sGroupByAttCode];
  578. if (is_subclass_of($sAttributeType, 'AttributeDateTime') || $sAttributeType == 'AttributeDateTime')
  579. {
  580. // Note: an alternative to this somewhat hardcoded way of doing things would be to implement...
  581. //$oExpr = Expression::FromOQL($this->sGroupByExpr);
  582. //$aTranslationData = array($oQuery->GetClassAlias() => array($this->sGroupByAttCode => new ScalarExpression(date('Y-m-d H:i:s', $iTime))));
  583. //$sRawValue = CMDBSource::QueryToScalar('SELECT '.$oExpr->Translate($aTranslationData)->Render());
  584. //$sValueLabel = $oExpr->MakeValueLabel(oFilter, $sRawValue, $sRawValue);
  585. // Anyhow, this requires :
  586. // - an update to the prototype of MakeValueLabel() so that it takes ModelReflection parameters
  587. // - propose clever date/times samples
  588. $aValues = array();
  589. switch($this->sFunction)
  590. {
  591. case 'hour':
  592. $aValues = array(8, 9, 15, 18);
  593. break;
  594. case 'month':
  595. $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'));
  596. break;
  597. case 'day_of_week':
  598. $aValues = array(Dict::S('DayOfWeek-Monday'), Dict::S('DayOfWeek-Wednesday'), Dict::S('DayOfWeek-Thursday'), Dict::S('DayOfWeek-Friday'));
  599. break;
  600. case 'day_of_month':
  601. $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');
  602. break;
  603. }
  604. foreach ($aValues as $sValue)
  605. {
  606. $aDisplayValues[] = array('label' => $sValue, 'value' => (int)rand(1, 15));
  607. }
  608. }
  609. elseif (is_subclass_of($sAttributeType, 'AttributeEnum') || $sAttributeType == 'AttributeEnum')
  610. {
  611. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $this->sGroupByAttCode);
  612. if ($aAllowed) // null for non enums
  613. {
  614. foreach ($aAllowed as $sValue => $sValueLabel)
  615. {
  616. $iCount = (int) rand(2, 100);
  617. $aDisplayValues[] = array(
  618. 'label' => $sValueLabel,
  619. 'value' => $iCount
  620. );
  621. }
  622. }
  623. }
  624. else
  625. {
  626. $aDisplayValues[] = array('label' => 'a', 'value' => 123);
  627. $aDisplayValues[] = array('label' => 'b', 'value' => 321);
  628. $aDisplayValues[] = array('label' => 'c', 'value' => 456);
  629. }
  630. }
  631. return $aDisplayValues;
  632. }
  633. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  634. {
  635. $oPage->add('<div class="dashlet-content">');
  636. $oPage->add('error!');
  637. $oPage->add('</div>');
  638. }
  639. protected function GetGroupByOptions($sOql)
  640. {
  641. $oQuery = $this->oModelReflection->GetQuery($sOql);
  642. $sClass = $oQuery->GetClass();
  643. $aGroupBy = array();
  644. foreach($this->oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType)
  645. {
  646. if ($sAttType == 'AttributeLinkedSet') continue;
  647. if (is_subclass_of($sAttType, 'AttributeLinkedSet')) continue;
  648. if ($sAttType == 'AttributeFriendlyName') continue;
  649. if (is_subclass_of($sAttType, 'AttributeFriendlyName')) continue;
  650. if ($sAttType == 'AttributeExternalField') continue;
  651. if (is_subclass_of($sAttType, 'AttributeExternalField')) continue;
  652. if ($sAttType == 'AttributeOneWayPassword') continue;
  653. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  654. $aGroupBy[$sAttCode] = $sLabel;
  655. if (is_subclass_of($sAttType, 'AttributeDateTime') || $sAttType == 'AttributeDateTime')
  656. {
  657. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  658. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  659. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  660. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  661. }
  662. }
  663. asort($aGroupBy);
  664. return $aGroupBy;
  665. }
  666. public function GetPropertiesFields(DesignerForm $oForm)
  667. {
  668. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  669. $oForm->AddField($oField);
  670. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  671. $oField->SetMandatory();
  672. $oForm->AddField($oField);
  673. try
  674. {
  675. // Group by field: build the list of possible values (attribute codes + ...)
  676. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  677. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  678. $oField->SetMandatory();
  679. $oField->SetAllowedValues($aGroupBy);
  680. }
  681. catch(Exception $e)
  682. {
  683. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  684. $oField->SetReadOnly();
  685. }
  686. $oForm->AddField($oField);
  687. $aStyles = array(
  688. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  689. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  690. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  691. );
  692. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  693. $oField->SetMandatory();
  694. $oField->SetAllowedValues($aStyles);
  695. $oForm->AddField($oField);
  696. }
  697. public function Update($aValues, $aUpdatedFields)
  698. {
  699. if (in_array('query', $aUpdatedFields))
  700. {
  701. try
  702. {
  703. $sCurrQuery = $aValues['query'];
  704. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  705. $sCurrClass = $oCurrSearch->GetClass();
  706. $sPrevQuery = $this->aProperties['query'];
  707. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  708. $sPrevClass = $oPrevSearch->GetClass();
  709. if ($sCurrClass != $sPrevClass)
  710. {
  711. $this->bFormRedrawNeeded = true;
  712. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  713. $this->aProperties['group_by'] = '';
  714. }
  715. }
  716. catch(Exception $e)
  717. {
  718. $this->bFormRedrawNeeded = true;
  719. }
  720. }
  721. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  722. if (in_array('style', $aUpdatedFields))
  723. {
  724. switch($aValues['style'])
  725. {
  726. // Style changed, mutate to the specified type of chart
  727. case 'pie':
  728. $oDashlet = new DashletGroupByPie($this->oModelReflection, $this->sId);
  729. break;
  730. case 'bars':
  731. $oDashlet = new DashletGroupByBars($this->oModelReflection, $this->sId);
  732. break;
  733. case 'table':
  734. $oDashlet = new DashletGroupByTable($this->oModelReflection, $this->sId);
  735. break;
  736. }
  737. $oDashlet->FromParams($aValues);
  738. $oDashlet->bRedrawNeeded = true;
  739. $oDashlet->bFormRedrawNeeded = true;
  740. }
  741. return $oDashlet;
  742. }
  743. static public function GetInfo()
  744. {
  745. // Note: no need to translate, should never be visible to the end-user!
  746. return array(
  747. 'label' => 'Objects grouped by...',
  748. 'icon' => 'images/dashlet-object-grouped.png',
  749. 'description' => 'Grouped objects dashlet (abstract)',
  750. );
  751. }
  752. static public function CanCreateFromOQL()
  753. {
  754. return true;
  755. }
  756. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  757. {
  758. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  759. $oForm->AddField($oField);
  760. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  761. $oField->SetMandatory();
  762. $oForm->AddField($oField);
  763. if (!is_null($sOQL))
  764. {
  765. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  766. $aGroupBy = $this->GetGroupByOptions($sOQL);
  767. $oField->SetAllowedValues($aGroupBy);
  768. }
  769. else
  770. {
  771. // Creating a form for reading parameters!
  772. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  773. }
  774. $oField->SetMandatory();
  775. $oForm->AddField($oField);
  776. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  777. $oField->SetMandatory();
  778. $oForm->AddField($oField);
  779. }
  780. }
  781. class DashletGroupByPie extends DashletGroupBy
  782. {
  783. public function __construct($oModelReflection, $sId)
  784. {
  785. parent::__construct($oModelReflection, $sId);
  786. $this->aProperties['style'] = 'pie';
  787. }
  788. static public function GetInfo()
  789. {
  790. return array(
  791. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  792. 'icon' => 'images/dashlet-pie-chart.png',
  793. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  794. );
  795. }
  796. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  797. {
  798. $sTitle = $this->aProperties['title'];
  799. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  800. $HTMLsTitle = ($sTitle != '') ? '<h1 style="text-align:center">'.htmlentities($sTitle, ENT_QUOTES, 'UTF-8').'</h1>' : '';
  801. $oPage->add("<div style=\"background-color:#fff;padding:0.25em;\">$HTMLsTitle<div id=\"$sBlockId\" style=\"background-color:#fff;\"></div></div>");
  802. $aDisplayValues = $this->MakeSimulatedData();
  803. $aColumns = array();
  804. $aNames = array();
  805. foreach($aDisplayValues as $idx => $aValue)
  806. {
  807. $aColumns[] = array('series_'.$idx, (int)$aValue['value']);
  808. $aNames['series_'.$idx] = $aValue['label'];
  809. }
  810. $sJSColumns = json_encode($aColumns);
  811. $sJSNames = json_encode($aNames);
  812. $oPage->add_ready_script(
  813. <<<EOF
  814. window.setTimeout(function() {
  815. var chart = c3.generate({
  816. bindto: '#{$sBlockId}',
  817. data: {
  818. columns: $sJSColumns,
  819. type: 'pie',
  820. names: $sJSNames,
  821. },
  822. legend: {
  823. show: true,
  824. position: 'right',
  825. },
  826. tooltip: {
  827. format: {
  828. value: function (value, ratio, id) { return value; }
  829. }
  830. }
  831. });}, 100);
  832. EOF
  833. );
  834. }
  835. }
  836. class DashletGroupByBars extends DashletGroupBy
  837. {
  838. public function __construct($oModelReflection, $sId)
  839. {
  840. parent::__construct($oModelReflection, $sId);
  841. $this->aProperties['style'] = 'bars';
  842. }
  843. static public function GetInfo()
  844. {
  845. return array(
  846. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  847. 'icon' => 'images/dashlet-bar-chart.png',
  848. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  849. );
  850. }
  851. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  852. {
  853. $sTitle = $this->aProperties['title'];
  854. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  855. $HTMLsTitle = ($sTitle != '') ? '<h1 style="text-align:center">'.htmlentities($sTitle, ENT_QUOTES, 'UTF-8').'</h1>' : '';
  856. $oPage->add("<div style=\"background-color:#fff;padding:0.25em;\">$HTMLsTitle<div id=\"$sBlockId\" style=\"background-color:#fff;\"></div></div>");
  857. $aDisplayValues = $this->MakeSimulatedData();
  858. $aNames = array();
  859. foreach($aDisplayValues as $idx => $aValue)
  860. {
  861. $aNames[$idx] = $aValue['label'];
  862. }
  863. $sJSNames = json_encode($aNames);
  864. $sJson = json_encode($aDisplayValues);
  865. $sJSCount = json_encode(Dict::S('UI:GroupBy:Count'));
  866. $oPage->add_ready_script(
  867. <<<EOF
  868. window.setTimeout(function() {
  869. var chart = c3.generate({
  870. bindto: '#{$sBlockId}',
  871. data: {
  872. json: $sJson,
  873. keys: {
  874. x: 'label',
  875. value: ["value"]
  876. },
  877. selection: {
  878. enabled: true
  879. },
  880. type: 'bar'
  881. },
  882. axis: {
  883. x: {
  884. tick: {
  885. culling: {max: 25}, // Maximum 24 labels on x axis (2 years).
  886. centered: true,
  887. rotate: 90,
  888. multiline: false
  889. },
  890. type: 'category' // this needed to load string x value
  891. }
  892. },
  893. grid: {
  894. y: {
  895. show: true
  896. }
  897. },
  898. legend: {
  899. show: false,
  900. },
  901. tooltip: {
  902. grouped: false,
  903. format: {
  904. title: function() { return '' },
  905. name: function (name, ratio, id, index) {
  906. var aNames = $sJSNames;
  907. return aNames[index];
  908. }
  909. }
  910. }
  911. });
  912. }, 100);
  913. EOF
  914. );
  915. }
  916. }
  917. class DashletGroupByTable extends DashletGroupBy
  918. {
  919. public function __construct($oModelReflection, $sId)
  920. {
  921. parent::__construct($oModelReflection, $sId);
  922. $this->aProperties['style'] = 'table';
  923. }
  924. static public function GetInfo()
  925. {
  926. return array(
  927. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  928. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  929. 'icon' => 'images/dashlet-groupby-table.png',
  930. );
  931. }
  932. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  933. {
  934. $sTitle = $this->aProperties['title'];
  935. $aDisplayValues = $this->MakeSimulatedData();
  936. $iTotal = 0;
  937. foreach($aDisplayValues as $iRow => $aDisplayData)
  938. {
  939. $iTotal += $aDisplayData['value'];
  940. }
  941. $oPage->add('<div class="dashlet-content">');
  942. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  943. $oPage->add('<div id="'.$sBlockId.'" class="display_block">');
  944. $oPage->add('<p>'.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'</p>');
  945. $oPage->add('<table class="listResults">');
  946. $oPage->add('<thead>');
  947. $oPage->add('<tr>');
  948. $oPage->add('<th class="header" title="">'.$this->sGroupByLabel.'</th>');
  949. $oPage->add('<th class="header" title="'.Dict::S('UI:GroupBy:Count+').'">'.Dict::S('UI:GroupBy:Count').'</th>');
  950. $oPage->add('</tr>');
  951. $oPage->add('</thead>');
  952. $oPage->add('<tbody>');
  953. foreach($aDisplayValues as $aDisplayData)
  954. {
  955. $oPage->add('<tr class="even">');
  956. $oPage->add('<td class=""><span title="Active">'.$aDisplayData['label'].'</span></td>');
  957. $oPage->add('<td class=""><a>'.$aDisplayData['value'].'</a></td>');
  958. $oPage->add('</tr>');
  959. }
  960. $oPage->add('</tbody>');
  961. $oPage->add('</table>');
  962. $oPage->add('</div>');
  963. $oPage->add('</div>');
  964. }
  965. }
  966. class DashletHeaderStatic extends Dashlet
  967. {
  968. public function __construct($oModelReflection, $sId)
  969. {
  970. parent::__construct($oModelReflection, $sId);
  971. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  972. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  973. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  974. }
  975. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  976. {
  977. $sTitle = $this->aProperties['title'];
  978. $sIcon = $this->aProperties['icon'];
  979. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  980. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  981. $oPage->add('<div class="dashlet-content">');
  982. $oPage->add('<div class="main_header">');
  983. $oPage->add('<img src="'.$sIconPath.'">');
  984. $oPage->add('<h1>'.$this->oModelReflection->DictString($sTitle).'</h1>');
  985. $oPage->add('</div>');
  986. $oPage->add('</div>');
  987. }
  988. public function GetPropertiesFields(DesignerForm $oForm)
  989. {
  990. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  991. $oForm->AddField($oField);
  992. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  993. $oForm->AddField($oField);
  994. }
  995. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  996. {
  997. if ($sProperty == 'icon')
  998. {
  999. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1000. return $oIconField->ValueFromDOMNode($oDOMNode);
  1001. }
  1002. else
  1003. {
  1004. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1005. }
  1006. }
  1007. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1008. {
  1009. if ($sProperty == 'icon')
  1010. {
  1011. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1012. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1013. }
  1014. else
  1015. {
  1016. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1017. }
  1018. }
  1019. static public function GetInfo()
  1020. {
  1021. return array(
  1022. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  1023. 'icon' => 'images/dashlet-header.png',
  1024. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  1025. );
  1026. }
  1027. }
  1028. class DashletHeaderDynamic extends Dashlet
  1029. {
  1030. public function __construct($oModelReflection, $sId)
  1031. {
  1032. parent::__construct($oModelReflection, $sId);
  1033. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  1034. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1035. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1036. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  1037. $this->aProperties['query'] = 'SELECT Contact';
  1038. $this->aProperties['group_by'] = 'status';
  1039. $this->aProperties['values'] = array('active', 'inactive');
  1040. }
  1041. protected function GetValues()
  1042. {
  1043. $sQuery = $this->aProperties['query'];
  1044. $sGroupBy = $this->aProperties['group_by'];
  1045. $aValues = $this->aProperties['values'];
  1046. if (empty($aValues))
  1047. {
  1048. $aValues = array();
  1049. }
  1050. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1051. $sClass = $oQuery->GetClass();
  1052. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  1053. {
  1054. if (count($aValues) == 0)
  1055. {
  1056. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  1057. if (is_array($aAllowed))
  1058. {
  1059. $aValues = array_keys($aAllowed);
  1060. }
  1061. }
  1062. }
  1063. return $aValues;
  1064. }
  1065. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1066. {
  1067. $sTitle = $this->aProperties['title'];
  1068. $sIcon = $this->aProperties['icon'];
  1069. $sSubtitle = $this->aProperties['subtitle'];
  1070. $sQuery = $this->aProperties['query'];
  1071. $sGroupBy = $this->aProperties['group_by'];
  1072. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1073. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1074. $aValues = $this->GetValues();
  1075. if (count($aValues) > 0)
  1076. {
  1077. // Stats grouped by <group_by>
  1078. $sCSV = implode(',', $aValues);
  1079. $aExtraParams = array(
  1080. 'title[block]' => $sTitle,
  1081. 'label[block]' => $sSubtitle,
  1082. 'status[block]' => $sGroupBy,
  1083. 'status_codes[block]' => $sCSV,
  1084. 'context_filter' => 1,
  1085. );
  1086. }
  1087. else
  1088. {
  1089. // Simple stats
  1090. $aExtraParams = array(
  1091. 'title[block]' => $sTitle,
  1092. 'label[block]' => $sSubtitle,
  1093. 'context_filter' => 1,
  1094. );
  1095. }
  1096. $oPage->add('<div class="dashlet-content">');
  1097. $oPage->add('<div class="main_header">');
  1098. $oPage->add('<img src="'.$sIconPath.'">');
  1099. $oFilter = DBObjectSearch::FromOQL($sQuery);
  1100. $oBlock = new DisplayBlock($oFilter, 'summary');
  1101. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1102. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1103. $oPage->add('</div>');
  1104. $oPage->add('</div>');
  1105. }
  1106. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1107. {
  1108. $sTitle = $this->aProperties['title'];
  1109. $sIcon = $this->aProperties['icon'];
  1110. $sSubtitle = $this->aProperties['subtitle'];
  1111. $sQuery = $this->aProperties['query'];
  1112. $sGroupBy = $this->aProperties['group_by'];
  1113. $aValues = $this->aProperties['values'];
  1114. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1115. $sClass = $oQuery->GetClass();
  1116. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1117. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1118. $oPage->add('<div class="dashlet-content">');
  1119. $oPage->add('<div class="main_header">');
  1120. $oPage->add('<img src="'.$sIconPath.'">');
  1121. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1122. $iTotal = 0;
  1123. $aValues = $this->GetValues();
  1124. if (count($aValues) > 0)
  1125. {
  1126. // Stats grouped by <group_by>
  1127. }
  1128. else
  1129. {
  1130. // Simple stats
  1131. }
  1132. $oPage->add('<div class="display_block" id="'.$sBlockId.'">');
  1133. $oPage->add('<div class="summary-details">');
  1134. $oPage->add('<table><tbody>');
  1135. $oPage->add('<tr>');
  1136. foreach ($aValues as $sValue)
  1137. {
  1138. $sValueLabel = $this->oModelReflection->GetValueLabel($sClass, $sGroupBy, $sValue);
  1139. $oPage->add(' <th>'.$sValueLabel.'</th>');
  1140. }
  1141. $oPage->add('</tr>');
  1142. $oPage->add('<tr>');
  1143. foreach ($aValues as $sValue)
  1144. {
  1145. $iCount = (int) rand(2, 100);
  1146. $iTotal += $iCount;
  1147. $oPage->add(' <td>'.$iCount.'</td>');
  1148. }
  1149. $oPage->add('</tr>');
  1150. $oPage->add('</tbody></table>');
  1151. $oPage->add('</div>');
  1152. $sTitle = $this->oModelReflection->DictString($sTitle);
  1153. $sSubtitle = $this->oModelReflection->DictFormat($sSubtitle, $iTotal);
  1154. // $sSubtitle = "original: $sSubtitle, S:".$this->oModelReflection->DictString($sSubtitle).", Format: '".$this->oModelReflection->DictFormat($sSubtitle, $iTotal)."'";
  1155. $oPage->add('<h1>'.$sTitle.'</h1>');
  1156. $oPage->add('<a class="summary">'.$sSubtitle.'</a>');
  1157. $oPage->add('</div>');
  1158. $oPage->add('</div>');
  1159. $oPage->add('</div>');
  1160. }
  1161. public function GetPropertiesFields(DesignerForm $oForm)
  1162. {
  1163. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  1164. $oForm->AddField($oField);
  1165. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  1166. $oForm->AddField($oField);
  1167. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  1168. $oForm->AddField($oField);
  1169. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  1170. $oField->SetMandatory();
  1171. $oForm->AddField($oField);
  1172. try
  1173. {
  1174. // Group by field: build the list of possible values (attribute codes + ...)
  1175. $oQuery = $this->oModelReflection->GetQuery($this->aProperties['query']);
  1176. $sClass = $oQuery->GetClass();
  1177. $aGroupBy = array();
  1178. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  1179. {
  1180. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  1181. {
  1182. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  1183. }
  1184. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  1185. $aGroupBy[$sAttCode] = $sLabel;
  1186. }
  1187. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1188. $oField->SetMandatory();
  1189. $oField->SetAllowedValues($aGroupBy);
  1190. }
  1191. catch(Exception $e)
  1192. {
  1193. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1194. $oField->SetReadOnly();
  1195. }
  1196. $oForm->AddField($oField);
  1197. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  1198. $oField->MultipleSelection(true);
  1199. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  1200. {
  1201. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  1202. $oField->SetAllowedValues($aValues);
  1203. }
  1204. else
  1205. {
  1206. $oField->SetReadOnly();
  1207. }
  1208. $oForm->AddField($oField);
  1209. }
  1210. public function Update($aValues, $aUpdatedFields)
  1211. {
  1212. if (in_array('query', $aUpdatedFields))
  1213. {
  1214. try
  1215. {
  1216. $sCurrQuery = $aValues['query'];
  1217. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  1218. $sCurrClass = $oCurrSearch->GetClass();
  1219. $sPrevQuery = $this->aProperties['query'];
  1220. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  1221. $sPrevClass = $oPrevSearch->GetClass();
  1222. if ($sCurrClass != $sPrevClass)
  1223. {
  1224. $this->bFormRedrawNeeded = true;
  1225. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  1226. $this->aProperties['group_by'] = '';
  1227. $this->aProperties['values'] = array();
  1228. }
  1229. }
  1230. catch(Exception $e)
  1231. {
  1232. $this->bFormRedrawNeeded = true;
  1233. }
  1234. }
  1235. if (in_array('group_by', $aUpdatedFields))
  1236. {
  1237. $this->bFormRedrawNeeded = true;
  1238. $this->aProperties['values'] = array();
  1239. }
  1240. return parent::Update($aValues, $aUpdatedFields);
  1241. }
  1242. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1243. {
  1244. if ($sProperty == 'icon')
  1245. {
  1246. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1247. return $oIconField->ValueFromDOMNode($oDOMNode);
  1248. }
  1249. else
  1250. {
  1251. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1252. }
  1253. }
  1254. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1255. {
  1256. if ($sProperty == 'icon')
  1257. {
  1258. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1259. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1260. }
  1261. else
  1262. {
  1263. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1264. }
  1265. }
  1266. static public function GetInfo()
  1267. {
  1268. return array(
  1269. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  1270. 'icon' => 'images/dashlet-header-stats.png',
  1271. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  1272. );
  1273. }
  1274. }
  1275. class DashletBadge extends Dashlet
  1276. {
  1277. public function __construct($oModelReflection, $sId)
  1278. {
  1279. parent::__construct($oModelReflection, $sId);
  1280. $this->aProperties['class'] = 'Contact';
  1281. $this->aCSSClasses[] = 'dashlet-inline';
  1282. $this->aCSSClasses[] = 'dashlet-badge';
  1283. }
  1284. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1285. {
  1286. $sClass = $this->aProperties['class'];
  1287. $oPage->add('<div class="dashlet-content">');
  1288. $oFilter = new DBObjectSearch($sClass);
  1289. $oBlock = new DisplayBlock($oFilter, 'actions');
  1290. $aExtraParams = array(
  1291. 'context_filter' => 1,
  1292. );
  1293. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1294. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1295. $oPage->add('</div>');
  1296. }
  1297. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1298. {
  1299. $sClass = $this->aProperties['class'];
  1300. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1301. $sClassLabel = $this->oModelReflection->GetName($sClass);
  1302. $oPage->add('<div class="dashlet-content">');
  1303. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  1304. $oPage->add('<p>');
  1305. $oPage->add(' <a class="actions"><img src="'.$sIconUrl.'" style="vertical-align:middle;float;left;margin-right:10px;border:0;">'.$sClassLabel.': 947</a>');
  1306. $oPage->add('</p>');
  1307. $oPage->add('<p>');
  1308. $oPage->add(' <a>'.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'</a>');
  1309. $oPage->add(' <br/>');
  1310. $oPage->add(' <a>'.Dict::Format('UI:SearchFor_Class', $sClassLabel).'</a>');
  1311. $oPage->add('</p>');
  1312. $oPage->add('</div>');
  1313. $oPage->add('</div>');
  1314. }
  1315. static protected $aClassList = null;
  1316. public function GetPropertiesFields(DesignerForm $oForm)
  1317. {
  1318. if (is_null(self::$aClassList))
  1319. {
  1320. // Cache the ordered list of classes (ordered on the label)
  1321. // (has a significant impact when editing a page with lots of badges)
  1322. //
  1323. $aClasses = array();
  1324. foreach($this->oModelReflection->GetClasses('bizmodel', true /*exclude links*/) as $sClass)
  1325. {
  1326. $aClasses[$sClass] = $this->oModelReflection->GetName($sClass);
  1327. }
  1328. asort($aClasses);
  1329. self::$aClassList = array();
  1330. foreach($aClasses as $sClass => $sLabel)
  1331. {
  1332. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1333. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  1334. if ($sIconUrl == '')
  1335. {
  1336. // The icon does not exist, let's use a transparent one of the same size.
  1337. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  1338. }
  1339. self::$aClassList[] = array('value' => $sClass, 'label' => $sLabel, 'icon' => $sIconUrl);
  1340. }
  1341. }
  1342. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  1343. $oField->SetAllowedValues(self::$aClassList);
  1344. $oForm->AddField($oField);
  1345. }
  1346. static public function GetInfo()
  1347. {
  1348. return array(
  1349. 'label' => Dict::S('UI:DashletBadge:Label'),
  1350. 'icon' => 'images/dashlet-badge.png',
  1351. 'description' => Dict::S('UI:DashletBadge:Description'),
  1352. );
  1353. }
  1354. }
  1355. ?>