ajax.render.php 25 KB

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