ajax.render.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Handles various ajax requests
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  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. try
  32. {
  33. require_once(APPROOT.'/application/startup.inc.php');
  34. require_once(APPROOT.'/application/user.preferences.class.inc.php');
  35. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  36. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  37. $oPage = new ajax_page("");
  38. $oPage->no_cache();
  39. $operation = utils::ReadParam('operation', '');
  40. $sFilter = stripslashes(utils::ReadParam('filter', ''));
  41. $sEncoding = utils::ReadParam('encoding', 'serialize');
  42. $sClass = utils::ReadParam('class', 'MissingAjaxParam');
  43. $sStyle = utils::ReadParam('style', 'list');
  44. switch($operation)
  45. {
  46. case 'pagination':
  47. $sExtraParams = stripslashes(utils::ReadParam('extra_param', ''));
  48. $aExtraParams = array();
  49. if (!empty($sExtraParams))
  50. {
  51. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  52. }
  53. if ($sEncoding == 'oql')
  54. {
  55. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  56. }
  57. else
  58. {
  59. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  60. }
  61. $iStart = utils::ReadParam('start',0);
  62. $iEnd = utils::ReadParam('end',1);
  63. $iSortCol = utils::ReadParam('sort_col','null');
  64. $sSelectMode = utils::ReadParam('select_mode', '');
  65. $bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
  66. $aList = utils::ReadParam('display_list', array());
  67. $sClassName = $oFilter->GetClass();
  68. //$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, 'list'));
  69. // Filter the list to removed linked set since we are not able to display them here
  70. $aOrderBy = array();
  71. $aConfig = array();
  72. $iSortIndex = 0;
  73. if ($sSelectMode != '')
  74. {
  75. $aConfig['form::select'] = array();
  76. $iSortIndex++; // Take into account the extra (non-sortable) column for the checkbox/radio button.
  77. }
  78. if ($bDisplayKey)
  79. {
  80. $aConfig['key'] = array();
  81. if (($iSortCol != 'null') && ($iSortIndex == $iSortCol))
  82. {
  83. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  84. }
  85. $iSortIndex++;
  86. }
  87. foreach($aList as $sAttCode)
  88. {
  89. $aConfig[$sAttCode] = array();
  90. }
  91. foreach($aList as $index => $sAttCode)
  92. {
  93. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  94. if ($oAttDef instanceof AttributeLinkedSet)
  95. {
  96. // Removed from the display list
  97. unset($aList[$index]);
  98. }
  99. if ($iSortCol == $iSortIndex)
  100. {
  101. if ($oAttDef->IsExternalKey())
  102. {
  103. $sSortCol = $sAttCode.'_friendlyname';
  104. }
  105. else
  106. {
  107. $sSortCol = $sAttCode;
  108. }
  109. $aOrderBy[$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  110. }
  111. $iSortIndex++;
  112. }
  113. if (count($aOrderBy) == 0)
  114. {
  115. $aOrderBy['friendlyname'] = true; // By default, sort by name
  116. }
  117. else
  118. {
  119. // $oPage->add("</p>ICI: OrderBy already set to: <pre>'".print_r($aOrderBy, true)."'</pre></p>\n");
  120. }
  121. // Load only the requested columns
  122. $oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart);
  123. $sClassAlias = $oSet->GetFilter()->GetClassAlias();
  124. $oSet->OptimizeColumnLoad(array($sClassAlias => $aList));
  125. while($oObj = $oSet->Fetch())
  126. {
  127. $aRow = array();
  128. $sDisabled = '';
  129. switch ($sSelectMode)
  130. {
  131. case 'single':
  132. $aRow['form::select'] = "<input type=\"radio\" $sDisabled name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  133. break;
  134. case 'multiple':
  135. $aRow['form::select'] = "<input type=\"checkBox\" $sDisabled name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  136. break;
  137. }
  138. if ($bDisplayKey)
  139. {
  140. $aRow['key'] = $oObj->GetHyperLink();
  141. }
  142. $sHilightClass = $oObj->GetHilightClass();
  143. if ($sHilightClass != '')
  144. {
  145. $aRow['@class'] = $sHilightClass;
  146. }
  147. foreach($aList as $sAttCode)
  148. {
  149. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  150. }
  151. $sRow = $oPage->GetTableRow($aRow, $aConfig);
  152. $oPage->add($sRow);
  153. }
  154. break;
  155. // ui.linkswidget
  156. case 'searchObjectsToAdd':
  157. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  158. $sAttCode = utils::ReadParam('sAttCode', '');
  159. $iInputId = utils::ReadParam('iInputId', '');
  160. $sSuffix = utils::ReadParam('sSuffix', '');
  161. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  162. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  163. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  164. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  165. break;
  166. ////////////////////////////////////////////////////////////
  167. // ui.extkeywidget
  168. case 'searchObjectsToSelect':
  169. $sTargetClass = utils::ReadParam('sTargetClass', '');
  170. $iInputId = utils::ReadParam('iInputId', '');
  171. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  172. $sFilter = utils::ReadParam('sFilter');
  173. $sJson = utils::ReadParam('json', '');
  174. if (!empty($sJson))
  175. {
  176. $oWizardHelper = WizardHelper::FromJSON($sJson);
  177. $oObj = $oWizardHelper->GetTargetObject();
  178. }
  179. else
  180. {
  181. // Search form: no current object
  182. $oObj = null;
  183. }
  184. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  185. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  186. break;
  187. // ui.extkeywidget: autocomplete
  188. case 'ac_extkey':
  189. $sTargetClass = utils::ReadParam('sTargetClass', '');
  190. $iInputId = utils::ReadParam('iInputId', '');
  191. $sFilter = utils::ReadParam('sFilter');
  192. $sJson = utils::ReadParam('json', '');
  193. $sContains = utils::ReadParam('q', '');
  194. if (!empty($sJson))
  195. {
  196. $oWizardHelper = WizardHelper::FromJSON($sJson);
  197. $oObj = $oWizardHelper->GetTargetObject();
  198. }
  199. else
  200. {
  201. // Search form: no current object
  202. $oObj = null;
  203. }
  204. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  205. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  206. break;
  207. // ui.extkeywidget
  208. case 'objectSearchForm':
  209. $sTargetClass = utils::ReadParam('sTargetClass', '');
  210. $iInputId = utils::ReadParam('iInputId', '');
  211. $sTitle = utils::ReadParam('sTitle');
  212. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  213. $oWidget->GetSearchDialog($oPage, $sTitle);
  214. break;
  215. // ui.extkeywidget
  216. case 'objectCreationForm':
  217. $sTargetClass = utils::ReadParam('sTargetClass', '');
  218. $iInputId = utils::ReadParam('iInputId', '');
  219. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  220. $oWidget->GetObjectCreationForm($oPage);
  221. break;
  222. // ui.extkeywidget
  223. case 'doCreateObject':
  224. $sTargetClass = utils::ReadParam('sTargetClass', '');
  225. $iInputId = utils::ReadParam('iInputId', '');
  226. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  227. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  228. $aResult = $oWidget->DoCreateObject($oPage);
  229. echo json_encode($aResult);
  230. break;
  231. // ui.extkeywidget
  232. case 'getObjectName':
  233. $sTargetClass = utils::ReadParam('sTargetClass', '');
  234. $iInputId = utils::ReadParam('iInputId', '');
  235. $iObjectId = utils::ReadParam('iObjectId', '');
  236. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  237. $sName = $oWidget->GetObjectName($iObjectId);
  238. echo json_encode(array('name' => $sName));
  239. break;
  240. // ui.extkeywidget
  241. case 'displayHierarchy':
  242. $sTargetClass = utils::ReadParam('sTargetClass', '');
  243. $sInputId = utils::ReadParam('sInputId', '');
  244. $sFilter = utils::ReadParam('sFilter');
  245. $sJson = utils::ReadParam('json', '');
  246. $currValue = utils::ReadParam('value', '');
  247. if (!empty($sJson))
  248. {
  249. $oWizardHelper = WizardHelper::FromJSON($sJson);
  250. $oObj = $oWizardHelper->GetTargetObject();
  251. }
  252. else
  253. {
  254. // Search form: no current object
  255. $oObj = null;
  256. }
  257. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId);
  258. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  259. break;
  260. ////////////////////////////////////////////////////
  261. // ui.linkswidget
  262. case 'doAddObjects':
  263. $sAttCode = utils::ReadParam('sAttCode', '');
  264. $iInputId = utils::ReadParam('iInputId', '');
  265. $sSuffix = utils::ReadParam('sSuffix', '');
  266. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass);
  267. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  268. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  269. if ($sFilter != '')
  270. {
  271. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  272. }
  273. else
  274. {
  275. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  276. }
  277. $oWidget->DoAddObjects($oPage, $oFullSetFilter);
  278. break;
  279. case 'wizard_helper_preview':
  280. $sJson = utils::ReadParam('json_obj', '');
  281. $oWizardHelper = WizardHelper::FromJSON($sJson);
  282. $oObj = $oWizardHelper->GetTargetObject();
  283. $oObj->DisplayBareProperties($oPage);
  284. break;
  285. case 'wizard_helper':
  286. $sJson = utils::ReadParam('json_obj', '');
  287. $oWizardHelper = WizardHelper::FromJSON($sJson);
  288. $oObj = $oWizardHelper->GetTargetObject();
  289. $sClass = $oWizardHelper->GetTargetClass();
  290. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  291. {
  292. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  293. $defaultValue = $oAttDef->GetDefaultValue();
  294. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  295. $oObj->Set($sAttCode, $defaultValue);
  296. }
  297. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  298. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  299. {
  300. $sId = $oWizardHelper->GetIdForField($sAttCode);
  301. if ($sId != '')
  302. {
  303. // It may happen that the field we'd like to update does not
  304. // exist in the form. For example, if the field should be hidden/read-only
  305. // in the current state of the object
  306. $value = $oObj->Get($sAttCode);
  307. $displayValue = $oObj->GetEditValue($sAttCode);
  308. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  309. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  310. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  311. // Make sure that we immediatly validate the field when we reload it
  312. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  313. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  314. }
  315. }
  316. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  317. break;
  318. // DisplayBlock
  319. case 'ajax':
  320. if ($sFilter != "")
  321. {
  322. $sExtraParams = stripslashes(utils::ReadParam('extra_params', ''));
  323. $aExtraParams = array();
  324. if (!empty($sExtraParams))
  325. {
  326. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  327. }
  328. if ($sEncoding == 'oql')
  329. {
  330. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  331. }
  332. else
  333. {
  334. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  335. }
  336. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  337. $aExtraParams['display_limit'] = true;
  338. $aExtraParams['truncated'] = true;
  339. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  340. }
  341. else
  342. {
  343. $oPage->p("Invalid query (empty filter).");
  344. }
  345. break;
  346. case 'displayCSVHistory':
  347. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  348. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  349. break;
  350. case 'details':
  351. $key = utils::ReadParam('id', 0);
  352. $oFilter = new DBObjectSearch($sClass);
  353. $oFilter->AddCondition('id', $key, '=');
  354. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  355. $oDisplayBlock->RenderContent($oPage);
  356. break;
  357. case 'pie_chart':
  358. $sGroupBy = utils::ReadParam('group_by', '');
  359. if ($sFilter != '')
  360. {
  361. if ($sEncoding == 'oql')
  362. {
  363. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  364. }
  365. else
  366. {
  367. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  368. }
  369. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  370. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  371. }
  372. else
  373. {
  374. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  375. }
  376. break;
  377. case 'open_flash_chart':
  378. $aParams = utils::ReadParam('params', array());
  379. if ($sFilter != '')
  380. {
  381. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  382. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  383. $oDisplayBlock->RenderContent($oPage, $aParams);
  384. }
  385. else
  386. {
  387. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  388. }
  389. break;
  390. case 'modal_details':
  391. $key = utils::ReadParam('id', 0);
  392. $oFilter = new DBObjectSearch($sClass);
  393. $oFilter->AddCondition('id', $key, '=');
  394. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  395. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  396. $oDisplayBlock->RenderContent($oPage);
  397. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  398. break;
  399. case 'link':
  400. $sClass = utils::ReadParam('sclass', 'logInfra');
  401. $sAttCode = utils::ReadParam('attCode', 'name');
  402. //$sOrg = utils::ReadParam('org_id', '');
  403. $sName = utils::ReadParam('q', '');
  404. $iMaxCount = utils::ReadParam('max', 30);
  405. $iCount = 0;
  406. $oFilter = new DBObjectSearch($sClass);
  407. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  408. //$oFilter->AddCondition('org_id', $sOrg, '=');
  409. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  410. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  411. {
  412. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  413. $iCount++;
  414. }
  415. break;
  416. case 'create':
  417. case 'create_menu':
  418. $sClass = utils::ReadParam('class', '');
  419. $sFilter = utils::ReadParam('filter', '');
  420. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  421. break;
  422. case 'combo_options':
  423. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  424. $oSet = new CMDBObjectSet($oFilter);
  425. while( $oObj = $oSet->fetch())
  426. {
  427. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  428. }
  429. break;
  430. case 'display_document':
  431. $id = utils::ReadParam('id', '');
  432. $sField = utils::ReadParam('field', '');
  433. if (!empty($sClass) && !empty($id) && !empty($sField))
  434. {
  435. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  436. }
  437. break;
  438. case 'download_document':
  439. $id = utils::ReadParam('id', '');
  440. $sField = utils::ReadParam('field', '');
  441. if (!empty($sClass) && !empty($id) && !empty($sField))
  442. {
  443. DownloadDocument($oPage, $sClass, $id, $sField, 'attachement');
  444. }
  445. break;
  446. case 'search_form':
  447. $sClass = utils::ReadParam('className', '');
  448. $sRootClass = utils::ReadParam('baseClass', '');
  449. $currentId = utils::ReadParam('currentId', '');
  450. $sAction = utils::ReadParam('action', '');
  451. $oFilter = new DBObjectSearch($sClass);
  452. $oSet = new CMDBObjectSet($oFilter);
  453. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction));
  454. $oPage->add($sHtml);
  455. break;
  456. case 'set_pref':
  457. $sCode = utils::ReadPostedParam('code', '');
  458. $sValue = utils::ReadPostedParam('value', '');
  459. appUserPreferences::SetPref($sCode, $sValue);
  460. break;
  461. case 'erase_all_pref':
  462. // Can be useful in case a user got some corrupted prefs...
  463. appUserPreferences::ClearPreferences();
  464. break;
  465. case 'on_form_cancel':
  466. // Called when a creation/modification form is cancelled by the end-user
  467. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  468. $iTransactionId = utils::ReadParam('transaction_id', 0);
  469. $sTempId = session_id().'_'.$iTransactionId;
  470. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  471. {
  472. $oExtensionInstance->OnFormCancel($sTempId);
  473. }
  474. break;
  475. default:
  476. $oPage->p("Invalid query.");
  477. }
  478. $oPage->output();
  479. }
  480. catch (Exception $e)
  481. {
  482. echo $e->GetMessage();
  483. IssueLog::Error($e->getMessage());
  484. }
  485. /**
  486. * Downloads a document to the browser, either as 'inline' or 'attachment'
  487. *
  488. * @param WebPage $oPage The web page for the output
  489. * @param string $sClass Class name of the object
  490. * @param mixed $id Identifier of the object
  491. * @param string $sAttCode Name of the attribute containing the document to download
  492. * @param string $sContentDisposition Either 'inline' or 'attachment'
  493. * @return none
  494. */
  495. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  496. {
  497. try
  498. {
  499. $oObj = MetaModel::GetObject($sClass, $id);
  500. if (is_object($oObj))
  501. {
  502. $oDocument = $oObj->Get($sAttCode);
  503. if (is_object($oDocument))
  504. {
  505. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  506. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  507. $oPage->add($oDocument->GetData());
  508. }
  509. }
  510. }
  511. catch(Exception $e)
  512. {
  513. $oPage->p($e->getMessage());
  514. }
  515. }
  516. ?>