ajax.render.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. ////////////////////////////////////////////////////////////
  267. // ui.extkeywidget
  268. case 'searchObjectsToSelect':
  269. $oPage->SetContentType('text/html');
  270. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  271. $iInputId = utils::ReadParam('iInputId', '');
  272. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  273. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  274. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  275. $sAttCode = utils::ReadParam('sAttCode', '');
  276. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  277. if (!empty($sJson))
  278. {
  279. $oWizardHelper = WizardHelper::FromJSON($sJson);
  280. $oObj = $oWizardHelper->GetTargetObject();
  281. }
  282. else
  283. {
  284. // Search form: no current object
  285. $oObj = null;
  286. }
  287. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  288. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  289. break;
  290. // ui.extkeywidget: autocomplete
  291. case 'ac_extkey':
  292. $oPage->SetContentType('text/plain');
  293. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  294. $iInputId = utils::ReadParam('iInputId', '');
  295. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  296. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  297. $sContains = utils::ReadParam('q', '', false, 'raw_data');
  298. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  299. if ($sContains !='')
  300. {
  301. if (!empty($sJson))
  302. {
  303. $oWizardHelper = WizardHelper::FromJSON($sJson);
  304. $oObj = $oWizardHelper->GetTargetObject();
  305. }
  306. else
  307. {
  308. // Search form: no current object
  309. $oObj = null;
  310. }
  311. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  312. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  313. }
  314. break;
  315. // ui.extkeywidget
  316. case 'objectSearchForm':
  317. $oPage->SetContentType('text/html');
  318. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  319. $iInputId = utils::ReadParam('iInputId', '');
  320. $sTitle = utils::ReadParam('sTitle', '', false, 'raw_data');
  321. $sAttCode = utils::ReadParam('sAttCode', '');
  322. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  323. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  324. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  325. if (!empty($sJson))
  326. {
  327. $oWizardHelper = WizardHelper::FromJSON($sJson);
  328. $oObj = $oWizardHelper->GetTargetObject();
  329. }
  330. else
  331. {
  332. // Search form: no current object
  333. $oObj = null;
  334. }
  335. $oWidget->GetSearchDialog($oPage, $sTitle, $oObj);
  336. break;
  337. // ui.extkeywidget
  338. case 'objectCreationForm':
  339. $oPage->SetContentType('text/html');
  340. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  341. $iInputId = utils::ReadParam('iInputId', '');
  342. $sAttCode = utils::ReadParam('sAttCode', '');
  343. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  344. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  345. if (!empty($sJson))
  346. {
  347. $oWizardHelper = WizardHelper::FromJSON($sJson);
  348. $oObj = $oWizardHelper->GetTargetObject();
  349. }
  350. else
  351. {
  352. // Search form: no current object
  353. $oObj = null;
  354. }
  355. $oWidget->GetObjectCreationForm($oPage, $oObj);
  356. break;
  357. // ui.extkeywidget
  358. case 'doCreateObject':
  359. $oPage->SetContentType('application/json');
  360. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  361. $iInputId = utils::ReadParam('iInputId', '');
  362. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  363. $sAttCode = utils::ReadParam('sAttCode', '');
  364. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  365. $aResult = $oWidget->DoCreateObject($oPage);
  366. echo json_encode($aResult);
  367. break;
  368. // ui.extkeywidget
  369. case 'getObjectName':
  370. $oPage->SetContentType('application/json');
  371. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  372. $iInputId = utils::ReadParam('iInputId', '');
  373. $iObjectId = utils::ReadParam('iObjectId', '');
  374. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  375. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  376. $sName = $oWidget->GetObjectName($iObjectId);
  377. echo json_encode(array('name' => $sName));
  378. break;
  379. // ui.extkeywidget
  380. case 'displayHierarchy':
  381. $oPage->SetContentType('text/html');
  382. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  383. $sInputId = utils::ReadParam('sInputId', '');
  384. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  385. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  386. $currValue = utils::ReadParam('value', '');
  387. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  388. if (!empty($sJson))
  389. {
  390. $oWizardHelper = WizardHelper::FromJSON($sJson);
  391. $oObj = $oWizardHelper->GetTargetObject();
  392. }
  393. else
  394. {
  395. // Search form: no current object
  396. $oObj = null;
  397. }
  398. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId, '', $bSearchMode);
  399. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  400. break;
  401. ////////////////////////////////////////////////////
  402. // ui.linkswidget
  403. case 'doAddObjects':
  404. $oPage->SetContentType('text/html');
  405. $sAttCode = utils::ReadParam('sAttCode', '');
  406. $iInputId = utils::ReadParam('iInputId', '');
  407. $sSuffix = utils::ReadParam('sSuffix', '');
  408. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
  409. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  410. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  411. $oWizardHelper = WizardHelper::FromJSON($sJson);
  412. $oObj = $oWizardHelper->GetTargetObject();
  413. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  414. if ($sFilter != '')
  415. {
  416. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  417. }
  418. else
  419. {
  420. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  421. }
  422. $oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);
  423. break;
  424. case 'wizard_helper_preview':
  425. $oPage->SetContentType('text/html');
  426. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  427. $oWizardHelper = WizardHelper::FromJSON($sJson);
  428. $oObj = $oWizardHelper->GetTargetObject();
  429. $oObj->DisplayBareProperties($oPage);
  430. break;
  431. case 'wizard_helper':
  432. $oPage->SetContentType('text/html');
  433. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  434. $oWizardHelper = WizardHelper::FromJSON($sJson);
  435. $oObj = $oWizardHelper->GetTargetObject();
  436. $sClass = $oWizardHelper->GetTargetClass();
  437. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  438. {
  439. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  440. $defaultValue = $oAttDef->GetDefaultValue();
  441. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  442. $oObj->Set($sAttCode, $defaultValue);
  443. }
  444. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  445. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  446. {
  447. $sId = $oWizardHelper->GetIdForField($sAttCode);
  448. if ($sId != '')
  449. {
  450. if ($oObj->IsNew())
  451. {
  452. $iFlags = $oObj->GetInitialStateAttributeFlags($sAttCode);
  453. }
  454. else
  455. {
  456. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  457. }
  458. if ($iFlags & OPT_ATT_READONLY)
  459. {
  460. $sHTMLValue = "<span id=\"field_{$sId}\">".$oObj->GetAsHTML($sAttCode);
  461. $sHTMLValue .= '<input type="hidden" id="'.$sId.'" name="attr_'.$sFormPrefix.$sAttCode.'" value="'.htmlentities($oObj->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/></span>';
  462. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  463. }
  464. else
  465. {
  466. // It may happen that the field we'd like to update does not
  467. // exist in the form. For example, if the field should be hidden/read-only
  468. // in the current state of the object
  469. $value = $oObj->Get($sAttCode);
  470. $displayValue = $oObj->GetEditValue($sAttCode);
  471. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  472. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  473. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  474. // Make sure that we immediately validate the field when we reload it
  475. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  476. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  477. }
  478. }
  479. }
  480. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  481. break;
  482. case 'obj_creation_form':
  483. $oPage->SetContentType('text/html');
  484. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  485. $oWizardHelper = WizardHelper::FromJSON($sJson);
  486. $oObj = $oWizardHelper->GetTargetObject();
  487. $sClass = $oWizardHelper->GetTargetClass();
  488. $sTargetState = utils::ReadParam('target_state', '');
  489. $iTransactionId = utils::ReadParam('transaction_id', '');
  490. $oObj->Set(MetaModel::GetStateAttributeCode($sClass), $sTargetState);
  491. cmdbAbstractObject::DisplayCreationForm($oPage, $sClass, $oObj, array(), array('action' => utils::GetAbsoluteUrlAppRoot().'pages/UI.php', 'transaction_id' => $iTransactionId));
  492. break;
  493. // DisplayBlock
  494. case 'ajax':
  495. $oPage->SetContentType('text/html');
  496. if ($sFilter != "")
  497. {
  498. $sExtraParams = stripslashes(utils::ReadParam('extra_params', '', false, 'raw_data'));
  499. $aExtraParams = array();
  500. if (!empty($sExtraParams))
  501. {
  502. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  503. }
  504. // Restore the app context from the ExtraParams
  505. $oAppContext = new ApplicationContext(false); // false => don't read the context yet !
  506. $aContext = array();
  507. foreach($oAppContext->GetNames() as $sName)
  508. {
  509. $sParamName = 'c['.$sName.']';
  510. if (isset($aExtraParams[$sParamName]))
  511. {
  512. $aContext[$sName] = $aExtraParams[$sParamName];
  513. }
  514. }
  515. $_REQUEST['c'] = $aContext;
  516. if ($sEncoding == 'oql')
  517. {
  518. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  519. }
  520. else
  521. {
  522. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  523. }
  524. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  525. $aExtraParams['display_limit'] = true;
  526. $aExtraParams['truncated'] = true;
  527. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  528. }
  529. else
  530. {
  531. $oPage->p("Invalid query (empty filter).");
  532. }
  533. break;
  534. case 'displayCSVHistory':
  535. $oPage->SetContentType('text/html');
  536. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  537. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  538. break;
  539. case 'details':
  540. $oPage->SetContentType('text/html');
  541. $key = utils::ReadParam('id', 0);
  542. $oFilter = new DBObjectSearch($sClass);
  543. $oFilter->AddCondition('id', $key, '=');
  544. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  545. $oDisplayBlock->RenderContent($oPage);
  546. break;
  547. case 'pie_chart':
  548. $oPage->SetContentType('application/json');
  549. $sGroupBy = utils::ReadParam('group_by', '');
  550. if ($sFilter != '')
  551. {
  552. if ($sEncoding == 'oql')
  553. {
  554. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  555. }
  556. else
  557. {
  558. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  559. }
  560. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  561. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  562. }
  563. else
  564. {
  565. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  566. }
  567. break;
  568. case 'open_flash_chart':
  569. // Workaround for IE8 + IIS + HTTPS
  570. // See TRAC #363, fix described here: http://forums.codecharge.com/posts.php?post_id=97771
  571. $oPage->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
  572. $oPage->add_header("Cache-Control: cache, must-revalidate");
  573. $oPage->add_header("Pragma: public");
  574. $oPage->SetContentType('application/json');
  575. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  576. if ($sFilter != '')
  577. {
  578. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  579. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  580. $oDisplayBlock->RenderContent($oPage, $aParams);
  581. }
  582. else
  583. {
  584. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  585. }
  586. break;
  587. case 'modal_details':
  588. $oPage->SetContentType('text/html');
  589. $key = utils::ReadParam('id', 0);
  590. $oFilter = new DBObjectSearch($sClass);
  591. $oFilter->AddCondition('id', $key, '=');
  592. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  593. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  594. $oDisplayBlock->RenderContent($oPage);
  595. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  596. break;
  597. case 'link':
  598. $oPage->SetContentType('text/html');
  599. $sClass = utils::ReadParam('sclass', 'logInfra', false, 'class');
  600. $sAttCode = utils::ReadParam('attCode', 'name');
  601. //$sOrg = utils::ReadParam('org_id', '');
  602. $sName = utils::ReadParam('q', '');
  603. $iMaxCount = utils::ReadParam('max', 30);
  604. $iCount = 0;
  605. $oFilter = new DBObjectSearch($sClass);
  606. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  607. //$oFilter->AddCondition('org_id', $sOrg, '=');
  608. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  609. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  610. {
  611. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  612. $iCount++;
  613. }
  614. break;
  615. case 'combo_options':
  616. $oPage->SetContentType('text/html');
  617. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  618. $oSet = new CMDBObjectSet($oFilter);
  619. while( $oObj = $oSet->fetch())
  620. {
  621. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  622. }
  623. break;
  624. case 'display_document':
  625. $id = utils::ReadParam('id', '');
  626. $sField = utils::ReadParam('field', '');
  627. if (!empty($sClass) && !empty($id) && !empty($sField))
  628. {
  629. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  630. }
  631. break;
  632. case 'download_document':
  633. $id = utils::ReadParam('id', '');
  634. $sField = utils::ReadParam('field', '');
  635. if (!empty($sClass) && !empty($id) && !empty($sField))
  636. {
  637. DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  638. }
  639. break;
  640. case 'search_form':
  641. $oPage->SetContentType('text/html');
  642. $sClass = utils::ReadParam('className', '', false, 'class');
  643. $sRootClass = utils::ReadParam('baseClass', '', false, 'class');
  644. $currentId = utils::ReadParam('currentId', '');
  645. $sTableId = utils::ReadParam('_table_id_', null, false, 'raw_data');
  646. $sAction = utils::ReadParam('action', '');
  647. $oFilter = new DBObjectSearch($sClass);
  648. $oSet = new CMDBObjectSet($oFilter);
  649. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction, 'table_id' => $sTableId));
  650. $oPage->add($sHtml);
  651. break;
  652. case 'set_pref':
  653. $sCode = utils::ReadPostedParam('code', '');
  654. $sValue = utils::ReadPostedParam('value', '', 'raw_data');
  655. appUserPreferences::SetPref($sCode, $sValue);
  656. break;
  657. case 'erase_all_pref':
  658. // Can be useful in case a user got some corrupted prefs...
  659. appUserPreferences::ClearPreferences();
  660. break;
  661. case 'on_form_cancel':
  662. // Called when a creation/modification form is cancelled by the end-user
  663. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  664. $iTransactionId = utils::ReadParam('transaction_id', 0);
  665. $sTempId = session_id().'_'.$iTransactionId;
  666. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  667. {
  668. $oExtensionInstance->OnFormCancel($sTempId);
  669. }
  670. break;
  671. case 'dashboard_editor':
  672. $sId = utils::ReadParam('id', '', false, 'raw_data');
  673. ApplicationMenu::LoadAdditionalMenus();
  674. $idx = ApplicationMenu::GetMenuIndexById($sId);
  675. $oMenu = ApplicationMenu::GetMenuNode($idx);
  676. $oMenu->RenderEditor($oPage);
  677. break;
  678. case 'new_dashlet':
  679. require_once(APPROOT.'application/forms.class.inc.php');
  680. require_once(APPROOT.'application/dashlet.class.inc.php');
  681. $sDashletClass = utils::ReadParam('dashlet_class', '');
  682. $sDashletId = utils::ReadParam('dashlet_id', '', false, 'raw_data');
  683. if (is_subclass_of($sDashletClass, 'Dashlet'))
  684. {
  685. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  686. $offset = $oPage->start_capture();
  687. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  688. $sHtml = addslashes($oPage->end_capture($offset));
  689. $sHtml = str_replace("\n", '', $sHtml);
  690. $sHtml = str_replace("\r", '', $sHtml);
  691. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml')"); // in ajax web page add_script has the same effect as add_ready_script
  692. // but is executed BEFORE all 'ready_scripts'
  693. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  694. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  695. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, ':itop-dashboard'));
  696. $sHtml = str_replace("\n", '', $sHtml);
  697. $sHtml = str_replace("\r", '', $sHtml);
  698. $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'
  699. }
  700. break;
  701. case 'update_dashlet_property':
  702. require_once(APPROOT.'application/forms.class.inc.php');
  703. require_once(APPROOT.'application/dashlet.class.inc.php');
  704. $aParams = utils::ReadParam('params', '', false, 'raw_data');
  705. $sDashletClass = $aParams['attr_dashlet_class'];
  706. $sDashletId = $aParams['attr_dashlet_id'];
  707. $aUpdatedProperties = $aParams['updated']; // Code of the changed properties as an array: 'attr_xxx', 'attr_xxy', etc...
  708. $aPreviousValues = $aParams['previous_values']; // hash array: 'attr_xxx' => 'old_value'
  709. if (is_subclass_of($sDashletClass, 'Dashlet'))
  710. {
  711. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  712. $oForm = $oDashlet->GetForm();
  713. $aValues = $oForm->ReadParams(); // hash array: 'xxx' => 'new_value'
  714. $aCurrentValues = $aValues;
  715. $aUpdatedDecoded = array();
  716. foreach($aUpdatedProperties as $sProp)
  717. {
  718. $sDecodedProp = str_replace('attr_', '', $sProp); // Remove the attr_ prefix
  719. $aCurrentValues[$sDecodedProp] = $aPreviousValues[$sProp]; // Set the previous value
  720. $aUpdatedDecoded[] = $sDecodedProp;
  721. }
  722. $oDashlet->FromParams($aCurrentValues);
  723. $sPrevClass = get_class($oDashlet);
  724. $oDashlet = $oDashlet->Update($aValues, $aUpdatedDecoded);
  725. $sNewClass = get_class($oDashlet);
  726. if ($sNewClass != $sPrevClass)
  727. {
  728. $oPage->add_ready_script("$('#dashlet_$sDashletId').dashlet('option', {dashlet_class: '$sNewClass'});");
  729. }
  730. if ($oDashlet->IsRedrawNeeded())
  731. {
  732. $offset = $oPage->start_capture();
  733. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  734. $sHtml = addslashes($oPage->end_capture($offset));
  735. $sHtml = str_replace("\n", '', $sHtml);
  736. $sHtml = str_replace("\r", '', $sHtml);
  737. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  738. // but is executed BEFORE all 'ready_scripts'
  739. }
  740. if ($oDashlet->IsFormRedrawNeeded())
  741. {
  742. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  743. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  744. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, ':itop-dashboard'));
  745. $sHtml = str_replace("\n", '', $sHtml);
  746. $sHtml = str_replace("\r", '', $sHtml);
  747. $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'
  748. // but is executed BEFORE all 'ready_scripts'
  749. }
  750. }
  751. break;
  752. case 'save_dashboard':
  753. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  754. $aParams = array();
  755. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  756. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  757. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  758. $oDashboard = new RuntimeDashboard($sDashboardId);
  759. $oDashboard->FromParams($aParams);
  760. $oDashboard->Save();
  761. // trigger a reload of the current page since the dashboard just changed
  762. $oPage->add_ready_script(
  763. <<<EOF
  764. var sLocation = new String(window.location.href);
  765. var sNewLocation = sLocation.replace('&edit=1', '');
  766. 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
  767. window.location.href = sNewLocation;
  768. EOF
  769. );
  770. $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
  771. break;
  772. case 'revert_dashboard':
  773. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  774. $oDashboard = new RuntimeDashboard($sDashboardId);
  775. $oDashboard->Revert();
  776. // trigger a reload of the current page since the dashboard just changed
  777. $oPage->add_ready_script("window.location.href=window.location.href;"); // reloads the page, doing a GET even if we arrived via a POST
  778. break;
  779. case 'render_dashboard':
  780. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  781. $aParams = array();
  782. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  783. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  784. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  785. $oDashboard = new RuntimeDashboard($sDashboardId);
  786. $oDashboard->FromParams($aParams);
  787. $oDashboard->Render($oPage, true /* bEditMode */);
  788. break;
  789. case 'dashlet_creation_dlg':
  790. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  791. RuntimeDashboard::GetDashletCreationDlgFromOQL($oPage, $sOQL);
  792. break;
  793. case 'add_dashlet':
  794. $oForm = RuntimeDashboard::GetDashletCreationForm();
  795. $aValues = $oForm->ReadParams();
  796. $sDashletClass = $aValues['dashlet_class'];
  797. $sMenuId = $aValues['menu_id'];
  798. if (is_subclass_of($sDashletClass, 'Dashlet'))
  799. {
  800. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), 0);
  801. $oDashlet->FromParams($aValues);
  802. ApplicationMenu::LoadAdditionalMenus();
  803. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  804. $oMenu = ApplicationMenu::GetMenuNode($index);
  805. $oMenu->AddDashlet($oDashlet);
  806. // navigate to the dashboard page
  807. if ($aValues['open_editor'])
  808. {
  809. $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
  810. }
  811. }
  812. break;
  813. case 'shortcut_list_dlg':
  814. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  815. ShortcutOQL::GetCreationDlgFromOQL($oPage, $sOQL);
  816. break;
  817. case 'shortcut_list_create':
  818. $oForm = ShortcutOQL::GetCreationForm();
  819. $aValues = $oForm->ReadParams();
  820. $oAppContext = new ApplicationContext();
  821. $aContext = $oAppContext->GetAsHash();
  822. $sContext = serialize($aContext);
  823. $oShortcut = MetaModel::NewObject("ShortcutOQL");
  824. $oShortcut->Set('user_id', UserRights::GetUserId());
  825. $oShortcut->Set("context", $sContext);
  826. $oShortcut->Set("name", $aValues['name']);
  827. $oShortcut->Set("oql", $aValues['oql']);
  828. $iId = $oShortcut->DBInsertNoReload();
  829. // Add the menu node in the right place
  830. //
  831. // Mmmm... already done because the newly created menu is read from the DB
  832. // as soon as we invoke DisplayMenu
  833. // Refresh the menu pane
  834. $aExtraParams = array();
  835. ApplicationMenu::DisplayMenu($oPage, $aExtraParams);
  836. break;
  837. case 'shortcut_rename_dlg':
  838. $oSearch = new DBObjectSearch('Shortcut');
  839. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  840. $iShortcut = $aShortcuts[0];
  841. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  842. $oShortcut->StartRenameDialog($oPage);
  843. break;
  844. case 'shortcut_rename_go':
  845. $iShortcut = utils::ReadParam('id', 0);
  846. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  847. $sName = utils::ReadParam('attr_name', '', false, 'raw_data');
  848. if (strlen($sName) > 0)
  849. {
  850. $oShortcut->Set('name', $sName);
  851. $oShortcut->DBUpdate();
  852. $oPage->add_ready_script('window.location.reload();');
  853. }
  854. break;
  855. case 'shortcut_delete_go':
  856. $oSearch = new DBObjectSearch('Shortcut');
  857. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  858. foreach ($aShortcuts as $iShortcut)
  859. {
  860. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  861. $oShortcut->DBDelete();
  862. $oPage->add_ready_script('window.location.reload();');
  863. }
  864. break;
  865. case 'export_dashboard':
  866. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  867. ApplicationMenu::LoadAdditionalMenus();
  868. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  869. $oMenu = ApplicationMenu::GetMenuNode($index);
  870. if ($oMenu instanceof DashboardMenuNode)
  871. {
  872. $oDashboard = $oMenu->GetDashboard();
  873. $oPage->TrashUnexpectedOutput();
  874. $oPage->SetContentType('text/xml');
  875. $oPage->SetContentDisposition('attachment', $oMenu->GetLabel().'.xml');
  876. $oPage->add($oDashboard->ToXml());
  877. }
  878. break;
  879. case 'import_dashboard':
  880. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  881. ApplicationMenu::LoadAdditionalMenus();
  882. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  883. $oMenu = ApplicationMenu::GetMenuNode($index);
  884. $aResult = array('error' => '');
  885. try
  886. {
  887. if ($oMenu instanceof DashboardMenuNode)
  888. {
  889. $oDoc = utils::ReadPostedDocument('dashboard_upload_file');
  890. $oDashboard = $oMenu->GetDashboard();
  891. $oDashboard->FromXml($oDoc->GetData());
  892. $oDashboard->Save();
  893. }
  894. else
  895. {
  896. $aResult['error'] = 'Dashboard id="'.$sMenuId.'" not found.';
  897. }
  898. }
  899. catch(DOMException $e)
  900. {
  901. $aResult = array('error' => Dict::S('UI:Error:InvalidDashboardFile'));
  902. }
  903. catch(Exception $e)
  904. {
  905. $aResult = array('error' => $e->getMessage());
  906. }
  907. $oPage->add(json_encode($aResult));
  908. break;
  909. default:
  910. $oPage->p("Invalid query.");
  911. }
  912. $oPage->output();
  913. }
  914. catch (Exception $e)
  915. {
  916. // note: transform to cope with XSS attacks
  917. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  918. echo "<p>Debug trace: <pre>".$e->getTraceAsString()."</pre></p>\n";
  919. IssueLog::Error($e->getMessage());
  920. }
  921. /**
  922. * Downloads a document to the browser, either as 'inline' or 'attachment'
  923. *
  924. * @param WebPage $oPage The web page for the output
  925. * @param string $sClass Class name of the object
  926. * @param mixed $id Identifier of the object
  927. * @param string $sAttCode Name of the attribute containing the document to download
  928. * @param string $sContentDisposition Either 'inline' or 'attachment'
  929. * @return none
  930. */
  931. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  932. {
  933. try
  934. {
  935. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  936. if (!is_object($oObj))
  937. {
  938. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  939. }
  940. $oDocument = $oObj->Get($sAttCode);
  941. if (is_object($oDocument))
  942. {
  943. $oPage->TrashUnexpectedOutput();
  944. $oPage->SetContentType($oDocument->GetMimeType());
  945. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  946. $oPage->add($oDocument->GetData());
  947. }
  948. }
  949. catch(Exception $e)
  950. {
  951. $oPage->p($e->getMessage());
  952. }
  953. }
  954. ?>