ajax.render.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. <?php
  2. // Copyright (C) 2010-2013 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Handles various ajax requests
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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. if (!empty($sSelectMode) && ($sSelectMode != 'none'))
  77. {
  78. // The first column is used for the selection (radio / checkbox) and is not sortable
  79. $iSortCol--;
  80. }
  81. $bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
  82. $aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
  83. $aClassAliases = utils::ReadParam('class_aliases', array());
  84. $iListId = utils::ReadParam('list_id', 0);
  85. //$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, 'list'));
  86. // Filter the list to removed linked set since we are not able to display them here
  87. $aOrderBy = array();
  88. $iSortIndex = 0;
  89. $aColumnsLoad = array();
  90. foreach($aClassAliases as $sAlias => $sClassName)
  91. {
  92. $aColumnsLoad[$sAlias] = array();
  93. foreach($aColumns[$sAlias] as $sAttCode => $aData)
  94. {
  95. if ($aData['checked'] == 'true')
  96. {
  97. $aColumns[$sAlias][$sAttCode]['checked'] = true;
  98. if ($sAttCode == '_key_')
  99. {
  100. if ($iSortCol == $iSortIndex)
  101. {
  102. if (!MetaModel::HasChildrenClasses($oFilter->GetClass()))
  103. {
  104. $aNameSpec = MetaModel::GetNameSpec($oFilter->GetClass());
  105. if ($aNameSpec[0] == '%1$s')
  106. {
  107. // The name is made of a single column, let's sort according to the sort algorithm for this column
  108. $aOrderBy[$aNameSpec[1][0]] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  109. }
  110. else
  111. {
  112. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  113. }
  114. }
  115. else
  116. {
  117. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  118. }
  119. }
  120. }
  121. else
  122. {
  123. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  124. if ($oAttDef instanceof AttributeLinkedSet)
  125. {
  126. // Removed from the display list
  127. unset($aColumns[$sAlias][$sAttCode]);
  128. }
  129. else
  130. {
  131. $aColumnsLoad[$sAlias][] = $sAttCode;
  132. }
  133. if ($iSortCol == $iSortIndex)
  134. {
  135. if ($oAttDef->IsExternalKey())
  136. {
  137. $sSortCol = $sAttCode.'_friendlyname';
  138. }
  139. else
  140. {
  141. $sSortCol = $sAttCode;
  142. }
  143. $aOrderBy[$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  144. }
  145. }
  146. $iSortIndex++;
  147. }
  148. else
  149. {
  150. $aColumns[$sAlias][$sAttCode]['checked'] = false;
  151. }
  152. }
  153. }
  154. // Load only the requested columns
  155. $oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams['query_params'], null, $iEnd-$iStart, $iStart);
  156. $oSet->OptimizeColumnLoad($aColumnsLoad);
  157. $oDataTable = new DataTable($iListId, $oSet, $oSet->GetSelectedClasses());
  158. if ($operation == 'datatable')
  159. {
  160. // Redraw the whole table
  161. $sHtml = $oDataTable->UpdatePager($oPage, $iEnd-$iStart, $iStart); // Set the default page size
  162. $sHtml .= $oDataTable->GetHTMLTable($oPage, $aColumns, $sSelectMode, $iEnd-$iStart, $bDisplayKey, $aExtraParams);
  163. }
  164. else
  165. {
  166. // redraw just the needed rows
  167. $sHtml = $oDataTable->GetAsHTMLTableRows($oPage, $iEnd-$iStart, $aColumns, $sSelectMode, $bDisplayKey, $aExtraParams);
  168. }
  169. $oPage->add($sHtml);
  170. break;
  171. case 'datatable_save_settings':
  172. $oPage->SetContentType('text/plain');
  173. $iPageSize = utils::ReadParam('page_size', 10);
  174. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  175. $bSaveAsDefaults = (utils::ReadParam('defaults', 'true') == 'true');
  176. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  177. $aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
  178. foreach($aColumns as $sAlias => $aList)
  179. {
  180. foreach($aList as $sAttCode => $aData)
  181. {
  182. $aColumns[$sAlias][$sAttCode]['checked'] = ($aData['checked'] == 'true');
  183. $aColumns[$sAlias][$sAttCode]['disabled'] = ($aData['disabled'] == 'true');
  184. $aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']);
  185. }
  186. }
  187. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  188. $oSettings->iDefaultPageSize = $iPageSize;
  189. $oSettings->aColumns = $aColumns;
  190. if ($bSaveAsDefaults)
  191. {
  192. if ($sTableId != null)
  193. {
  194. $oCurrSettings = DataTableSettings::GetTableSettings($aClassAliases, $sTableId, true /* bOnlyTable */ );
  195. if ($oCurrSettings)
  196. {
  197. $oCurrSettings->ResetToDefault(false); // Reset this table to the defaults
  198. }
  199. }
  200. $bRet = $oSettings->SaveAsDefault();
  201. }
  202. else
  203. {
  204. $bRet = $oSettings->Save();
  205. }
  206. $oPage->add($bRet ? 'Ok' : 'KO');
  207. break;
  208. case 'datatable_reset_settings':
  209. $oPage->SetContentType('text/plain');
  210. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  211. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  212. $bResetAll = (utils::ReadParam('defaults', 'true') == 'true');
  213. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  214. $bRet = $oSettings->ResetToDefault($bResetAll);
  215. $oPage->add($bRet ? 'Ok' : 'KO');
  216. break;
  217. // ui.linkswidget
  218. case 'addObjects':
  219. $oPage->SetContentType('text/html');
  220. $sAttCode = utils::ReadParam('sAttCode', '');
  221. $iInputId = utils::ReadParam('iInputId', '');
  222. $sSuffix = utils::ReadParam('sSuffix', '');
  223. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  224. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  225. if (!empty($sJson))
  226. {
  227. $oWizardHelper = WizardHelper::FromJSON($sJson);
  228. $oObj = $oWizardHelper->GetTargetObject();
  229. }
  230. else
  231. {
  232. // Search form: no current object
  233. $oObj = null;
  234. }
  235. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  236. $oWidget->GetObjectPickerDialog($oPage, $oObj);
  237. break;
  238. // ui.linkswidget
  239. case 'searchObjectsToAdd':
  240. $oPage->SetContentType('text/html');
  241. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  242. $sAttCode = utils::ReadParam('sAttCode', '');
  243. $iInputId = utils::ReadParam('iInputId', '');
  244. $sSuffix = utils::ReadParam('sSuffix', '');
  245. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  246. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  247. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  248. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  249. break;
  250. //ui.linksdirectwidget
  251. case 'createObject':
  252. $oPage->SetContentType('text/html');
  253. $sClass = utils::ReadParam('class', '', false, 'class');
  254. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  255. $sAttCode = utils::ReadParam('att_code', '');
  256. $iInputId = utils::ReadParam('iInputId', '');
  257. $oPage->SetContentType('text/html');
  258. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  259. $oWidget->GetObjectCreationDlg($oPage, $sRealClass);
  260. break;
  261. // ui.linksdirectwidget
  262. case 'getLinksetRow':
  263. $oPage->SetContentType('text/html');
  264. $sClass = utils::ReadParam('class', '', false, 'class');
  265. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  266. $sAttCode = utils::ReadParam('att_code', '');
  267. $iInputId = utils::ReadParam('iInputId', '');
  268. $iTempId = utils::ReadParam('tempId', '');
  269. $aValues = utils::ReadParam('values', array(), false, 'raw_data');
  270. $oPage->SetContentType('text/html');
  271. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  272. $oPage->add($oWidget->GetRow($oPage, $sRealClass, $aValues, -$iTempId));
  273. break;
  274. // ui.linksdirectwidget
  275. case 'selectObjectsToAdd':
  276. $oPage->SetContentType('text/html');
  277. $sClass = utils::ReadParam('class', '', false, 'class');
  278. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  279. $oObj = null;
  280. if ($sJson != '')
  281. {
  282. $oWizardHelper = WizardHelper::FromJSON($sJson);
  283. $oObj = $oWizardHelper->GetTargetObject();
  284. }
  285. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  286. $sAttCode = utils::ReadParam('att_code', '');
  287. $iInputId = utils::ReadParam('iInputId', '');
  288. $iCurrObjectId = utils::ReadParam('iObjId', 0);
  289. $oPage->SetContentType('text/html');
  290. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  291. $oWidget->GetObjectsSelectionDlg($oPage, $oObj);
  292. break;
  293. // ui.linksdirectwidget
  294. case 'searchObjectsToAdd2':
  295. $oPage->SetContentType('text/html');
  296. $sClass = utils::ReadParam('class', '', false, 'class');
  297. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  298. $sAttCode = utils::ReadParam('att_code', '');
  299. $iInputId = utils::ReadParam('iInputId', '');
  300. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  301. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  302. $oObj = null;
  303. if ($sJson != '')
  304. {
  305. $oWizardHelper = WizardHelper::FromJSON($sJson);
  306. $oObj = $oWizardHelper->GetTargetObject();
  307. }
  308. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  309. $oWidget->SearchObjectsToAdd($oPage, $sRealClass, $aAlreadyLinked, $oObj);
  310. break;
  311. // ui.linksdirectwidget
  312. case 'doAddObjects2':
  313. $oPage->SetContentType('text/html');
  314. $oPage->SetContentType('text/html');
  315. $sClass = utils::ReadParam('class', '', false, 'class');
  316. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  317. $sAttCode = utils::ReadParam('att_code', '');
  318. $iInputId = utils::ReadParam('iInputId', '');
  319. $iCurrObjectId = utils::ReadParam('iObjId', 0);
  320. $sFilter = utils::ReadParam('filter', '', false, 'raw_data');
  321. if ($sFilter != '')
  322. {
  323. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  324. }
  325. else
  326. {
  327. $oLinksetDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  328. $valuesDef = $oLinksetDef->GetValuesDef();
  329. if ($valuesDef === null)
  330. {
  331. $oFullSetFilter = new DBObjectSearch($oLinksetDef->GetLinkedClass());
  332. }
  333. else
  334. {
  335. if (!$valuesDef instanceof ValueSetObjects)
  336. {
  337. throw new Exception('Error: only ValueSetObjects are supported for "allowed_values" in AttributeLinkedSet ('.$this->sClass.'/'.$this->sAttCode.').');
  338. }
  339. $oFullSetFilter = DBObjectSearch::FromOQL($valuesDef->GetFilterExpression());
  340. }
  341. }
  342. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  343. $oWidget->DoAddObjects($oPage, $oFullSetFilter);
  344. break;
  345. ////////////////////////////////////////////////////////////
  346. // ui.extkeywidget
  347. case 'searchObjectsToSelect':
  348. $oPage->SetContentType('text/html');
  349. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  350. $iInputId = utils::ReadParam('iInputId', '');
  351. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  352. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  353. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  354. $sAttCode = utils::ReadParam('sAttCode', '');
  355. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  356. if (!empty($sJson))
  357. {
  358. $oWizardHelper = WizardHelper::FromJSON($sJson);
  359. $oObj = $oWizardHelper->GetTargetObject();
  360. }
  361. else
  362. {
  363. // Search form: no current object
  364. $oObj = null;
  365. }
  366. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  367. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  368. break;
  369. // ui.extkeywidget: autocomplete
  370. case 'ac_extkey':
  371. $oPage->SetContentType('text/plain');
  372. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  373. $iInputId = utils::ReadParam('iInputId', '');
  374. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  375. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  376. $sContains = utils::ReadParam('q', '', false, 'raw_data');
  377. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  378. if ($sContains !='')
  379. {
  380. if (!empty($sJson))
  381. {
  382. $oWizardHelper = WizardHelper::FromJSON($sJson);
  383. $oObj = $oWizardHelper->GetTargetObject();
  384. }
  385. else
  386. {
  387. // Search form: no current object
  388. $oObj = null;
  389. }
  390. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  391. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  392. }
  393. break;
  394. // ui.extkeywidget
  395. case 'objectSearchForm':
  396. $oPage->SetContentType('text/html');
  397. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  398. $iInputId = utils::ReadParam('iInputId', '');
  399. $sTitle = utils::ReadParam('sTitle', '', false, 'raw_data');
  400. $sAttCode = utils::ReadParam('sAttCode', '');
  401. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  402. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  403. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  404. if (!empty($sJson))
  405. {
  406. $oWizardHelper = WizardHelper::FromJSON($sJson);
  407. $oObj = $oWizardHelper->GetTargetObject();
  408. }
  409. else
  410. {
  411. // Search form: no current object
  412. $oObj = null;
  413. }
  414. $oWidget->GetSearchDialog($oPage, $sTitle, $oObj);
  415. break;
  416. // ui.extkeywidget
  417. case 'objectCreationForm':
  418. $oPage->SetContentType('text/html');
  419. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  420. $iInputId = utils::ReadParam('iInputId', '');
  421. $sAttCode = utils::ReadParam('sAttCode', '');
  422. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  423. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  424. if (!empty($sJson))
  425. {
  426. $oWizardHelper = WizardHelper::FromJSON($sJson);
  427. $oObj = $oWizardHelper->GetTargetObject();
  428. }
  429. else
  430. {
  431. // Search form: no current object
  432. $oObj = null;
  433. }
  434. $oWidget->GetObjectCreationForm($oPage, $oObj);
  435. break;
  436. // ui.extkeywidget
  437. case 'doCreateObject':
  438. $oPage->SetContentType('application/json');
  439. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  440. $iInputId = utils::ReadParam('iInputId', '');
  441. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  442. $sAttCode = utils::ReadParam('sAttCode', '');
  443. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  444. $aResult = $oWidget->DoCreateObject($oPage);
  445. echo json_encode($aResult);
  446. break;
  447. // ui.extkeywidget
  448. case 'getObjectName':
  449. $oPage->SetContentType('application/json');
  450. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  451. $iInputId = utils::ReadParam('iInputId', '');
  452. $iObjectId = utils::ReadParam('iObjectId', '');
  453. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  454. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  455. $sName = $oWidget->GetObjectName($iObjectId);
  456. echo json_encode(array('name' => $sName));
  457. break;
  458. // ui.extkeywidget
  459. case 'displayHierarchy':
  460. $oPage->SetContentType('text/html');
  461. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  462. $sInputId = utils::ReadParam('sInputId', '');
  463. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  464. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  465. $currValue = utils::ReadParam('value', '');
  466. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  467. if (!empty($sJson))
  468. {
  469. $oWizardHelper = WizardHelper::FromJSON($sJson);
  470. $oObj = $oWizardHelper->GetTargetObject();
  471. }
  472. else
  473. {
  474. // Search form: no current object
  475. $oObj = null;
  476. }
  477. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId, '', $bSearchMode);
  478. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  479. break;
  480. ////////////////////////////////////////////////////
  481. // ui.linkswidget
  482. case 'doAddObjects':
  483. $oPage->SetContentType('text/html');
  484. $sAttCode = utils::ReadParam('sAttCode', '');
  485. $iInputId = utils::ReadParam('iInputId', '');
  486. $sSuffix = utils::ReadParam('sSuffix', '');
  487. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
  488. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  489. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  490. $oWizardHelper = WizardHelper::FromJSON($sJson);
  491. $oObj = $oWizardHelper->GetTargetObject();
  492. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  493. if ($sFilter != '')
  494. {
  495. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  496. }
  497. else
  498. {
  499. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  500. }
  501. $oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);
  502. break;
  503. ////////////////////////////////////////////////////////////
  504. case 'wizard_helper_preview':
  505. $oPage->SetContentType('text/html');
  506. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  507. $oWizardHelper = WizardHelper::FromJSON($sJson);
  508. $oObj = $oWizardHelper->GetTargetObject();
  509. $oObj->DisplayBareProperties($oPage);
  510. break;
  511. case 'wizard_helper':
  512. $oPage->SetContentType('text/html');
  513. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  514. $oWizardHelper = WizardHelper::FromJSON($sJson);
  515. $oObj = $oWizardHelper->GetTargetObject();
  516. $sClass = $oWizardHelper->GetTargetClass();
  517. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  518. {
  519. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  520. $defaultValue = $oAttDef->GetDefaultValue();
  521. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  522. $oObj->Set($sAttCode, $defaultValue);
  523. }
  524. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  525. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  526. {
  527. $sId = $oWizardHelper->GetIdForField($sAttCode);
  528. if ($sId != '')
  529. {
  530. if ($oObj->IsNew())
  531. {
  532. $iFlags = $oObj->GetInitialStateAttributeFlags($sAttCode);
  533. }
  534. else
  535. {
  536. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  537. }
  538. if ($iFlags & OPT_ATT_READONLY)
  539. {
  540. $sHTMLValue = "<span id=\"field_{$sId}\">".$oObj->GetAsHTML($sAttCode);
  541. $sHTMLValue .= '<input type="hidden" id="'.$sId.'" name="attr_'.$sFormPrefix.$sAttCode.'" value="'.htmlentities($oObj->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/></span>';
  542. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  543. }
  544. else
  545. {
  546. // It may happen that the field we'd like to update does not
  547. // exist in the form. For example, if the field should be hidden/read-only
  548. // in the current state of the object
  549. $value = $oObj->Get($sAttCode);
  550. $displayValue = $oObj->GetEditValue($sAttCode);
  551. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  552. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  553. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  554. // Make sure that we immediately validate the field when we reload it
  555. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  556. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  557. }
  558. }
  559. }
  560. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  561. break;
  562. case 'obj_creation_form':
  563. $oPage->SetContentType('text/html');
  564. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  565. $oWizardHelper = WizardHelper::FromJSON($sJson);
  566. $oObj = $oWizardHelper->GetTargetObject();
  567. $sClass = $oWizardHelper->GetTargetClass();
  568. $sTargetState = utils::ReadParam('target_state', '');
  569. $iTransactionId = utils::ReadParam('transaction_id', '');
  570. $oObj->Set(MetaModel::GetStateAttributeCode($sClass), $sTargetState);
  571. cmdbAbstractObject::DisplayCreationForm($oPage, $sClass, $oObj, array(), array('action' => utils::GetAbsoluteUrlAppRoot().'pages/UI.php', 'transaction_id' => $iTransactionId));
  572. break;
  573. // DisplayBlock
  574. case 'ajax':
  575. $oPage->SetContentType('text/html');
  576. if ($sFilter != "")
  577. {
  578. $sExtraParams = stripslashes(utils::ReadParam('extra_params', '', false, 'raw_data'));
  579. $aExtraParams = array();
  580. if (!empty($sExtraParams))
  581. {
  582. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  583. }
  584. // Restore the app context from the ExtraParams
  585. $oAppContext = new ApplicationContext(false); // false => don't read the context yet !
  586. $aContext = array();
  587. foreach($oAppContext->GetNames() as $sName)
  588. {
  589. $sParamName = 'c['.$sName.']';
  590. if (isset($aExtraParams[$sParamName]))
  591. {
  592. $aContext[$sName] = $aExtraParams[$sParamName];
  593. }
  594. }
  595. $_REQUEST['c'] = $aContext;
  596. if ($sEncoding == 'oql')
  597. {
  598. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  599. }
  600. else
  601. {
  602. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  603. }
  604. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  605. $aExtraParams['display_limit'] = true;
  606. $aExtraParams['truncated'] = true;
  607. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  608. }
  609. else
  610. {
  611. $oPage->p("Invalid query (empty filter).");
  612. }
  613. break;
  614. case 'displayCSVHistory':
  615. $oPage->SetContentType('text/html');
  616. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  617. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  618. break;
  619. case 'details':
  620. $oPage->SetContentType('text/html');
  621. $key = utils::ReadParam('id', 0);
  622. $oFilter = new DBObjectSearch($sClass);
  623. $oFilter->AddCondition('id', $key, '=');
  624. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  625. $oDisplayBlock->RenderContent($oPage);
  626. break;
  627. case 'pie_chart':
  628. $oPage->SetContentType('application/json');
  629. $sGroupBy = utils::ReadParam('group_by', '');
  630. if ($sFilter != '')
  631. {
  632. if ($sEncoding == 'oql')
  633. {
  634. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  635. }
  636. else
  637. {
  638. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  639. }
  640. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  641. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  642. }
  643. else
  644. {
  645. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  646. }
  647. break;
  648. case 'open_flash_chart':
  649. // Workaround for IE8 + IIS + HTTPS
  650. // See TRAC #363, fix described here: http://forums.codecharge.com/posts.php?post_id=97771
  651. $oPage->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
  652. $oPage->add_header("Cache-Control: cache, must-revalidate");
  653. $oPage->add_header("Pragma: public");
  654. $oPage->SetContentType('application/json');
  655. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  656. if ($sFilter != '')
  657. {
  658. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  659. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  660. $oDisplayBlock->RenderContent($oPage, $aParams);
  661. }
  662. else
  663. {
  664. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  665. }
  666. break;
  667. case 'modal_details':
  668. $oPage->SetContentType('text/html');
  669. $key = utils::ReadParam('id', 0);
  670. $oFilter = new DBObjectSearch($sClass);
  671. $oFilter->AddCondition('id', $key, '=');
  672. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  673. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  674. $oDisplayBlock->RenderContent($oPage);
  675. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  676. break;
  677. case 'link':
  678. $oPage->SetContentType('text/html');
  679. $sClass = utils::ReadParam('sclass', 'logInfra', false, 'class');
  680. $sAttCode = utils::ReadParam('attCode', 'name');
  681. //$sOrg = utils::ReadParam('org_id', '');
  682. $sName = utils::ReadParam('q', '');
  683. $iMaxCount = utils::ReadParam('max', 30);
  684. $iCount = 0;
  685. $oFilter = new DBObjectSearch($sClass);
  686. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  687. //$oFilter->AddCondition('org_id', $sOrg, '=');
  688. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  689. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  690. {
  691. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  692. $iCount++;
  693. }
  694. break;
  695. case 'combo_options':
  696. $oPage->SetContentType('text/html');
  697. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  698. $oSet = new CMDBObjectSet($oFilter);
  699. while( $oObj = $oSet->fetch())
  700. {
  701. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  702. }
  703. break;
  704. case 'display_document':
  705. $id = utils::ReadParam('id', '');
  706. $sField = utils::ReadParam('field', '');
  707. if (!empty($sClass) && !empty($id) && !empty($sField))
  708. {
  709. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  710. }
  711. break;
  712. case 'download_document':
  713. $id = utils::ReadParam('id', '');
  714. $sField = utils::ReadParam('field', '');
  715. if (!empty($sClass) && !empty($id) && !empty($sField))
  716. {
  717. DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  718. }
  719. break;
  720. case 'search_form':
  721. $oPage->SetContentType('text/html');
  722. $sClass = utils::ReadParam('className', '', false, 'class');
  723. $sRootClass = utils::ReadParam('baseClass', '', false, 'class');
  724. $currentId = utils::ReadParam('currentId', '');
  725. $sTableId = utils::ReadParam('_table_id_', null, false, 'raw_data');
  726. $sAction = utils::ReadParam('action', '');
  727. $oFilter = new DBObjectSearch($sClass);
  728. $oSet = new CMDBObjectSet($oFilter);
  729. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction, 'table_id' => $sTableId));
  730. $oPage->add($sHtml);
  731. break;
  732. case 'set_pref':
  733. $sCode = utils::ReadPostedParam('code', '');
  734. $sValue = utils::ReadPostedParam('value', '', 'raw_data');
  735. appUserPreferences::SetPref($sCode, $sValue);
  736. break;
  737. case 'erase_all_pref':
  738. // Can be useful in case a user got some corrupted prefs...
  739. appUserPreferences::ClearPreferences();
  740. break;
  741. case 'on_form_cancel':
  742. // Called when a creation/modification form is cancelled by the end-user
  743. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  744. $iTransactionId = utils::ReadParam('transaction_id', 0);
  745. $sTempId = session_id().'_'.$iTransactionId;
  746. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  747. {
  748. $oExtensionInstance->OnFormCancel($sTempId);
  749. }
  750. break;
  751. case 'reload_dashboard':
  752. $oPage->SetContentType('text/html');
  753. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  754. $aExtraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
  755. ApplicationMenu::LoadAdditionalMenus();
  756. $idx = ApplicationMenu::GetMenuIndexById($sDashboardId);
  757. $oMenu = ApplicationMenu::GetMenuNode($idx);
  758. $oDashboard = $oMenu->GetDashboard();
  759. $oDashboard->Render($oPage, false, $aExtraParams);
  760. break;
  761. case 'dashboard_editor':
  762. $sId = utils::ReadParam('id', '', false, 'raw_data');
  763. ApplicationMenu::LoadAdditionalMenus();
  764. $idx = ApplicationMenu::GetMenuIndexById($sId);
  765. $oMenu = ApplicationMenu::GetMenuNode($idx);
  766. $oMenu->RenderEditor($oPage);
  767. break;
  768. case 'new_dashlet':
  769. require_once(APPROOT.'application/forms.class.inc.php');
  770. require_once(APPROOT.'application/dashlet.class.inc.php');
  771. $sDashletClass = utils::ReadParam('dashlet_class', '');
  772. $sDashletId = utils::ReadParam('dashlet_id', '', false, 'raw_data');
  773. if (is_subclass_of($sDashletClass, 'Dashlet'))
  774. {
  775. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  776. $offset = $oPage->start_capture();
  777. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  778. $sHtml = addslashes($oPage->end_capture($offset));
  779. $sHtml = str_replace("\n", '', $sHtml);
  780. $sHtml = str_replace("\r", '', $sHtml);
  781. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  782. // but is executed BEFORE all 'ready_scripts'
  783. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  784. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  785. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  786. $sHtml = str_replace("\n", '', $sHtml);
  787. $sHtml = str_replace("\r", '', $sHtml);
  788. $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'
  789. }
  790. break;
  791. case 'update_dashlet_property':
  792. require_once(APPROOT.'application/forms.class.inc.php');
  793. require_once(APPROOT.'application/dashlet.class.inc.php');
  794. $aParams = utils::ReadParam('params', '', false, 'raw_data');
  795. $sDashletClass = $aParams['attr_dashlet_class'];
  796. $sDashletId = $aParams['attr_dashlet_id'];
  797. $aUpdatedProperties = $aParams['updated']; // Code of the changed properties as an array: 'attr_xxx', 'attr_xxy', etc...
  798. $aPreviousValues = $aParams['previous_values']; // hash array: 'attr_xxx' => 'old_value'
  799. if (is_subclass_of($sDashletClass, 'Dashlet'))
  800. {
  801. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  802. $oForm = $oDashlet->GetForm();
  803. $aValues = $oForm->ReadParams(); // hash array: 'xxx' => 'new_value'
  804. $aCurrentValues = $aValues;
  805. $aUpdatedDecoded = array();
  806. foreach($aUpdatedProperties as $sProp)
  807. {
  808. $sDecodedProp = str_replace('attr_', '', $sProp); // Remove the attr_ prefix
  809. $aCurrentValues[$sDecodedProp] = $aPreviousValues[$sProp]; // Set the previous value
  810. $aUpdatedDecoded[] = $sDecodedProp;
  811. }
  812. $oDashlet->FromParams($aCurrentValues);
  813. $sPrevClass = get_class($oDashlet);
  814. $oDashlet = $oDashlet->Update($aValues, $aUpdatedDecoded);
  815. $sNewClass = get_class($oDashlet);
  816. if ($sNewClass != $sPrevClass)
  817. {
  818. $oPage->add_ready_script("$('#dashlet_$sDashletId').dashlet('option', {dashlet_class: '$sNewClass'});");
  819. }
  820. if ($oDashlet->IsRedrawNeeded())
  821. {
  822. $offset = $oPage->start_capture();
  823. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  824. $sHtml = addslashes($oPage->end_capture($offset));
  825. $sHtml = str_replace("\n", '', $sHtml);
  826. $sHtml = str_replace("\r", '', $sHtml);
  827. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  828. // but is executed BEFORE all 'ready_scripts'
  829. }
  830. if ($oDashlet->IsFormRedrawNeeded())
  831. {
  832. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  833. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  834. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  835. $sHtml = str_replace("\n", '', $sHtml);
  836. $sHtml = str_replace("\r", '', $sHtml);
  837. $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'
  838. // but is executed BEFORE all 'ready_scripts'
  839. }
  840. }
  841. break;
  842. case 'save_dashboard':
  843. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  844. $aParams = array();
  845. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  846. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  847. $aParams['auto_reload'] = utils::ReadParam('auto_reload', false);
  848. $aParams['auto_reload_sec'] = utils::ReadParam('auto_reload_sec', 300);
  849. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  850. $oDashboard = new RuntimeDashboard($sDashboardId);
  851. $oDashboard->FromParams($aParams);
  852. $oDashboard->Save();
  853. // trigger a reload of the current page since the dashboard just changed
  854. $oPage->add_ready_script(
  855. <<<EOF
  856. var sLocation = new String(window.location.href);
  857. var sNewLocation = sLocation.replace('&edit=1', '');
  858. sNewLocation = sLocation.replace(/#(.?)$/, ''); // Strips everything after the hash, since IF the URL does not change AND contains a hash, then Chrome does not reload the page
  859. window.location.href = sNewLocation;
  860. EOF
  861. );
  862. $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
  863. break;
  864. case 'revert_dashboard':
  865. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  866. $oDashboard = new RuntimeDashboard($sDashboardId);
  867. $oDashboard->Revert();
  868. // trigger a reload of the current page since the dashboard just changed
  869. $oPage->add_ready_script("window.location.href=window.location.href;"); // reloads the page, doing a GET even if we arrived via a POST
  870. break;
  871. case 'render_dashboard':
  872. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  873. $aParams = array();
  874. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  875. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  876. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  877. $aParams['auto_reload'] = utils::ReadParam('auto_reload', false);
  878. $aParams['auto_reload_sec'] = utils::ReadParam('auto_reload_sec', 300);
  879. $oDashboard = new RuntimeDashboard($sDashboardId);
  880. $oDashboard->FromParams($aParams);
  881. $oDashboard->Render($oPage, true /* bEditMode */);
  882. break;
  883. case 'dashlet_creation_dlg':
  884. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  885. RuntimeDashboard::GetDashletCreationDlgFromOQL($oPage, $sOQL);
  886. break;
  887. case 'add_dashlet':
  888. $oForm = RuntimeDashboard::GetDashletCreationForm();
  889. $aValues = $oForm->ReadParams();
  890. $sDashletClass = $aValues['dashlet_class'];
  891. $sMenuId = $aValues['menu_id'];
  892. if (is_subclass_of($sDashletClass, 'Dashlet'))
  893. {
  894. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), 0);
  895. $oDashlet->FromParams($aValues);
  896. ApplicationMenu::LoadAdditionalMenus();
  897. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  898. $oMenu = ApplicationMenu::GetMenuNode($index);
  899. $oMenu->AddDashlet($oDashlet);
  900. // navigate to the dashboard page
  901. if ($aValues['open_editor'])
  902. {
  903. $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
  904. }
  905. }
  906. break;
  907. case 'shortcut_list_dlg':
  908. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  909. $sSourceTableId = utils::ReadParam('source_table_id', '', false, 'raw_data');
  910. ShortcutOQL::GetCreationDlgFromOQL($oPage, $sOQL, $sSourceTableId);
  911. break;
  912. case 'shortcut_list_create':
  913. $oForm = ShortcutOQL::GetCreationForm();
  914. $aValues = $oForm->ReadParams();
  915. $oAppContext = new ApplicationContext();
  916. $aContext = $oAppContext->GetAsHash();
  917. $sContext = serialize($aContext);
  918. $oShortcut = MetaModel::NewObject("ShortcutOQL");
  919. $oShortcut->Set('user_id', UserRights::GetUserId());
  920. $oShortcut->Set("context", $sContext);
  921. $oShortcut->Set("name", $aValues['name']);
  922. $oShortcut->Set("oql", $aValues['oql']);
  923. $iAutoReload = (int)$aValues['auto_reload_sec'];
  924. if (($aValues['auto_reload']) && ($iAutoReload > 0))
  925. {
  926. $oShortcut->Set("auto_reload_sec", max(5, $iAutoReload));
  927. $oShortcut->Set("auto_reload", 'custom');
  928. }
  929. $iId = $oShortcut->DBInsertNoReload();
  930. $oShortcut->CloneTableSettings($aValues['source_table_id']);
  931. // Add the menu node in the right place
  932. //
  933. // Mmmm... already done because the newly created menu is read from the DB
  934. // as soon as we invoke DisplayMenu
  935. // Refresh the menu pane
  936. $aExtraParams = array();
  937. ApplicationMenu::DisplayMenu($oPage, $aExtraParams);
  938. break;
  939. case 'shortcut_rename_dlg':
  940. $oSearch = new DBObjectSearch('Shortcut');
  941. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  942. $iShortcut = $aShortcuts[0];
  943. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  944. $oShortcut->StartRenameDialog($oPage);
  945. break;
  946. case 'shortcut_rename_go':
  947. $iShortcut = utils::ReadParam('id', 0);
  948. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  949. $sName = utils::ReadParam('attr_name', '', false, 'raw_data');
  950. if (strlen($sName) > 0)
  951. {
  952. $oShortcut->Set('name', $sName);
  953. $oShortcut->DBUpdate();
  954. $oPage->add_ready_script('window.location.reload();');
  955. }
  956. break;
  957. case 'shortcut_delete_go':
  958. $oSearch = new DBObjectSearch('Shortcut');
  959. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  960. foreach ($aShortcuts as $iShortcut)
  961. {
  962. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  963. $oShortcut->DBDelete();
  964. $oPage->add_ready_script('window.location.reload();');
  965. }
  966. break;
  967. case 'export_dashboard':
  968. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  969. ApplicationMenu::LoadAdditionalMenus();
  970. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  971. $oMenu = ApplicationMenu::GetMenuNode($index);
  972. if ($oMenu instanceof DashboardMenuNode)
  973. {
  974. $oDashboard = $oMenu->GetDashboard();
  975. $oPage->TrashUnexpectedOutput();
  976. $oPage->SetContentType('text/xml');
  977. $oPage->SetContentDisposition('attachment', $oMenu->GetLabel().'.xml');
  978. $oPage->add($oDashboard->ToXml());
  979. }
  980. break;
  981. case 'import_dashboard':
  982. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  983. ApplicationMenu::LoadAdditionalMenus();
  984. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  985. $oMenu = ApplicationMenu::GetMenuNode($index);
  986. $aResult = array('error' => '');
  987. try
  988. {
  989. if ($oMenu instanceof DashboardMenuNode)
  990. {
  991. $oDoc = utils::ReadPostedDocument('dashboard_upload_file');
  992. $oDashboard = $oMenu->GetDashboard();
  993. $oDashboard->FromXml($oDoc->GetData());
  994. $oDashboard->Save();
  995. }
  996. else
  997. {
  998. $aResult['error'] = 'Dashboard id="'.$sMenuId.'" not found.';
  999. }
  1000. }
  1001. catch(DOMException $e)
  1002. {
  1003. $aResult = array('error' => Dict::S('UI:Error:InvalidDashboardFile'));
  1004. }
  1005. catch(Exception $e)
  1006. {
  1007. $aResult = array('error' => $e->getMessage());
  1008. }
  1009. $oPage->add(json_encode($aResult));
  1010. break;
  1011. default:
  1012. $oPage->p("Invalid query.");
  1013. }
  1014. $oPage->output();
  1015. }
  1016. catch (Exception $e)
  1017. {
  1018. // note: transform to cope with XSS attacks
  1019. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  1020. echo "<p>Debug trace: <pre>".$e->getTraceAsString()."</pre></p>\n";
  1021. IssueLog::Error($e->getMessage());
  1022. }
  1023. /**
  1024. * Downloads a document to the browser, either as 'inline' or 'attachment'
  1025. *
  1026. * @param WebPage $oPage The web page for the output
  1027. * @param string $sClass Class name of the object
  1028. * @param mixed $id Identifier of the object
  1029. * @param string $sAttCode Name of the attribute containing the document to download
  1030. * @param string $sContentDisposition Either 'inline' or 'attachment'
  1031. * @return none
  1032. */
  1033. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  1034. {
  1035. try
  1036. {
  1037. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  1038. if (!is_object($oObj))
  1039. {
  1040. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  1041. }
  1042. $oDocument = $oObj->Get($sAttCode);
  1043. if (is_object($oDocument))
  1044. {
  1045. $oPage->TrashUnexpectedOutput();
  1046. $oPage->SetContentType($oDocument->GetMimeType());
  1047. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  1048. $oPage->add($oDocument->GetData());
  1049. }
  1050. }
  1051. catch(Exception $e)
  1052. {
  1053. $oPage->p($e->getMessage());
  1054. }
  1055. }
  1056. ?>