dashboard.class.inc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. <?php
  2. // Copyright (C) 2010-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/dashboardlayout.class.inc.php');
  19. require_once(APPROOT.'application/dashlet.class.inc.php');
  20. require_once(APPROOT.'core/modelreflection.class.inc.php');
  21. /**
  22. * A user editable dashboard page
  23. *
  24. * @copyright Copyright (C) 2010-2012 Combodo SARL
  25. * @license http://opensource.org/licenses/AGPL-3.0
  26. */
  27. abstract class Dashboard
  28. {
  29. protected $sTitle;
  30. protected $bAutoReload;
  31. protected $iAutoReloadSec;
  32. protected $sLayoutClass;
  33. protected $aWidgetsData;
  34. protected $oDOMNode;
  35. protected $sId;
  36. protected $aCells;
  37. protected $oMetaModel;
  38. public function __construct($sId)
  39. {
  40. $this->sTitle = '';
  41. $this->sLayoutClass = 'DashboardLayoutOneCol';
  42. $this->bAutoReload = false;
  43. $this->iAutoReloadSec = MetaModel::GetConfig()->GetStandardReloadInterval();
  44. $this->aCells = array();
  45. $this->oDOMNode = null;
  46. $this->sId = $sId;
  47. }
  48. public function FromXml($sXml)
  49. {
  50. $this->aCells = array(); // reset the content of the dashboard
  51. set_error_handler(array('Dashboard', 'ErrorHandler'));
  52. $oDoc = new DOMDocument();
  53. $oDoc->loadXML($sXml);
  54. restore_error_handler();
  55. $this->FromDOMDocument($oDoc);
  56. }
  57. public function FromDOMDocument(DOMDocument $oDoc)
  58. {
  59. $this->oDOMNode = $oDoc->getElementsByTagName('dashboard')->item(0);
  60. if ($oLayoutNode = $this->oDOMNode->getElementsByTagName('layout')->item(0))
  61. {
  62. $this->sLayoutClass = $oLayoutNode->textContent;
  63. }
  64. else
  65. {
  66. $this->sLayoutClass = 'DashboardLayoutOneCol';
  67. }
  68. if ($oTitleNode = $this->oDOMNode->getElementsByTagName('title')->item(0))
  69. {
  70. $this->sTitle = $oTitleNode->textContent;
  71. }
  72. else
  73. {
  74. $this->sTitle = '';
  75. }
  76. $this->bAutoReload = false;
  77. $this->iAutoReloadSec = MetaModel::GetConfig()->GetStandardReloadInterval();
  78. if ($oAutoReloadNode = $this->oDOMNode->getElementsByTagName('auto_reload')->item(0))
  79. {
  80. if ($oAutoReloadEnabled = $oAutoReloadNode->getElementsByTagName('enabled')->item(0))
  81. {
  82. $this->bAutoReload = ($oAutoReloadEnabled->textContent == 'true');
  83. }
  84. if ($oAutoReloadInterval = $oAutoReloadNode->getElementsByTagName('interval')->item(0))
  85. {
  86. $this->iAutoReloadSec = max(MetaModel::GetConfig()->Get('min_reload_interval'), (int)$oAutoReloadInterval->textContent);
  87. }
  88. }
  89. if ($oCellsNode = $this->oDOMNode->getElementsByTagName('cells')->item(0))
  90. {
  91. $oCellsList = $oCellsNode->getElementsByTagName('cell');
  92. $aCellOrder = array();
  93. $iCellRank = 0;
  94. foreach($oCellsList as $oCellNode)
  95. {
  96. $aDashletList = array();
  97. $oCellRank = $oCellNode->getElementsByTagName('rank')->item(0);
  98. if ($oCellRank)
  99. {
  100. $iCellRank = (float)$oCellRank->textContent;
  101. }
  102. $oDashletsNode = $oCellNode->getElementsByTagName('dashlets')->item(0);
  103. {
  104. $oDashletList = $oDashletsNode->getElementsByTagName('dashlet');
  105. $iRank = 0;
  106. $aDashletOrder = array();
  107. foreach($oDashletList as $oDomNode)
  108. {
  109. $sDashletClass = $oDomNode->getAttribute('xsi:type');
  110. $oRank = $oDomNode->getElementsByTagName('rank')->item(0);
  111. if ($oRank)
  112. {
  113. $iRank = (float)$oRank->textContent;
  114. }
  115. $sId = $oDomNode->getAttribute('id');
  116. $oNewDashlet = new $sDashletClass($this->oMetaModel, $sId);
  117. $oNewDashlet->FromDOMNode($oDomNode);
  118. $aDashletOrder[] = array('rank' => $iRank, 'dashlet' => $oNewDashlet);
  119. }
  120. usort($aDashletOrder, array(get_class($this), 'SortOnRank'));
  121. $aDashletList = array();
  122. foreach($aDashletOrder as $aItem)
  123. {
  124. $aDashletList[] = $aItem['dashlet'];
  125. }
  126. $aCellOrder[] = array('rank' => $iCellRank, 'dashlets' => $aDashletList);
  127. }
  128. }
  129. usort($aCellOrder, array(get_class($this), 'SortOnRank'));
  130. foreach($aCellOrder as $aItem)
  131. {
  132. $this->aCells[] = $aItem['dashlets'];
  133. }
  134. }
  135. else
  136. {
  137. $this->aCells = array();
  138. }
  139. }
  140. static function SortOnRank($aItem1, $aItem2)
  141. {
  142. return ($aItem1['rank'] > $aItem2['rank']) ? +1 : -1;
  143. }
  144. /**
  145. * Error handler to turn XML loading warnings into exceptions
  146. */
  147. public static function ErrorHandler($errno, $errstr, $errfile, $errline)
  148. {
  149. if ($errno == E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0))
  150. {
  151. throw new DOMException($errstr);
  152. }
  153. else
  154. {
  155. return false;
  156. }
  157. }
  158. public function ToXml()
  159. {
  160. $oDoc = new DOMDocument();
  161. $oDoc->formatOutput = true; // indent (must be loaded with option LIBXML_NOBLANKS)
  162. $oDoc->preserveWhiteSpace = true; // otherwise the formatOutput option would have no effect
  163. $oMainNode = $oDoc->createElement('dashboard');
  164. $oMainNode->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");
  165. $oDoc->appendChild($oMainNode);
  166. $this->ToDOMNode($oMainNode);
  167. $sXml = $oDoc->saveXML();
  168. return $sXml;
  169. }
  170. public function ToDOMNode($oDefinition)
  171. {
  172. $oDoc = $oDefinition->ownerDocument;
  173. $oNode = $oDoc->createElement('layout', $this->sLayoutClass);
  174. $oDefinition->appendChild($oNode);
  175. $oNode = $oDoc->createElement('title', $this->sTitle);
  176. $oDefinition->appendChild($oNode);
  177. $oAutoReloadNode = $oDoc->createElement('auto_reload');
  178. $oDefinition->appendChild($oAutoReloadNode);
  179. $oNode = $oDoc->createElement('enabled', $this->bAutoReload ? 'true' : 'false');
  180. $oAutoReloadNode->appendChild($oNode);
  181. $oNode = $oDoc->createElement('interval', $this->iAutoReloadSec);
  182. $oAutoReloadNode->appendChild($oNode);
  183. $oCellsNode = $oDoc->createElement('cells');
  184. $oDefinition->appendChild($oCellsNode);
  185. $iCellRank = 0;
  186. foreach ($this->aCells as $aCell)
  187. {
  188. $oCellNode = $oDoc->createElement('cell');
  189. $oCellNode->setAttribute('id', $iCellRank);
  190. $oCellsNode->appendChild($oCellNode);
  191. $oCellRank = $oDoc->createElement('rank', $iCellRank);
  192. $oCellNode->appendChild($oCellRank);
  193. $iCellRank++;
  194. $iDashletRank = 0;
  195. $oDashletsNode = $oDoc->createElement('dashlets');
  196. $oCellNode->appendChild($oDashletsNode);
  197. foreach ($aCell as $oDashlet)
  198. {
  199. $oNode = $oDoc->createElement('dashlet');
  200. $oDashletsNode->appendChild($oNode);
  201. $oNode->setAttribute('id', $oDashlet->GetID());
  202. $oNode->setAttribute('xsi:type', get_class($oDashlet));
  203. $oDashletRank = $oDoc->createElement('rank', $iDashletRank);
  204. $oNode->appendChild($oDashletRank);
  205. $iDashletRank++;
  206. $oDashlet->ToDOMNode($oNode);
  207. }
  208. }
  209. }
  210. public function FromParams($aParams)
  211. {
  212. $this->sLayoutClass = $aParams['layout_class'];
  213. $this->sTitle = $aParams['title'];
  214. $this->bAutoReload = $aParams['auto_reload'] == 'true';
  215. $this->iAutoReloadSec = max(MetaModel::GetConfig()->Get('min_reload_interval'), (int) $aParams['auto_reload_sec']);
  216. foreach($aParams['cells'] as $aCell)
  217. {
  218. $aCellDashlets = array();
  219. foreach($aCell as $aDashletParams)
  220. {
  221. $sDashletClass = $aDashletParams['dashlet_class'];
  222. $sId = $aDashletParams['dashlet_id'];
  223. $oNewDashlet = new $sDashletClass($this->oMetaModel, $sId);
  224. $oForm = $oNewDashlet->GetForm();
  225. $oForm->SetParamsContainer($sId);
  226. $oForm->SetPrefix('');
  227. $aValues = $oForm->ReadParams();
  228. $oNewDashlet->FromParams($aValues);
  229. $aCellDashlets[] = $oNewDashlet;
  230. }
  231. $this->aCells[] = $aCellDashlets;
  232. }
  233. }
  234. public function Save()
  235. {
  236. }
  237. public function GetLayout()
  238. {
  239. return $this->sLayoutClass;
  240. }
  241. public function SetLayout($sLayoutClass)
  242. {
  243. $this->sLayoutClass = $sLayoutClass;
  244. }
  245. public function GetTitle()
  246. {
  247. return $this->sTitle;
  248. }
  249. public function SetTitle($sTitle)
  250. {
  251. $this->sTitle = $sTitle;
  252. }
  253. public function GetAutoReload()
  254. {
  255. return $this->bAutoReload;
  256. }
  257. public function SetAutoReload($bAutoReload)
  258. {
  259. $this->bAutoReload = $bAutoReload;
  260. }
  261. public function GetAutoReloadInterval()
  262. {
  263. return $this->iAutoReloadSec;
  264. }
  265. public function SetAutoReloadInterval($iAutoReloadSec)
  266. {
  267. $this->iAutoReloadSec = max(MetaModel::GetConfig()->Get('min_reload_interval'), (int)$iAutoReloadSec);
  268. }
  269. public function AddDashlet($oDashlet)
  270. {
  271. $sId = $this->GetNewDashletId();
  272. $oDashlet->SetId($sId);
  273. $this->aCells[] = array($oDashlet);
  274. }
  275. public function Render($oPage, $bEditMode = false, $aExtraParams = array())
  276. {
  277. $oPage->add('<h1>'.htmlentities(Dict::S($this->sTitle), ENT_QUOTES, 'UTF-8', false).'</h1>');
  278. $oLayout = new $this->sLayoutClass;
  279. $oLayout->Render($oPage, $this->aCells, $bEditMode, $aExtraParams);
  280. if (!$bEditMode)
  281. {
  282. $oPage->add_linked_script('../js/dashlet.js');
  283. $oPage->add_linked_script('../js/dashboard.js');
  284. }
  285. }
  286. public function RenderProperties($oPage)
  287. {
  288. // menu to pick a layout and edit other properties of the dashboard
  289. $oPage->add('<div class="ui-widget-content ui-corner-all"><div class="ui-widget-header ui-corner-all" style="text-align:center; padding: 2px;">'.Dict::S('UI:DashboardEdit:Properties').'</div>');
  290. $sUrl = utils::GetAbsoluteUrlAppRoot();
  291. $oPage->add('<div style="text-align:center">'.Dict::S('UI:DashboardEdit:Layout').'</div>');
  292. $oPage->add('<div id="select_layout" style="text-align:center">');
  293. foreach( get_declared_classes() as $sLayoutClass)
  294. {
  295. if (is_subclass_of($sLayoutClass, 'DashboardLayout'))
  296. {
  297. $oReflection = new ReflectionClass($sLayoutClass);
  298. if (!$oReflection->isAbstract())
  299. {
  300. $aCallSpec = array($sLayoutClass, 'GetInfo');
  301. $aInfo = call_user_func($aCallSpec);
  302. $sChecked = ($this->sLayoutClass == $sLayoutClass) ? 'checked' : '';
  303. $oPage->add('<input type="radio" name="layout_class" '.$sChecked.' value="'.$sLayoutClass.'" id="layout_'.$sLayoutClass.'"><label for="layout_'.$sLayoutClass.'"><img src="'.$sUrl.$aInfo['icon'].'" /></label>'); // title="" on either the img or the label does nothing !
  304. }
  305. }
  306. }
  307. $oPage->add('</div>');
  308. $oForm = new DesignerForm();
  309. $oField = new DesignerHiddenField('dashboard_id', '', $this->sId);
  310. $oForm->AddField($oField);
  311. $oField = new DesignerLongTextField('dashboard_title', Dict::S('UI:DashboardEdit:DashboardTitle'), $this->sTitle);
  312. $oForm->AddField($oField);
  313. $oField = new DesignerBooleanField('auto_reload', Dict::S('UI:DashboardEdit:AutoReload'), $this->bAutoReload);
  314. $oForm->AddField($oField);
  315. $oField = new DesignerIntegerField('auto_reload_sec', Dict::S('UI:DashboardEdit:AutoReloadSec'), $this->iAutoReloadSec);
  316. $oField->SetBoundaries(MetaModel::GetConfig()->Get('min_reload_interval'), null); // no upper limit
  317. $oForm->AddField($oField);
  318. $this->SetFormParams($oForm);
  319. $oForm->RenderAsPropertySheet($oPage, false, '.itop-dashboard');
  320. $oPage->add('</div>');
  321. $sRateTitle = addslashes(Dict::Format('UI:DashboardEdit:AutoReloadSec+', MetaModel::GetConfig()->Get('min_reload_interval')));
  322. $oPage->add_ready_script(
  323. <<<EOF
  324. // Note: the title gets deleted by the validation mechanism
  325. $("#attr_auto_reload_sec").tooltip({items: 'input', content: '$sRateTitle'});
  326. $("#attr_auto_reload_sec").prop('disabled', !$('#attr_auto_reload').is(':checked'));
  327. $('#attr_auto_reload').change( function(ev) {
  328. $("#attr_auto_reload_sec").prop('disabled', !$(this).is(':checked'));
  329. } );
  330. $('#select_layout').buttonset();
  331. $('#select_dashlet').droppable({
  332. accept: '.dashlet',
  333. drop: function(event, ui) {
  334. $( this ).find( ".placeholder" ).remove();
  335. var oDashlet = ui.draggable.data('itopDashlet');
  336. oDashlet._remove_dashlet();
  337. },
  338. });
  339. $('#event_bus').bind('dashlet-selected', function(event, data){
  340. var sDashletId = data.dashlet_id;
  341. var sPropId = 'dashlet_properties_'+sDashletId;
  342. $('.dashlet_properties').each(function() {
  343. var sId = $(this).attr('id');
  344. var bShow = (sId == sPropId);
  345. if (bShow)
  346. {
  347. $(this).show();
  348. }
  349. else
  350. {
  351. $(this).hide();
  352. }
  353. });
  354. });
  355. EOF
  356. );
  357. }
  358. public function RenderDashletsSelection($oPage)
  359. {
  360. // Toolbox/palette to drag and drop dashlets
  361. $oPage->add('<div class="ui-widget-content ui-corner-all"><div class="ui-widget-header ui-corner-all" style="text-align:center; padding: 2px;">'.Dict::S('UI:DashboardEdit:Dashlets').'</div>');
  362. $sUrl = utils::GetAbsoluteUrlAppRoot();
  363. $oPage->add('<div id="select_dashlet" style="text-align:center">');
  364. foreach( get_declared_classes() as $sDashletClass)
  365. {
  366. if (is_subclass_of($sDashletClass, 'Dashlet'))
  367. {
  368. $oReflection = new ReflectionClass($sDashletClass);
  369. if (!$oReflection->isAbstract())
  370. {
  371. $aCallSpec = array($sDashletClass, 'IsVisible');
  372. $bVisible = call_user_func($aCallSpec);
  373. if ($bVisible)
  374. {
  375. $aCallSpec = array($sDashletClass, 'GetInfo');
  376. $aInfo = call_user_func($aCallSpec);
  377. $oPage->add('<span dashlet_class="'.$sDashletClass.'" class="dashlet_icon ui-widget-content ui-corner-all" id="dashlet_'.$sDashletClass.'" title="'.$aInfo['label'].'" style="width:34px; height:34px; display:inline-block; margin:2px;"><img src="'.$sUrl.$aInfo['icon'].'" /></span>');
  378. }
  379. }
  380. }
  381. }
  382. $oPage->add('</div>');
  383. $oPage->add('</div>');
  384. $oPage->add_ready_script("$('.dashlet_icon').draggable({helper: 'clone', appendTo: 'body', zIndex: 10000, revert:'invalid'});");
  385. }
  386. public function RenderDashletsProperties($oPage)
  387. {
  388. // Toolbox/palette to edit the properties of each dashlet
  389. $oPage->add('<div class="ui-widget-content ui-corner-all"><div class="ui-widget-header ui-corner-all" style="text-align:center; padding: 2px;">'.Dict::S('UI:DashboardEdit:DashletProperties').'</div>');
  390. $oPage->add('<div id="dashlet_properties" style="text-align:center">');
  391. foreach($this->aCells as $aCell)
  392. {
  393. foreach($aCell as $oDashlet)
  394. {
  395. $sId = $oDashlet->GetID();
  396. $sClass = get_class($oDashlet);
  397. if ($oDashlet->IsVisible())
  398. {
  399. $oPage->add('<div class="dashlet_properties" id="dashlet_properties_'.$sId.'" style="display:none">');
  400. $oForm = $oDashlet->GetForm();
  401. $this->SetFormParams($oForm);
  402. $oForm->RenderAsPropertySheet($oPage, false, '.itop-dashboard');
  403. $oPage->add('</div>');
  404. }
  405. }
  406. }
  407. $oPage->add('</div>');
  408. $oPage->add('</div>');
  409. }
  410. protected function GetNewDashletId()
  411. {
  412. $iNewId = 0;
  413. foreach($this->aCells as $aDashlets)
  414. {
  415. foreach($aDashlets as $oDashlet)
  416. {
  417. $iNewId = max($iNewId, (int)$oDashlet->GetID());
  418. }
  419. }
  420. return $iNewId + 1;
  421. }
  422. abstract protected function SetFormParams($oForm);
  423. }
  424. class RuntimeDashboard extends Dashboard
  425. {
  426. protected $bCustomized;
  427. public function __construct($sId)
  428. {
  429. parent::__construct($sId);
  430. $this->bCustomized = false;
  431. $this->oMetaModel = new ModelReflectionRuntime();
  432. }
  433. public function SetCustomFlag($bCustomized)
  434. {
  435. $this->bCustomized = $bCustomized;
  436. }
  437. protected function SetFormParams($oForm)
  438. {
  439. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  440. }
  441. public function Save()
  442. {
  443. $sXml = $this->ToXml();
  444. $oUDSearch = new DBObjectSearch('UserDashboard');
  445. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  446. $oUDSearch->AddCondition('menu_code', $this->sId, '=');
  447. $oUDSet = new DBObjectSet($oUDSearch);
  448. if ($oUDSet->Count() > 0)
  449. {
  450. // Assuming there is at most one couple {user, menu}!
  451. $oUserDashboard = $oUDSet->Fetch();
  452. $oUserDashboard->Set('contents', $sXml);
  453. $oUserDashboard->DBUpdate();
  454. }
  455. else
  456. {
  457. // No such customized dasboard for the current user, let's create a new record
  458. $oUserDashboard = new UserDashboard();
  459. $oUserDashboard->Set('user_id', UserRights::GetUserId());
  460. $oUserDashboard->Set('menu_code', $this->sId);
  461. $oUserDashboard->Set('contents', $sXml);
  462. $oUserDashboard->DBInsert();
  463. }
  464. }
  465. public function Revert()
  466. {
  467. $oUDSearch = new DBObjectSearch('UserDashboard');
  468. $oUDSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  469. $oUDSearch->AddCondition('menu_code', $this->sId, '=');
  470. $oUDSet = new DBObjectSet($oUDSearch);
  471. if ($oUDSet->Count() > 0)
  472. {
  473. // Assuming there is at most one couple {user, menu}!
  474. $oUserDashboard = $oUDSet->Fetch();
  475. $oUserDashboard->DBDelete();
  476. }
  477. }
  478. public function RenderEditionTools($oPage)
  479. {
  480. $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.iframe-transport.js');
  481. $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.fileupload.js');
  482. $sEditMenu = "<td><span id=\"DashboardMenu\"><ul><li><img src=\"../images/edit.png\"><ul>";
  483. $aActions = array();
  484. $oEdit = new JSPopupMenuItem('UI:Dashboard:Edit', Dict::S('UI:Dashboard:Edit'), "return EditDashboard('{$this->sId}')");
  485. $aActions[$oEdit->GetUID()] = $oEdit->GetMenuItem();
  486. if ($this->bCustomized)
  487. {
  488. $oRevert = new JSPopupMenuItem('UI:Dashboard:RevertConfirm', Dict::S('UI:Dashboard:Revert'),
  489. "if (confirm('".addslashes(Dict::S('UI:Dashboard:RevertConfirm'))."')) return RevertDashboard('{$this->sId}'); else return false");
  490. $aActions[$oRevert->GetUID()] = $oRevert->GetMenuItem();
  491. }
  492. utils::GetPopupMenuItems($oPage, iPopupMenuExtension::MENU_DASHBOARD_ACTIONS, $this, $aActions);
  493. $sEditMenu .= $oPage->RenderPopupMenuItems($aActions);
  494. $sEditMenu = addslashes($sEditMenu);
  495. //$sEditBtn = addslashes('<div style="display: inline-block; height: 55px; width:200px;vertical-align:center;line-height:60px;text-align:left;"><button onclick="EditDashboard(\''.$this->sId.'\');">Edit This Page</button></div>');
  496. $oPage->add_ready_script(
  497. <<<EOF
  498. $('#logOffBtn').parent().before('$sEditMenu');
  499. $('#DashboardMenu>ul').popupmenu();
  500. EOF
  501. );
  502. $oPage->add_script(
  503. <<<EOF
  504. function EditDashboard(sId)
  505. {
  506. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'dashboard_editor', id: sId},
  507. function(data)
  508. {
  509. $('body').append(data);
  510. }
  511. );
  512. return false;
  513. }
  514. function RevertDashboard(sId)
  515. {
  516. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', {operation: 'revert_dashboard', dashboard_id: sId},
  517. function(data)
  518. {
  519. $('body').append(data);
  520. }
  521. );
  522. return false;
  523. }
  524. EOF
  525. );
  526. }
  527. public function RenderProperties($oPage)
  528. {
  529. parent::RenderProperties($oPage);
  530. $oPage->add_ready_script(
  531. <<<EOF
  532. $('#select_layout input').click( function() {
  533. var sLayoutClass = $(this).val();
  534. $('.itop-dashboard').runtimedashboard('option', {layout_class: sLayoutClass});
  535. } );
  536. $('#row_attr_dashboard_title').property_field('option', {parent_selector: '.itop-dashboard', auto_apply: false, 'do_apply': function() {
  537. var sTitle = $('#attr_dashboard_title').val();
  538. $('.itop-dashboard').runtimedashboard('option', {title: sTitle});
  539. return true;
  540. }
  541. });
  542. $('#row_attr_auto_reload').property_field('option', {parent_selector: '.itop-dashboard', auto_apply: true, 'do_apply': function() {
  543. var bAutoReload = $('#attr_auto_reload').is(':checked');
  544. $('.itop-dashboard').runtimedashboard('option', {auto_reload: bAutoReload});
  545. return true;
  546. }
  547. });
  548. $('#row_attr_auto_reload_sec').property_field('option', {parent_selector: '.itop-dashboard', auto_apply: true, 'do_apply': function() {
  549. var iAutoReloadSec = $('#attr_auto_reload_sec').val();
  550. $('.itop-dashboard').runtimedashboard('option', {auto_reload_sec: iAutoReloadSec});
  551. return true;
  552. }
  553. });
  554. EOF
  555. );
  556. }
  557. public function RenderEditor($oPage)
  558. {
  559. $oPage->add('<div id="dashboard_editor">');
  560. $oPage->add('<div class="ui-layout-center">');
  561. $this->Render($oPage, true);
  562. $oPage->add('</div>');
  563. $oPage->add('<div class="ui-layout-east">');
  564. $this->RenderProperties($oPage);
  565. $this->RenderDashletsSelection($oPage);
  566. $this->RenderDashletsProperties($oPage);
  567. $oPage->add('</div>');
  568. $oPage->add('<div id="event_bus"/>'); // For exchanging messages between the panes, same as in the designer
  569. $oPage->add('</div>');
  570. $sDialogTitle = Dict::S('UI:DashboardEdit:Title');
  571. $sOkButtonLabel = Dict::S('UI:Button:Save');
  572. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  573. $sId = addslashes($this->sId);
  574. $sLayoutClass = addslashes($this->sLayoutClass);
  575. $sAutoReload = $this->bAutoReload ? 'true' : 'false';
  576. $sAutoReloadSec = (string) $this->iAutoReloadSec;
  577. $sTitle = addslashes($this->sTitle);
  578. $sUrl = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php';
  579. $sExitConfirmationMessage = addslashes(Dict::S('UI:NavigateAwayConfirmationMessage'));
  580. $sCancelConfirmationMessage = addslashes(Dict::S('UI:CancelConfirmationMessage'));
  581. $sAutoApplyConfirmationMessage = addslashes(Dict::S('UI:AutoApplyConfirmationMessage'));
  582. $oPage->add_ready_script(
  583. <<<EOF
  584. window.bLeavingOnUserAction = false;
  585. $('#dashboard_editor').dialog({
  586. height: $('body').height() - 50,
  587. width: $('body').width() - 50,
  588. modal: true,
  589. title: '$sDialogTitle',
  590. buttons: [
  591. { text: "$sOkButtonLabel", click: function() {
  592. var oDashboard = $('.itop-dashboard').data('itopRuntimedashboard');
  593. if (oDashboard.is_dirty())
  594. {
  595. if (!confirm('$sAutoApplyConfirmationMessage'))
  596. {
  597. return;
  598. }
  599. else
  600. {
  601. oDashboard.apply_changes();
  602. }
  603. }
  604. window.bLeavingOnUserAction = true;
  605. oDashboard.save();
  606. } },
  607. { text: "$sCancelButtonLabel", click: function() {
  608. var oDashboard = $('.itop-dashboard').data('itopRuntimedashboard');
  609. if (oDashboard.is_modified())
  610. {
  611. if (!confirm('$sCancelConfirmationMessage'))
  612. {
  613. return;
  614. }
  615. }
  616. window.bLeavingOnUserAction = true;
  617. $(this).dialog( "close" );
  618. $(this).remove();
  619. } },
  620. ],
  621. close: function() { $(this).remove(); }
  622. });
  623. $('#dashboard_editor .ui-layout-center').runtimedashboard({
  624. dashboard_id: '$sId', layout_class: '$sLayoutClass', title: '$sTitle',
  625. auto_reload: $sAutoReload, auto_reload_sec: $sAutoReloadSec,
  626. submit_to: '$sUrl', submit_parameters: {operation: 'save_dashboard'},
  627. render_to: '$sUrl', render_parameters: {operation: 'render_dashboard'},
  628. new_dashlet_parameters: {operation: 'new_dashlet'}
  629. });
  630. dashboard_prop_size = GetUserPreference('dashboard_prop_size', 350);
  631. $('#dashboard_editor').layout({
  632. east: {
  633. minSize: 200,
  634. size: dashboard_prop_size,
  635. togglerLength_open: 0,
  636. togglerLength_closed: 0,
  637. onresize_end: function(name, elt, state, options, layout)
  638. {
  639. if (state.isSliding == false)
  640. {
  641. SetUserPreference('dashboard_prop_size', state.size, true);
  642. }
  643. },
  644. }
  645. });
  646. window.onbeforeunload = function() {
  647. if (!window.bLeavingOnUserAction)
  648. {
  649. var oDashboard = $('.itop-dashboard').data('itopRuntimedashboard');
  650. if (oDashboard)
  651. {
  652. if (oDashboard.is_dirty())
  653. {
  654. return '$sExitConfirmationMessage';
  655. }
  656. if (oDashboard.is_modified())
  657. {
  658. return '$sExitConfirmationMessage';
  659. }
  660. }
  661. }
  662. // return nothing ! safer for IE
  663. };
  664. EOF
  665. );
  666. $oPage->add_ready_script("");
  667. }
  668. public static function GetDashletCreationForm($sOQL = null)
  669. {
  670. $oForm = new DesignerForm();
  671. // Get the list of all 'dashboard' menus in which we can insert a dashlet
  672. $aAllMenus = ApplicationMenu::ReflectionMenuNodes();
  673. $aAllowedDashboards = array();
  674. foreach($aAllMenus as $idx => $aMenu)
  675. {
  676. $oMenu = $aMenu['node'];
  677. $sParentId = $aMenu['parent'];
  678. if ($oMenu instanceof DashboardMenuNode)
  679. {
  680. $sMenuLabel = $oMenu->GetTitle();
  681. $sParentLabel = Dict::S('Menu:'.$sParentId);
  682. if ($sParentLabel != $sMenuLabel)
  683. {
  684. $aAllowedDashboards[$oMenu->GetMenuId()] = $sParentLabel.' - '.$sMenuLabel;
  685. }
  686. else
  687. {
  688. $aAllowedDashboards[$oMenu->GetMenuId()] = $sMenuLabel;
  689. }
  690. }
  691. }
  692. asort($aAllowedDashboards);
  693. $aKeys = array_keys($aAllowedDashboards); // Select the first one by default
  694. $sDefaultDashboard = $aKeys[0];
  695. $oField = new DesignerComboField('menu_id', Dict::S('UI:DashletCreation:Dashboard'), $sDefaultDashboard);
  696. $oField->SetAllowedValues($aAllowedDashboards);
  697. $oField->SetMandatory(true);
  698. $oForm->AddField($oField);
  699. // Get the list of possible dashlets that support a creation from
  700. // an OQL
  701. $aDashlets = array();
  702. foreach(get_declared_classes() as $sDashletClass)
  703. {
  704. if (is_subclass_of($sDashletClass, 'Dashlet'))
  705. {
  706. $oReflection = new ReflectionClass($sDashletClass);
  707. if (!$oReflection->isAbstract())
  708. {
  709. $aCallSpec = array($sDashletClass, 'CanCreateFromOQL');
  710. $bShorcutMode = call_user_func($aCallSpec);
  711. if ($bShorcutMode)
  712. {
  713. $aCallSpec = array($sDashletClass, 'GetInfo');
  714. $aInfo = call_user_func($aCallSpec);
  715. $aDashlets[$sDashletClass] = array('label' => $aInfo['label'], 'class' => $sDashletClass, 'icon' => $aInfo['icon']);
  716. }
  717. }
  718. }
  719. }
  720. $oSelectorField = new DesignerFormSelectorField('dashlet_class', Dict::S('UI:DashletCreation:DashletType'), '');
  721. $oForm->AddField($oSelectorField);
  722. foreach($aDashlets as $sDashletClass => $aDashletInfo)
  723. {
  724. $oSubForm = new DesignerForm();
  725. $oMetaModel = new ModelReflectionRuntime();
  726. $oDashlet = new $sDashletClass($oMetaModel, 0);
  727. $oDashlet->GetPropertiesFieldsFromOQL($oSubForm, $sOQL);
  728. $oSelectorField->AddSubForm($oSubForm, $aDashletInfo['label'], $aDashletInfo['class']);
  729. }
  730. $oField = new DesignerBooleanField('open_editor', Dict::S('UI:DashletCreation:EditNow'), true);
  731. $oForm->AddField($oField);
  732. return $oForm;
  733. }
  734. public static function GetDashletCreationDlgFromOQL($oPage, $sOQL)
  735. {
  736. $oPage->add('<div id="dashlet_creation_dlg">');
  737. $oForm = self::GetDashletCreationForm($sOQL);
  738. $oForm->Render($oPage);
  739. $oPage->add('</div>');
  740. $sDialogTitle = Dict::S('UI:DashletCreation:Title');
  741. $sOkButtonLabel = Dict::S('UI:Button:Ok');
  742. $sCancelButtonLabel = Dict::S('UI:Button:Cancel');
  743. $oPage->add_ready_script(
  744. <<<EOF
  745. $('#dashlet_creation_dlg').dialog({
  746. width: 400,
  747. modal: true,
  748. title: '$sDialogTitle',
  749. buttons: [
  750. { text: "$sOkButtonLabel", click: function() {
  751. var oForm = $(this).find('form');
  752. var sFormId = oForm.attr('id');
  753. var oParams = null;
  754. var aErrors = ValidateForm(sFormId, false);
  755. if (aErrors.length == 0)
  756. {
  757. oParams = ReadFormParams(sFormId);
  758. }
  759. oParams.operation = 'add_dashlet';
  760. var me = $(this);
  761. $.post(GetAbsoluteUrlAppRoot()+'pages/ajax.render.php', oParams, function(data) {
  762. me.dialog( "close" );
  763. me.remove();
  764. $('body').append(data);
  765. });
  766. } },
  767. { text: "$sCancelButtonLabel", click: function() {
  768. $(this).dialog( "close" ); $(this).remove();
  769. } },
  770. ],
  771. close: function() { $(this).remove(); }
  772. });
  773. EOF
  774. );
  775. }
  776. }