ajax.render.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Handles various ajax requests
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../approot.inc.php');
  25. require_once(APPROOT.'/application/application.inc.php');
  26. require_once(APPROOT.'/application/webpage.class.inc.php');
  27. require_once(APPROOT.'/application/ajaxwebpage.class.inc.php');
  28. require_once(APPROOT.'/application/wizardhelper.class.inc.php');
  29. require_once(APPROOT.'/application/ui.linkswidget.class.inc.php');
  30. require_once(APPROOT.'/application/ui.extkeywidget.class.inc.php');
  31. try
  32. {
  33. require_once(APPROOT.'/application/startup.inc.php');
  34. require_once(APPROOT.'/application/user.preferences.class.inc.php');
  35. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  36. LoginWebPage::DoLogin(false /* bMustBeAdmin */, true /* IsAllowedToPortalUsers */); // Check user rights and prompt if needed
  37. $oPage = new ajax_page("");
  38. $oPage->no_cache();
  39. $operation = utils::ReadParam('operation', '');
  40. $sFilter = stripslashes(utils::ReadParam('filter', ''));
  41. $sEncoding = utils::ReadParam('encoding', 'serialize');
  42. $sClass = utils::ReadParam('class', 'MissingAjaxParam');
  43. $sStyle = utils::ReadParam('style', 'list');
  44. switch($operation)
  45. {
  46. case 'pagination':
  47. $sExtraParams = stripslashes(utils::ReadParam('extra_params', ''));
  48. $aExtraParams = array();
  49. if (!empty($sExtraParams))
  50. {
  51. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  52. }
  53. if ($sEncoding == 'oql')
  54. {
  55. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  56. }
  57. else
  58. {
  59. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  60. }
  61. $iStart = utils::ReadParam('start',0);
  62. $iEnd = utils::ReadParam('end',1);
  63. $iSortCol = utils::ReadParam('sort_col',null);
  64. $sSelectMode = utils::ReadParam('select_mode', '');
  65. $bDisplayKey = utils::ReadParam('display_key', 'true') == 'true';
  66. $aList = utils::ReadParam('display_list', array());
  67. $sClassName = $oFilter->GetClass();
  68. //$aList = cmdbAbstractObject::FlattenZList(MetaModel::GetZListItems($sClassName, 'list'));
  69. // Filter the list to removed linked set since we are not able to display them here
  70. $aOrderBy = array();
  71. $aConfig = array();
  72. $iSortIndex = 0;
  73. if ($sSelectMode != '')
  74. {
  75. $aConfig['form::select'] = array();
  76. $iSortIndex++; // Take into account the extra (non-sortable) column for the checkbox/radio button.
  77. }
  78. if ($bDisplayKey)
  79. {
  80. $aConfig['key'] = array();
  81. if ($iSortIndex == $iSortCol)
  82. {
  83. $aOrderBy['friendlyname'] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  84. }
  85. $iSortIndex++;
  86. }
  87. foreach($aList as $sAttCode)
  88. {
  89. $aConfig[$sAttCode] = array();
  90. }
  91. foreach($aList as $index => $sAttCode)
  92. {
  93. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  94. if ($oAttDef instanceof AttributeLinkedSet)
  95. {
  96. // Removed from the display list
  97. unset($aList[$index]);
  98. }
  99. if ($iSortCol == $iSortIndex)
  100. {
  101. if ($oAttDef->IsExternalKey())
  102. {
  103. $sSortCol = $sAttCode.'_friendlyname';
  104. }
  105. else
  106. {
  107. $sSortCol = $sAttCode;
  108. }
  109. $aOrderBy[$sSortCol] = (utils::ReadParam('sort_order', 'asc') == 'asc');
  110. }
  111. $iSortIndex++;
  112. }
  113. // Load only the requested columns
  114. $oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart);
  115. $sClassAlias = $oSet->GetFilter()->GetClassAlias();
  116. $oSet->OptimizeColumnLoad(array($sClassAlias => $aList));
  117. while($oObj = $oSet->Fetch())
  118. {
  119. $aRow = array();
  120. $sDisabled = '';
  121. switch ($sSelectMode)
  122. {
  123. case 'single':
  124. $aRow['form::select'] = "<input type=\"radio\" $sDisabled name=\"selectObject\" value=\"".$oObj->GetKey()."\"></input>";
  125. break;
  126. case 'multiple':
  127. $aRow['form::select'] = "<input type=\"checkBox\" $sDisabled name=\"selectObject[]\" value=\"".$oObj->GetKey()."\"></input>";
  128. break;
  129. }
  130. if ($bDisplayKey)
  131. {
  132. $aRow['key'] = $oObj->GetHyperLink();
  133. }
  134. $sHilightClass = $oObj->GetHilightClass();
  135. if ($sHilightClass != '')
  136. {
  137. $aRow['@class'] = $sHilightClass;
  138. }
  139. foreach($aList as $sAttCode)
  140. {
  141. $aRow[$sAttCode] = $oObj->GetAsHTML($sAttCode);
  142. }
  143. $sRow = $oPage->GetTableRow($aRow, $aConfig);
  144. $oPage->add($sRow);
  145. }
  146. break;
  147. case 'addObjects':
  148. require_once(APPROOT.'/application/uilinkswizard.class.inc.php');
  149. $sClass = utils::ReadParam('class', '', 'get');
  150. $sLinkedClass = utils::ReadParam('linkedClass', '');
  151. $sLinkageAttr = utils::ReadParam('linkageAttr', '');
  152. $iObjectId = utils::ReadParam('objectId', '');
  153. $oLinksWizard = new UILinksWizard($sClass, $sLinkageAttr, $iObjectId, $sLinkedClass);
  154. $oLinksWizard->DisplayAddForm($oPage);
  155. break;
  156. // ui.linkswidget
  157. case 'searchObjectsToAdd':
  158. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  159. $sAttCode = utils::ReadParam('sAttCode', '');
  160. $iInputId = utils::ReadParam('iInputId', '');
  161. $sSuffix = utils::ReadParam('sSuffix', '');
  162. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  163. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  164. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  165. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  166. break;
  167. ////////////////////////////////////////////////////////////
  168. // ui.extkeywidget
  169. case 'searchObjectsToSelect':
  170. $sTargetClass = utils::ReadParam('sTargetClass', '');
  171. $iInputId = utils::ReadParam('iInputId', '');
  172. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  173. $sFilter = utils::ReadParam('sFilter');
  174. $sJson = utils::ReadParam('json', '');
  175. if (!empty($sJson))
  176. {
  177. $oWizardHelper = WizardHelper::FromJSON($sJson);
  178. $oObj = $oWizardHelper->GetTargetObject();
  179. }
  180. else
  181. {
  182. // Search form: no current object
  183. $oObj = null;
  184. }
  185. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  186. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  187. break;
  188. // ui.extkeywidget: autocomplete
  189. case 'ac_extkey':
  190. $sTargetClass = utils::ReadParam('sTargetClass', '');
  191. $iInputId = utils::ReadParam('iInputId', '');
  192. $sFilter = utils::ReadParam('sFilter');
  193. $sJson = utils::ReadParam('json', '');
  194. $sContains = utils::ReadParam('q', '');
  195. if (!empty($sJson))
  196. {
  197. $oWizardHelper = WizardHelper::FromJSON($sJson);
  198. $oObj = $oWizardHelper->GetTargetObject();
  199. }
  200. else
  201. {
  202. // Search form: no current object
  203. $oObj = null;
  204. }
  205. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  206. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  207. break;
  208. // ui.extkeywidget
  209. case 'objectSearchForm':
  210. $sTargetClass = utils::ReadParam('sTargetClass', '');
  211. $iInputId = utils::ReadParam('iInputId', '');
  212. $sTitle = utils::ReadParam('sTitle');
  213. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  214. $oWidget->GetSearchDialog($oPage, $sTitle);
  215. break;
  216. // ui.extkeywidget
  217. case 'objectCreationForm':
  218. $sTargetClass = utils::ReadParam('sTargetClass', '');
  219. $iInputId = utils::ReadParam('iInputId', '');
  220. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  221. $oWidget->GetObjectCreationForm($oPage);
  222. break;
  223. // ui.extkeywidget
  224. case 'doCreateObject':
  225. $sTargetClass = utils::ReadParam('sTargetClass', '');
  226. $iInputId = utils::ReadParam('iInputId', '');
  227. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  228. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  229. $aResult = $oWidget->DoCreateObject($oPage);
  230. echo json_encode($aResult);
  231. break;
  232. // ui.extkeywidget
  233. case 'getObjectName':
  234. $sTargetClass = utils::ReadParam('sTargetClass', '');
  235. $iInputId = utils::ReadParam('iInputId', '');
  236. $iObjectId = utils::ReadParam('iObjectId', '');
  237. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  238. $sName = $oWidget->GetObjectName($iObjectId);
  239. echo json_encode(array('name' => $sName));
  240. break;
  241. ////////////////////////////////////////////////////
  242. // ui.linkswidget
  243. case 'doAddObjects':
  244. $sAttCode = utils::ReadParam('sAttCode', '');
  245. $iInputId = utils::ReadParam('iInputId', '');
  246. $sSuffix = utils::ReadParam('sSuffix', '');
  247. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  248. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  249. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  250. $oWidget->DoAddObjects($oPage, $sRemoteClass);
  251. break;
  252. case 'wizard_helper_preview':
  253. $sJson = utils::ReadParam('json_obj', '');
  254. $oWizardHelper = WizardHelper::FromJSON($sJson);
  255. $oObj = $oWizardHelper->GetTargetObject();
  256. $oObj->DisplayBareProperties($oPage);
  257. break;
  258. case 'wizard_helper':
  259. $sJson = utils::ReadParam('json_obj', '');
  260. $oWizardHelper = WizardHelper::FromJSON($sJson);
  261. $oObj = $oWizardHelper->GetTargetObject();
  262. $sClass = $oWizardHelper->GetTargetClass();
  263. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  264. {
  265. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  266. $defaultValue = $oAttDef->GetDefaultValue();
  267. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  268. $oObj->Set($sAttCode, $defaultValue);
  269. }
  270. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  271. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  272. {
  273. $sId = $oWizardHelper->GetIdForField($sAttCode);
  274. if ($sId != '')
  275. {
  276. // It may happen that the field we'd like to update does not
  277. // exist in the form. For example, if the field should be hidden/read-only
  278. // in the current state of the object
  279. $value = $oObj->Get($sAttCode);
  280. $displayValue = $oObj->GetEditValue($sAttCode);
  281. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  282. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  283. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  284. // Make sure that we immediatly validate the field when we reload it
  285. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  286. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  287. }
  288. }
  289. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  290. break;
  291. // DisplayBlock
  292. case 'ajax':
  293. if ($sFilter != "")
  294. {
  295. $sExtraParams = stripslashes(utils::ReadParam('extra_params', ''));
  296. $aExtraParams = array();
  297. if (!empty($sExtraParams))
  298. {
  299. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  300. }
  301. if ($sEncoding == 'oql')
  302. {
  303. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  304. }
  305. else
  306. {
  307. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  308. }
  309. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  310. $aExtraParams['display_limit'] = true;
  311. $aExtraParams['truncated'] = true;
  312. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  313. }
  314. else
  315. {
  316. $oPage->p("Invalid query (empty filter).");
  317. }
  318. break;
  319. case 'displayCSVHistory':
  320. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  321. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  322. break;
  323. case 'details':
  324. $key = utils::ReadParam('id', 0);
  325. $oFilter = new DBObjectSearch($sClass);
  326. $oFilter->AddCondition('id', $key, '=');
  327. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  328. $oDisplayBlock->RenderContent($oPage);
  329. break;
  330. case 'preview':
  331. $key = utils::ReadParam('id', 0);
  332. $oFilter = new DBObjectSearch($sClass);
  333. $oFilter->AddCondition('id', $key, '=');
  334. $oDisplayBlock = new DisplayBlock($oFilter, 'preview', false);
  335. $oDisplayBlock->RenderContent($oPage);
  336. break;
  337. case 'pie_chart':
  338. $sGroupBy = utils::ReadParam('group_by', '');
  339. if ($sFilter != '')
  340. {
  341. if ($sEncoding == 'oql')
  342. {
  343. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  344. }
  345. else
  346. {
  347. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  348. }
  349. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  350. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  351. }
  352. else
  353. {
  354. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  355. }
  356. break;
  357. case 'open_flash_chart':
  358. $aParams = utils::ReadParam('params', array());
  359. if ($sFilter != '')
  360. {
  361. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  362. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  363. $oDisplayBlock->RenderContent($oPage, $aParams);
  364. }
  365. else
  366. {
  367. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  368. }
  369. break;
  370. case 'modal_details':
  371. $key = utils::ReadParam('id', 0);
  372. $oFilter = new DBObjectSearch($sClass);
  373. $oFilter->AddCondition('id', $key, '=');
  374. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  375. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  376. $oDisplayBlock->RenderContent($oPage);
  377. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  378. break;
  379. case 'link':
  380. $sClass = utils::ReadParam('sclass', 'logInfra');
  381. $sAttCode = utils::ReadParam('attCode', 'name');
  382. //$sOrg = utils::ReadParam('org_id', '');
  383. $sName = utils::ReadParam('q', '');
  384. $iMaxCount = utils::ReadParam('max', 30);
  385. $iCount = 0;
  386. $oFilter = new DBObjectSearch($sClass);
  387. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  388. //$oFilter->AddCondition('org_id', $sOrg, '=');
  389. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  390. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  391. {
  392. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  393. $iCount++;
  394. }
  395. break;
  396. case 'create':
  397. case 'create_menu':
  398. $sClass = utils::ReadParam('class', '');
  399. $sFilter = utils::ReadParam('filter', '');
  400. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  401. break;
  402. case 'combo_options':
  403. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  404. $oSet = new CMDBObjectSet($oFilter);
  405. while( $oObj = $oSet->fetch())
  406. {
  407. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  408. }
  409. break;
  410. case 'display_document':
  411. $id = utils::ReadParam('id', '');
  412. $sField = utils::ReadParam('field', '');
  413. if (!empty($sClass) && !empty($id) && !empty($sField))
  414. {
  415. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  416. }
  417. break;
  418. case 'download_document':
  419. $id = utils::ReadParam('id', '');
  420. $sField = utils::ReadParam('field', '');
  421. if (!empty($sClass) && !empty($id) && !empty($sField))
  422. {
  423. DownloadDocument($oPage, $sClass, $id, $sField, 'attachement');
  424. }
  425. break;
  426. case 'search_form':
  427. $sClass = utils::ReadParam('className', '');
  428. $sRootClass = utils::ReadParam('baseClass', '');
  429. $currentId = utils::ReadParam('currentId', '');
  430. $sAction = utils::ReadParam('action', '');
  431. $oFilter = new DBObjectSearch($sClass);
  432. $oSet = new CMDBObjectSet($oFilter);
  433. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction));
  434. $oPage->add($sHtml);
  435. break;
  436. case 'set_pref':
  437. $sCode = utils::ReadPostedParam('code', '');
  438. $sValue = utils::ReadPostedParam('value', '');
  439. appUserPreferences::SetPref($sCode, $sValue);
  440. break;
  441. case 'erase_all_pref':
  442. // Can be useful in case a user got some corrupted prefs...
  443. appUserPreferences::ClearPreferences();
  444. break;
  445. default:
  446. $oPage->p("Invalid query.");
  447. }
  448. $oPage->output();
  449. }
  450. catch (Exception $e)
  451. {
  452. echo $e->GetMessage();
  453. IssueLog::Error($e->getMessage());
  454. }
  455. /**
  456. * Downloads a document to the browser, either as 'inline' or 'attachment'
  457. *
  458. * @param WebPage $oPage The web page for the output
  459. * @param string $sClass Class name of the object
  460. * @param mixed $id Identifier of the object
  461. * @param string $sAttCode Name of the attribute containing the document to download
  462. * @param string $sContentDisposition Either 'inline' or 'attachment'
  463. * @return none
  464. */
  465. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  466. {
  467. try
  468. {
  469. $oObj = MetaModel::GetObject($sClass, $id);
  470. if (is_object($oObj))
  471. {
  472. $oDocument = $oObj->Get($sAttCode);
  473. if (is_object($oDocument))
  474. {
  475. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  476. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  477. $oPage->add($oDocument->GetData());
  478. }
  479. }
  480. }
  481. catch(Exception $e)
  482. {
  483. $oPage->p($e->getMessage());
  484. }
  485. }
  486. ?>