dashboard.class.inc.php 23 KB

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