ajax.render.php 38 KB

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