ajax.render.php 49 KB

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