ajax.render.php 35 KB

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