dashboard.class.inc.php 20 KB

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