dashboard.class.inc.php 22 KB

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