dashboard.class.inc.php 22 KB

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