dashboard.class.inc.php 20 KB

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