ajax.render.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. $aExtraParams = array();
  52. if (is_array($extraParams))
  53. {
  54. $aExtraParams = $extraParams;
  55. }
  56. else
  57. {
  58. $sExtraParams = stripslashes($extraParams);
  59. if (!empty($sExtraParams))
  60. {
  61. $val = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  62. if ($val !== null)
  63. {
  64. $aExtraParams = $val;
  65. }
  66. }
  67. }
  68. if ($sEncoding == 'oql')
  69. {
  70. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  71. }
  72. else
  73. {
  74. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  75. }
  76. $iStart = utils::ReadParam('start',0);
  77. $iEnd = utils::ReadParam('end',1);
  78. $iSortCol = utils::ReadParam('sort_col','null');
  79. $sSelectMode = utils::ReadParam('select_mode', '');
  80. if (!empty($sSelectMode) && ($sSelectMode != 'none'))
  81. {
  82. // The first column is used for the selection (radio / checkbox) and is not sortable
  83. $iSortCol--;
  84. }
  85. $bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
  86. $aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
  87. $aClassAliases = utils::ReadParam('class_aliases', array());
  88. $iListId = utils::ReadParam('list_id', 0);
  89. //$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, 'list'));
  90. // Filter the list to removed linked set since we are not able to display them here
  91. $aOrderBy = array();
  92. $iSortIndex = 0;
  93. $aColumnsLoad = array();
  94. foreach($aClassAliases as $sAlias => $sClassName)
  95. {
  96. $aColumnsLoad[$sAlias] = array();
  97. foreach($aColumns[$sAlias] as $sAttCode => $aData)
  98. {
  99. if ($aData['checked'] == 'true')
  100. {
  101. $aColumns[$sAlias][$sAttCode]['checked'] = true;
  102. if ($sAttCode == '_key_')
  103. {
  104. if ($iSortCol == $iSortIndex)
  105. {
  106. if (!MetaModel::HasChildrenClasses($oFilter->GetClass()))
  107. {
  108. $aNameSpec = MetaModel::GetNameSpec($oFilter->GetClass());
  109. if ($aNameSpec[0] == '%1$s')
  110. {
  111. // The name is made of a single column, let's sort according to the sort algorithm for this column
  112. $aOrderBy[$aNameSpec[1][0]] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  113. }
  114. else
  115. {
  116. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  117. }
  118. }
  119. else
  120. {
  121. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  122. }
  123. }
  124. }
  125. else
  126. {
  127. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  128. if ($oAttDef instanceof AttributeLinkedSet)
  129. {
  130. // Removed from the display list
  131. unset($aColumns[$sAlias][$sAttCode]);
  132. }
  133. else
  134. {
  135. $aColumnsLoad[$sAlias][] = $sAttCode;
  136. }
  137. if ($iSortCol == $iSortIndex)
  138. {
  139. if ($oAttDef->IsExternalKey())
  140. {
  141. $sSortCol = $sAttCode.'_friendlyname';
  142. }
  143. else
  144. {
  145. $sSortCol = $sAttCode;
  146. }
  147. $aOrderBy[$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  148. }
  149. }
  150. $iSortIndex++;
  151. }
  152. else
  153. {
  154. $aColumns[$sAlias][$sAttCode]['checked'] = false;
  155. }
  156. }
  157. }
  158. // Load only the requested columns
  159. $oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart);
  160. $oSet->OptimizeColumnLoad($aColumnsLoad);
  161. $oDataTable = new DataTable($iListId, $oSet, $oSet->GetSelectedClasses());
  162. if ($operation == 'datatable')
  163. {
  164. // Redraw the whole table
  165. $sHtml = $oDataTable->UpdatePager($oPage, $iEnd-$iStart, $iStart); // Set the default page size
  166. $sHtml .= $oDataTable->GetHTMLTable($oPage, $aColumns, $sSelectMode, $iEnd-$iStart, $bDisplayKey, $aExtraParams);
  167. }
  168. else
  169. {
  170. // redraw just the needed rows
  171. $sHtml = $oDataTable->GetAsHTMLTableRows($oPage, $iEnd-$iStart, $aColumns, $sSelectMode, $bDisplayKey, $aExtraParams);
  172. }
  173. $oPage->add($sHtml);
  174. break;
  175. case 'datatable_save_settings':
  176. $oPage->SetContentType('text/plain');
  177. $iPageSize = utils::ReadParam('page_size', 10);
  178. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  179. $bSaveAsDefaults = (utils::ReadParam('defaults', 'true') == 'true');
  180. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  181. $aColumns = utils::ReadParam('columns', array(), false, 'raw_data');
  182. foreach($aColumns as $sAlias => $aList)
  183. {
  184. foreach($aList as $sAttCode => $aData)
  185. {
  186. $aColumns[$sAlias][$sAttCode]['checked'] = ($aData['checked'] == 'true');
  187. $aColumns[$sAlias][$sAttCode]['disabled'] = ($aData['disabled'] == 'true');
  188. $aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']);
  189. }
  190. }
  191. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  192. $oSettings->iDefaultPageSize = $iPageSize;
  193. $oSettings->aColumns = $aColumns;
  194. if ($bSaveAsDefaults)
  195. {
  196. if ($sTableId != null)
  197. {
  198. $oCurrSettings = DataTableSettings::GetTableSettings($aClassAliases, $sTableId, true /* bOnlyTable */ );
  199. if ($oCurrSettings)
  200. {
  201. $oCurrSettings->ResetToDefault(false); // Reset this table to the defaults
  202. }
  203. }
  204. $bRet = $oSettings->SaveAsDefault();
  205. }
  206. else
  207. {
  208. $bRet = $oSettings->Save();
  209. }
  210. $oPage->add($bRet ? 'Ok' : 'KO');
  211. break;
  212. case 'datatable_reset_settings':
  213. $oPage->SetContentType('text/plain');
  214. $sTableId = utils::ReadParam('table_id', null, false, 'raw_data');
  215. $aClassAliases = utils::ReadParam('class_aliases', array(), false, 'raw_data');
  216. $bResetAll = (utils::ReadParam('defaults', 'true') == 'true');
  217. $oSettings = new DataTableSettings($aClassAliases, $sTableId);
  218. $bRet = $oSettings->ResetToDefault($bResetAll);
  219. $oPage->add($bRet ? 'Ok' : 'KO');
  220. break;
  221. // ui.linkswidget
  222. case 'addObjects':
  223. $oPage->SetContentType('text/html');
  224. $sAttCode = utils::ReadParam('sAttCode', '');
  225. $iInputId = utils::ReadParam('iInputId', '');
  226. $sSuffix = utils::ReadParam('sSuffix', '');
  227. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  228. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  229. if (!empty($sJson))
  230. {
  231. $oWizardHelper = WizardHelper::FromJSON($sJson);
  232. $oObj = $oWizardHelper->GetTargetObject();
  233. }
  234. else
  235. {
  236. // Search form: no current object
  237. $oObj = null;
  238. }
  239. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  240. $oWidget->GetObjectPickerDialog($oPage, $oObj);
  241. break;
  242. // ui.linkswidget
  243. case 'searchObjectsToAdd':
  244. $oPage->SetContentType('text/html');
  245. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  246. $sAttCode = utils::ReadParam('sAttCode', '');
  247. $iInputId = utils::ReadParam('iInputId', '');
  248. $sSuffix = utils::ReadParam('sSuffix', '');
  249. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  250. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  251. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  252. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  253. break;
  254. //ui.linksdirectwidget
  255. case 'createObject':
  256. $oPage->SetContentType('text/html');
  257. $sClass = utils::ReadParam('class', '', false, 'class');
  258. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  259. $sAttCode = utils::ReadParam('att_code', '');
  260. $iInputId = utils::ReadParam('iInputId', '');
  261. $oPage->SetContentType('text/html');
  262. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  263. $oWidget->GetObjectCreationDlg($oPage, $sRealClass);
  264. break;
  265. // ui.linksdirectwidget
  266. case 'getLinksetRow':
  267. $oPage->SetContentType('text/html');
  268. $sClass = utils::ReadParam('class', '', false, 'class');
  269. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  270. $sAttCode = utils::ReadParam('att_code', '');
  271. $iInputId = utils::ReadParam('iInputId', '');
  272. $iTempId = utils::ReadParam('tempId', '');
  273. $aValues = utils::ReadParam('values', array(), false, 'raw_data');
  274. $oPage->SetContentType('text/html');
  275. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  276. $oPage->add($oWidget->GetRow($oPage, $sRealClass, $aValues, -$iTempId));
  277. break;
  278. // ui.linksdirectwidget
  279. case 'selectObjectsToAdd':
  280. $oPage->SetContentType('text/html');
  281. $sClass = utils::ReadParam('class', '', false, 'class');
  282. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  283. $oObj = null;
  284. if ($sJson != '')
  285. {
  286. $oWizardHelper = WizardHelper::FromJSON($sJson);
  287. $oObj = $oWizardHelper->GetTargetObject();
  288. }
  289. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  290. $sAttCode = utils::ReadParam('att_code', '');
  291. $iInputId = utils::ReadParam('iInputId', '');
  292. $iCurrObjectId = utils::ReadParam('iObjId', 0);
  293. $oPage->SetContentType('text/html');
  294. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  295. $oWidget->GetObjectsSelectionDlg($oPage, $oObj);
  296. break;
  297. // ui.linksdirectwidget
  298. case 'searchObjectsToAdd2':
  299. $oPage->SetContentType('text/html');
  300. $sClass = utils::ReadParam('class', '', false, 'class');
  301. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  302. $sAttCode = utils::ReadParam('att_code', '');
  303. $iInputId = utils::ReadParam('iInputId', '');
  304. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  305. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  306. $oObj = null;
  307. if ($sJson != '')
  308. {
  309. $oWizardHelper = WizardHelper::FromJSON($sJson);
  310. $oObj = $oWizardHelper->GetTargetObject();
  311. }
  312. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  313. $oWidget->SearchObjectsToAdd($oPage, $sRealClass, $aAlreadyLinked, $oObj);
  314. break;
  315. // ui.linksdirectwidget
  316. case 'doAddObjects2':
  317. $oPage->SetContentType('text/html');
  318. $oPage->SetContentType('text/html');
  319. $sClass = utils::ReadParam('class', '', false, 'class');
  320. $sRealClass = utils::ReadParam('real_class', '', false, 'class');
  321. $sAttCode = utils::ReadParam('att_code', '');
  322. $iInputId = utils::ReadParam('iInputId', '');
  323. $iCurrObjectId = utils::ReadParam('iObjId', 0);
  324. $sFilter = utils::ReadParam('filter', '', false, 'raw_data');
  325. if ($sFilter != '')
  326. {
  327. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  328. }
  329. else
  330. {
  331. $oLinksetDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  332. $valuesDef = $oLinksetDef->GetValuesDef();
  333. if ($valuesDef === null)
  334. {
  335. $oFullSetFilter = new DBObjectSearch($oLinksetDef->GetLinkedClass());
  336. }
  337. else
  338. {
  339. if (!$valuesDef instanceof ValueSetObjects)
  340. {
  341. throw new Exception('Error: only ValueSetObjects are supported for "allowed_values" in AttributeLinkedSet ('.$this->sClass.'/'.$this->sAttCode.').');
  342. }
  343. $oFullSetFilter = DBObjectSearch::FromOQL($valuesDef->GetFilterExpression());
  344. }
  345. }
  346. $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iInputId);
  347. $oWidget->DoAddObjects($oPage, $oFullSetFilter);
  348. break;
  349. ////////////////////////////////////////////////////////////
  350. // ui.extkeywidget
  351. case 'searchObjectsToSelect':
  352. $oPage->SetContentType('text/html');
  353. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  354. $iInputId = utils::ReadParam('iInputId', '');
  355. $sRemoteClass = utils::ReadParam('sRemoteClass', '', false, 'class');
  356. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  357. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  358. $sAttCode = utils::ReadParam('sAttCode', '');
  359. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  360. if (!empty($sJson))
  361. {
  362. $oWizardHelper = WizardHelper::FromJSON($sJson);
  363. $oObj = $oWizardHelper->GetTargetObject();
  364. }
  365. else
  366. {
  367. // Search form: no current object
  368. $oObj = null;
  369. }
  370. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  371. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  372. break;
  373. // ui.extkeywidget: autocomplete
  374. case 'ac_extkey':
  375. $oPage->SetContentType('text/plain');
  376. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  377. $iInputId = utils::ReadParam('iInputId', '');
  378. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  379. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  380. $sContains = utils::ReadParam('q', '', false, 'raw_data');
  381. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  382. if ($sContains !='')
  383. {
  384. if (!empty($sJson))
  385. {
  386. $oWizardHelper = WizardHelper::FromJSON($sJson);
  387. $oObj = $oWizardHelper->GetTargetObject();
  388. }
  389. else
  390. {
  391. // Search form: no current object
  392. $oObj = null;
  393. }
  394. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  395. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  396. }
  397. break;
  398. // ui.extkeywidget
  399. case 'objectSearchForm':
  400. $oPage->SetContentType('text/html');
  401. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  402. $iInputId = utils::ReadParam('iInputId', '');
  403. $sTitle = utils::ReadParam('sTitle', '', false, 'raw_data');
  404. $sAttCode = utils::ReadParam('sAttCode', '');
  405. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  406. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, $bSearchMode);
  407. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  408. if (!empty($sJson))
  409. {
  410. $oWizardHelper = WizardHelper::FromJSON($sJson);
  411. $oObj = $oWizardHelper->GetTargetObject();
  412. }
  413. else
  414. {
  415. // Search form: no current object
  416. $oObj = null;
  417. }
  418. $oWidget->GetSearchDialog($oPage, $sTitle, $oObj);
  419. break;
  420. // ui.extkeywidget
  421. case 'objectCreationForm':
  422. $oPage->SetContentType('text/html');
  423. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  424. $iInputId = utils::ReadParam('iInputId', '');
  425. $sAttCode = utils::ReadParam('sAttCode', '');
  426. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  427. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  428. if (!empty($sJson))
  429. {
  430. $oWizardHelper = WizardHelper::FromJSON($sJson);
  431. $oObj = $oWizardHelper->GetTargetObject();
  432. }
  433. else
  434. {
  435. // Search form: no current object
  436. $oObj = null;
  437. }
  438. $oWidget->GetObjectCreationForm($oPage, $oObj);
  439. break;
  440. // ui.extkeywidget
  441. case 'doCreateObject':
  442. $oPage->SetContentType('application/json');
  443. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  444. $iInputId = utils::ReadParam('iInputId', '');
  445. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  446. $sAttCode = utils::ReadParam('sAttCode', '');
  447. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, $sAttCode, false);
  448. $aResult = $oWidget->DoCreateObject($oPage);
  449. echo json_encode($aResult);
  450. break;
  451. // ui.extkeywidget
  452. case 'getObjectName':
  453. $oPage->SetContentType('application/json');
  454. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  455. $iInputId = utils::ReadParam('iInputId', '');
  456. $iObjectId = utils::ReadParam('iObjectId', '');
  457. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  458. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId, '', $bSearchMode);
  459. $sName = $oWidget->GetObjectName($iObjectId);
  460. echo json_encode(array('name' => $sName));
  461. break;
  462. // ui.extkeywidget
  463. case 'displayHierarchy':
  464. $oPage->SetContentType('text/html');
  465. $sTargetClass = utils::ReadParam('sTargetClass', '', false, 'class');
  466. $sInputId = utils::ReadParam('sInputId', '');
  467. $sFilter = utils::ReadParam('sFilter', '', false, 'raw_data');
  468. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  469. $currValue = utils::ReadParam('value', '');
  470. $bSearchMode = (utils::ReadParam('bSearchMode', 'false') == 'true');
  471. if (!empty($sJson))
  472. {
  473. $oWizardHelper = WizardHelper::FromJSON($sJson);
  474. $oObj = $oWizardHelper->GetTargetObject();
  475. }
  476. else
  477. {
  478. // Search form: no current object
  479. $oObj = null;
  480. }
  481. $oWidget = new UIExtKeyWidget($sTargetClass, $sInputId, '', $bSearchMode);
  482. $oWidget->DisplayHierarchy($oPage, $sFilter, $currValue, $oObj);
  483. break;
  484. ////////////////////////////////////////////////////
  485. // ui.linkswidget
  486. case 'doAddObjects':
  487. $oPage->SetContentType('text/html');
  488. $sAttCode = utils::ReadParam('sAttCode', '');
  489. $iInputId = utils::ReadParam('iInputId', '');
  490. $sSuffix = utils::ReadParam('sSuffix', '');
  491. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass, false, 'class');
  492. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  493. $sJson = utils::ReadParam('json', '', false, 'raw_data');
  494. $oWizardHelper = WizardHelper::FromJSON($sJson);
  495. $oObj = $oWizardHelper->GetTargetObject();
  496. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  497. if ($sFilter != '')
  498. {
  499. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  500. }
  501. else
  502. {
  503. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  504. }
  505. $oWidget->DoAddObjects($oPage, $oFullSetFilter, $oObj);
  506. break;
  507. ////////////////////////////////////////////////////////////
  508. case 'wizard_helper_preview':
  509. $oPage->SetContentType('text/html');
  510. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  511. $oWizardHelper = WizardHelper::FromJSON($sJson);
  512. $oObj = $oWizardHelper->GetTargetObject();
  513. $oObj->DisplayBareProperties($oPage);
  514. break;
  515. case 'wizard_helper':
  516. $oPage->SetContentType('text/html');
  517. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  518. $oWizardHelper = WizardHelper::FromJSON($sJson);
  519. $oObj = $oWizardHelper->GetTargetObject();
  520. $sClass = $oWizardHelper->GetTargetClass();
  521. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  522. {
  523. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  524. $defaultValue = $oAttDef->GetDefaultValue();
  525. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  526. $oObj->Set($sAttCode, $defaultValue);
  527. }
  528. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  529. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  530. {
  531. $sId = $oWizardHelper->GetIdForField($sAttCode);
  532. if ($sId != '')
  533. {
  534. if ($oObj->IsNew())
  535. {
  536. $iFlags = $oObj->GetInitialStateAttributeFlags($sAttCode);
  537. }
  538. else
  539. {
  540. $iFlags = $oObj->GetAttributeFlags($sAttCode);
  541. }
  542. if ($iFlags & OPT_ATT_READONLY)
  543. {
  544. $sHTMLValue = "<span id=\"field_{$sId}\">".$oObj->GetAsHTML($sAttCode);
  545. $sHTMLValue .= '<input type="hidden" id="'.$sId.'" name="attr_'.$sFormPrefix.$sAttCode.'" value="'.htmlentities($oObj->Get($sAttCode), ENT_QUOTES, 'UTF-8').'"/></span>';
  546. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  547. }
  548. else
  549. {
  550. // It may happen that the field we'd like to update does not
  551. // exist in the form. For example, if the field should be hidden/read-only
  552. // in the current state of the object
  553. $value = $oObj->Get($sAttCode);
  554. $displayValue = $oObj->GetEditValue($sAttCode);
  555. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  556. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  557. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  558. // Make sure that we immediately validate the field when we reload it
  559. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  560. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  561. }
  562. }
  563. }
  564. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  565. break;
  566. case 'obj_creation_form':
  567. $oPage->SetContentType('text/html');
  568. $sJson = utils::ReadParam('json_obj', '', false, 'raw_data');
  569. $oWizardHelper = WizardHelper::FromJSON($sJson);
  570. $oObj = $oWizardHelper->GetTargetObject();
  571. $sClass = $oWizardHelper->GetTargetClass();
  572. $sTargetState = utils::ReadParam('target_state', '');
  573. $iTransactionId = utils::ReadParam('transaction_id', '');
  574. $oObj->Set(MetaModel::GetStateAttributeCode($sClass), $sTargetState);
  575. cmdbAbstractObject::DisplayCreationForm($oPage, $sClass, $oObj, array(), array('action' => utils::GetAbsoluteUrlAppRoot().'pages/UI.php', 'transaction_id' => $iTransactionId));
  576. break;
  577. // DisplayBlock
  578. case 'ajax':
  579. $oPage->SetContentType('text/html');
  580. if ($sFilter != "")
  581. {
  582. $sExtraParams = stripslashes(utils::ReadParam('extra_params', '', false, 'raw_data'));
  583. $aExtraParams = array();
  584. if (!empty($sExtraParams))
  585. {
  586. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  587. }
  588. // Restore the app context from the ExtraParams
  589. $oAppContext = new ApplicationContext(false); // false => don't read the context yet !
  590. $aContext = array();
  591. foreach($oAppContext->GetNames() as $sName)
  592. {
  593. $sParamName = 'c['.$sName.']';
  594. if (isset($aExtraParams[$sParamName]))
  595. {
  596. $aContext[$sName] = $aExtraParams[$sParamName];
  597. }
  598. }
  599. $_REQUEST['c'] = $aContext;
  600. if ($sEncoding == 'oql')
  601. {
  602. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  603. }
  604. else
  605. {
  606. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  607. }
  608. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  609. $aExtraParams['display_limit'] = true;
  610. $aExtraParams['truncated'] = true;
  611. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  612. }
  613. else
  614. {
  615. $oPage->p("Invalid query (empty filter).");
  616. }
  617. break;
  618. case 'displayCSVHistory':
  619. $oPage->SetContentType('text/html');
  620. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  621. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  622. break;
  623. case 'details':
  624. $oPage->SetContentType('text/html');
  625. $key = utils::ReadParam('id', 0);
  626. $oFilter = new DBObjectSearch($sClass);
  627. $oFilter->AddCondition('id', $key, '=');
  628. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  629. $oDisplayBlock->RenderContent($oPage);
  630. break;
  631. case 'pie_chart':
  632. $oPage->SetContentType('application/json');
  633. $sGroupBy = utils::ReadParam('group_by', '');
  634. if ($sFilter != '')
  635. {
  636. if ($sEncoding == 'oql')
  637. {
  638. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  639. }
  640. else
  641. {
  642. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  643. }
  644. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  645. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  646. }
  647. else
  648. {
  649. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  650. }
  651. break;
  652. case 'open_flash_chart':
  653. // Workaround for IE8 + IIS + HTTPS
  654. // See TRAC #363, fix described here: http://forums.codecharge.com/posts.php?post_id=97771
  655. $oPage->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT");
  656. $oPage->add_header("Cache-Control: cache, must-revalidate");
  657. $oPage->add_header("Pragma: public");
  658. $oPage->SetContentType('application/json');
  659. $aParams = utils::ReadParam('params', array(), false, 'raw_data');
  660. if ($sFilter != '')
  661. {
  662. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  663. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  664. $oDisplayBlock->RenderContent($oPage, $aParams);
  665. }
  666. else
  667. {
  668. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  669. }
  670. break;
  671. case 'modal_details':
  672. $oPage->SetContentType('text/html');
  673. $key = utils::ReadParam('id', 0);
  674. $oFilter = new DBObjectSearch($sClass);
  675. $oFilter->AddCondition('id', $key, '=');
  676. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  677. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  678. $oDisplayBlock->RenderContent($oPage);
  679. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  680. break;
  681. case 'link':
  682. $oPage->SetContentType('text/html');
  683. $sClass = utils::ReadParam('sclass', 'logInfra', false, 'class');
  684. $sAttCode = utils::ReadParam('attCode', 'name');
  685. //$sOrg = utils::ReadParam('org_id', '');
  686. $sName = utils::ReadParam('q', '');
  687. $iMaxCount = utils::ReadParam('max', 30);
  688. $iCount = 0;
  689. $oFilter = new DBObjectSearch($sClass);
  690. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  691. //$oFilter->AddCondition('org_id', $sOrg, '=');
  692. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  693. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  694. {
  695. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  696. $iCount++;
  697. }
  698. break;
  699. case 'combo_options':
  700. $oPage->SetContentType('text/html');
  701. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  702. $oSet = new CMDBObjectSet($oFilter);
  703. while( $oObj = $oSet->fetch())
  704. {
  705. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  706. }
  707. break;
  708. case 'display_document':
  709. $id = utils::ReadParam('id', '');
  710. $sField = utils::ReadParam('field', '');
  711. if (!empty($sClass) && !empty($id) && !empty($sField))
  712. {
  713. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  714. }
  715. break;
  716. case 'download_document':
  717. $id = utils::ReadParam('id', '');
  718. $sField = utils::ReadParam('field', '');
  719. if (!empty($sClass) && !empty($id) && !empty($sField))
  720. {
  721. DownloadDocument($oPage, $sClass, $id, $sField, 'attachment');
  722. }
  723. break;
  724. case 'search_form':
  725. $oPage->SetContentType('text/html');
  726. $sClass = utils::ReadParam('className', '', false, 'class');
  727. $sRootClass = utils::ReadParam('baseClass', '', false, 'class');
  728. $currentId = utils::ReadParam('currentId', '');
  729. $sTableId = utils::ReadParam('_table_id_', null, false, 'raw_data');
  730. $sAction = utils::ReadParam('action', '');
  731. $oFilter = new DBObjectSearch($sClass);
  732. $oSet = new CMDBObjectSet($oFilter);
  733. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction, 'table_id' => $sTableId));
  734. $oPage->add($sHtml);
  735. break;
  736. case 'set_pref':
  737. $sCode = utils::ReadPostedParam('code', '');
  738. $sValue = utils::ReadPostedParam('value', '', 'raw_data');
  739. appUserPreferences::SetPref($sCode, $sValue);
  740. break;
  741. case 'erase_all_pref':
  742. // Can be useful in case a user got some corrupted prefs...
  743. appUserPreferences::ClearPreferences();
  744. break;
  745. case 'on_form_cancel':
  746. // Called when a creation/modification form is cancelled by the end-user
  747. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  748. $iTransactionId = utils::ReadParam('transaction_id', 0);
  749. $sTempId = session_id().'_'.$iTransactionId;
  750. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  751. {
  752. $oExtensionInstance->OnFormCancel($sTempId);
  753. }
  754. break;
  755. case 'reload_dashboard':
  756. $oPage->SetContentType('text/html');
  757. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  758. $aExtraParams = utils::ReadParam('extra_params', '', false, 'raw_data');
  759. ApplicationMenu::LoadAdditionalMenus();
  760. $idx = ApplicationMenu::GetMenuIndexById($sDashboardId);
  761. $oMenu = ApplicationMenu::GetMenuNode($idx);
  762. $oDashboard = $oMenu->GetDashboard();
  763. $oDashboard->Render($oPage, false, $aExtraParams);
  764. break;
  765. case 'dashboard_editor':
  766. $sId = utils::ReadParam('id', '', false, 'raw_data');
  767. ApplicationMenu::LoadAdditionalMenus();
  768. $idx = ApplicationMenu::GetMenuIndexById($sId);
  769. $oMenu = ApplicationMenu::GetMenuNode($idx);
  770. $oMenu->RenderEditor($oPage);
  771. break;
  772. case 'new_dashlet':
  773. require_once(APPROOT.'application/forms.class.inc.php');
  774. require_once(APPROOT.'application/dashlet.class.inc.php');
  775. $sDashletClass = utils::ReadParam('dashlet_class', '');
  776. $sDashletId = utils::ReadParam('dashlet_id', '', false, 'raw_data');
  777. if (is_subclass_of($sDashletClass, 'Dashlet'))
  778. {
  779. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  780. $offset = $oPage->start_capture();
  781. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  782. $sHtml = addslashes($oPage->end_capture($offset));
  783. $sHtml = str_replace("\n", '', $sHtml);
  784. $sHtml = str_replace("\r", '', $sHtml);
  785. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  786. // but is executed BEFORE all 'ready_scripts'
  787. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  788. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  789. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  790. $sHtml = str_replace("\n", '', $sHtml);
  791. $sHtml = str_replace("\r", '', $sHtml);
  792. $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'
  793. }
  794. break;
  795. case 'update_dashlet_property':
  796. require_once(APPROOT.'application/forms.class.inc.php');
  797. require_once(APPROOT.'application/dashlet.class.inc.php');
  798. $aParams = utils::ReadParam('params', '', false, 'raw_data');
  799. $sDashletClass = $aParams['attr_dashlet_class'];
  800. $sDashletId = $aParams['attr_dashlet_id'];
  801. $aUpdatedProperties = $aParams['updated']; // Code of the changed properties as an array: 'attr_xxx', 'attr_xxy', etc...
  802. $aPreviousValues = $aParams['previous_values']; // hash array: 'attr_xxx' => 'old_value'
  803. if (is_subclass_of($sDashletClass, 'Dashlet'))
  804. {
  805. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), $sDashletId);
  806. $oForm = $oDashlet->GetForm();
  807. $aValues = $oForm->ReadParams(); // hash array: 'xxx' => 'new_value'
  808. $aCurrentValues = $aValues;
  809. $aUpdatedDecoded = array();
  810. foreach($aUpdatedProperties as $sProp)
  811. {
  812. $sDecodedProp = str_replace('attr_', '', $sProp); // Remove the attr_ prefix
  813. $aCurrentValues[$sDecodedProp] = $aPreviousValues[$sProp]; // Set the previous value
  814. $aUpdatedDecoded[] = $sDecodedProp;
  815. }
  816. $oDashlet->FromParams($aCurrentValues);
  817. $sPrevClass = get_class($oDashlet);
  818. $oDashlet = $oDashlet->Update($aValues, $aUpdatedDecoded);
  819. $sNewClass = get_class($oDashlet);
  820. if ($sNewClass != $sPrevClass)
  821. {
  822. $oPage->add_ready_script("$('#dashlet_$sDashletId').dashlet('option', {dashlet_class: '$sNewClass'});");
  823. }
  824. if ($oDashlet->IsRedrawNeeded())
  825. {
  826. $offset = $oPage->start_capture();
  827. $oDashlet->DoRender($oPage, true /* bEditMode */, false /* bEnclosingDiv */);
  828. $sHtml = addslashes($oPage->end_capture($offset));
  829. $sHtml = str_replace("\n", '', $sHtml);
  830. $sHtml = str_replace("\r", '', $sHtml);
  831. $oPage->add_script("$('#dashlet_$sDashletId').html('$sHtml');"); // in ajax web page add_script has the same effect as add_ready_script
  832. // but is executed BEFORE all 'ready_scripts'
  833. }
  834. if ($oDashlet->IsFormRedrawNeeded())
  835. {
  836. $oForm = $oDashlet->GetForm(); // Rebuild the form since the values/content changed
  837. $oForm->SetSubmitParams(utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php', array('operation' => 'update_dashlet_property'));
  838. $sHtml = addslashes($oForm->RenderAsPropertySheet($oPage, true /* bReturnHtml */, '.itop-dashboard'));
  839. $sHtml = str_replace("\n", '', $sHtml);
  840. $sHtml = str_replace("\r", '', $sHtml);
  841. $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'
  842. // but is executed BEFORE all 'ready_scripts'
  843. }
  844. }
  845. break;
  846. case 'save_dashboard':
  847. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  848. $aParams = array();
  849. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  850. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  851. $aParams['auto_reload'] = utils::ReadParam('auto_reload', false);
  852. $aParams['auto_reload_sec'] = utils::ReadParam('auto_reload_sec', 300);
  853. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  854. $oDashboard = new RuntimeDashboard($sDashboardId);
  855. $oDashboard->FromParams($aParams);
  856. $oDashboard->Save();
  857. // trigger a reload of the current page since the dashboard just changed
  858. $oPage->add_ready_script(
  859. <<<EOF
  860. var sLocation = new String(window.location.href);
  861. var sNewLocation = sLocation.replace('&edit=1', '');
  862. 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
  863. window.location.href = sNewLocation;
  864. EOF
  865. );
  866. $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
  867. break;
  868. case 'revert_dashboard':
  869. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  870. $oDashboard = new RuntimeDashboard($sDashboardId);
  871. $oDashboard->Revert();
  872. // trigger a reload of the current page since the dashboard just changed
  873. $oPage->add_ready_script("window.location.href=window.location.href;"); // reloads the page, doing a GET even if we arrived via a POST
  874. break;
  875. case 'render_dashboard':
  876. $sDashboardId = utils::ReadParam('dashboard_id', '', false, 'raw_data');
  877. $aParams = array();
  878. $aParams['layout_class'] = utils::ReadParam('layout_class', '');
  879. $aParams['title'] = utils::ReadParam('title', '', false, 'raw_data');
  880. $aParams['cells'] = utils::ReadParam('cells', array(), false, 'raw_data');
  881. $aParams['auto_reload'] = utils::ReadParam('auto_reload', false);
  882. $aParams['auto_reload_sec'] = utils::ReadParam('auto_reload_sec', 300);
  883. $oDashboard = new RuntimeDashboard($sDashboardId);
  884. $oDashboard->FromParams($aParams);
  885. $oDashboard->Render($oPage, true /* bEditMode */);
  886. break;
  887. case 'dashlet_creation_dlg':
  888. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  889. RuntimeDashboard::GetDashletCreationDlgFromOQL($oPage, $sOQL);
  890. break;
  891. case 'add_dashlet':
  892. $oForm = RuntimeDashboard::GetDashletCreationForm();
  893. $aValues = $oForm->ReadParams();
  894. $sDashletClass = $aValues['dashlet_class'];
  895. $sMenuId = $aValues['menu_id'];
  896. if (is_subclass_of($sDashletClass, 'Dashlet'))
  897. {
  898. $oDashlet = new $sDashletClass(new ModelReflectionRuntime(), 0);
  899. $oDashlet->FromParams($aValues);
  900. ApplicationMenu::LoadAdditionalMenus();
  901. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  902. $oMenu = ApplicationMenu::GetMenuNode($index);
  903. $oMenu->AddDashlet($oDashlet);
  904. // navigate to the dashboard page
  905. if ($aValues['open_editor'])
  906. {
  907. $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
  908. }
  909. }
  910. break;
  911. case 'shortcut_list_dlg':
  912. $sOQL = utils::ReadParam('oql', '', false, 'raw_data');
  913. $sTableSettings = utils::ReadParam('table_settings', '', false, 'raw_data');
  914. ShortcutOQL::GetCreationDlgFromOQL($oPage, $sOQL, $sTableSettings);
  915. break;
  916. case 'shortcut_list_create':
  917. $oForm = ShortcutOQL::GetCreationForm();
  918. $aValues = $oForm->ReadParams();
  919. $oAppContext = new ApplicationContext();
  920. $aContext = $oAppContext->GetAsHash();
  921. $sContext = serialize($aContext);
  922. $oShortcut = MetaModel::NewObject("ShortcutOQL");
  923. $oShortcut->Set('user_id', UserRights::GetUserId());
  924. $oShortcut->Set("context", $sContext);
  925. $oShortcut->Set("name", $aValues['name']);
  926. $oShortcut->Set("oql", $aValues['oql']);
  927. $iAutoReload = (int)$aValues['auto_reload_sec'];
  928. if (($aValues['auto_reload']) && ($iAutoReload > 0))
  929. {
  930. $oShortcut->Set("auto_reload_sec", max(5, $iAutoReload));
  931. $oShortcut->Set("auto_reload", 'custom');
  932. }
  933. $iId = $oShortcut->DBInsertNoReload();
  934. $oShortcut->CloneTableSettings($aValues['table_settings']);
  935. // Add the menu node in the right place
  936. //
  937. // Mmmm... already done because the newly created menu is read from the DB
  938. // as soon as we invoke DisplayMenu
  939. // Refresh the menu pane
  940. $aExtraParams = array();
  941. ApplicationMenu::DisplayMenu($oPage, $aExtraParams);
  942. break;
  943. case 'shortcut_rename_dlg':
  944. $oSearch = new DBObjectSearch('Shortcut');
  945. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  946. $iShortcut = $aShortcuts[0];
  947. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  948. $oShortcut->StartRenameDialog($oPage);
  949. break;
  950. case 'shortcut_rename_go':
  951. $iShortcut = utils::ReadParam('id', 0);
  952. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  953. $sName = utils::ReadParam('attr_name', '', false, 'raw_data');
  954. if (strlen($sName) > 0)
  955. {
  956. $oShortcut->Set('name', $sName);
  957. $oShortcut->DBUpdate();
  958. $oPage->add_ready_script('window.location.reload();');
  959. }
  960. break;
  961. case 'shortcut_delete_go':
  962. $oSearch = new DBObjectSearch('Shortcut');
  963. $oSearch->AddCondition('user_id', UserRights::GetUserId(), '=');
  964. $aShortcuts = utils::ReadMultipleSelection($oSearch);
  965. foreach ($aShortcuts as $iShortcut)
  966. {
  967. $oShortcut = MetaModel::GetObject('Shortcut', $iShortcut);
  968. $oShortcut->DBDelete();
  969. $oPage->add_ready_script('window.location.reload();');
  970. }
  971. break;
  972. case 'export_dashboard':
  973. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  974. ApplicationMenu::LoadAdditionalMenus();
  975. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  976. $oMenu = ApplicationMenu::GetMenuNode($index);
  977. if ($oMenu instanceof DashboardMenuNode)
  978. {
  979. $oDashboard = $oMenu->GetDashboard();
  980. $oPage->TrashUnexpectedOutput();
  981. $oPage->SetContentType('text/xml');
  982. $oPage->SetContentDisposition('attachment', $oMenu->GetLabel().'.xml');
  983. $oPage->add($oDashboard->ToXml());
  984. }
  985. break;
  986. case 'import_dashboard':
  987. $sMenuId = utils::ReadParam('id', '', false, 'raw_data');
  988. ApplicationMenu::LoadAdditionalMenus();
  989. $index = ApplicationMenu::GetMenuIndexById($sMenuId);
  990. $oMenu = ApplicationMenu::GetMenuNode($index);
  991. $aResult = array('error' => '');
  992. try
  993. {
  994. if ($oMenu instanceof DashboardMenuNode)
  995. {
  996. $oDoc = utils::ReadPostedDocument('dashboard_upload_file');
  997. $oDashboard = $oMenu->GetDashboard();
  998. $oDashboard->FromXml($oDoc->GetData());
  999. $oDashboard->Save();
  1000. }
  1001. else
  1002. {
  1003. $aResult['error'] = 'Dashboard id="'.$sMenuId.'" not found.';
  1004. }
  1005. }
  1006. catch(DOMException $e)
  1007. {
  1008. $aResult = array('error' => Dict::S('UI:Error:InvalidDashboardFile'));
  1009. }
  1010. catch(Exception $e)
  1011. {
  1012. $aResult = array('error' => $e->getMessage());
  1013. }
  1014. $oPage->add(json_encode($aResult));
  1015. break;
  1016. case 'about_box':
  1017. $oPage->SetContentType('text/html');
  1018. $sDialogTitle = addslashes(Dict::S('UI:About:Title'));
  1019. $oPage->add_ready_script(
  1020. <<<EOF
  1021. $('#about_box').dialog({
  1022. width: 700,
  1023. modal: true,
  1024. title: '$sDialogTitle',
  1025. close: function() { $(this).remove(); }
  1026. });
  1027. $("#collapse_support_details").click(function() {
  1028. $("#support_details").slideToggle('normal');
  1029. $("#collapse_support_details").toggleClass('open');
  1030. });
  1031. $('#support_details').toggle();
  1032. EOF
  1033. );
  1034. $sVersionString = Dict::Format('UI:iTopVersion:Long', ITOP_VERSION, ITOP_REVISION, ITOP_BUILD_DATE);
  1035. $sMySQLVersion = CMDBSource::GetDBVersion();
  1036. $sPHPVersion = phpversion();
  1037. $sOSVersion = PHP_OS;
  1038. $sWebServerVersion = $_SERVER["SERVER_SOFTWARE"];
  1039. $sModules = implode(', ', get_loaded_extensions());
  1040. // Get the datamodel directory
  1041. $oFilter = DBObjectSearch::FromOQL('SELECT ModuleInstallation WHERE name="datamodel"');
  1042. $oSet = new DBObjectSet($oFilter, array('installed' => false)); // Most recent first
  1043. $oLastInstall = $oSet->Fetch();
  1044. $sLastInstallDate = $oLastInstall->Get('installed');
  1045. $sDataModelVersion = $oLastInstall->Get('version');
  1046. $aDataModelInfo = json_decode($oLastInstall->Get('comment'), true);
  1047. $sDataModelSourceDir = $aDataModelInfo['source_dir'];
  1048. require_once(APPROOT.'setup/runtimeenv.class.inc.php');
  1049. $sCurrEnv = utils::GetCurrentEnvironment();
  1050. $oRuntimeEnv = new RunTimeEnvironment($sCurrEnv);
  1051. $aAvailableModules = $oRuntimeEnv->AnalyzeInstallation(MetaModel::GetConfig(), array(APPROOT.$sDataModelSourceDir));
  1052. require_once(APPROOT.'setup/setuputils.class.inc.php');
  1053. $aLicenses = SetupUtils::GetLicenses();
  1054. $aItopSettings = array('cron_max_execution_time', 'timezone');
  1055. $aPHPSettings = array('memory_limit', 'max_execution_time', 'upload_max_filesize', 'post_max_size');
  1056. $aMySQLSettings = array('max_allowed_packet', 'key_buffer_size', 'query_cache_size');
  1057. $aMySQLStatuses = array('Key_read_requests', 'Key_reads');
  1058. if (extension_loaded('suhosin'))
  1059. {
  1060. $aPHPSettings[] = 'suhosin.post.max_vars';
  1061. $aPHPSettings[] = 'suhosin.get.max_value_length';
  1062. }
  1063. $aMySQLVars = array();
  1064. foreach (CMDBSource::QueryToArray('SHOW VARIABLES') as $aRow)
  1065. {
  1066. $aMySQLVars[$aRow['Variable_name']] = $aRow['Value'];
  1067. }
  1068. $aMySQLStats = array();
  1069. foreach (CMDBSource::QueryToArray('SHOW GLOBAL STATUS') as $aRow)
  1070. {
  1071. $aMySQLStats[$aRow['Variable_name']] = $aRow['Value'];
  1072. }
  1073. // Display
  1074. //
  1075. $oPage->add("<div id=\"about_box\">");
  1076. $oPage->add('<div style="margin-left: 120px;">');
  1077. $oPage->add('<table>');
  1078. $oPage->add('<tr>');
  1079. $oPage->add('<td><a href="http://www.combodo.com" title="www.combodo.com" target="_blank" style="background: none;"><img src="../images/logo-combodo.png" style="float: right;"/></a></td>');
  1080. $oPage->add('<td style="padding-left: 20px;">');
  1081. $oPage->add($sVersionString.'<br/>');
  1082. $oPage->add(Dict::S('UI:About:DataModel').': '.$sDataModelVersion.'<br/>');
  1083. $oPage->add('MySQL: '.$sMySQLVersion.'<br/>');
  1084. $oPage->add('PHP: '.$sPHPVersion.'<br/>');
  1085. $oPage->add('</td>');
  1086. $oPage->add('</tr>');
  1087. $oPage->add('</table>');
  1088. $oPage->add("</div>");
  1089. $oPage->add("<div>");
  1090. $oPage->add('<fieldset>');
  1091. $oPage->add('<legend>'.Dict::S('UI:About:Licenses').'</legend>');
  1092. $oPage->add('<ul style="margin: 0; font-size: smaller;">');
  1093. foreach($aLicenses as $index => $oLicense)
  1094. {
  1095. $oPage->add('<li><b>'.$oLicense->product.'</b>, &copy; '.$oLicense->author.' is licensed under the <b>'.$oLicense->license_type.' license</b>. (<a id="toggle_'.$index.'" class="CollapsibleLabel" style="cursor:pointer;">Details</a>)');
  1096. $oPage->add('<div id="license_'.$index.'" class="license_text" style="display:none;overflow:auto;max-height:10em;font-size:small;border:1px #696969 solid;margin-bottom:1em; margin-top:0.5em;padding:0.5em;">'.$oLicense->text.'</div>');
  1097. $oPage->add_ready_script('$("#toggle_'.$index.'").click( function() { $("#license_'.$index.'").slideToggle("normal"); } );');
  1098. }
  1099. $oPage->add('</ul>');
  1100. $oPage->add('</fieldset>');
  1101. $oPage->add("</div>");
  1102. $oPage->add('<fieldset>');
  1103. $oPage->add('<legend>'.Dict::S('UI:About:Modules').'</legend>');
  1104. //$oPage->add(print_r($aAvailableModules, true));
  1105. $oPage->add("<div style=\"height: 150px; overflow: auto; font-size: smaller;\">");
  1106. $oPage->add('<ul style="margin: 0;">');
  1107. foreach ($aAvailableModules as $sModuleId => $aModuleData)
  1108. {
  1109. if ($sModuleId == '_Root_') continue;
  1110. if (!$aModuleData['visible']) continue;
  1111. if ($aModuleData['version_db'] == '') continue;
  1112. $oPage->add('<li>'.$aModuleData['label'].' ('.$aModuleData['version_db'].')</li>');
  1113. }
  1114. $oPage->add('</ul>');
  1115. $oPage->add("</div>");
  1116. $oPage->add('</fieldset>');
  1117. // MUST NOT be localized, as the information given here will be sent to the support
  1118. $oPage->add("<a id=\"collapse_support_details\" class=\"CollapsibleLabel\" href=\"#\">".Dict::S('UI:About:Support')."</a></br>\n");
  1119. $oPage->add("<div id=\"support_details\">");
  1120. $oPage->add('<textarea readonly style="width: 660px; height: 150px; font-size: smaller;">');
  1121. $oPage->add("===== begin =====\n");
  1122. $oPage->add('iTopVersion: '.ITOP_VERSION."\n");
  1123. $oPage->add('iTopBuild: '.ITOP_REVISION."\n");
  1124. $oPage->add('iTopBuildDate: '.ITOP_BUILD_DATE."\n");
  1125. $oPage->add('DataModelVersion: '.$sDataModelVersion."\n");
  1126. $oPage->add('MySQLVersion: '.$sMySQLVersion."\n");
  1127. $oPage->add('PHPVersion: '. $sPHPVersion."\n");
  1128. $oPage->add('OSVersion: '.$sOSVersion."\n");
  1129. $oPage->add('WebServerVersion: '.$sWebServerVersion."\n");
  1130. $oPage->add('PHPModules: '.$sModules."\n");
  1131. foreach ($aItopSettings as $siTopVar)
  1132. {
  1133. $oPage->add('ItopSetting/'.$siTopVar.': '.MetaModel::GetConfig()->Get($siTopVar)."\n");
  1134. }
  1135. foreach ($aPHPSettings as $sPHPVar)
  1136. {
  1137. $oPage->add('PHPSetting/'.$sPHPVar.': '.ini_get($sPHPVar)."\n");
  1138. }
  1139. foreach ($aMySQLSettings as $sMySQLVar)
  1140. {
  1141. $oPage->add('MySQLSetting/'.$sMySQLVar.': '.$aMySQLVars[$sMySQLVar]."\n");
  1142. }
  1143. foreach ($aMySQLStatuses as $sMySQLStatus)
  1144. {
  1145. $oPage->add('MySQLStatus/'.$sMySQLStatus.': '.$aMySQLStats[$sMySQLStatus]."\n");
  1146. }
  1147. $oPage->add('InstallDate: '.$sLastInstallDate."\n");
  1148. $oPage->add('InstallPath: '.APPROOT."\n");
  1149. foreach ($aAvailableModules as $sModuleId => $aModuleData)
  1150. {
  1151. if ($sModuleId == '_Root_') continue;
  1152. if ($aModuleData['version_db'] == '') continue;
  1153. $oPage->add('InstalledModule/'.$sModuleId.': '.$aModuleData['version_db']."\n");
  1154. }
  1155. $oPage->add('===== end =====');
  1156. $oPage->add('</textarea>');
  1157. $oPage->add("</div>");
  1158. $oPage->add("</div>");
  1159. break;
  1160. default:
  1161. $oPage->p("Invalid query.");
  1162. }
  1163. $oPage->output();
  1164. }
  1165. catch (Exception $e)
  1166. {
  1167. // note: transform to cope with XSS attacks
  1168. echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
  1169. echo "<p>Debug trace: <pre>".$e->getTraceAsString()."</pre></p>\n";
  1170. IssueLog::Error($e->getMessage());
  1171. }
  1172. /**
  1173. * Downloads a document to the browser, either as 'inline' or 'attachment'
  1174. *
  1175. * @param WebPage $oPage The web page for the output
  1176. * @param string $sClass Class name of the object
  1177. * @param mixed $id Identifier of the object
  1178. * @param string $sAttCode Name of the attribute containing the document to download
  1179. * @param string $sContentDisposition Either 'inline' or 'attachment'
  1180. * @return none
  1181. */
  1182. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachment')
  1183. {
  1184. try
  1185. {
  1186. $oObj = MetaModel::GetObject($sClass, $id, false, false);
  1187. if (!is_object($oObj))
  1188. {
  1189. throw new Exception("Invalid id ($id) for class '$sClass' - the object does not exist or you are not allowed to view it");
  1190. }
  1191. $oDocument = $oObj->Get($sAttCode);
  1192. if (is_object($oDocument))
  1193. {
  1194. $oPage->TrashUnexpectedOutput();
  1195. $oPage->SetContentType($oDocument->GetMimeType());
  1196. $oPage->SetContentDisposition($sContentDisposition,$oDocument->GetFileName());
  1197. $oPage->add($oDocument->GetData());
  1198. }
  1199. }
  1200. catch(Exception $e)
  1201. {
  1202. $oPage->p($e->getMessage());
  1203. }
  1204. }
  1205. ?>