ajax.render.php 42 KB

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