ajax.render.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. <?php
  2. // Copyright (C) 2010-2013 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 'reload_dashboard':
  731. $oPage->SetContentType('text/html');
  732. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  733. $aExtraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
  734. ApplicationMenu::LoadAdditionalMenus();
  735. $idx = ApplicationMenu::GetMenuIndexById($sDashboardId);
  736. $oMenu = ApplicationMenu::GetMenuNode($idx);
  737. $oDashboard = $oMenu->GetDashboard();
  738. $oDashboard->Render($oPage, false, $aExtraParams);
  739. break;
  740. case 'dashboard_editor':
  741. $sId = utils::ReadParam('id', '', false, 'raw_data');
  742. ApplicationMenu::LoadAdditionalMenus();
  743. $idx = ApplicationMenu::GetMenuIndexById($sId);
  744. $oMenu = ApplicationMenu::GetMenuNode($idx);
  745. $oMenu->RenderEditor($oPage);
  746. break;
  747. case 'new_dashlet':
  748. require_once(APPROOT.'application/forms.class.inc.php');
  749. require_once(APPROOT.'application/dashlet.class.inc.php');
  750. $sDashletClass = utils::ReadParam('dashlet_class', '');
  751. $sDashletId = utils::ReadParam('dashlet_id', '', false, 'raw_data');
  752. if (is_subclass_of($sDashletClass, 'Dashlet'))
  753. {
  754. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  755. $offset = $oPage->start_capture();
  756. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  757. $sHtml = addslashes($oPage->end_capture($offset));
  758. $sHtml = str_replace("\n", '', $sHtml);
  759. $sHtml = str_replace("\r", '', $sHtml);
  760. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  761. // but is executed BEFORE all 'ready_scripts'
  762. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  763. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  764. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  765. $sHtml = str_replace("\n", '', $sHtml);
  766. $sHtml = str_replace("\r", '', $sHtml);
  767. $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'
  768. }
  769. break;
  770. case 'update_dashlet_property':
  771. require_once(APPROOT.'application/forms.class.inc.php');
  772. require_once(APPROOT.'application/dashlet.class.inc.php');
  773. $aParams = utils::ReadParam('params', '', false, 'raw_data');
  774. $sDashletClass = $aParams['attr_dashlet_class'];
  775. $sDashletId = $aParams['attr_dashlet_id'];
  776. $aUpdatedProperties = $aParams['updated']; // Code of the changed properties as an array: 'attr_xxx', 'attr_xxy', etc...
  777. $aPreviousValues = $aParams['previous_values']; // hash array: 'attr_xxx' => 'old_value'
  778. if (is_subclass_of($sDashletClass, 'Dashlet'))
  779. {
  780. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  781. $oForm = $oDashlet->GetForm();
  782. $aValues = $oForm->ReadParams(); // hash array: 'xxx' => 'new_value'
  783. $aCurrentValues = $aValues;
  784. $aUpdatedDecoded = array();
  785. foreach($aUpdatedProperties as $sProp)
  786. {
  787. $sDecodedProp = str_replace('attr_', '', $sProp); // Remove the attr_ prefix
  788. $aCurrentValues[$sDecodedProp] = $aPreviousValues[$sProp]; // Set the previous value
  789. $aUpdatedDecoded[] = $sDecodedProp;
  790. }
  791. $oDashlet->FromParams($aCurrentValues);
  792. $sPrevClass = get_class($oDashlet);
  793. $oDashlet = $oDashlet->Update($aValues, $aUpdatedDecoded);
  794. $sNewClass = get_class($oDashlet);
  795. if ($sNewClass != $sPrevClass)
  796. {
  797. $oPage->add_ready_script("$('#dashlet_$sDashletId').dashlet('option', {dashlet_class: '$sNewClass'});");
  798. }
  799. if ($oDashlet->IsRedrawNeeded())
  800. {
  801. $offset = $oPage->start_capture();
  802. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  803. $sHtml = addslashes($oPage->end_capture($offset));
  804. $sHtml = str_replace("\n", '', $sHtml);
  805. $sHtml = str_replace("\r", '', $sHtml);
  806. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  807. // but is executed BEFORE all 'ready_scripts'
  808. }
  809. if ($oDashlet->IsFormRedrawNeeded())
  810. {
  811. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  812. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  813. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  814. $sHtml = str_replace("\n", '', $sHtml);
  815. $sHtml = str_replace("\r", '', $sHtml);
  816. $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'
  817. // but is executed BEFORE all 'ready_scripts'
  818. }
  819. }
  820. break;
  821. case 'save_dashboard':
  822. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  823. $aParams = array();
  824. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  825. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  826. $aParams['auto_reload'] = utils::ReadParam('auto_reload', false);
  827. $aParams['auto_reload_sec'] = utils::ReadParam('auto_reload_sec', 300);
  828. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  829. $oDashboard = new RuntimeDashboard($sDashboardId);
  830. $oDashboard->FromParams($aParams);
  831. $oDashboard->Save();
  832. // trigger a reload of the current page since the dashboard just changed
  833. $oPage->add_ready_script(
  834. <<<EOF
  835. var sLocation = new String(window.location.href);
  836. var sNewLocation = sLocation.replace('&edit=1', '');
  837. 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
  838. window.location.href = sNewLocation;
  839. EOF
  840. );
  841. $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
  842. break;
  843. case 'revert_dashboard':
  844. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  845. $oDashboard = new RuntimeDashboard($sDashboardId);
  846. $oDashboard->Revert();
  847. // trigger a reload of the current page since the dashboard just changed
  848. $oPage->add_ready_script("window.location.href=window.location.href;"); // reloads the page, doing a GET even if we arrived via a POST
  849. break;
  850. case 'render_dashboard':
  851. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  852. $aParams = array();
  853. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  854. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  855. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  856. $aParams['auto_reload'] = utils::ReadParam('auto_reload', false);
  857. $aParams['auto_reload_sec'] = utils::ReadParam('auto_reload_sec', 300);
  858. $oDashboard = new RuntimeDashboard($sDashboardId);
  859. $oDashboard->FromParams($aParams);
  860. $oDashboard->Render($oPage, true /* bEditMode */);
  861. break;
  862. case 'dashlet_creation_dlg':
  863. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  864. RuntimeDashboard::GetDashletCreationDlgFromOQL($oPage, $sOQL);
  865. break;
  866. case 'add_dashlet':
  867. $oForm = RuntimeDashboard::GetDashletCreationForm();
  868. $aValues = $oForm->ReadParams();
  869. $sDashletClass = $aValues['dashlet_class'];
  870. $sMenuId = $aValues['menu_id'];
  871. if (is_subclass_of($sDashletClass, 'Dashlet'))
  872. {
  873. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), 0);
  874. $oDashlet->FromParams($aValues);
  875. ApplicationMenu::LoadAdditionalMenus();
  876. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  877. $oMenu = ApplicationMenu::GetMenuNode($index);
  878. $oMenu->AddDashlet($oDashlet);
  879. // navigate to the dashboard page
  880. if ($aValues['open_editor'])
  881. {
  882. $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
  883. }
  884. }
  885. break;
  886. case 'shortcut_list_dlg':
  887. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  888. ShortcutOQL::GetCreationDlgFromOQL($oPage, $sOQL);
  889. break;
  890. case 'shortcut_list_create':
  891. $oForm = ShortcutOQL::GetCreationForm();
  892. $aValues = $oForm->ReadParams();
  893. $oAppContext = new ApplicationContext();
  894. $aContext = $oAppContext->GetAsHash();
  895. $sContext = serialize($aContext);
  896. $oShortcut = MetaModel::NewObject("ShortcutOQL");
  897. $oShortcut->Set('user_id', UserRights::GetUserId());
  898. $oShortcut->Set("context", $sContext);
  899. $oShortcut->Set("name", $aValues['name']);
  900. $oShortcut->Set("oql", $aValues['oql']);
  901. $iAutoReload = (int)$aValues['auto_reload_sec'];
  902. if (($aValues['auto_reload']) && ($iAutoReload > 0))
  903. {
  904. $oShortcut->Set("auto_reload_sec", max(5, $iAutoReload));
  905. $oShortcut->Set("auto_reload", 'custom');
  906. }
  907. $iId = $oShortcut->DBInsertNoReload();
  908. // Add the menu node in the right place
  909. //
  910. // Mmmm... already done because the newly created menu is read from the DB
  911. // as soon as we invoke DisplayMenu
  912. // Refresh the menu pane
  913. $aExtraParams = array();
  914. ApplicationMenu::DisplayMenu($oPage, $aExtraParams);
  915. break;
  916. case 'shortcut_rename_dlg':
  917. $oSearch = new DBObjectSearch('Shortcut');
  918. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  919. $iShortcut = $aShortcuts[0];
  920. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  921. $oShortcut->StartRenameDialog($oPage);
  922. break;
  923. case 'shortcut_rename_go':
  924. $iShortcut = utils::ReadParam('id', 0);
  925. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  926. $sName = utils::ReadParam('attr_name', '', false, 'raw_data');
  927. if (strlen($sName) > 0)
  928. {
  929. $oShortcut->Set('name', $sName);
  930. $oShortcut->DBUpdate();
  931. $oPage->add_ready_script('window.location.reload();');
  932. }
  933. break;
  934. case 'shortcut_delete_go':
  935. $oSearch = new DBObjectSearch('Shortcut');
  936. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  937. foreach ($aShortcuts as $iShortcut)
  938. {
  939. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  940. $oShortcut->DBDelete();
  941. $oPage->add_ready_script('window.location.reload();');
  942. }
  943. break;
  944. case 'export_dashboard':
  945. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  946. ApplicationMenu::LoadAdditionalMenus();
  947. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  948. $oMenu = ApplicationMenu::GetMenuNode($index);
  949. if ($oMenu instanceof DashboardMenuNode)
  950. {
  951. $oDashboard = $oMenu->GetDashboard();
  952. $oPage->TrashUnexpectedOutput();
  953. $oPage->SetContentType('text/xml');
  954. $oPage->SetContentDisposition('attachment', $oMenu->GetLabel().'.xml');
  955. $oPage->add($oDashboard->ToXml());
  956. }
  957. break;
  958. case 'import_dashboard':
  959. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  960. ApplicationMenu::LoadAdditionalMenus();
  961. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  962. $oMenu = ApplicationMenu::GetMenuNode($index);
  963. $aResult = array('error' => '');
  964. try
  965. {
  966. if ($oMenu instanceof DashboardMenuNode)
  967. {
  968. $oDoc = utils::ReadPostedDocument('dashboard_upload_file');
  969. $oDashboard = $oMenu->GetDashboard();
  970. $oDashboard->FromXml($oDoc->GetData());
  971. $oDashboard->Save();
  972. }
  973. else
  974. {
  975. $aResult['error'] = 'Dashboard id="'.$sMenuId.'" not found.';
  976. }
  977. }
  978. catch(DOMException $e)
  979. {
  980. $aResult = array('error' => Dict::S('UI:Error:InvalidDashboardFile'));
  981. }
  982. catch(Exception $e)
  983. {
  984. $aResult = array('error' => $e->getMessage());
  985. }
  986. $oPage->add(json_encode($aResult));
  987. break;
  988. default:
  989. $oPage->p("Invalid query.");
  990. }
  991. $oPage->output();
  992. }
  993. catch (Exception $e)
  994. {
  995. // note: transform to cope with XSS attacks
  996. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  997. echo "<p>Debug trace: <pre>".$e->getTraceAsString()."</pre></p>\n";
  998. IssueLog::Error($e->getMessage());
  999. }
  1000. /**
  1001. * Downloads a document to the browser, either as 'inline' or 'attachment'
  1002. *
  1003. * @param WebPage $oPage The web page for the output
  1004. * @param string $sClass Class name of the object
  1005. * @param mixed $id Identifier of the object
  1006. * @param string $sAttCode Name of the attribute containing the document to download
  1007. * @param string $sContentDisposition Either 'inline' or 'attachment'
  1008. * @return none
  1009. */
  1010. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  1011. {
  1012. try
  1013. {
  1014. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  1015. if (!is_object($oObj))
  1016. {
  1017. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  1018. }
  1019. $oDocument = $oObj->Get($sAttCode);
  1020. if (is_object($oDocument))
  1021. {
  1022. $oPage->TrashUnexpectedOutput();
  1023. $oPage->SetContentType($oDocument->GetMimeType());
  1024. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  1025. $oPage->add($oDocument->GetData());
  1026. }
  1027. }
  1028. catch(Exception $e)
  1029. {
  1030. $oPage->p($e->getMessage());
  1031. }
  1032. }
  1033. ?>