dashlet.class.inc.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. <?php
  2. // Copyright (C) 2012-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. require_once(APPROOT.'application/forms.class.inc.php');
  19. /**
  20. * Base class for all 'dashlets' (i.e. widgets to be inserted into a dashboard)
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. abstract class Dashlet
  26. {
  27. protected $oModelReflection;
  28. protected $sId;
  29. protected $bRedrawNeeded;
  30. protected $bFormRedrawNeeded;
  31. protected $aProperties; // array of {property => value}
  32. protected $aCSSClasses;
  33. public function __construct(ModelReflection $oModelReflection, $sId)
  34. {
  35. $this->oModelReflection = $oModelReflection;
  36. $this->sId = $sId;
  37. $this->bRedrawNeeded = true; // By default: redraw each time a property changes
  38. $this->bFormRedrawNeeded = false; // By default: no need to redraw the form (independent fields)
  39. $this->aProperties = array(); // By default: there is no property
  40. $this->aCSSClasses = array('dashlet');
  41. }
  42. // Assuming that a property has the type of its default value, set in the constructor
  43. //
  44. public function Str2Prop($sProperty, $sValue)
  45. {
  46. $refValue = $this->aProperties[$sProperty];
  47. $sRefType = gettype($refValue);
  48. if (gettype($sValue) == $sRefType)
  49. {
  50. // Do not change anything in that case!
  51. $ret = $sValue;
  52. }
  53. elseif ($sRefType == 'boolean')
  54. {
  55. $ret = ($sValue == 'true');
  56. }
  57. elseif ($sRefType == 'array')
  58. {
  59. $ret = explode(',', $sValue);
  60. }
  61. else
  62. {
  63. $ret = $sValue;
  64. settype($ret, $sRefType);
  65. }
  66. return $ret;
  67. }
  68. public function Prop2Str($value)
  69. {
  70. $sType = gettype($value);
  71. if ($sType == 'boolean')
  72. {
  73. $sRet = $value ? 'true' : 'false';
  74. }
  75. elseif ($sType == 'array')
  76. {
  77. $sRet = implode(',', $value);
  78. }
  79. else
  80. {
  81. $sRet = (string) $value;
  82. }
  83. return $sRet;
  84. }
  85. protected function OnUpdate()
  86. {
  87. }
  88. public function FromDOMNode($oDOMNode)
  89. {
  90. foreach ($this->aProperties as $sProperty => $value)
  91. {
  92. $oPropNode = $oDOMNode->getElementsByTagName($sProperty)->item(0);
  93. if ($oPropNode != null)
  94. {
  95. $this->aProperties[$sProperty] = $this->PropertyFromDOMNode($oPropNode, $sProperty);
  96. }
  97. }
  98. $this->OnUpdate();
  99. }
  100. public function ToDOMNode($oDOMNode)
  101. {
  102. foreach ($this->aProperties as $sProperty => $value)
  103. {
  104. $oPropNode = $oDOMNode->ownerDocument->createElement($sProperty);
  105. $oDOMNode->appendChild($oPropNode);
  106. $this->PropertyToDOMNode($oPropNode, $sProperty, $value);
  107. }
  108. }
  109. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  110. {
  111. $res = $this->Str2Prop($sProperty, $oDOMNode->textContent);
  112. return $res;
  113. }
  114. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  115. {
  116. $sXmlValue = $this->Prop2Str($value);
  117. $oTextNode = $oDOMNode->ownerDocument->createTextNode($sXmlValue);
  118. $oDOMNode->appendChild($oTextNode);
  119. }
  120. public function FromXml($sXml)
  121. {
  122. $oDomDoc = new DOMDocument('1.0', 'UTF-8');
  123. $oDomDoc->loadXml($sXml);
  124. $this->FromDOMNode($oDomDoc->firstChild);
  125. }
  126. public function FromParams($aParams)
  127. {
  128. foreach ($this->aProperties as $sProperty => $value)
  129. {
  130. if (array_key_exists($sProperty, $aParams))
  131. {
  132. $this->aProperties[$sProperty] = $aParams[$sProperty];
  133. }
  134. }
  135. $this->OnUpdate();
  136. }
  137. public function DoRender($oPage, $bEditMode = false, $bEnclosingDiv = true, $aExtraParams = array())
  138. {
  139. $sCSSClasses = implode(' ', $this->aCSSClasses);
  140. $sId = $this->GetID();
  141. if ($bEnclosingDiv)
  142. {
  143. if ($bEditMode)
  144. {
  145. $oPage->add('<div class="'.$sCSSClasses.'" id="dashlet_'.$sId.'">');
  146. }
  147. else
  148. {
  149. $oPage->add('<div class="'.$sCSSClasses.'">');
  150. }
  151. }
  152. else
  153. {
  154. foreach ($this->aCSSClasses as $sCSSClass)
  155. {
  156. $oPage->add_ready_script("$('#dashlet_".$sId."').addClass('$sCSSClass');");
  157. }
  158. }
  159. try
  160. {
  161. if (get_class($this->oModelReflection) == 'ModelReflectionRuntime')
  162. {
  163. $this->Render($oPage, $bEditMode, $aExtraParams);
  164. }
  165. else
  166. {
  167. $this->RenderNoData($oPage, $bEditMode, $aExtraParams);
  168. }
  169. }
  170. catch(UnknownClassOqlException $e)
  171. {
  172. // Maybe the class is part of a non-installed module, fail silently
  173. // Except in Edit mode
  174. if ($bEditMode)
  175. {
  176. $oPage->add('<div class="dashlet-content">');
  177. $oPage->add('<h2>'.$e->GetUserFriendlyDescription().'</h2>');
  178. $oPage->add('</div>');
  179. }
  180. }
  181. catch(OqlException $e)
  182. {
  183. $oPage->add('<div class="dashlet-content">');
  184. $oPage->p($e->GetUserFriendlyDescription());
  185. $oPage->add('</div>');
  186. }
  187. catch(Exception $e)
  188. {
  189. $oPage->add('<div class="dashlet-content">');
  190. $oPage->p($e->getMessage());
  191. $oPage->add('</div>');
  192. }
  193. if ($bEnclosingDiv)
  194. {
  195. $oPage->add('</div>');
  196. }
  197. if ($bEditMode)
  198. {
  199. $sClass = get_class($this);
  200. $oPage->add_ready_script(
  201. <<<EOF
  202. $('#dashlet_$sId').dashlet({dashlet_id: '$sId', dashlet_class: '$sClass'});
  203. EOF
  204. );
  205. }
  206. }
  207. public function SetID($sId)
  208. {
  209. $this->sId = $sId;
  210. }
  211. public function GetID()
  212. {
  213. return $this->sId;
  214. }
  215. abstract public function Render($oPage, $bEditMode = false, $aExtraParams = array());
  216. /* Rendering without the real data */
  217. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  218. {
  219. $this->Render($oPage, $bEditMode, $aExtraParams);
  220. }
  221. abstract public function GetPropertiesFields(DesignerForm $oForm);
  222. public function ToXml(DOMNode $oContainerNode)
  223. {
  224. }
  225. public function Update($aValues, $aUpdatedFields)
  226. {
  227. foreach($aUpdatedFields as $sProp)
  228. {
  229. if (array_key_exists($sProp, $this->aProperties))
  230. {
  231. $this->aProperties[$sProp] = $this->Str2Prop($sProp, $aValues[$sProp]);
  232. }
  233. }
  234. $this->OnUpdate();
  235. return $this;
  236. }
  237. public function IsRedrawNeeded()
  238. {
  239. return $this->bRedrawNeeded;
  240. }
  241. public function IsFormRedrawNeeded()
  242. {
  243. return $this->bFormRedrawNeeded;
  244. }
  245. static public function GetInfo()
  246. {
  247. return array(
  248. 'label' => '',
  249. 'icon' => '',
  250. 'description' => '',
  251. );
  252. }
  253. public function GetForm()
  254. {
  255. $oForm = new DesignerForm();
  256. $oForm->SetPrefix("dashlet_". $this->GetID());
  257. $oForm->SetParamsContainer('params');
  258. $this->GetPropertiesFields($oForm);
  259. $oDashletClassField = new DesignerHiddenField('dashlet_class', '', get_class($this));
  260. $oForm->AddField($oDashletClassField);
  261. $oDashletIdField = new DesignerHiddenField('dashlet_id', '', $this->GetID());
  262. $oForm->AddField($oDashletIdField);
  263. return $oForm;
  264. }
  265. static public function IsVisible()
  266. {
  267. return true;
  268. }
  269. static public function CanCreateFromOQL()
  270. {
  271. return false;
  272. }
  273. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  274. {
  275. // Default: do nothing since it's not supported
  276. }
  277. }
  278. class DashletEmptyCell extends Dashlet
  279. {
  280. public function __construct($oModelReflection, $sId)
  281. {
  282. parent::__construct($oModelReflection, $sId);
  283. }
  284. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  285. {
  286. $oPage->add('&nbsp;');
  287. }
  288. public function GetPropertiesFields(DesignerForm $oForm)
  289. {
  290. }
  291. static public function GetInfo()
  292. {
  293. return array(
  294. 'label' => 'Empty Cell',
  295. 'icon' => 'images/dashlet-text.png',
  296. 'description' => 'Empty Cell Dashlet Placeholder',
  297. );
  298. }
  299. static public function IsVisible()
  300. {
  301. return false;
  302. }
  303. }
  304. class DashletPlainText extends Dashlet
  305. {
  306. public function __construct($oModelReflection, $sId)
  307. {
  308. parent::__construct($oModelReflection, $sId);
  309. $this->aProperties['text'] = Dict::S('UI:DashletPlainText:Prop-Text:Default');
  310. }
  311. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  312. {
  313. $sText = htmlentities($this->aProperties['text'], ENT_QUOTES, 'UTF-8');
  314. $sText = str_replace(array("\r\n", "\n", "\r"), "<br/>", $sText);
  315. $sId = 'plaintext_'.($bEditMode? 'edit_' : '').$this->sId;
  316. $oPage->add('<div id="'.$sId.'" class="dashlet-content">'.$sText.'</div>');
  317. }
  318. public function GetPropertiesFields(DesignerForm $oForm)
  319. {
  320. $oField = new DesignerLongTextField('text', Dict::S('UI:DashletPlainText:Prop-Text'), $this->aProperties['text']);
  321. $oField->SetMandatory();
  322. $oForm->AddField($oField);
  323. }
  324. static public function GetInfo()
  325. {
  326. return array(
  327. 'label' => Dict::S('UI:DashletPlainText:Label'),
  328. 'icon' => 'images/dashlet-text.png',
  329. 'description' => Dict::S('UI:DashletPlainText:Description'),
  330. );
  331. }
  332. }
  333. class DashletObjectList extends Dashlet
  334. {
  335. public function __construct($oModelReflection, $sId)
  336. {
  337. parent::__construct($oModelReflection, $sId);
  338. $this->aProperties['title'] = '';
  339. $this->aProperties['query'] = 'SELECT Contact';
  340. $this->aProperties['menu'] = false;
  341. }
  342. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  343. {
  344. $sTitle = $this->aProperties['title'];
  345. $sQuery = $this->aProperties['query'];
  346. $sShowMenu = $this->aProperties['menu'] ? '1' : '0';
  347. $oPage->add('<div class="dashlet-content">');
  348. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  349. if ($sHtmlTitle != '')
  350. {
  351. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  352. }
  353. $oFilter = DBObjectSearch::FromOQL($sQuery);
  354. $oBlock = new DisplayBlock($oFilter, 'list');
  355. $aExtraParams = array(
  356. 'menu' => $sShowMenu,
  357. 'table_id' => 'Dashlet'.$this->sId,
  358. );
  359. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  360. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  361. $oPage->add('</div>');
  362. }
  363. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  364. {
  365. $sTitle = $this->aProperties['title'];
  366. $sQuery = $this->aProperties['query'];
  367. $bShowMenu = $this->aProperties['menu'];
  368. $oPage->add('<div class="dashlet-content">');
  369. $sHtmlTitle = htmlentities($this->oModelReflection->DictString($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  370. if ($sHtmlTitle != '')
  371. {
  372. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  373. }
  374. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  375. $sClass = $oQuery->GetClass();
  376. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  377. $oPage->p(Dict::S('UI:NoObjectToDisplay'));
  378. if ($bShowMenu)
  379. {
  380. $oPage->p('<a>'.Dict::Format('UI:ClickToCreateNew', $this->oModelReflection->GetName($sClass)).'</a>');
  381. }
  382. $oPage->add('</div>');
  383. $oPage->add('</div>');
  384. }
  385. public function GetPropertiesFields(DesignerForm $oForm)
  386. {
  387. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), $this->aProperties['title']);
  388. $oForm->AddField($oField);
  389. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $this->aProperties['query']);
  390. $oField->SetMandatory();
  391. $oForm->AddField($oField);
  392. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  393. $oForm->AddField($oField);
  394. }
  395. static public function GetInfo()
  396. {
  397. return array(
  398. 'label' => Dict::S('UI:DashletObjectList:Label'),
  399. 'icon' => 'images/dashlet-list.png',
  400. 'description' => Dict::S('UI:DashletObjectList:Description'),
  401. );
  402. }
  403. static public function CanCreateFromOQL()
  404. {
  405. return true;
  406. }
  407. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  408. {
  409. $oField = new DesignerTextField('title', Dict::S('UI:DashletObjectList:Prop-Title'), '');
  410. $oForm->AddField($oField);
  411. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletObjectList:Prop-Query'), $sOQL);
  412. $oField->SetMandatory();
  413. $oForm->AddField($oField);
  414. $oField = new DesignerBooleanField('menu', Dict::S('UI:DashletObjectList:Prop-Menu'), $this->aProperties['menu']);
  415. $oForm->AddField($oField);
  416. }
  417. }
  418. abstract class DashletGroupBy extends Dashlet
  419. {
  420. public function __construct($oModelReflection, $sId)
  421. {
  422. parent::__construct($oModelReflection, $sId);
  423. $this->aProperties['title'] = '';
  424. $this->aProperties['query'] = 'SELECT Contact';
  425. $this->aProperties['group_by'] = 'status';
  426. $this->aProperties['style'] = 'table';
  427. }
  428. protected $sGroupByLabel = null;
  429. protected $sGroupByExpr = null;
  430. protected $sGroupByAttCode = null;
  431. protected $sFunction = null;
  432. /**
  433. * Compute Grouping
  434. */
  435. public function OnUpdate()
  436. {
  437. $this->sGroupByExpr = null;
  438. $this->sGroupByLabel = null;
  439. $this->sGroupByAttCode = null;
  440. $this->sFunction = null;
  441. $sQuery = $this->aProperties['query'];
  442. $sGroupBy = $this->aProperties['group_by'];
  443. $sStyle = $this->aProperties['style'];
  444. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  445. try
  446. {
  447. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  448. $sClass = $oQuery->GetClass();
  449. $sClassAlias = $oQuery->GetClassAlias();
  450. }
  451. catch(Exception $e)
  452. {
  453. // Invalid query, let the user edit the dashlet/dashboard anyhow
  454. $sClass = '';
  455. $sClassAlias = '';
  456. }
  457. // Check groupby... it can be wrong at this stage
  458. if (preg_match('/^(.*):(.*)$/', $sGroupBy, $aMatches))
  459. {
  460. $this->sGroupByAttCode = $aMatches[1];
  461. $this->sFunction = $aMatches[2];
  462. }
  463. else
  464. {
  465. $this->sGroupByAttCode = $sGroupBy;
  466. $this->sFunction = null;
  467. }
  468. if ($this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  469. {
  470. $sAttLabel = $this->oModelReflection->GetLabel($sClass, $this->sGroupByAttCode);
  471. if (!is_null($this->sFunction))
  472. {
  473. switch($this->sFunction)
  474. {
  475. case 'hour':
  476. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Hour', $sAttLabel);
  477. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%H')"; // 0 -> 23
  478. break;
  479. case 'month':
  480. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Month', $sAttLabel);
  481. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%Y-%m')"; // yyyy-mm
  482. break;
  483. case 'day_of_week':
  484. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfWeek', $sAttLabel);
  485. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%w')";
  486. break;
  487. case 'day_of_month':
  488. $this->sGroupByLabel = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:DayOfMonth', $sAttLabel);
  489. $this->sGroupByExpr = "DATE_FORMAT($sClassAlias.{$this->sGroupByAttCode}, '%Y-%m-%d')"; // mm-dd
  490. break;
  491. default:
  492. $this->sGroupByLabel = 'Unknown group by function '.$this->sFunction;
  493. $this->sGroupByExpr = $sClassAlias.'.'.$this->sGroupByAttCode;
  494. }
  495. }
  496. else
  497. {
  498. $this->sGroupByExpr = $sClassAlias.'.'.$this->sGroupByAttCode;
  499. $this->sGroupByLabel = $sAttLabel;
  500. }
  501. }
  502. else
  503. {
  504. $this->sGroupByAttCode = null;
  505. }
  506. }
  507. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  508. {
  509. $sTitle = $this->aProperties['title'];
  510. $sQuery = $this->aProperties['query'];
  511. $sGroupBy = $this->aProperties['group_by'];
  512. $sStyle = $this->aProperties['style'];
  513. // First perform the query - if the OQL is not ok, it will generate an exception : no need to go further
  514. $oFilter = DBObjectSearch::FromOQL($sQuery);
  515. $sClass = $oFilter->GetClass();
  516. $sClassAlias = $oFilter->GetClassAlias();
  517. if (!$this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  518. {
  519. $oPage->add('<p>'.Dict::S('UI:DashletGroupBy:MissingGroupBy').'</p>');
  520. }
  521. else
  522. {
  523. switch($sStyle)
  524. {
  525. case 'bars':
  526. $sType = 'open_flash_chart';
  527. $aExtraParams = array(
  528. 'chart_type' => 'bars',
  529. 'chart_title' => $sTitle,
  530. 'group_by' => $this->sGroupByExpr,
  531. 'group_by_label' => $this->sGroupByLabel,
  532. );
  533. $sHtmlTitle = ''; // done in the itop block
  534. break;
  535. case 'pie':
  536. $sType = 'open_flash_chart';
  537. $aExtraParams = array(
  538. 'chart_type' => 'pie',
  539. 'chart_title' => $sTitle,
  540. 'group_by' => $this->sGroupByExpr,
  541. 'group_by_label' => $this->sGroupByLabel,
  542. );
  543. $sHtmlTitle = ''; // done in the itop block
  544. break;
  545. case 'table':
  546. default:
  547. $sHtmlTitle = htmlentities(Dict::S($sTitle), ENT_QUOTES, 'UTF-8'); // done in the itop block
  548. $sType = 'count';
  549. $aExtraParams = array(
  550. 'group_by' => $this->sGroupByExpr,
  551. 'group_by_label' => $this->sGroupByLabel,
  552. );
  553. break;
  554. }
  555. $oPage->add('<div style="text-align:center" class="dashlet-content">');
  556. if ($sHtmlTitle != '')
  557. {
  558. $oPage->add('<h1>'.$sHtmlTitle.'</h1>');
  559. }
  560. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  561. $oBlock = new DisplayBlock($oFilter, $sType);
  562. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  563. $oPage->add('</div>');
  564. }
  565. }
  566. protected function MakeSimulatedData()
  567. {
  568. $sQuery = $this->aProperties['query'];
  569. $sGroupBy = $this->aProperties['group_by'];
  570. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  571. $sClass = $oQuery->GetClass();
  572. $aDisplayValues = array();
  573. if ($this->oModelReflection->IsValidAttCode($sClass, $this->sGroupByAttCode))
  574. {
  575. $aAttributeTypes = $this->oModelReflection->ListAttributes($sClass);
  576. $sAttributeType = $aAttributeTypes[$this->sGroupByAttCode];
  577. if (is_subclass_of($sAttributeType, 'AttributeDateTime') || $sAttributeType == 'AttributeDateTime')
  578. {
  579. // Note: an alternative to this somewhat hardcoded way of doing things would be to implement...
  580. //$oExpr = Expression::FromOQL($this->sGroupByExpr);
  581. //$aTranslationData = array($oQuery->GetClassAlias() => array($this->sGroupByAttCode => new ScalarExpression(date('Y-m-d H:i:s', $iTime))));
  582. //$sRawValue = CMDBSource::QueryToScalar('SELECT '.$oExpr->Translate($aTranslationData)->Render());
  583. //$sValueLabel = $oExpr->MakeValueLabel(oFilter, $sRawValue, $sRawValue);
  584. // Anyhow, this requires :
  585. // - an update to the prototype of MakeValueLabel() so that it takes ModelReflection parameters
  586. // - propose clever date/times samples
  587. $aValues = array();
  588. switch($this->sFunction)
  589. {
  590. case 'hour':
  591. $aValues = array(8, 9, 15, 18);
  592. break;
  593. case 'month':
  594. $aValues = array('2013 '.Dict::S('Month-11'), '2013 '.Dict::S('Month-12'), '2014 '.Dict::S('Month-01'), '2014 '.Dict::S('Month-02'), '2014 '.Dict::S('Month-03'));
  595. break;
  596. case 'day_of_week':
  597. $aValues = array(Dict::S('DayOfWeek-Monday'), Dict::S('DayOfWeek-Wednesday'), Dict::S('DayOfWeek-Thursday'), Dict::S('DayOfWeek-Friday'));
  598. break;
  599. case 'day_of_month':
  600. $aValues = array(Dict::S('Month-03'). ' 30', Dict::S('Month-03'). ' 31', Dict::S('Month-04'). ' 01', Dict::S('Month-04'). ' 02', Dict::S('Month-04'). ' 03');
  601. break;
  602. }
  603. foreach ($aValues as $sValue)
  604. {
  605. $aDisplayValues[] = array('label' => $sValue, 'count' => (int)rand(1, 15));
  606. }
  607. }
  608. elseif (is_subclass_of($sAttributeType, 'AttributeEnum') || $sAttributeType == 'AttributeEnum')
  609. {
  610. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $this->sGroupByAttCode);
  611. if ($aAllowed) // null for non enums
  612. {
  613. foreach ($aAllowed as $sValue => $sValueLabel)
  614. {
  615. $iCount = (int) rand(2, 100);
  616. $aDisplayValues[] = array(
  617. 'label' => $sValueLabel,
  618. 'count' => $iCount
  619. );
  620. }
  621. }
  622. }
  623. else
  624. {
  625. $aDisplayValues[] = array('label' => 'a', 'count' => 123);
  626. $aDisplayValues[] = array('label' => 'b', 'count' => 321);
  627. $aDisplayValues[] = array('label' => 'c', 'count' => 456);
  628. }
  629. }
  630. return $aDisplayValues;
  631. }
  632. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  633. {
  634. $oPage->add('<div class="dashlet-content">');
  635. $oPage->add('error!');
  636. $oPage->add('</div>');
  637. }
  638. protected function GetGroupByOptions($sOql)
  639. {
  640. $oQuery = $this->oModelReflection->GetQuery($sOql);
  641. $sClass = $oQuery->GetClass();
  642. $aGroupBy = array();
  643. foreach($this->oModelReflection->ListAttributes($sClass) as $sAttCode => $sAttType)
  644. {
  645. if ($sAttType == 'AttributeLinkedSet') continue;
  646. if (is_subclass_of($sAttType, 'AttributeLinkedSet')) continue;
  647. if ($sAttType == 'AttributeFriendlyName') continue;
  648. if (is_subclass_of($sAttType, 'AttributeFriendlyName')) continue;
  649. if ($sAttType == 'AttributeExternalField') continue;
  650. if (is_subclass_of($sAttType, 'AttributeExternalField')) continue;
  651. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  652. $aGroupBy[$sAttCode] = $sLabel;
  653. if (is_subclass_of($sAttType, 'AttributeDateTime') || $sAttType == 'AttributeDateTime')
  654. {
  655. $aGroupBy[$sAttCode.':hour'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Hour', $sLabel);
  656. $aGroupBy[$sAttCode.':month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-Month', $sLabel);
  657. $aGroupBy[$sAttCode.':day_of_week'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfWeek', $sLabel);
  658. $aGroupBy[$sAttCode.':day_of_month'] = Dict::Format('UI:DashletGroupBy:Prop-GroupBy:Select-DayOfMonth', $sLabel);
  659. }
  660. }
  661. asort($aGroupBy);
  662. return $aGroupBy;
  663. }
  664. public function GetPropertiesFields(DesignerForm $oForm)
  665. {
  666. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), $this->aProperties['title']);
  667. $oForm->AddField($oField);
  668. $oField = new DesignerLongTextField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $this->aProperties['query']);
  669. $oField->SetMandatory();
  670. $oForm->AddField($oField);
  671. try
  672. {
  673. // Group by field: build the list of possible values (attribute codes + ...)
  674. $aGroupBy = $this->GetGroupByOptions($this->aProperties['query']);
  675. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  676. $oField->SetMandatory();
  677. $oField->SetAllowedValues($aGroupBy);
  678. }
  679. catch(Exception $e)
  680. {
  681. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), $this->aProperties['group_by']);
  682. $oField->SetReadOnly();
  683. }
  684. $oForm->AddField($oField);
  685. $aStyles = array(
  686. 'pie' => Dict::S('UI:DashletGroupByPie:Label'),
  687. 'bars' => Dict::S('UI:DashletGroupByBars:Label'),
  688. 'table' => Dict::S('UI:DashletGroupByTable:Label'),
  689. );
  690. $oField = new DesignerComboField('style', Dict::S('UI:DashletGroupBy:Prop-Style'), $this->aProperties['style']);
  691. $oField->SetMandatory();
  692. $oField->SetAllowedValues($aStyles);
  693. $oForm->AddField($oField);
  694. }
  695. public function Update($aValues, $aUpdatedFields)
  696. {
  697. if (in_array('query', $aUpdatedFields))
  698. {
  699. try
  700. {
  701. $sCurrQuery = $aValues['query'];
  702. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  703. $sCurrClass = $oCurrSearch->GetClass();
  704. $sPrevQuery = $this->aProperties['query'];
  705. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  706. $sPrevClass = $oPrevSearch->GetClass();
  707. if ($sCurrClass != $sPrevClass)
  708. {
  709. $this->bFormRedrawNeeded = true;
  710. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  711. $this->aProperties['group_by'] = '';
  712. }
  713. }
  714. catch(Exception $e)
  715. {
  716. $this->bFormRedrawNeeded = true;
  717. }
  718. }
  719. $oDashlet = parent::Update($aValues, $aUpdatedFields);
  720. if (in_array('style', $aUpdatedFields))
  721. {
  722. switch($aValues['style'])
  723. {
  724. // Style changed, mutate to the specified type of chart
  725. case 'pie':
  726. $oDashlet = new DashletGroupByPie($this->oModelReflection, $this->sId);
  727. break;
  728. case 'bars':
  729. $oDashlet = new DashletGroupByBars($this->oModelReflection, $this->sId);
  730. break;
  731. case 'table':
  732. $oDashlet = new DashletGroupByTable($this->oModelReflection, $this->sId);
  733. break;
  734. }
  735. $oDashlet->FromParams($aValues);
  736. $oDashlet->bRedrawNeeded = true;
  737. $oDashlet->bFormRedrawNeeded = true;
  738. }
  739. return $oDashlet;
  740. }
  741. static public function GetInfo()
  742. {
  743. // Note: no need to translate, should never be visible to the end-user!
  744. return array(
  745. 'label' => 'Objects grouped by...',
  746. 'icon' => 'images/dashlet-object-grouped.png',
  747. 'description' => 'Grouped objects dashlet (abstract)',
  748. );
  749. }
  750. static public function CanCreateFromOQL()
  751. {
  752. return true;
  753. }
  754. public function GetPropertiesFieldsFromOQL(DesignerForm $oForm, $sOQL = null)
  755. {
  756. $oField = new DesignerTextField('title', Dict::S('UI:DashletGroupBy:Prop-Title'), '');
  757. $oForm->AddField($oField);
  758. $oField = new DesignerHiddenField('query', Dict::S('UI:DashletGroupBy:Prop-Query'), $sOQL);
  759. $oField->SetMandatory();
  760. $oForm->AddField($oField);
  761. if (!is_null($sOQL))
  762. {
  763. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  764. $aGroupBy = $this->GetGroupByOptions($sOQL);
  765. $oField->SetAllowedValues($aGroupBy);
  766. }
  767. else
  768. {
  769. // Creating a form for reading parameters!
  770. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletGroupBy:Prop-GroupBy'), null);
  771. }
  772. $oField->SetMandatory();
  773. $oForm->AddField($oField);
  774. $oField = new DesignerHiddenField('style', '', $this->aProperties['style']);
  775. $oField->SetMandatory();
  776. $oForm->AddField($oField);
  777. }
  778. }
  779. class DashletGroupByPie extends DashletGroupBy
  780. {
  781. public function __construct($oModelReflection, $sId)
  782. {
  783. parent::__construct($oModelReflection, $sId);
  784. $this->aProperties['style'] = 'pie';
  785. }
  786. static public function GetInfo()
  787. {
  788. return array(
  789. 'label' => Dict::S('UI:DashletGroupByPie:Label'),
  790. 'icon' => 'images/dashlet-pie-chart.png',
  791. 'description' => Dict::S('UI:DashletGroupByPie:Description'),
  792. );
  793. }
  794. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  795. {
  796. $sTitle = $this->aProperties['title'];
  797. $aDisplayValues = $this->MakeSimulatedData();
  798. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  799. $oChart = new open_flash_chart();
  800. $aGroupBy = array();
  801. $aLabels = array();
  802. foreach($aDisplayValues as $iRow => $aDisplayData)
  803. {
  804. $aLabels[$iRow] = $aDisplayData['label'];
  805. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  806. }
  807. $oChartElement = new pie();
  808. $oChartElement->set_start_angle( 35 );
  809. $oChartElement->set_animate( true );
  810. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  811. $oChartElement->set_colours( array('#FF8A00', '#909980', '#2C2B33', '#CCC08D', '#596664') );
  812. $aData = array();
  813. foreach($aGroupBy as $iRow => $iCount)
  814. {
  815. $sFlashLabel = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  816. $PieValue = new pie_value($iCount, $sFlashLabel);
  817. $aData[] = $PieValue;
  818. }
  819. $oChartElement->set_values($aData);
  820. $oChart->x_axis = null;
  821. if (!empty($sTitle))
  822. {
  823. // The title has been given in an url, and urlencoded...
  824. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  825. // Example: é (C3A9 becomes %E9)
  826. // As a consequence, json_encode (called within open-flash-chart.php)
  827. // was returning 'null' and the graph was not displayed at all
  828. // To make sure that the graph is displayed AND to get a correct title
  829. // (at least for european characters) let's transform back into utf-8 !
  830. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  831. // If the title is a dictionnary entry, fetch it
  832. $sTitle = $this->oModelReflection->DictString($sTitle);
  833. $oTitle = new title($sTitle);
  834. $oChart->set_title($oTitle);
  835. $oTitle->set_style("{font-size: 16px; font-family: Tahoma; font-weight: bold; text-align: center;}");
  836. }
  837. $oChart->set_bg_colour('#FFFFFF');
  838. $oChart->add_element($oChartElement);
  839. $sData = $oChart->toPrettyString();
  840. $sData = json_encode($sData);
  841. $oPage->add_script(
  842. <<< EOF
  843. function ofc_get_data_dashlet_{$this->sId}()
  844. {
  845. return $sData;
  846. }
  847. EOF
  848. );
  849. $oPage->add('<div class="dashlet-content">');
  850. $oPage->add("<div id=\"dashlet_chart_{$this->sId}\">If the chart does not display, <a href=\"http://get.adobe.com/flash/\" target=\"_blank\">install Flash</a></div>\n");
  851. $oPage->add('</div>');
  852. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  853. $oPage->add_ready_script(
  854. <<<EOF
  855. swfobject.embedSWF( "../images/open-flash-chart.swf",
  856. "dashlet_chart_{$this->sId}",
  857. "100%", "300","9.0.0",
  858. "expressInstall.swf",
  859. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  860. {'wmode': 'transparent'}
  861. );
  862. EOF
  863. );
  864. }
  865. }
  866. class DashletGroupByBars extends DashletGroupBy
  867. {
  868. public function __construct($oModelReflection, $sId)
  869. {
  870. parent::__construct($oModelReflection, $sId);
  871. $this->aProperties['style'] = 'bars';
  872. }
  873. static public function GetInfo()
  874. {
  875. return array(
  876. 'label' => Dict::S('UI:DashletGroupByBars:Label'),
  877. 'icon' => 'images/dashlet-bar-chart.png',
  878. 'description' => Dict::S('UI:DashletGroupByBars:Description'),
  879. );
  880. }
  881. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  882. {
  883. $sTitle = $this->aProperties['title'];
  884. $aDisplayValues = $this->MakeSimulatedData();
  885. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  886. $oChart = new open_flash_chart();
  887. $aGroupBy = array();
  888. $aLabels = array();
  889. foreach($aDisplayValues as $iRow => $aDisplayData)
  890. {
  891. $aLabels[$iRow] = $aDisplayData['label'];
  892. $aGroupBy[$iRow] = (int) $aDisplayData['count'];
  893. }
  894. $oChartElement = new bar_glass();
  895. $aData = array();
  896. $aChartLabels = array();
  897. $maxValue = 0;
  898. foreach($aGroupBy as $iRow => $iCount)
  899. {
  900. $oBarValue = new bar_value($iCount);
  901. $aData[] = $oBarValue;
  902. if ($iCount > $maxValue) $maxValue = $iCount;
  903. $aChartLabels[] = html_entity_decode($aLabels[$iRow], ENT_QUOTES, 'UTF-8');
  904. }
  905. $oYAxis = new y_axis();
  906. $aMagicValues = array(1,2,5,10);
  907. $iMultiplier = 1;
  908. $index = 0;
  909. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  910. while($maxValue > $iTop)
  911. {
  912. $index++;
  913. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  914. if (($index % count($aMagicValues)) == 0)
  915. {
  916. $iMultiplier = $iMultiplier * 10;
  917. }
  918. }
  919. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  920. $oYAxis->set_range(0, $iTop, $iMultiplier);
  921. $oChart->set_y_axis( $oYAxis );
  922. $oChartElement->set_values( $aData );
  923. $oXAxis = new x_axis();
  924. $oXLabels = new x_axis_labels();
  925. // set them vertical
  926. $oXLabels->set_vertical();
  927. // set the label text
  928. $oXLabels->set_labels($aChartLabels);
  929. // Add the X Axis Labels to the X Axis
  930. $oXAxis->set_labels( $oXLabels );
  931. $oChart->set_x_axis( $oXAxis );
  932. if (!empty($sTitle))
  933. {
  934. // The title has been given in an url, and urlencoded...
  935. // and urlencode transforms utf-8 into something similar to ISO-8859-1
  936. // Example: é (C3A9 becomes %E9)
  937. // As a consequence, json_encode (called within open-flash-chart.php)
  938. // was returning 'null' and the graph was not displayed at all
  939. // To make sure that the graph is displayed AND to get a correct title
  940. // (at least for european characters) let's transform back into utf-8 !
  941. $sTitle = iconv("ISO-8859-1", "UTF-8//IGNORE", $sTitle);
  942. // If the title is a dictionnary entry, fetch it
  943. $sTitle = $this->oModelReflection->DictString($sTitle);
  944. $oTitle = new title($sTitle);
  945. $oChart->set_title($oTitle);
  946. $oTitle->set_style("{font-size: 16px; font-family: Tahoma; font-weight: bold; text-align: center;}");
  947. }
  948. $oChart->set_bg_colour('#FFFFFF');
  949. $oChart->add_element($oChartElement);
  950. $sData = $oChart->toPrettyString();
  951. $sData = json_encode($sData);
  952. $oPage->add_script(
  953. <<< EOF
  954. function ofc_get_data_dashlet_{$this->sId}()
  955. {
  956. return $sData;
  957. }
  958. EOF
  959. );
  960. $oPage->add('<div class="dashlet-content">');
  961. $oPage->add("<div id=\"dashlet_chart_{$this->sId}\">If the chart does not display, <a href=\"http://get.adobe.com/flash/\" target=\"_blank\">install Flash</a></div>\n");
  962. $oPage->add('</div>');
  963. // $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  964. $oPage->add_ready_script(
  965. <<<EOF
  966. swfobject.embedSWF( "../images/open-flash-chart.swf",
  967. "dashlet_chart_{$this->sId}",
  968. "100%", "300","9.0.0",
  969. "expressInstall.swf",
  970. {"get-data":"ofc_get_data_dashlet_{$this->sId}", "id":"dashlet_chart_{$this->sId}"},
  971. {'wmode': 'transparent'}
  972. );
  973. EOF
  974. );
  975. }
  976. }
  977. class DashletGroupByTable extends DashletGroupBy
  978. {
  979. public function __construct($oModelReflection, $sId)
  980. {
  981. parent::__construct($oModelReflection, $sId);
  982. $this->aProperties['style'] = 'table';
  983. }
  984. static public function GetInfo()
  985. {
  986. return array(
  987. 'label' => Dict::S('UI:DashletGroupByTable:Label'),
  988. 'description' => Dict::S('UI:DashletGroupByTable:Description'),
  989. 'icon' => 'images/dashlet-groupby-table.png',
  990. );
  991. }
  992. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  993. {
  994. $sTitle = $this->aProperties['title'];
  995. $aDisplayValues = $this->MakeSimulatedData();
  996. $iTotal = 0;
  997. foreach($aDisplayValues as $iRow => $aDisplayData)
  998. {
  999. $iTotal += $aDisplayData['count'];
  1000. }
  1001. $oPage->add('<div class="dashlet-content">');
  1002. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1003. $oPage->add('<div id="'.$sBlockId.'" class="display_block">');
  1004. $oPage->add('<p>'.Dict::Format('UI:Pagination:HeaderNoSelection', $iTotal).'</p>');
  1005. $oPage->add('<table class="listResults">');
  1006. $oPage->add('<thead>');
  1007. $oPage->add('<tr>');
  1008. $oPage->add('<th class="header" title="">'.$this->sGroupByLabel.'</th>');
  1009. $oPage->add('<th class="header" title="'.Dict::S('UI:GroupBy:Count+').'">'.Dict::S('UI:GroupBy:Count').'</th>');
  1010. $oPage->add('</tr>');
  1011. $oPage->add('</thead>');
  1012. $oPage->add('<tbody>');
  1013. foreach($aDisplayValues as $aDisplayData)
  1014. {
  1015. $oPage->add('<tr class="even">');
  1016. $oPage->add('<td class=""><span title="Active">'.$aDisplayData['label'].'</span></td>');
  1017. $oPage->add('<td class=""><a>'.$aDisplayData['count'].'</a></td>');
  1018. $oPage->add('</tr>');
  1019. }
  1020. $oPage->add('</tbody>');
  1021. $oPage->add('</table>');
  1022. $oPage->add('</div>');
  1023. $oPage->add('</div>');
  1024. }
  1025. }
  1026. class DashletHeaderStatic extends Dashlet
  1027. {
  1028. public function __construct($oModelReflection, $sId)
  1029. {
  1030. parent::__construct($oModelReflection, $sId);
  1031. $this->aProperties['title'] = Dict::S('UI:DashletHeaderStatic:Prop-Title:Default');
  1032. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1033. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1034. }
  1035. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1036. {
  1037. $sTitle = $this->aProperties['title'];
  1038. $sIcon = $this->aProperties['icon'];
  1039. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1040. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1041. $oPage->add('<div class="dashlet-content">');
  1042. $oPage->add('<div class="main_header">');
  1043. $oPage->add('<img src="'.$sIconPath.'">');
  1044. $oPage->add('<h1>'.$this->oModelReflection->DictString($sTitle).'</h1>');
  1045. $oPage->add('</div>');
  1046. $oPage->add('</div>');
  1047. }
  1048. public function GetPropertiesFields(DesignerForm $oForm)
  1049. {
  1050. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderStatic:Prop-Title'), $this->aProperties['title']);
  1051. $oForm->AddField($oField);
  1052. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderStatic:Prop-Icon'), $this->aProperties['icon']);
  1053. $oForm->AddField($oField);
  1054. }
  1055. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1056. {
  1057. if ($sProperty == 'icon')
  1058. {
  1059. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1060. return $oIconField->ValueFromDOMNode($oDOMNode);
  1061. }
  1062. else
  1063. {
  1064. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1065. }
  1066. }
  1067. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1068. {
  1069. if ($sProperty == 'icon')
  1070. {
  1071. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1072. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1073. }
  1074. else
  1075. {
  1076. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1077. }
  1078. }
  1079. static public function GetInfo()
  1080. {
  1081. return array(
  1082. 'label' => Dict::S('UI:DashletHeaderStatic:Label'),
  1083. 'icon' => 'images/dashlet-header.png',
  1084. 'description' => Dict::S('UI:DashletHeaderStatic:Description'),
  1085. );
  1086. }
  1087. }
  1088. class DashletHeaderDynamic extends Dashlet
  1089. {
  1090. public function __construct($oModelReflection, $sId)
  1091. {
  1092. parent::__construct($oModelReflection, $sId);
  1093. $this->aProperties['title'] = Dict::S('UI:DashletHeaderDynamic:Prop-Title:Default');
  1094. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1095. $this->aProperties['icon'] = $oIconSelect->GetDefaultValue('Contact');
  1096. $this->aProperties['subtitle'] = Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle:Default');
  1097. $this->aProperties['query'] = 'SELECT Contact';
  1098. $this->aProperties['group_by'] = 'status';
  1099. $this->aProperties['values'] = array('active', 'inactive');
  1100. }
  1101. protected function GetValues()
  1102. {
  1103. $sQuery = $this->aProperties['query'];
  1104. $sGroupBy = $this->aProperties['group_by'];
  1105. $aValues = $this->aProperties['values'];
  1106. if (empty($aValues))
  1107. {
  1108. $aValues = array();
  1109. }
  1110. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1111. $sClass = $oQuery->GetClass();
  1112. if ($this->oModelReflection->IsValidAttCode($sClass, $sGroupBy))
  1113. {
  1114. if (count($aValues) == 0)
  1115. {
  1116. $aAllowed = $this->oModelReflection->GetAllowedValues_att($sClass, $sGroupBy);
  1117. if (is_array($aAllowed))
  1118. {
  1119. $aValues = array_keys($aAllowed);
  1120. }
  1121. }
  1122. }
  1123. return $aValues;
  1124. }
  1125. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1126. {
  1127. $sTitle = $this->aProperties['title'];
  1128. $sIcon = $this->aProperties['icon'];
  1129. $sSubtitle = $this->aProperties['subtitle'];
  1130. $sQuery = $this->aProperties['query'];
  1131. $sGroupBy = $this->aProperties['group_by'];
  1132. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1133. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1134. $aValues = $this->GetValues();
  1135. if (count($aValues) > 0)
  1136. {
  1137. // Stats grouped by <group_by>
  1138. $sCSV = implode(',', $aValues);
  1139. $aExtraParams = array(
  1140. 'title[block]' => $sTitle,
  1141. 'label[block]' => $sSubtitle,
  1142. 'status[block]' => $sGroupBy,
  1143. 'status_codes[block]' => $sCSV,
  1144. 'context_filter' => 1,
  1145. );
  1146. }
  1147. else
  1148. {
  1149. // Simple stats
  1150. $aExtraParams = array(
  1151. 'title[block]' => $sTitle,
  1152. 'label[block]' => $sSubtitle,
  1153. 'context_filter' => 1,
  1154. );
  1155. }
  1156. $oPage->add('<div class="dashlet-content">');
  1157. $oPage->add('<div class="main_header">');
  1158. $oPage->add('<img src="'.$sIconPath.'">');
  1159. $oFilter = DBObjectSearch::FromOQL($sQuery);
  1160. $oBlock = new DisplayBlock($oFilter, 'summary');
  1161. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1162. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1163. $oPage->add('</div>');
  1164. $oPage->add('</div>');
  1165. }
  1166. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1167. {
  1168. $sTitle = $this->aProperties['title'];
  1169. $sIcon = $this->aProperties['icon'];
  1170. $sSubtitle = $this->aProperties['subtitle'];
  1171. $sQuery = $this->aProperties['query'];
  1172. $sGroupBy = $this->aProperties['group_by'];
  1173. $aValues = $this->aProperties['values'];
  1174. $oQuery = $this->oModelReflection->GetQuery($sQuery);
  1175. $sClass = $oQuery->GetClass();
  1176. $oIconSelect = $this->oModelReflection->GetIconSelectionField('icon');
  1177. $sIconPath = $oIconSelect->MakeFileUrl($sIcon);
  1178. $oPage->add('<div class="dashlet-content">');
  1179. $oPage->add('<div class="main_header">');
  1180. $oPage->add('<img src="'.$sIconPath.'">');
  1181. $sBlockId = 'block_fake_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1182. $iTotal = 0;
  1183. $aValues = $this->GetValues();
  1184. if (count($aValues) > 0)
  1185. {
  1186. // Stats grouped by <group_by>
  1187. }
  1188. else
  1189. {
  1190. // Simple stats
  1191. }
  1192. $oPage->add('<div class="display_block" id="'.$sBlockId.'">');
  1193. $oPage->add('<div class="summary-details">');
  1194. $oPage->add('<table><tbody>');
  1195. $oPage->add('<tr>');
  1196. foreach ($aValues as $sValue)
  1197. {
  1198. $sValueLabel = $this->oModelReflection->GetValueLabel($sClass, $sGroupBy, $sValue);
  1199. $oPage->add(' <th>'.$sValueLabel.'</th>');
  1200. }
  1201. $oPage->add('</tr>');
  1202. $oPage->add('<tr>');
  1203. foreach ($aValues as $sValue)
  1204. {
  1205. $iCount = (int) rand(2, 100);
  1206. $iTotal += $iCount;
  1207. $oPage->add(' <td>'.$iCount.'</td>');
  1208. }
  1209. $oPage->add('</tr>');
  1210. $oPage->add('</tbody></table>');
  1211. $oPage->add('</div>');
  1212. $sTitle = $this->oModelReflection->DictString($sTitle);
  1213. $sSubtitle = $this->oModelReflection->DictFormat($sSubtitle, $iTotal);
  1214. // $sSubtitle = "original: $sSubtitle, S:".$this->oModelReflection->DictString($sSubtitle).", Format: '".$this->oModelReflection->DictFormat($sSubtitle, $iTotal)."'";
  1215. $oPage->add('<h1>'.$sTitle.'</h1>');
  1216. $oPage->add('<a class="summary">'.$sSubtitle.'</a>');
  1217. $oPage->add('</div>');
  1218. $oPage->add('</div>');
  1219. $oPage->add('</div>');
  1220. }
  1221. public function GetPropertiesFields(DesignerForm $oForm)
  1222. {
  1223. $oField = new DesignerTextField('title', Dict::S('UI:DashletHeaderDynamic:Prop-Title'), $this->aProperties['title']);
  1224. $oForm->AddField($oField);
  1225. $oField = $this->oModelReflection->GetIconSelectionField('icon', Dict::S('UI:DashletHeaderDynamic:Prop-Icon'), $this->aProperties['icon']);
  1226. $oForm->AddField($oField);
  1227. $oField = new DesignerTextField('subtitle', Dict::S('UI:DashletHeaderDynamic:Prop-Subtitle'), $this->aProperties['subtitle']);
  1228. $oForm->AddField($oField);
  1229. $oField = new DesignerTextField('query', Dict::S('UI:DashletHeaderDynamic:Prop-Query'), $this->aProperties['query']);
  1230. $oField->SetMandatory();
  1231. $oForm->AddField($oField);
  1232. try
  1233. {
  1234. // Group by field: build the list of possible values (attribute codes + ...)
  1235. $oQuery = $this->oModelReflection->GetQuery($this->aProperties['query']);
  1236. $sClass = $oQuery->GetClass();
  1237. $aGroupBy = array();
  1238. foreach($this->oModelReflection->ListAttributes($sClass, 'AttributeEnum,AttributeFinalClass') as $sAttCode => $sAttType)
  1239. {
  1240. if (is_subclass_of($sAttType, 'AttributeFinalClass') || ($sAttType == 'AttributeFinalClass'))
  1241. {
  1242. if (!$this->oModelReflection->HasChildrenClasses($sClass)) continue;
  1243. }
  1244. $sLabel = $this->oModelReflection->GetLabel($sClass, $sAttCode);
  1245. $aGroupBy[$sAttCode] = $sLabel;
  1246. }
  1247. $oField = new DesignerComboField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1248. $oField->SetMandatory();
  1249. $oField->SetAllowedValues($aGroupBy);
  1250. }
  1251. catch(Exception $e)
  1252. {
  1253. $oField = new DesignerTextField('group_by', Dict::S('UI:DashletHeaderDynamic:Prop-GroupBy'), $this->aProperties['group_by']);
  1254. $oField->SetReadOnly();
  1255. }
  1256. $oForm->AddField($oField);
  1257. $oField = new DesignerComboField('values', Dict::S('UI:DashletHeaderDynamic:Prop-Values'), $this->aProperties['values']);
  1258. $oField->MultipleSelection(true);
  1259. if (isset($sClass) && $this->oModelReflection->IsValidAttCode($sClass, $this->aProperties['group_by']))
  1260. {
  1261. $aValues = $this->oModelReflection->GetAllowedValues_att($sClass, $this->aProperties['group_by']);
  1262. $oField->SetAllowedValues($aValues);
  1263. }
  1264. else
  1265. {
  1266. $oField->SetReadOnly();
  1267. }
  1268. $oForm->AddField($oField);
  1269. }
  1270. public function Update($aValues, $aUpdatedFields)
  1271. {
  1272. if (in_array('query', $aUpdatedFields))
  1273. {
  1274. try
  1275. {
  1276. $sCurrQuery = $aValues['query'];
  1277. $oCurrSearch = $this->oModelReflection->GetQuery($sCurrQuery);
  1278. $sCurrClass = $oCurrSearch->GetClass();
  1279. $sPrevQuery = $this->aProperties['query'];
  1280. $oPrevSearch = $this->oModelReflection->GetQuery($sPrevQuery);
  1281. $sPrevClass = $oPrevSearch->GetClass();
  1282. if ($sCurrClass != $sPrevClass)
  1283. {
  1284. $this->bFormRedrawNeeded = true;
  1285. // wrong but not necessary - unset($aUpdatedFields['group_by']);
  1286. $this->aProperties['group_by'] = '';
  1287. $this->aProperties['values'] = array();
  1288. }
  1289. }
  1290. catch(Exception $e)
  1291. {
  1292. $this->bFormRedrawNeeded = true;
  1293. }
  1294. }
  1295. if (in_array('group_by', $aUpdatedFields))
  1296. {
  1297. $this->bFormRedrawNeeded = true;
  1298. $this->aProperties['values'] = array();
  1299. }
  1300. return parent::Update($aValues, $aUpdatedFields);
  1301. }
  1302. protected function PropertyFromDOMNode($oDOMNode, $sProperty)
  1303. {
  1304. if ($sProperty == 'icon')
  1305. {
  1306. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1307. return $oIconField->ValueFromDOMNode($oDOMNode);
  1308. }
  1309. else
  1310. {
  1311. return parent::PropertyFromDOMNode($oDOMNode, $sProperty);
  1312. }
  1313. }
  1314. protected function PropertyToDOMNode($oDOMNode, $sProperty, $value)
  1315. {
  1316. if ($sProperty == 'icon')
  1317. {
  1318. $oIconField = $this->oModelReflection->GetIconSelectionField('icon');
  1319. $oIconField->ValueToDOMNode($oDOMNode, $value);
  1320. }
  1321. else
  1322. {
  1323. parent::PropertyToDOMNode($oDOMNode, $sProperty, $value);
  1324. }
  1325. }
  1326. static public function GetInfo()
  1327. {
  1328. return array(
  1329. 'label' => Dict::S('UI:DashletHeaderDynamic:Label'),
  1330. 'icon' => 'images/dashlet-header-stats.png',
  1331. 'description' => Dict::S('UI:DashletHeaderDynamic:Description'),
  1332. );
  1333. }
  1334. }
  1335. class DashletBadge extends Dashlet
  1336. {
  1337. public function __construct($oModelReflection, $sId)
  1338. {
  1339. parent::__construct($oModelReflection, $sId);
  1340. $this->aProperties['class'] = 'Contact';
  1341. $this->aCSSClasses[] = 'dashlet-inline';
  1342. $this->aCSSClasses[] = 'dashlet-badge';
  1343. }
  1344. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  1345. {
  1346. $sClass = $this->aProperties['class'];
  1347. $oPage->add('<div class="dashlet-content">');
  1348. $oFilter = new DBObjectSearch($sClass);
  1349. $oBlock = new DisplayBlock($oFilter, 'actions');
  1350. $aExtraParams = array(
  1351. 'context_filter' => 1,
  1352. );
  1353. $sBlockId = 'block_'.$this->sId.($bEditMode ? '_edit' : ''); // make a unique id (edition occuring in the same DOM)
  1354. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  1355. $oPage->add('</div>');
  1356. }
  1357. public function RenderNoData($oPage, $bEditMode = false, $aExtraParams = array())
  1358. {
  1359. $sClass = $this->aProperties['class'];
  1360. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1361. $sClassLabel = $this->oModelReflection->GetName($sClass);
  1362. $oPage->add('<div class="dashlet-content">');
  1363. $oPage->add('<div id="block_fake_'.$this->sId.'" class="display_block">');
  1364. $oPage->add('<p>');
  1365. $oPage->add(' <a class="actions"><img src="'.$sIconUrl.'" style="vertical-align:middle;float;left;margin-right:10px;border:0;">'.$sClassLabel.': 947</a>');
  1366. $oPage->add('</p>');
  1367. $oPage->add('<p>');
  1368. $oPage->add(' <a>'.Dict::Format('UI:ClickToCreateNew', $sClassLabel).'</a>');
  1369. $oPage->add(' <br/>');
  1370. $oPage->add(' <a>'.Dict::Format('UI:SearchFor_Class', $sClassLabel).'</a>');
  1371. $oPage->add('</p>');
  1372. $oPage->add('</div>');
  1373. $oPage->add('</div>');
  1374. }
  1375. static protected $aClassList = null;
  1376. public function GetPropertiesFields(DesignerForm $oForm)
  1377. {
  1378. if (is_null(self::$aClassList))
  1379. {
  1380. // Cache the ordered list of classes (ordered on the label)
  1381. // (has a significant impact when editing a page with lots of badges)
  1382. //
  1383. $aClasses = array();
  1384. foreach($this->oModelReflection->GetClasses('bizmodel', true /*exclude links*/) as $sClass)
  1385. {
  1386. $aClasses[$sClass] = $this->oModelReflection->GetName($sClass);
  1387. }
  1388. asort($aClasses);
  1389. self::$aClassList = array();
  1390. foreach($aClasses as $sClass => $sLabel)
  1391. {
  1392. $sIconUrl = $this->oModelReflection->GetClassIcon($sClass, false);
  1393. $sIconFilePath = str_replace(utils::GetAbsoluteUrlAppRoot(), APPROOT, $sIconUrl);
  1394. if ($sIconUrl == '')
  1395. {
  1396. // The icon does not exist, let's use a transparent one of the same size.
  1397. $sIconUrl = utils::GetAbsoluteUrlAppRoot().'images/transparent_32_32.png';
  1398. }
  1399. self::$aClassList[] = array('value' => $sClass, 'label' => $sLabel, 'icon' => $sIconUrl);
  1400. }
  1401. }
  1402. $oField = new DesignerIconSelectionField('class', Dict::S('UI:DashletBadge:Prop-Class'), $this->aProperties['class']);
  1403. $oField->SetAllowedValues(self::$aClassList);
  1404. $oForm->AddField($oField);
  1405. }
  1406. static public function GetInfo()
  1407. {
  1408. return array(
  1409. 'label' => Dict::S('UI:DashletBadge:Label'),
  1410. 'icon' => 'images/dashlet-badge.png',
  1411. 'description' => Dict::S('UI:DashletBadge:Description'),
  1412. );
  1413. }
  1414. }
  1415. ?>