ajax.render.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 'addObjects':
  158. $oPage->SetContentType('text/html');
  159. $sAttCode = utils::ReadParam('sAttCode', '');
  160. $iInputId = utils::ReadParam('iInputId', '');
  161. $sSuffix = utils::ReadParam('sSuffix', '');
  162. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  163. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  164. if (!empty($sJson))
  165. {
  166. $oWizardHelper = WizardHelper::FromJSON($sJson);
  167. $oObj = $oWizardHelper->GetTargetObject();
  168. }
  169. else
  170. {
  171. // Search form: no current object
  172. $oObj = null;
  173. }
  174. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  175. $oWidget->GetObjectPickerDialog($oPage, $oObj);
  176. break;
  177. // ui.linkswidget
  178. case 'searchObjectsToAdd':
  179. $oPage->SetContentType('text/html');
  180. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  181. $sAttCode = utils::ReadParam('sAttCode', '');
  182. $iInputId = utils::ReadParam('iInputId', '');
  183. $sSuffix = utils::ReadParam('sSuffix', '');
  184. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  185. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  186. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  187. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  188. break;
  189. ////////////////////////////////////////////////////////////
  190. // ui.extkeywidget
  191. case 'searchObjectsToSelect':
  192. $oPage->SetContentType('text/html');
  193. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  194. $iInputId = utils::ReadParam('iInputId', '');
  195. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  196. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  197. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  198. $sAttCode = utils::ReadParam('sAttCode', '');
  199. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  200. if (!empty($sJson))
  201. {
  202. $oWizardHelper = WizardHelper::FromJSON($sJson);
  203. $oObj = $oWizardHelper->GetTargetObject();
  204. }
  205. else
  206. {
  207. // Search form: no current object
  208. $oObj = null;
  209. }
  210. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  211. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  212. break;
  213. // ui.extkeywidget: autocomplete
  214. case 'ac_extkey':
  215. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  216. $iInputId = utils::ReadParam('iInputId', '');
  217. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  218. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  219. $sContains = utils::ReadParam('q', '', false, 'raw_data');
  220. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  221. if ($sContains !='')
  222. {
  223. if (!empty($sJson))
  224. {
  225. $oWizardHelper = WizardHelper::FromJSON($sJson);
  226. $oObj = $oWizardHelper->GetTargetObject();
  227. }
  228. else
  229. {
  230. // Search form: no current object
  231. $oObj = null;
  232. }
  233. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  234. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  235. }
  236. break;
  237. // ui.extkeywidget
  238. case 'objectSearchForm':
  239. $oPage->SetContentType('text/html');
  240. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  241. $iInputId = utils::ReadParam('iInputId', '');
  242. $sTitle = utils::ReadParam('sTitle', '', false, 'raw_data');
  243. $sAttCode = utils::ReadParam('sAttCode', '');
  244. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  245. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  246. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  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->GetSearchDialog($oPage, $sTitle, $oObj);
  258. break;
  259. // ui.extkeywidget
  260. case 'objectCreationForm':
  261. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  262. $iInputId = utils::ReadParam('iInputId', '');
  263. $sAttCode = utils::ReadParam('sAttCode', '');
  264. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  265. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  266. if (!empty($sJson))
  267. {
  268. $oWizardHelper = WizardHelper::FromJSON($sJson);
  269. $oObj = $oWizardHelper->GetTargetObject();
  270. }
  271. else
  272. {
  273. // Search form: no current object
  274. $oObj = null;
  275. }
  276. $oWidget->GetObjectCreationForm($oPage, $oObj);
  277. break;
  278. // ui.extkeywidget
  279. case 'doCreateObject':
  280. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  281. $iInputId = utils::ReadParam('iInputId', '');
  282. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  283. $sAttCode = utils::ReadParam('sAttCode', '');
  284. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  285. $aResult = $oWidget->DoCreateObject($oPage);
  286. echo json_encode($aResult);
  287. break;
  288. // ui.extkeywidget
  289. case 'getObjectName':
  290. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  291. $iInputId = utils::ReadParam('iInputId', '');
  292. $iObjectId = utils::ReadParam('iObjectId', '');
  293. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  294. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  295. $sName = $oWidget->GetObjectName($iObjectId);
  296. echo json_encode(array('name' => $sName));
  297. break;
  298. // ui.extkeywidget
  299. case 'displayHierarchy':
  300. $oPage->SetContentType('text/html');
  301. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  302. $sInputId = utils::ReadParam('sInputId', '');
  303. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  304. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  305. $currValue = utils::ReadParam('value', '');
  306. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  307. if (!empty($sJson))
  308. {
  309. $oWizardHelper = WizardHelper::FromJSON($sJson);
  310. $oObj = $oWizardHelper->GetTargetObject();
  311. }
  312. else
  313. {
  314. // Search form: no current object
  315. $oObj = null;
  316. }
  317. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId, '', $bSearchMode);
  318. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  319. break;
  320. ////////////////////////////////////////////////////
  321. // ui.linkswidget
  322. case 'doAddObjects':
  323. $oPage->SetContentType('text/html');
  324. $sAttCode = utils::ReadParam('sAttCode', '');
  325. $iInputId = utils::ReadParam('iInputId', '');
  326. $sSuffix = utils::ReadParam('sSuffix', '');
  327. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
  328. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  329. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  330. $oWizardHelper = WizardHelper::FromJSON($sJson);
  331. $oObj = $oWizardHelper->GetTargetObject();
  332. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  333. if ($sFilter != '')
  334. {
  335. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  336. }
  337. else
  338. {
  339. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  340. }
  341. $oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);
  342. break;
  343. case 'wizard_helper_preview':
  344. $oPage->SetContentType('text/html');
  345. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  346. $oWizardHelper = WizardHelper::FromJSON($sJson);
  347. $oObj = $oWizardHelper->GetTargetObject();
  348. $oObj->DisplayBareProperties($oPage);
  349. break;
  350. case 'wizard_helper':
  351. $oPage->SetContentType('text/html');
  352. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  353. $oWizardHelper = WizardHelper::FromJSON($sJson);
  354. $oObj = $oWizardHelper->GetTargetObject();
  355. $sClass = $oWizardHelper->GetTargetClass();
  356. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  357. {
  358. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  359. $defaultValue = $oAttDef->GetDefaultValue();
  360. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  361. $oObj->Set($sAttCode, $defaultValue);
  362. }
  363. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  364. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  365. {
  366. $sId = $oWizardHelper->GetIdForField($sAttCode);
  367. if ($sId != '')
  368. {
  369. if ($oObj->IsNew())
  370. {
  371. $iFlags = $oObj->GetInitialStateAttributeFlags($sAttCode);
  372. }
  373. else
  374. {
  375. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  376. }
  377. if ($iFlags & OPT_ATT_READONLY)
  378. {
  379. $sHTMLValue = "<span id=\"field_{$sId}\">".$oObj->GetAsHTML($sAttCode);
  380. $sHTMLValue .= '<input type="hidden" id="'.$sId.'" name="attr_'.$sFormPrefix.$sAttCode.'" value="'.htmlentities($oObj->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/></span>';
  381. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  382. }
  383. else
  384. {
  385. // It may happen that the field we'd like to update does not
  386. // exist in the form. For example, if the field should be hidden/read-only
  387. // in the current state of the object
  388. $value = $oObj->Get($sAttCode);
  389. $displayValue = $oObj->GetEditValue($sAttCode);
  390. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  391. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  392. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  393. // Make sure that we immediately validate the field when we reload it
  394. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  395. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  396. }
  397. }
  398. }
  399. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  400. break;
  401. case 'obj_creation_form':
  402. $oPage->SetContentType('text/html');
  403. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  404. $oWizardHelper = WizardHelper::FromJSON($sJson);
  405. $oObj = $oWizardHelper->GetTargetObject();
  406. $sClass = $oWizardHelper->GetTargetClass();
  407. $sTargetState = utils::ReadParam('target_state', '');
  408. $iTransactionId = utils::ReadParam('transaction_id', '');
  409. $oObj->Set(MetaModel::GetStateAttributeCode($sClass), $sTargetState);
  410. cmdbAbstractObject::DisplayCreationForm($oPage, $sClass, $oObj, array(), array('action' => utils::GetAbsoluteUrlAppRoot().'pages/UI.php', 'transaction_id' => $iTransactionId));
  411. break;
  412. // DisplayBlock
  413. case 'ajax':
  414. $oPage->SetContentType('text/html');
  415. if ($sFilter != "")
  416. {
  417. $sExtraParams = stripslashes(utils::ReadParam('extra_params', '', false, 'raw_data'));
  418. $aExtraParams = array();
  419. if (!empty($sExtraParams))
  420. {
  421. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  422. }
  423. // Restore the app context from the ExtraParams
  424. $oAppContext = new ApplicationContext(false); // false => don't read the context yet !
  425. $aContext = array();
  426. foreach($oAppContext->GetNames() as $sName)
  427. {
  428. $sParamName = 'c['.$sName.']';
  429. if (isset($aExtraParams[$sParamName]))
  430. {
  431. $aContext[$sName] = $aExtraParams[$sParamName];
  432. }
  433. }
  434. $_REQUEST['c'] = $aContext;
  435. if ($sEncoding == 'oql')
  436. {
  437. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  438. }
  439. else
  440. {
  441. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  442. }
  443. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  444. $aExtraParams['display_limit'] = true;
  445. $aExtraParams['truncated'] = true;
  446. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  447. }
  448. else
  449. {
  450. $oPage->p("Invalid query (empty filter).");
  451. }
  452. break;
  453. case 'displayCSVHistory':
  454. $oPage->SetContentType('text/html');
  455. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  456. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  457. break;
  458. case 'details':
  459. $oPage->SetContentType('text/html');
  460. $key = utils::ReadParam('id', 0);
  461. $oFilter = new DBObjectSearch($sClass);
  462. $oFilter->AddCondition('id', $key, '=');
  463. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  464. $oDisplayBlock->RenderContent($oPage);
  465. break;
  466. case 'pie_chart':
  467. $oPage->SetContentType('application/json');
  468. $sGroupBy = utils::ReadParam('group_by', '');
  469. if ($sFilter != '')
  470. {
  471. if ($sEncoding == 'oql')
  472. {
  473. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  474. }
  475. else
  476. {
  477. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  478. }
  479. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  480. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  481. }
  482. else
  483. {
  484. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  485. }
  486. break;
  487. case 'open_flash_chart':
  488. // Workaround for IE8 + IIS + HTTPS
  489. // See TRAC #363, fix described here: http://forums.codecharge.com/posts.php?post_id=97771
  490. $oPage->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
  491. $oPage->add_header("Cache-Control: cache, must-revalidate");
  492. $oPage->add_header("Pragma: public");
  493. $oPage->SetContentType('application/json');
  494. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  495. if ($sFilter != '')
  496. {
  497. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  498. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  499. $oDisplayBlock->RenderContent($oPage, $aParams);
  500. }
  501. else
  502. {
  503. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  504. }
  505. break;
  506. case 'modal_details':
  507. $oPage->SetContentType('text/html');
  508. $key = utils::ReadParam('id', 0);
  509. $oFilter = new DBObjectSearch($sClass);
  510. $oFilter->AddCondition('id', $key, '=');
  511. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  512. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  513. $oDisplayBlock->RenderContent($oPage);
  514. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  515. break;
  516. case 'link':
  517. $oPage->SetContentType('text/html');
  518. $sClass = utils::ReadParam('sclass', 'logInfra', false, 'class');
  519. $sAttCode = utils::ReadParam('attCode', 'name');
  520. //$sOrg = utils::ReadParam('org_id', '');
  521. $sName = utils::ReadParam('q', '');
  522. $iMaxCount = utils::ReadParam('max', 30);
  523. $iCount = 0;
  524. $oFilter = new DBObjectSearch($sClass);
  525. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  526. //$oFilter->AddCondition('org_id', $sOrg, '=');
  527. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  528. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  529. {
  530. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  531. $iCount++;
  532. }
  533. break;
  534. case 'combo_options':
  535. $oPage->SetContentType('text/html');
  536. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  537. $oSet = new CMDBObjectSet($oFilter);
  538. while( $oObj = $oSet->fetch())
  539. {
  540. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  541. }
  542. break;
  543. case 'display_document':
  544. $id = utils::ReadParam('id', '');
  545. $sField = utils::ReadParam('field', '');
  546. if (!empty($sClass) && !empty($id) && !empty($sField))
  547. {
  548. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  549. }
  550. break;
  551. case 'download_document':
  552. $id = utils::ReadParam('id', '');
  553. $sField = utils::ReadParam('field', '');
  554. if (!empty($sClass) && !empty($id) && !empty($sField))
  555. {
  556. DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  557. }
  558. break;
  559. case 'search_form':
  560. $oPage->SetContentType('text/html');
  561. $sClass = utils::ReadParam('className', '', false, 'class');
  562. $sRootClass = utils::ReadParam('baseClass', '', false, 'class');
  563. $currentId = utils::ReadParam('currentId', '');
  564. $sAction = utils::ReadParam('action', '');
  565. $oFilter = new DBObjectSearch($sClass);
  566. $oSet = new CMDBObjectSet($oFilter);
  567. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction));
  568. $oPage->add($sHtml);
  569. break;
  570. case 'set_pref':
  571. $sCode = utils::ReadPostedParam('code', '');
  572. $sValue = utils::ReadPostedParam('value', '', 'raw_data');
  573. appUserPreferences::SetPref($sCode, $sValue);
  574. break;
  575. case 'erase_all_pref':
  576. // Can be useful in case a user got some corrupted prefs...
  577. appUserPreferences::ClearPreferences();
  578. break;
  579. case 'on_form_cancel':
  580. // Called when a creation/modification form is cancelled by the end-user
  581. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  582. $iTransactionId = utils::ReadParam('transaction_id', 0);
  583. $sTempId = session_id().'_'.$iTransactionId;
  584. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  585. {
  586. $oExtensionInstance->OnFormCancel($sTempId);
  587. }
  588. break;
  589. default:
  590. $oPage->p("Invalid query.");
  591. }
  592. $oPage->output();
  593. }
  594. catch (Exception $e)
  595. {
  596. echo $e->GetMessage();
  597. IssueLog::Error($e->getMessage());
  598. }
  599. /**
  600. * Downloads a document to the browser, either as 'inline' or 'attachment'
  601. *
  602. * @param WebPage $oPage The web page for the output
  603. * @param string $sClass Class name of the object
  604. * @param mixed $id Identifier of the object
  605. * @param string $sAttCode Name of the attribute containing the document to download
  606. * @param string $sContentDisposition Either 'inline' or 'attachment'
  607. * @return none
  608. */
  609. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  610. {
  611. try
  612. {
  613. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  614. if (!is_object($oObj))
  615. {
  616. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  617. }
  618. $oDocument = $oObj->Get($sAttCode);
  619. if (is_object($oDocument))
  620. {
  621. $oPage->SetContentType($oDocument->GetMimeType());
  622. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  623. $oPage->add($oDocument->GetData());
  624. }
  625. }
  626. catch(Exception $e)
  627. {
  628. $oPage->p($e->getMessage());
  629. }
  630. }
  631. ?>