ajax.render.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. $aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']);
  163. }
  164. }
  165. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  166. $oSettings->iDefaultPageSize = $iPageSize;
  167. $oSettings->aColumns = $aColumns;
  168. if ($bSaveAsDefaults)
  169. {
  170. $bRet = $oSettings->SaveAsDefault();
  171. }
  172. else
  173. {
  174. $bRet = $oSettings->Save();
  175. }
  176. $oPage->add($bRet ? 'Ok' : 'KO');
  177. break;
  178. case 'datatable_reset_settings':
  179. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  180. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  181. $bResetAll = (utils::ReadParam('defaults', 'true') == 'true');
  182. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  183. $bRet = $oSettings->ResetToDefault($bResetAll);
  184. $oPage->add($bRet ? 'Ok' : 'KO');
  185. break;
  186. // ui.linkswidget
  187. case 'addObjects':
  188. $oPage->SetContentType('text/html');
  189. $sAttCode = utils::ReadParam('sAttCode', '');
  190. $iInputId = utils::ReadParam('iInputId', '');
  191. $sSuffix = utils::ReadParam('sSuffix', '');
  192. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  193. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  194. if (!empty($sJson))
  195. {
  196. $oWizardHelper = WizardHelper::FromJSON($sJson);
  197. $oObj = $oWizardHelper->GetTargetObject();
  198. }
  199. else
  200. {
  201. // Search form: no current object
  202. $oObj = null;
  203. }
  204. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  205. $oWidget->GetObjectPickerDialog($oPage, $oObj);
  206. break;
  207. // ui.linkswidget
  208. case 'searchObjectsToAdd':
  209. $oPage->SetContentType('text/html');
  210. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  211. $sAttCode = utils::ReadParam('sAttCode', '');
  212. $iInputId = utils::ReadParam('iInputId', '');
  213. $sSuffix = utils::ReadParam('sSuffix', '');
  214. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  215. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  216. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  217. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  218. break;
  219. //ui.linksdirectwidget
  220. case 'createObject':
  221. $sClass = utils::ReadParam('class', '', false, 'class');
  222. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  223. $sAttCode = utils::ReadParam('att_code', '');
  224. $iInputId = utils::ReadParam('iInputId', '');
  225. $oPage->SetContentType('text/html');
  226. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  227. $oWidget->GetObjectCreationDlg($oPage, $sRealClass);
  228. break;
  229. // ui.linksdirectwidget
  230. case 'getLinksetRow':
  231. $sClass = utils::ReadParam('class', '', false, 'class');
  232. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  233. $sAttCode = utils::ReadParam('att_code', '');
  234. $iInputId = utils::ReadParam('iInputId', '');
  235. $iTempId = utils::ReadParam('tempId', '');
  236. $aValues = utils::ReadParam('values', array(), false, 'raw_data');
  237. $oPage->SetContentType('text/html');
  238. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  239. $oPage->add($oWidget->GetRow($oPage, $sRealClass, $aValues, $iTempId));
  240. break;
  241. ////////////////////////////////////////////////////////////
  242. // ui.extkeywidget
  243. case 'searchObjectsToSelect':
  244. $oPage->SetContentType('text/html');
  245. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  246. $iInputId = utils::ReadParam('iInputId', '');
  247. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  248. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  249. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  250. $sAttCode = utils::ReadParam('sAttCode', '');
  251. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  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, $sAttCode, $bSearchMode);
  263. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  264. break;
  265. // ui.extkeywidget: autocomplete
  266. case 'ac_extkey':
  267. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  268. $iInputId = utils::ReadParam('iInputId', '');
  269. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  270. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  271. $sContains = utils::ReadParam('q', '', false, 'raw_data');
  272. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  273. if ($sContains !='')
  274. {
  275. if (!empty($sJson))
  276. {
  277. $oWizardHelper = WizardHelper::FromJSON($sJson);
  278. $oObj = $oWizardHelper->GetTargetObject();
  279. }
  280. else
  281. {
  282. // Search form: no current object
  283. $oObj = null;
  284. }
  285. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  286. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  287. }
  288. break;
  289. // ui.extkeywidget
  290. case 'objectSearchForm':
  291. $oPage->SetContentType('text/html');
  292. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  293. $iInputId = utils::ReadParam('iInputId', '');
  294. $sTitle = utils::ReadParam('sTitle', '', false, 'raw_data');
  295. $sAttCode = utils::ReadParam('sAttCode', '');
  296. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  297. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  298. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  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->GetSearchDialog($oPage, $sTitle, $oObj);
  310. break;
  311. // ui.extkeywidget
  312. case 'objectCreationForm':
  313. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  314. $iInputId = utils::ReadParam('iInputId', '');
  315. $sAttCode = utils::ReadParam('sAttCode', '');
  316. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  317. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  318. if (!empty($sJson))
  319. {
  320. $oWizardHelper = WizardHelper::FromJSON($sJson);
  321. $oObj = $oWizardHelper->GetTargetObject();
  322. }
  323. else
  324. {
  325. // Search form: no current object
  326. $oObj = null;
  327. }
  328. $oWidget->GetObjectCreationForm($oPage, $oObj);
  329. break;
  330. // ui.extkeywidget
  331. case 'doCreateObject':
  332. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  333. $iInputId = utils::ReadParam('iInputId', '');
  334. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  335. $sAttCode = utils::ReadParam('sAttCode', '');
  336. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  337. $aResult = $oWidget->DoCreateObject($oPage);
  338. echo json_encode($aResult);
  339. break;
  340. // ui.extkeywidget
  341. case 'getObjectName':
  342. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  343. $iInputId = utils::ReadParam('iInputId', '');
  344. $iObjectId = utils::ReadParam('iObjectId', '');
  345. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  346. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  347. $sName = $oWidget->GetObjectName($iObjectId);
  348. echo json_encode(array('name' => $sName));
  349. break;
  350. // ui.extkeywidget
  351. case 'displayHierarchy':
  352. $oPage->SetContentType('text/html');
  353. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  354. $sInputId = utils::ReadParam('sInputId', '');
  355. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  356. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  357. $currValue = utils::ReadParam('value', '');
  358. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  359. if (!empty($sJson))
  360. {
  361. $oWizardHelper = WizardHelper::FromJSON($sJson);
  362. $oObj = $oWizardHelper->GetTargetObject();
  363. }
  364. else
  365. {
  366. // Search form: no current object
  367. $oObj = null;
  368. }
  369. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId, '', $bSearchMode);
  370. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  371. break;
  372. ////////////////////////////////////////////////////
  373. // ui.linkswidget
  374. case 'doAddObjects':
  375. $oPage->SetContentType('text/html');
  376. $sAttCode = utils::ReadParam('sAttCode', '');
  377. $iInputId = utils::ReadParam('iInputId', '');
  378. $sSuffix = utils::ReadParam('sSuffix', '');
  379. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
  380. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  381. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  382. $oWizardHelper = WizardHelper::FromJSON($sJson);
  383. $oObj = $oWizardHelper->GetTargetObject();
  384. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  385. if ($sFilter != '')
  386. {
  387. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  388. }
  389. else
  390. {
  391. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  392. }
  393. $oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);
  394. break;
  395. case 'wizard_helper_preview':
  396. $oPage->SetContentType('text/html');
  397. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  398. $oWizardHelper = WizardHelper::FromJSON($sJson);
  399. $oObj = $oWizardHelper->GetTargetObject();
  400. $oObj->DisplayBareProperties($oPage);
  401. break;
  402. case 'wizard_helper':
  403. $oPage->SetContentType('text/html');
  404. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  405. $oWizardHelper = WizardHelper::FromJSON($sJson);
  406. $oObj = $oWizardHelper->GetTargetObject();
  407. $sClass = $oWizardHelper->GetTargetClass();
  408. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  409. {
  410. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  411. $defaultValue = $oAttDef->GetDefaultValue();
  412. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  413. $oObj->Set($sAttCode, $defaultValue);
  414. }
  415. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  416. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  417. {
  418. $sId = $oWizardHelper->GetIdForField($sAttCode);
  419. if ($sId != '')
  420. {
  421. if ($oObj->IsNew())
  422. {
  423. $iFlags = $oObj->GetInitialStateAttributeFlags($sAttCode);
  424. }
  425. else
  426. {
  427. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  428. }
  429. if ($iFlags & OPT_ATT_READONLY)
  430. {
  431. $sHTMLValue = "<span id=\"field_{$sId}\">".$oObj->GetAsHTML($sAttCode);
  432. $sHTMLValue .= '<input type="hidden" id="'.$sId.'" name="attr_'.$sFormPrefix.$sAttCode.'" value="'.htmlentities($oObj->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/></span>';
  433. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  434. }
  435. else
  436. {
  437. // It may happen that the field we'd like to update does not
  438. // exist in the form. For example, if the field should be hidden/read-only
  439. // in the current state of the object
  440. $value = $oObj->Get($sAttCode);
  441. $displayValue = $oObj->GetEditValue($sAttCode);
  442. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  443. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  444. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  445. // Make sure that we immediately validate the field when we reload it
  446. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  447. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  448. }
  449. }
  450. }
  451. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  452. break;
  453. case 'obj_creation_form':
  454. $oPage->SetContentType('text/html');
  455. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  456. $oWizardHelper = WizardHelper::FromJSON($sJson);
  457. $oObj = $oWizardHelper->GetTargetObject();
  458. $sClass = $oWizardHelper->GetTargetClass();
  459. $sTargetState = utils::ReadParam('target_state', '');
  460. $iTransactionId = utils::ReadParam('transaction_id', '');
  461. $oObj->Set(MetaModel::GetStateAttributeCode($sClass), $sTargetState);
  462. cmdbAbstractObject::DisplayCreationForm($oPage, $sClass, $oObj, array(), array('action' => utils::GetAbsoluteUrlAppRoot().'pages/UI.php', 'transaction_id' => $iTransactionId));
  463. break;
  464. // DisplayBlock
  465. case 'ajax':
  466. $oPage->SetContentType('text/html');
  467. if ($sFilter != "")
  468. {
  469. $sExtraParams = stripslashes(utils::ReadParam('extra_params', '', false, 'raw_data'));
  470. $aExtraParams = array();
  471. if (!empty($sExtraParams))
  472. {
  473. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  474. }
  475. // Restore the app context from the ExtraParams
  476. $oAppContext = new ApplicationContext(false); // false => don't read the context yet !
  477. $aContext = array();
  478. foreach($oAppContext->GetNames() as $sName)
  479. {
  480. $sParamName = 'c['.$sName.']';
  481. if (isset($aExtraParams[$sParamName]))
  482. {
  483. $aContext[$sName] = $aExtraParams[$sParamName];
  484. }
  485. }
  486. $_REQUEST['c'] = $aContext;
  487. if ($sEncoding == 'oql')
  488. {
  489. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  490. }
  491. else
  492. {
  493. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  494. }
  495. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  496. $aExtraParams['display_limit'] = true;
  497. $aExtraParams['truncated'] = true;
  498. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  499. }
  500. else
  501. {
  502. $oPage->p("Invalid query (empty filter).");
  503. }
  504. break;
  505. case 'displayCSVHistory':
  506. $oPage->SetContentType('text/html');
  507. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  508. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  509. break;
  510. case 'details':
  511. $oPage->SetContentType('text/html');
  512. $key = utils::ReadParam('id', 0);
  513. $oFilter = new DBObjectSearch($sClass);
  514. $oFilter->AddCondition('id', $key, '=');
  515. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  516. $oDisplayBlock->RenderContent($oPage);
  517. break;
  518. case 'pie_chart':
  519. $oPage->SetContentType('application/json');
  520. $sGroupBy = utils::ReadParam('group_by', '');
  521. if ($sFilter != '')
  522. {
  523. if ($sEncoding == 'oql')
  524. {
  525. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  526. }
  527. else
  528. {
  529. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  530. }
  531. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  532. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  533. }
  534. else
  535. {
  536. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  537. }
  538. break;
  539. case 'open_flash_chart':
  540. // Workaround for IE8 + IIS + HTTPS
  541. // See TRAC #363, fix described here: http://forums.codecharge.com/posts.php?post_id=97771
  542. $oPage->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
  543. $oPage->add_header("Cache-Control: cache, must-revalidate");
  544. $oPage->add_header("Pragma: public");
  545. $oPage->SetContentType('application/json');
  546. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  547. if ($sFilter != '')
  548. {
  549. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  550. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  551. $oDisplayBlock->RenderContent($oPage, $aParams);
  552. }
  553. else
  554. {
  555. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  556. }
  557. break;
  558. case 'modal_details':
  559. $oPage->SetContentType('text/html');
  560. $key = utils::ReadParam('id', 0);
  561. $oFilter = new DBObjectSearch($sClass);
  562. $oFilter->AddCondition('id', $key, '=');
  563. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  564. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  565. $oDisplayBlock->RenderContent($oPage);
  566. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  567. break;
  568. case 'link':
  569. $oPage->SetContentType('text/html');
  570. $sClass = utils::ReadParam('sclass', 'logInfra', false, 'class');
  571. $sAttCode = utils::ReadParam('attCode', 'name');
  572. //$sOrg = utils::ReadParam('org_id', '');
  573. $sName = utils::ReadParam('q', '');
  574. $iMaxCount = utils::ReadParam('max', 30);
  575. $iCount = 0;
  576. $oFilter = new DBObjectSearch($sClass);
  577. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  578. //$oFilter->AddCondition('org_id', $sOrg, '=');
  579. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  580. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  581. {
  582. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  583. $iCount++;
  584. }
  585. break;
  586. case 'combo_options':
  587. $oPage->SetContentType('text/html');
  588. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  589. $oSet = new CMDBObjectSet($oFilter);
  590. while( $oObj = $oSet->fetch())
  591. {
  592. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  593. }
  594. break;
  595. case 'display_document':
  596. $id = utils::ReadParam('id', '');
  597. $sField = utils::ReadParam('field', '');
  598. if (!empty($sClass) && !empty($id) && !empty($sField))
  599. {
  600. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  601. }
  602. break;
  603. case 'download_document':
  604. $id = utils::ReadParam('id', '');
  605. $sField = utils::ReadParam('field', '');
  606. if (!empty($sClass) && !empty($id) && !empty($sField))
  607. {
  608. DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  609. }
  610. break;
  611. case 'search_form':
  612. $oPage->SetContentType('text/html');
  613. $sClass = utils::ReadParam('className', '', false, 'class');
  614. $sRootClass = utils::ReadParam('baseClass', '', false, 'class');
  615. $currentId = utils::ReadParam('currentId', '');
  616. $sTableId = utils::ReadParam('_table_id_', null, false, 'raw_data');
  617. $sAction = utils::ReadParam('action', '');
  618. $oFilter = new DBObjectSearch($sClass);
  619. $oSet = new CMDBObjectSet($oFilter);
  620. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction, 'table_id' => $sTableId));
  621. $oPage->add($sHtml);
  622. break;
  623. case 'set_pref':
  624. $sCode = utils::ReadPostedParam('code', '');
  625. $sValue = utils::ReadPostedParam('value', '', 'raw_data');
  626. appUserPreferences::SetPref($sCode, $sValue);
  627. break;
  628. case 'erase_all_pref':
  629. // Can be useful in case a user got some corrupted prefs...
  630. appUserPreferences::ClearPreferences();
  631. break;
  632. case 'on_form_cancel':
  633. // Called when a creation/modification form is cancelled by the end-user
  634. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  635. $iTransactionId = utils::ReadParam('transaction_id', 0);
  636. $sTempId = session_id().'_'.$iTransactionId;
  637. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  638. {
  639. $oExtensionInstance->OnFormCancel($sTempId);
  640. }
  641. break;
  642. case 'dashboard_editor':
  643. $sId = utils::ReadParam('id', '', false, 'raw_data');
  644. ApplicationMenu::LoadAdditionalMenus();
  645. $idx = ApplicationMenu::GetMenuIndexById($sId);
  646. $oMenu = ApplicationMenu::GetMenuNode($idx);
  647. $oMenu->RenderEditor($oPage);
  648. break;
  649. case 'new_dashlet':
  650. require_once(APPROOT.'application/forms.class.inc.php');
  651. require_once(APPROOT.'application/dashlet.class.inc.php');
  652. $sDashletClass = utils::ReadParam('dashlet_class', '');
  653. $sDashletId = utils::ReadParam('dashlet_id', '', false, 'raw_data');
  654. if (is_subclass_of($sDashletClass, 'Dashlet'))
  655. {
  656. $oDashlet = new $sDashletClass($sDashletId);
  657. $offset = $oPage->start_capture();
  658. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  659. $sHtml = addslashes($oPage->end_capture($offset));
  660. $sHtml = str_replace("\n", '', $sHtml);
  661. $sHtml = str_replace("\r", '', $sHtml);
  662. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml')"); // in ajax web page add_script has the same effect as add_ready_script
  663. // but is executed BEFORE all 'ready_scripts'
  664. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  665. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  666. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, ':itop-dashboard'));
  667. $sHtml = str_replace("\n", '', $sHtml);
  668. $sHtml = str_replace("\r", '', $sHtml);
  669. $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'
  670. }
  671. break;
  672. case 'update_dashlet_property':
  673. require_once(APPROOT.'application/forms.class.inc.php');
  674. require_once(APPROOT.'application/dashlet.class.inc.php');
  675. $aParams = utils::ReadParam('params', '', false, 'raw_data');
  676. $sDashletClass = $aParams['attr_dashlet_class'];
  677. $sDashletId = $aParams['attr_dashlet_id'];
  678. $aUpdatedProperties = $aParams['updated']; // Code of the changed properties as an array: 'attr_xxx', 'attr_xxy', etc...
  679. $aPreviousValues = $aParams['previous_values']; // hash array: 'attr_xxx' => 'old_value'
  680. if (is_subclass_of($sDashletClass, 'Dashlet'))
  681. {
  682. $oDashlet = new $sDashletClass($sDashletId);
  683. $oForm = $oDashlet->GetForm();
  684. $aValues = $oForm->ReadParams(); // hash array: 'xxx' => 'new_value'
  685. $aCurrentValues = $aValues;
  686. $aUpdatedDecoded = array();
  687. foreach($aUpdatedProperties as $sProp)
  688. {
  689. $sDecodedProp = str_replace('attr_', '', $sProp); // Remove the attr_ prefix
  690. $aCurrentValues[$sDecodedProp] = $aPreviousValues[$sProp]; // Set the previous value
  691. $aUpdatedDecoded[] = $sDecodedProp;
  692. }
  693. $oDashlet->FromParams($aCurrentValues);
  694. $sPrevClass = get_class($oDashlet);
  695. $oDashlet = $oDashlet->Update($aValues, $aUpdatedDecoded);
  696. $sNewClass = get_class($oDashlet);
  697. if ($sNewClass != $sPrevClass)
  698. {
  699. $oPage->add_ready_script("$('#dashlet_$sDashletId').dashlet('option', {dashlet_class: '$sNewClass'});");
  700. }
  701. if ($oDashlet->IsRedrawNeeded())
  702. {
  703. $offset = $oPage->start_capture();
  704. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  705. $sHtml = addslashes($oPage->end_capture($offset));
  706. $sHtml = str_replace("\n", '', $sHtml);
  707. $sHtml = str_replace("\r", '', $sHtml);
  708. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  709. // but is executed BEFORE all 'ready_scripts'
  710. }
  711. if ($oDashlet->IsFormRedrawNeeded())
  712. {
  713. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  714. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  715. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, ':itop-dashboard'));
  716. $sHtml = str_replace("\n", '', $sHtml);
  717. $sHtml = str_replace("\r", '', $sHtml);
  718. $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'
  719. // but is executed BEFORE all 'ready_scripts'
  720. }
  721. }
  722. break;
  723. case 'save_dashboard':
  724. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  725. $aParams = array();
  726. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  727. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  728. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  729. $oDashboard = new RuntimeDashboard($sDashboardId);
  730. $oDashboard->FromParams($aParams);
  731. $oDashboard->Save();
  732. // trigger a reload of the current page since the dashboard just changed
  733. $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
  734. break;
  735. case 'revert_dashboard':
  736. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  737. $oDashboard = new RuntimeDashboard($sDashboardId);
  738. $oDashboard->Revert();
  739. // trigger a reload of the current page since the dashboard just changed
  740. $oPage->add_ready_script("window.location.href=window.location.href;"); // reloads the page, doing a GET even if we arrived via a POST
  741. break;
  742. case 'render_dashboard':
  743. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  744. $aParams = array();
  745. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  746. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  747. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  748. $oDashboard = new RuntimeDashboard($sDashboardId);
  749. $oDashboard->FromParams($aParams);
  750. $oDashboard->Render($oPage, true /* bEditMode */);
  751. break;
  752. case 'dashlet_creation_dlg':
  753. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  754. RuntimeDashboard::GetDashletCreationDlgFromOQL($oPage, $sOQL);
  755. break;
  756. case 'add_dashlet':
  757. $oForm = RuntimeDashboard::GetDashletCreationForm();
  758. $aValues = $oForm->ReadParams();
  759. $sDashletClass = $aValues['dashlet_class'];
  760. $sMenuId = $aValues['menu_id'];
  761. if (is_subclass_of($sDashletClass, 'Dashlet'))
  762. {
  763. $oDashlet = new $sDashletClass(0);
  764. $oDashlet->FromParams($aValues);
  765. ApplicationMenu::LoadAdditionalMenus();
  766. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  767. $oMenu = ApplicationMenu::GetMenuNode($index);
  768. $oMenu->AddDashlet($oDashlet);
  769. // navigate to the dashboard page
  770. if ($aValues['open_editor'])
  771. {
  772. $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
  773. }
  774. }
  775. break;
  776. case 'export_dashboard':
  777. $sMenuId = utils::ReadParam('id', '');
  778. ApplicationMenu::LoadAdditionalMenus();
  779. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  780. $oMenu = ApplicationMenu::GetMenuNode($index);
  781. if ($oMenu instanceof DashboardMenuNode)
  782. {
  783. $oDashboard = $oMenu->GetDashboard();
  784. $oPage->TrashUnexpectedOutput();
  785. $oPage->SetContentType('text/xml');
  786. $oPage->SetContentDisposition('attachment', $oMenu->GetLabel().'.xml');
  787. $oPage->add($oDashboard->ToXml());
  788. }
  789. break;
  790. case 'import_dashboard':
  791. $sMenuId = utils::ReadParam('id', '');
  792. ApplicationMenu::LoadAdditionalMenus();
  793. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  794. $oMenu = ApplicationMenu::GetMenuNode($index);
  795. $aResult = array('error' => '');
  796. try
  797. {
  798. if ($oMenu instanceof DashboardMenuNode)
  799. {
  800. $oDoc = utils::ReadPostedDocument('dashboard_upload_file');
  801. $oDashboard = $oMenu->GetDashboard();
  802. $oDashboard->FromXml($oDoc->GetData());
  803. $oDashboard->Save();
  804. }
  805. else
  806. {
  807. $aResult['error'] = 'Dashboard id="'.$sMenuId.'" not found.';
  808. }
  809. }
  810. catch(DOMException $e)
  811. {
  812. $aResult = array('error' => Dict::S('UI:Error:InvalidDashboardFile'));
  813. }
  814. catch(Exception $e)
  815. {
  816. $aResult = array('error' => $e->getMessage());
  817. }
  818. $oPage->add(json_encode($aResult));
  819. break;
  820. default:
  821. $oPage->p("Invalid query.");
  822. }
  823. $oPage->output();
  824. }
  825. catch (Exception $e)
  826. {
  827. // note: transform to cope with XSS attacks
  828. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  829. echo "<p>Debug trace: <pre>".print_r($e->getTrace(), true)."</pre></p>\n";
  830. IssueLog::Error($e->getMessage());
  831. }
  832. /**
  833. * Downloads a document to the browser, either as 'inline' or 'attachment'
  834. *
  835. * @param WebPage $oPage The web page for the output
  836. * @param string $sClass Class name of the object
  837. * @param mixed $id Identifier of the object
  838. * @param string $sAttCode Name of the attribute containing the document to download
  839. * @param string $sContentDisposition Either 'inline' or 'attachment'
  840. * @return none
  841. */
  842. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  843. {
  844. try
  845. {
  846. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  847. if (!is_object($oObj))
  848. {
  849. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  850. }
  851. $oDocument = $oObj->Get($sAttCode);
  852. if (is_object($oDocument))
  853. {
  854. $oPage->TrashUnexpectedOutput();
  855. $oPage->SetContentType($oDocument->GetMimeType());
  856. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  857. $oPage->add($oDocument->GetData());
  858. }
  859. }
  860. catch(Exception $e)
  861. {
  862. $oPage->p($e->getMessage());
  863. }
  864. }
  865. ?>