ajax.render.php 34 KB

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