dashboard.class.inc.php 20 KB

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