ajax.render.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. /**
  19. * Handles various ajax requests
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once('../approot.inc.php');
  25. require_once(APPROOT.'/application/application.inc.php');
  26. require_once(APPROOT.'/application/webpage.class.inc.php');
  27. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  28. require_once(APPROOT.'/application/wizardhelper.class.inc.php');
  29. require_once(APPROOT.'/application/ui.linkswidget.class.inc.php');
  30. require_once(APPROOT.'/application/ui.extkeywidget.class.inc.php');
  31. require_once(APPROOT.'/application/datatable.class.inc.php');
  32. try
  33. {
  34. require_once(APPROOT.'/application/startup.inc.php');
  35. require_once(APPROOT.'/application/user.preferences.class.inc.php');
  36. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  37. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  38. $oPage = new ajax_page("");
  39. $oPage->no_cache();
  40. $operation = utils::ReadParam('operation', '');
  41. $sFilter = stripslashes(utils::ReadParam('filter', '', false, 'raw_data'));
  42. $sEncoding = utils::ReadParam('encoding', 'serialize');
  43. $sClass = utils::ReadParam('class', 'MissingAjaxParam', false, 'class');
  44. $sStyle = utils::ReadParam('style', 'list');
  45. switch($operation)
  46. {
  47. case 'datatable':
  48. case 'pagination':
  49. $oPage->SetContentType('text/html');
  50. $extraParams = utils::ReadParam('extra_param', '', false, 'raw_data');
  51. if (is_array($extraParams))
  52. {
  53. $aExtraParams = $extraParams;
  54. }
  55. else
  56. {
  57. $sExtraParams = stripslashes($extraParams);
  58. $aExtraParams = array();
  59. if (!empty($sExtraParams))
  60. {
  61. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  62. }
  63. }
  64. if ($sEncoding == 'oql')
  65. {
  66. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  67. }
  68. else
  69. {
  70. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  71. }
  72. $iStart = utils::ReadParam('start',0);
  73. $iEnd = utils::ReadParam('end',1);
  74. $iSortCol = utils::ReadParam('sort_col','null');
  75. $sSelectMode = utils::ReadParam('select_mode', '');
  76. if (!empty($sSelectMode) && ($sSelectMode != 'none'))
  77. {
  78. // The first column is used for the selection (radio / checkbox) and is not sortable
  79. $iSortCol--;
  80. }
  81. $bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
  82. $aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
  83. $aClassAliases = utils::ReadParam('class_aliases', array());
  84. $iListId = utils::ReadParam('list_id', 0);
  85. //$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, 'list'));
  86. // Filter the list to removed linked set since we are not able to display them here
  87. $aOrderBy = array();
  88. $iSortIndex = 0;
  89. $aColumnsLoad = array();
  90. foreach($aClassAliases as $sAlias => $sClassName)
  91. {
  92. $aColumnsLoad[$sAlias] = array();
  93. foreach($aColumns[$sAlias] as $sAttCode => $aData)
  94. {
  95. if ($aData['checked'] == 'true')
  96. {
  97. $aColumns[$sAlias][$sAttCode]['checked'] = true;
  98. if ($sAttCode == '_key_')
  99. {
  100. if ($iSortCol == $iSortIndex)
  101. {
  102. if (!MetaModel::HasChildrenClasses($oFilter->GetClass()))
  103. {
  104. $aNameSpec = MetaModel::GetNameSpec($oFilter->GetClass());
  105. if ($aNameSpec[0] == '%1$s')
  106. {
  107. // The name is made of a single column, let's sort according to the sort algorithm for this column
  108. $aOrderBy[$aNameSpec[1][0]] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  109. }
  110. else
  111. {
  112. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  113. }
  114. }
  115. else
  116. {
  117. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  118. }
  119. }
  120. }
  121. else
  122. {
  123. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  124. if ($oAttDef instanceof AttributeLinkedSet)
  125. {
  126. // Removed from the display list
  127. unset($aColumns[$sAlias][$sAttCode]);
  128. }
  129. else
  130. {
  131. $aColumnsLoad[$sAlias][] = $sAttCode;
  132. }
  133. if ($iSortCol == $iSortIndex)
  134. {
  135. if ($oAttDef->IsExternalKey())
  136. {
  137. $sSortCol = $sAttCode.'_friendlyname';
  138. }
  139. else
  140. {
  141. $sSortCol = $sAttCode;
  142. }
  143. $aOrderBy[$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  144. }
  145. }
  146. $iSortIndex++;
  147. }
  148. else
  149. {
  150. $aColumns[$sAlias][$sAttCode]['checked'] = false;
  151. }
  152. }
  153. }
  154. // Load only the requested columns
  155. $oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart);
  156. $oSet->OptimizeColumnLoad($aColumnsLoad);
  157. $oDataTable = new DataTable($iListId, $oSet, $oSet->GetSelectedClasses());
  158. if ($operation == 'datatable')
  159. {
  160. // Redraw the whole table
  161. $sHtml = $oDataTable->UpdatePager($oPage, $iEnd-$iStart, $iStart); // Set the default page size
  162. $sHtml .= $oDataTable->GetHTMLTable($oPage, $aColumns, $sSelectMode, $iEnd-$iStart, $bDisplayKey, $aExtraParams);
  163. }
  164. else
  165. {
  166. // redraw just the needed rows
  167. $sHtml = $oDataTable->GetAsHTMLTableRows($oPage, $iEnd-$iStart, $aColumns, $sSelectMode, $bDisplayKey, $aExtraParams);
  168. }
  169. $oPage->add($sHtml);
  170. break;
  171. case 'datatable_save_settings':
  172. $oPage->SetContentType('text/plain');
  173. $iPageSize = utils::ReadParam('page_size', 10);
  174. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  175. $bSaveAsDefaults = (utils::ReadParam('defaults', 'true') == 'true');
  176. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  177. $aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
  178. foreach($aColumns as $sAlias => $aList)
  179. {
  180. foreach($aList as $sAttCode => $aData)
  181. {
  182. $aColumns[$sAlias][$sAttCode]['checked'] = ($aData['checked'] == 'true');
  183. $aColumns[$sAlias][$sAttCode]['disabled'] = ($aData['disabled'] == 'true');
  184. $aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']);
  185. }
  186. }
  187. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  188. $oSettings->iDefaultPageSize = $iPageSize;
  189. $oSettings->aColumns = $aColumns;
  190. if ($bSaveAsDefaults)
  191. {
  192. $bRet = $oSettings->SaveAsDefault();
  193. }
  194. else
  195. {
  196. $bRet = $oSettings->Save();
  197. }
  198. $oPage->add($bRet ? 'Ok' : 'KO');
  199. break;
  200. case 'datatable_reset_settings':
  201. $oPage->SetContentType('text/plain');
  202. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  203. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  204. $bResetAll = (utils::ReadParam('defaults', 'true') == 'true');
  205. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  206. $bRet = $oSettings->ResetToDefault($bResetAll);
  207. $oPage->add($bRet ? 'Ok' : 'KO');
  208. break;
  209. // ui.linkswidget
  210. case 'addObjects':
  211. $oPage->SetContentType('text/html');
  212. $sAttCode = utils::ReadParam('sAttCode', '');
  213. $iInputId = utils::ReadParam('iInputId', '');
  214. $sSuffix = utils::ReadParam('sSuffix', '');
  215. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  216. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  217. if (!empty($sJson))
  218. {
  219. $oWizardHelper = WizardHelper::FromJSON($sJson);
  220. $oObj = $oWizardHelper->GetTargetObject();
  221. }
  222. else
  223. {
  224. // Search form: no current object
  225. $oObj = null;
  226. }
  227. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  228. $oWidget->GetObjectPickerDialog($oPage, $oObj);
  229. break;
  230. // ui.linkswidget
  231. case 'searchObjectsToAdd':
  232. $oPage->SetContentType('text/html');
  233. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  234. $sAttCode = utils::ReadParam('sAttCode', '');
  235. $iInputId = utils::ReadParam('iInputId', '');
  236. $sSuffix = utils::ReadParam('sSuffix', '');
  237. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  238. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  239. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  240. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  241. break;
  242. //ui.linksdirectwidget
  243. case 'createObject':
  244. $oPage->SetContentType('text/html');
  245. $sClass = utils::ReadParam('class', '', false, 'class');
  246. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  247. $sAttCode = utils::ReadParam('att_code', '');
  248. $iInputId = utils::ReadParam('iInputId', '');
  249. $oPage->SetContentType('text/html');
  250. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  251. $oWidget->GetObjectCreationDlg($oPage, $sRealClass);
  252. break;
  253. // ui.linksdirectwidget
  254. case 'getLinksetRow':
  255. $oPage->SetContentType('text/html');
  256. $sClass = utils::ReadParam('class', '', false, 'class');
  257. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  258. $sAttCode = utils::ReadParam('att_code', '');
  259. $iInputId = utils::ReadParam('iInputId', '');
  260. $iTempId = utils::ReadParam('tempId', '');
  261. $aValues = utils::ReadParam('values', array(), false, 'raw_data');
  262. $oPage->SetContentType('text/html');
  263. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  264. $oPage->add($oWidget->GetRow($oPage, $sRealClass, $aValues, -$iTempId));
  265. break;
  266. // ui.linksdirectwidget
  267. case 'selectObjectsToAdd':
  268. $oPage->SetContentType('text/html');
  269. $sClass = utils::ReadParam('class', '', false, 'class');
  270. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  271. $oObj = null;
  272. if ($sJson != '')
  273. {
  274. $oWizardHelper = WizardHelper::FromJSON($sJson);
  275. $oObj = $oWizardHelper->GetTargetObject();
  276. }
  277. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  278. $sAttCode = utils::ReadParam('att_code', '');
  279. $iInputId = utils::ReadParam('iInputId', '');
  280. $iCurrObjectId = utils::ReadParam('iObjId', 0);
  281. $oPage->SetContentType('text/html');
  282. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  283. $oWidget->GetObjectsSelectionDlg($oPage, $oObj);
  284. break;
  285. // ui.linksdirectwidget
  286. case 'searchObjectsToAdd2':
  287. $oPage->SetContentType('text/html');
  288. $sClass = utils::ReadParam('class', '', false, 'class');
  289. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  290. $sAttCode = utils::ReadParam('att_code', '');
  291. $iInputId = utils::ReadParam('iInputId', '');
  292. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  293. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  294. $oObj = null;
  295. if ($sJson != '')
  296. {
  297. $oWizardHelper = WizardHelper::FromJSON($sJson);
  298. $oObj = $oWizardHelper->GetTargetObject();
  299. }
  300. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  301. $oWidget->SearchObjectsToAdd($oPage, $sRealClass, $aAlreadyLinked, $oObj);
  302. break;
  303. // ui.linksdirectwidget
  304. case 'doAddObjects2':
  305. $oPage->SetContentType('text/html');
  306. $oPage->SetContentType('text/html');
  307. $sClass = utils::ReadParam('class', '', false, 'class');
  308. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  309. $sAttCode = utils::ReadParam('att_code', '');
  310. $iInputId = utils::ReadParam('iInputId', '');
  311. $iCurrObjectId = utils::ReadParam('iObjId', 0);
  312. $sFilter = utils::ReadParam('filter', '');
  313. if ($sFilter != '')
  314. {
  315. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  316. }
  317. else
  318. {
  319. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  320. }
  321. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  322. $oWidget->DoAddObjects($oPage, $oFullSetFilter);
  323. break;
  324. ////////////////////////////////////////////////////////////
  325. // ui.extkeywidget
  326. case 'searchObjectsToSelect':
  327. $oPage->SetContentType('text/html');
  328. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  329. $iInputId = utils::ReadParam('iInputId', '');
  330. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  331. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  332. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  333. $sAttCode = utils::ReadParam('sAttCode', '');
  334. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  335. if (!empty($sJson))
  336. {
  337. $oWizardHelper = WizardHelper::FromJSON($sJson);
  338. $oObj = $oWizardHelper->GetTargetObject();
  339. }
  340. else
  341. {
  342. // Search form: no current object
  343. $oObj = null;
  344. }
  345. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  346. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  347. break;
  348. // ui.extkeywidget: autocomplete
  349. case 'ac_extkey':
  350. $oPage->SetContentType('text/plain');
  351. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  352. $iInputId = utils::ReadParam('iInputId', '');
  353. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  354. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  355. $sContains = utils::ReadParam('q', '', false, 'raw_data');
  356. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  357. if ($sContains !='')
  358. {
  359. if (!empty($sJson))
  360. {
  361. $oWizardHelper = WizardHelper::FromJSON($sJson);
  362. $oObj = $oWizardHelper->GetTargetObject();
  363. }
  364. else
  365. {
  366. // Search form: no current object
  367. $oObj = null;
  368. }
  369. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  370. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  371. }
  372. break;
  373. // ui.extkeywidget
  374. case 'objectSearchForm':
  375. $oPage->SetContentType('text/html');
  376. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  377. $iInputId = utils::ReadParam('iInputId', '');
  378. $sTitle = utils::ReadParam('sTitle', '', false, 'raw_data');
  379. $sAttCode = utils::ReadParam('sAttCode', '');
  380. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  381. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  382. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  383. if (!empty($sJson))
  384. {
  385. $oWizardHelper = WizardHelper::FromJSON($sJson);
  386. $oObj = $oWizardHelper->GetTargetObject();
  387. }
  388. else
  389. {
  390. // Search form: no current object
  391. $oObj = null;
  392. }
  393. $oWidget->GetSearchDialog($oPage, $sTitle, $oObj);
  394. break;
  395. // ui.extkeywidget
  396. case 'objectCreationForm':
  397. $oPage->SetContentType('text/html');
  398. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  399. $iInputId = utils::ReadParam('iInputId', '');
  400. $sAttCode = utils::ReadParam('sAttCode', '');
  401. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  402. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  403. if (!empty($sJson))
  404. {
  405. $oWizardHelper = WizardHelper::FromJSON($sJson);
  406. $oObj = $oWizardHelper->GetTargetObject();
  407. }
  408. else
  409. {
  410. // Search form: no current object
  411. $oObj = null;
  412. }
  413. $oWidget->GetObjectCreationForm($oPage, $oObj);
  414. break;
  415. // ui.extkeywidget
  416. case 'doCreateObject':
  417. $oPage->SetContentType('application/json');
  418. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  419. $iInputId = utils::ReadParam('iInputId', '');
  420. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  421. $sAttCode = utils::ReadParam('sAttCode', '');
  422. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  423. $aResult = $oWidget->DoCreateObject($oPage);
  424. echo json_encode($aResult);
  425. break;
  426. // ui.extkeywidget
  427. case 'getObjectName':
  428. $oPage->SetContentType('application/json');
  429. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  430. $iInputId = utils::ReadParam('iInputId', '');
  431. $iObjectId = utils::ReadParam('iObjectId', '');
  432. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  433. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  434. $sName = $oWidget->GetObjectName($iObjectId);
  435. echo json_encode(array('name' => $sName));
  436. break;
  437. // ui.extkeywidget
  438. case 'displayHierarchy':
  439. $oPage->SetContentType('text/html');
  440. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  441. $sInputId = utils::ReadParam('sInputId', '');
  442. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  443. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  444. $currValue = utils::ReadParam('value', '');
  445. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  446. if (!empty($sJson))
  447. {
  448. $oWizardHelper = WizardHelper::FromJSON($sJson);
  449. $oObj = $oWizardHelper->GetTargetObject();
  450. }
  451. else
  452. {
  453. // Search form: no current object
  454. $oObj = null;
  455. }
  456. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId, '', $bSearchMode);
  457. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  458. break;
  459. ////////////////////////////////////////////////////
  460. // ui.linkswidget
  461. case 'doAddObjects':
  462. $oPage->SetContentType('text/html');
  463. $sAttCode = utils::ReadParam('sAttCode', '');
  464. $iInputId = utils::ReadParam('iInputId', '');
  465. $sSuffix = utils::ReadParam('sSuffix', '');
  466. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
  467. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  468. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  469. $oWizardHelper = WizardHelper::FromJSON($sJson);
  470. $oObj = $oWizardHelper->GetTargetObject();
  471. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  472. if ($sFilter != '')
  473. {
  474. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  475. }
  476. else
  477. {
  478. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  479. }
  480. $oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);
  481. break;
  482. ////////////////////////////////////////////////////////////
  483. case 'wizard_helper_preview':
  484. $oPage->SetContentType('text/html');
  485. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  486. $oWizardHelper = WizardHelper::FromJSON($sJson);
  487. $oObj = $oWizardHelper->GetTargetObject();
  488. $oObj->DisplayBareProperties($oPage);
  489. break;
  490. case 'wizard_helper':
  491. $oPage->SetContentType('text/html');
  492. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  493. $oWizardHelper = WizardHelper::FromJSON($sJson);
  494. $oObj = $oWizardHelper->GetTargetObject();
  495. $sClass = $oWizardHelper->GetTargetClass();
  496. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  497. {
  498. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  499. $defaultValue = $oAttDef->GetDefaultValue();
  500. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  501. $oObj->Set($sAttCode, $defaultValue);
  502. }
  503. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  504. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  505. {
  506. $sId = $oWizardHelper->GetIdForField($sAttCode);
  507. if ($sId != '')
  508. {
  509. if ($oObj->IsNew())
  510. {
  511. $iFlags = $oObj->GetInitialStateAttributeFlags($sAttCode);
  512. }
  513. else
  514. {
  515. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  516. }
  517. if ($iFlags & OPT_ATT_READONLY)
  518. {
  519. $sHTMLValue = "<span id=\"field_{$sId}\">".$oObj->GetAsHTML($sAttCode);
  520. $sHTMLValue .= '<input type="hidden" id="'.$sId.'" name="attr_'.$sFormPrefix.$sAttCode.'" value="'.htmlentities($oObj->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/></span>';
  521. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  522. }
  523. else
  524. {
  525. // It may happen that the field we'd like to update does not
  526. // exist in the form. For example, if the field should be hidden/read-only
  527. // in the current state of the object
  528. $value = $oObj->Get($sAttCode);
  529. $displayValue = $oObj->GetEditValue($sAttCode);
  530. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  531. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  532. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  533. // Make sure that we immediately validate the field when we reload it
  534. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  535. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  536. }
  537. }
  538. }
  539. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  540. break;
  541. case 'obj_creation_form':
  542. $oPage->SetContentType('text/html');
  543. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  544. $oWizardHelper = WizardHelper::FromJSON($sJson);
  545. $oObj = $oWizardHelper->GetTargetObject();
  546. $sClass = $oWizardHelper->GetTargetClass();
  547. $sTargetState = utils::ReadParam('target_state', '');
  548. $iTransactionId = utils::ReadParam('transaction_id', '');
  549. $oObj->Set(MetaModel::GetStateAttributeCode($sClass), $sTargetState);
  550. cmdbAbstractObject::DisplayCreationForm($oPage, $sClass, $oObj, array(), array('action' => utils::GetAbsoluteUrlAppRoot().'pages/UI.php', 'transaction_id' => $iTransactionId));
  551. break;
  552. // DisplayBlock
  553. case 'ajax':
  554. $oPage->SetContentType('text/html');
  555. if ($sFilter != "")
  556. {
  557. $sExtraParams = stripslashes(utils::ReadParam('extra_params', '', false, 'raw_data'));
  558. $aExtraParams = array();
  559. if (!empty($sExtraParams))
  560. {
  561. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  562. }
  563. // Restore the app context from the ExtraParams
  564. $oAppContext = new ApplicationContext(false); // false => don't read the context yet !
  565. $aContext = array();
  566. foreach($oAppContext->GetNames() as $sName)
  567. {
  568. $sParamName = 'c['.$sName.']';
  569. if (isset($aExtraParams[$sParamName]))
  570. {
  571. $aContext[$sName] = $aExtraParams[$sParamName];
  572. }
  573. }
  574. $_REQUEST['c'] = $aContext;
  575. if ($sEncoding == 'oql')
  576. {
  577. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  578. }
  579. else
  580. {
  581. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  582. }
  583. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  584. $aExtraParams['display_limit'] = true;
  585. $aExtraParams['truncated'] = true;
  586. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  587. }
  588. else
  589. {
  590. $oPage->p("Invalid query (empty filter).");
  591. }
  592. break;
  593. case 'displayCSVHistory':
  594. $oPage->SetContentType('text/html');
  595. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  596. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  597. break;
  598. case 'details':
  599. $oPage->SetContentType('text/html');
  600. $key = utils::ReadParam('id', 0);
  601. $oFilter = new DBObjectSearch($sClass);
  602. $oFilter->AddCondition('id', $key, '=');
  603. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  604. $oDisplayBlock->RenderContent($oPage);
  605. break;
  606. case 'pie_chart':
  607. $oPage->SetContentType('application/json');
  608. $sGroupBy = utils::ReadParam('group_by', '');
  609. if ($sFilter != '')
  610. {
  611. if ($sEncoding == 'oql')
  612. {
  613. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  614. }
  615. else
  616. {
  617. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  618. }
  619. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  620. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  621. }
  622. else
  623. {
  624. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  625. }
  626. break;
  627. case 'open_flash_chart':
  628. // Workaround for IE8 + IIS + HTTPS
  629. // See TRAC #363, fix described here: http://forums.codecharge.com/posts.php?post_id=97771
  630. $oPage->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
  631. $oPage->add_header("Cache-Control: cache, must-revalidate");
  632. $oPage->add_header("Pragma: public");
  633. $oPage->SetContentType('application/json');
  634. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  635. if ($sFilter != '')
  636. {
  637. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  638. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  639. $oDisplayBlock->RenderContent($oPage, $aParams);
  640. }
  641. else
  642. {
  643. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  644. }
  645. break;
  646. case 'modal_details':
  647. $oPage->SetContentType('text/html');
  648. $key = utils::ReadParam('id', 0);
  649. $oFilter = new DBObjectSearch($sClass);
  650. $oFilter->AddCondition('id', $key, '=');
  651. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  652. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  653. $oDisplayBlock->RenderContent($oPage);
  654. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  655. break;
  656. case 'link':
  657. $oPage->SetContentType('text/html');
  658. $sClass = utils::ReadParam('sclass', 'logInfra', false, 'class');
  659. $sAttCode = utils::ReadParam('attCode', 'name');
  660. //$sOrg = utils::ReadParam('org_id', '');
  661. $sName = utils::ReadParam('q', '');
  662. $iMaxCount = utils::ReadParam('max', 30);
  663. $iCount = 0;
  664. $oFilter = new DBObjectSearch($sClass);
  665. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  666. //$oFilter->AddCondition('org_id', $sOrg, '=');
  667. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  668. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  669. {
  670. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  671. $iCount++;
  672. }
  673. break;
  674. case 'combo_options':
  675. $oPage->SetContentType('text/html');
  676. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  677. $oSet = new CMDBObjectSet($oFilter);
  678. while( $oObj = $oSet->fetch())
  679. {
  680. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  681. }
  682. break;
  683. case 'display_document':
  684. $id = utils::ReadParam('id', '');
  685. $sField = utils::ReadParam('field', '');
  686. if (!empty($sClass) && !empty($id) && !empty($sField))
  687. {
  688. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  689. }
  690. break;
  691. case 'download_document':
  692. $id = utils::ReadParam('id', '');
  693. $sField = utils::ReadParam('field', '');
  694. if (!empty($sClass) && !empty($id) && !empty($sField))
  695. {
  696. DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  697. }
  698. break;
  699. case 'search_form':
  700. $oPage->SetContentType('text/html');
  701. $sClass = utils::ReadParam('className', '', false, 'class');
  702. $sRootClass = utils::ReadParam('baseClass', '', false, 'class');
  703. $currentId = utils::ReadParam('currentId', '');
  704. $sTableId = utils::ReadParam('_table_id_', null, false, 'raw_data');
  705. $sAction = utils::ReadParam('action', '');
  706. $oFilter = new DBObjectSearch($sClass);
  707. $oSet = new CMDBObjectSet($oFilter);
  708. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction, 'table_id' => $sTableId));
  709. $oPage->add($sHtml);
  710. break;
  711. case 'set_pref':
  712. $sCode = utils::ReadPostedParam('code', '');
  713. $sValue = utils::ReadPostedParam('value', '', 'raw_data');
  714. appUserPreferences::SetPref($sCode, $sValue);
  715. break;
  716. case 'erase_all_pref':
  717. // Can be useful in case a user got some corrupted prefs...
  718. appUserPreferences::ClearPreferences();
  719. break;
  720. case 'on_form_cancel':
  721. // Called when a creation/modification form is cancelled by the end-user
  722. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  723. $iTransactionId = utils::ReadParam('transaction_id', 0);
  724. $sTempId = session_id().'_'.$iTransactionId;
  725. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  726. {
  727. $oExtensionInstance->OnFormCancel($sTempId);
  728. }
  729. break;
  730. case 'dashboard_editor':
  731. $sId = utils::ReadParam('id', '', false, 'raw_data');
  732. ApplicationMenu::LoadAdditionalMenus();
  733. $idx = ApplicationMenu::GetMenuIndexById($sId);
  734. $oMenu = ApplicationMenu::GetMenuNode($idx);
  735. $oMenu->RenderEditor($oPage);
  736. break;
  737. case 'new_dashlet':
  738. require_once(APPROOT.'application/forms.class.inc.php');
  739. require_once(APPROOT.'application/dashlet.class.inc.php');
  740. $sDashletClass = utils::ReadParam('dashlet_class', '');
  741. $sDashletId = utils::ReadParam('dashlet_id', '', false, 'raw_data');
  742. if (is_subclass_of($sDashletClass, 'Dashlet'))
  743. {
  744. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  745. $offset = $oPage->start_capture();
  746. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  747. $sHtml = addslashes($oPage->end_capture($offset));
  748. $sHtml = str_replace("\n", '', $sHtml);
  749. $sHtml = str_replace("\r", '', $sHtml);
  750. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  751. // but is executed BEFORE all 'ready_scripts'
  752. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  753. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  754. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  755. $sHtml = str_replace("\n", '', $sHtml);
  756. $sHtml = str_replace("\r", '', $sHtml);
  757. $oPage->add_script("$('#dashlet_properties_$sDashletId').html('$sHtml')"); // in ajax web page add_script has the same effect as add_ready_script // but is executed BEFORE all 'ready_scripts'
  758. }
  759. break;
  760. case 'update_dashlet_property':
  761. require_once(APPROOT.'application/forms.class.inc.php');
  762. require_once(APPROOT.'application/dashlet.class.inc.php');
  763. $aParams = utils::ReadParam('params', '', false, 'raw_data');
  764. $sDashletClass = $aParams['attr_dashlet_class'];
  765. $sDashletId = $aParams['attr_dashlet_id'];
  766. $aUpdatedProperties = $aParams['updated']; // Code of the changed properties as an array: 'attr_xxx', 'attr_xxy', etc...
  767. $aPreviousValues = $aParams['previous_values']; // hash array: 'attr_xxx' => 'old_value'
  768. if (is_subclass_of($sDashletClass, 'Dashlet'))
  769. {
  770. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  771. $oForm = $oDashlet->GetForm();
  772. $aValues = $oForm->ReadParams(); // hash array: 'xxx' => 'new_value'
  773. $aCurrentValues = $aValues;
  774. $aUpdatedDecoded = array();
  775. foreach($aUpdatedProperties as $sProp)
  776. {
  777. $sDecodedProp = str_replace('attr_', '', $sProp); // Remove the attr_ prefix
  778. $aCurrentValues[$sDecodedProp] = $aPreviousValues[$sProp]; // Set the previous value
  779. $aUpdatedDecoded[] = $sDecodedProp;
  780. }
  781. $oDashlet->FromParams($aCurrentValues);
  782. $sPrevClass = get_class($oDashlet);
  783. $oDashlet = $oDashlet->Update($aValues, $aUpdatedDecoded);
  784. $sNewClass = get_class($oDashlet);
  785. if ($sNewClass != $sPrevClass)
  786. {
  787. $oPage->add_ready_script("$('#dashlet_$sDashletId').dashlet('option', {dashlet_class: '$sNewClass'});");
  788. }
  789. if ($oDashlet->IsRedrawNeeded())
  790. {
  791. $offset = $oPage->start_capture();
  792. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  793. $sHtml = addslashes($oPage->end_capture($offset));
  794. $sHtml = str_replace("\n", '', $sHtml);
  795. $sHtml = str_replace("\r", '', $sHtml);
  796. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  797. // but is executed BEFORE all 'ready_scripts'
  798. }
  799. if ($oDashlet->IsFormRedrawNeeded())
  800. {
  801. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  802. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  803. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  804. $sHtml = str_replace("\n", '', $sHtml);
  805. $sHtml = str_replace("\r", '', $sHtml);
  806. $oPage->add_script("$('#dashlet_properties_$sDashletId').html('$sHtml')"); // in ajax web page add_script has the same effect as add_ready_script // but is executed BEFORE all 'ready_scripts'
  807. // but is executed BEFORE all 'ready_scripts'
  808. }
  809. }
  810. break;
  811. case 'save_dashboard':
  812. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  813. $aParams = array();
  814. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  815. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  816. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  817. $oDashboard = new RuntimeDashboard($sDashboardId);
  818. $oDashboard->FromParams($aParams);
  819. $oDashboard->Save();
  820. // trigger a reload of the current page since the dashboard just changed
  821. $oPage->add_ready_script(
  822. <<<EOF
  823. var sLocation = new String(window.location.href);
  824. var sNewLocation = sLocation.replace('&edit=1', '');
  825. sNewLocation = sLocation.replace(/#(.?)$/, ''); // Strips everything after the hash, since IF the URL does not change AND contains a hash, then Chrome does not reload the page
  826. window.location.href = sNewLocation;
  827. EOF
  828. );
  829. $oPage->add_ready_script("sLocation = new String(window.location.href); window.location.href=sLocation.replace('&edit=1', '');"); // reloads the page, doing a GET even if we arrived via a POST
  830. break;
  831. case 'revert_dashboard':
  832. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  833. $oDashboard = new RuntimeDashboard($sDashboardId);
  834. $oDashboard->Revert();
  835. // trigger a reload of the current page since the dashboard just changed
  836. $oPage->add_ready_script("window.location.href=window.location.href;"); // reloads the page, doing a GET even if we arrived via a POST
  837. break;
  838. case 'render_dashboard':
  839. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  840. $aParams = array();
  841. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  842. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  843. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  844. $oDashboard = new RuntimeDashboard($sDashboardId);
  845. $oDashboard->FromParams($aParams);
  846. $oDashboard->Render($oPage, true /* bEditMode */);
  847. break;
  848. case 'dashlet_creation_dlg':
  849. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  850. RuntimeDashboard::GetDashletCreationDlgFromOQL($oPage, $sOQL);
  851. break;
  852. case 'add_dashlet':
  853. $oForm = RuntimeDashboard::GetDashletCreationForm();
  854. $aValues = $oForm->ReadParams();
  855. $sDashletClass = $aValues['dashlet_class'];
  856. $sMenuId = $aValues['menu_id'];
  857. if (is_subclass_of($sDashletClass, 'Dashlet'))
  858. {
  859. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), 0);
  860. $oDashlet->FromParams($aValues);
  861. ApplicationMenu::LoadAdditionalMenus();
  862. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  863. $oMenu = ApplicationMenu::GetMenuNode($index);
  864. $oMenu->AddDashlet($oDashlet);
  865. // navigate to the dashboard page
  866. if ($aValues['open_editor'])
  867. {
  868. $oPage->add_ready_script("window.location.href='".addslashes(utils::GetAbsoluteUrlAppRoot().'pages/UI.php?c[menu]='.urlencode($sMenuId))."&edit=1';"); // reloads the page, doing a GET even if we arrived via a POST
  869. }
  870. }
  871. break;
  872. case 'shortcut_list_dlg':
  873. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  874. ShortcutOQL::GetCreationDlgFromOQL($oPage, $sOQL);
  875. break;
  876. case 'shortcut_list_create':
  877. $oForm = ShortcutOQL::GetCreationForm();
  878. $aValues = $oForm->ReadParams();
  879. $oAppContext = new ApplicationContext();
  880. $aContext = $oAppContext->GetAsHash();
  881. $sContext = serialize($aContext);
  882. $oShortcut = MetaModel::NewObject("ShortcutOQL");
  883. $oShortcut->Set('user_id', UserRights::GetUserId());
  884. $oShortcut->Set("context", $sContext);
  885. $oShortcut->Set("name", $aValues['name']);
  886. $oShortcut->Set("oql", $aValues['oql']);
  887. $iAutoReload = (int)$aValues['auto_reload_sec'];
  888. if (($aValues['auto_reload']) && ($iAutoReload > 0))
  889. {
  890. $oShortcut->Set("auto_reload_sec", max(5, $iAutoReload));
  891. $oShortcut->Set("auto_reload", 'custom');
  892. }
  893. $iId = $oShortcut->DBInsertNoReload();
  894. // Add the menu node in the right place
  895. //
  896. // Mmmm... already done because the newly created menu is read from the DB
  897. // as soon as we invoke DisplayMenu
  898. // Refresh the menu pane
  899. $aExtraParams = array();
  900. ApplicationMenu::DisplayMenu($oPage, $aExtraParams);
  901. break;
  902. case 'shortcut_rename_dlg':
  903. $oSearch = new DBObjectSearch('Shortcut');
  904. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  905. $iShortcut = $aShortcuts[0];
  906. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  907. $oShortcut->StartRenameDialog($oPage);
  908. break;
  909. case 'shortcut_rename_go':
  910. $iShortcut = utils::ReadParam('id', 0);
  911. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  912. $sName = utils::ReadParam('attr_name', '', false, 'raw_data');
  913. if (strlen($sName) > 0)
  914. {
  915. $oShortcut->Set('name', $sName);
  916. $oShortcut->DBUpdate();
  917. $oPage->add_ready_script('window.location.reload();');
  918. }
  919. break;
  920. case 'shortcut_delete_go':
  921. $oSearch = new DBObjectSearch('Shortcut');
  922. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  923. foreach ($aShortcuts as $iShortcut)
  924. {
  925. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  926. $oShortcut->DBDelete();
  927. $oPage->add_ready_script('window.location.reload();');
  928. }
  929. break;
  930. case 'export_dashboard':
  931. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  932. ApplicationMenu::LoadAdditionalMenus();
  933. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  934. $oMenu = ApplicationMenu::GetMenuNode($index);
  935. if ($oMenu instanceof DashboardMenuNode)
  936. {
  937. $oDashboard = $oMenu->GetDashboard();
  938. $oPage->TrashUnexpectedOutput();
  939. $oPage->SetContentType('text/xml');
  940. $oPage->SetContentDisposition('attachment', $oMenu->GetLabel().'.xml');
  941. $oPage->add($oDashboard->ToXml());
  942. }
  943. break;
  944. case 'import_dashboard':
  945. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  946. ApplicationMenu::LoadAdditionalMenus();
  947. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  948. $oMenu = ApplicationMenu::GetMenuNode($index);
  949. $aResult = array('error' => '');
  950. try
  951. {
  952. if ($oMenu instanceof DashboardMenuNode)
  953. {
  954. $oDoc = utils::ReadPostedDocument('dashboard_upload_file');
  955. $oDashboard = $oMenu->GetDashboard();
  956. $oDashboard->FromXml($oDoc->GetData());
  957. $oDashboard->Save();
  958. }
  959. else
  960. {
  961. $aResult['error'] = 'Dashboard id="'.$sMenuId.'" not found.';
  962. }
  963. }
  964. catch(DOMException $e)
  965. {
  966. $aResult = array('error' => Dict::S('UI:Error:InvalidDashboardFile'));
  967. }
  968. catch(Exception $e)
  969. {
  970. $aResult = array('error' => $e->getMessage());
  971. }
  972. $oPage->add(json_encode($aResult));
  973. break;
  974. default:
  975. $oPage->p("Invalid query.");
  976. }
  977. $oPage->output();
  978. }
  979. catch (Exception $e)
  980. {
  981. // note: transform to cope with XSS attacks
  982. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  983. echo "<p>Debug trace: <pre>".$e->getTraceAsString()."</pre></p>\n";
  984. IssueLog::Error($e->getMessage());
  985. }
  986. /**
  987. * Downloads a document to the browser, either as 'inline' or 'attachment'
  988. *
  989. * @param WebPage $oPage The web page for the output
  990. * @param string $sClass Class name of the object
  991. * @param mixed $id Identifier of the object
  992. * @param string $sAttCode Name of the attribute containing the document to download
  993. * @param string $sContentDisposition Either 'inline' or 'attachment'
  994. * @return none
  995. */
  996. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  997. {
  998. try
  999. {
  1000. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  1001. if (!is_object($oObj))
  1002. {
  1003. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  1004. }
  1005. $oDocument = $oObj->Get($sAttCode);
  1006. if (is_object($oDocument))
  1007. {
  1008. $oPage->TrashUnexpectedOutput();
  1009. $oPage->SetContentType($oDocument->GetMimeType());
  1010. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  1011. $oPage->add($oDocument->GetData());
  1012. }
  1013. }
  1014. catch(Exception $e)
  1015. {
  1016. $oPage->p($e->getMessage());
  1017. }
  1018. }
  1019. ?>