ajax.render.php 21 KB

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