ajax.render.php 32 KB

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