ajax.render.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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_param', ''));
  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. // ui.linkswidget
  148. case 'searchObjectsToAdd':
  149. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  150. $sAttCode = utils::ReadParam('sAttCode', '');
  151. $iInputId = utils::ReadParam('iInputId', '');
  152. $sSuffix = utils::ReadParam('sSuffix', '');
  153. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  154. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  155. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  156. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  157. break;
  158. ////////////////////////////////////////////////////////////
  159. // ui.extkeywidget
  160. case 'searchObjectsToSelect':
  161. $sTargetClass = utils::ReadParam('sTargetClass', '');
  162. $iInputId = utils::ReadParam('iInputId', '');
  163. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  164. $sFilter = utils::ReadParam('sFilter');
  165. $sJson = utils::ReadParam('json', '');
  166. if (!empty($sJson))
  167. {
  168. $oWizardHelper = WizardHelper::FromJSON($sJson);
  169. $oObj = $oWizardHelper->GetTargetObject();
  170. }
  171. else
  172. {
  173. // Search form: no current object
  174. $oObj = null;
  175. }
  176. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  177. $oWidget->SearchObjectsToSelect($oPage, $sFilter, $sRemoteClass, $oObj);
  178. break;
  179. // ui.extkeywidget: autocomplete
  180. case 'ac_extkey':
  181. $sTargetClass = utils::ReadParam('sTargetClass', '');
  182. $iInputId = utils::ReadParam('iInputId', '');
  183. $sFilter = utils::ReadParam('sFilter');
  184. $sJson = utils::ReadParam('json', '');
  185. $sContains = utils::ReadParam('q', '');
  186. if (!empty($sJson))
  187. {
  188. $oWizardHelper = WizardHelper::FromJSON($sJson);
  189. $oObj = $oWizardHelper->GetTargetObject();
  190. }
  191. else
  192. {
  193. // Search form: no current object
  194. $oObj = null;
  195. }
  196. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  197. $oWidget->AutoComplete($oPage, $sFilter, $oObj, $sContains);
  198. break;
  199. // ui.extkeywidget
  200. case 'objectSearchForm':
  201. $sTargetClass = utils::ReadParam('sTargetClass', '');
  202. $iInputId = utils::ReadParam('iInputId', '');
  203. $sTitle = utils::ReadParam('sTitle');
  204. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  205. $oWidget->GetSearchDialog($oPage, $sTitle);
  206. break;
  207. // ui.extkeywidget
  208. case 'objectCreationForm':
  209. $sTargetClass = utils::ReadParam('sTargetClass', '');
  210. $iInputId = utils::ReadParam('iInputId', '');
  211. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  212. $oWidget->GetObjectCreationForm($oPage);
  213. break;
  214. // ui.extkeywidget
  215. case 'doCreateObject':
  216. $sTargetClass = utils::ReadParam('sTargetClass', '');
  217. $iInputId = utils::ReadParam('iInputId', '');
  218. $sFormPrefix = utils::ReadParam('sFormPrefix', '');
  219. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  220. $aResult = $oWidget->DoCreateObject($oPage);
  221. echo json_encode($aResult);
  222. break;
  223. // ui.extkeywidget
  224. case 'getObjectName':
  225. $sTargetClass = utils::ReadParam('sTargetClass', '');
  226. $iInputId = utils::ReadParam('iInputId', '');
  227. $iObjectId = utils::ReadParam('iObjectId', '');
  228. $oWidget = new UIExtKeyWidget($sTargetClass, $iInputId);
  229. $sName = $oWidget->GetObjectName($iObjectId);
  230. echo json_encode(array('name' => $sName));
  231. break;
  232. ////////////////////////////////////////////////////
  233. // ui.linkswidget
  234. case 'doAddObjects':
  235. $sAttCode = utils::ReadParam('sAttCode', '');
  236. $iInputId = utils::ReadParam('iInputId', '');
  237. $sSuffix = utils::ReadParam('sSuffix', '');
  238. $sRemoteClass = utils::ReadParam('sRemoteClass', $sClass);
  239. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  240. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  241. if ($sFilter != '')
  242. {
  243. $oFullSetFilter = DBObjectSearch::unserialize($sFilter);
  244. }
  245. else
  246. {
  247. $oFullSetFilter = new DBObjectSearch($sRemoteClass);
  248. }
  249. $oWidget->DoAddObjects($oPage, $oFullSetFilter);
  250. break;
  251. case 'wizard_helper_preview':
  252. $sJson = utils::ReadParam('json_obj', '');
  253. $oWizardHelper = WizardHelper::FromJSON($sJson);
  254. $oObj = $oWizardHelper->GetTargetObject();
  255. $oObj->DisplayBareProperties($oPage);
  256. break;
  257. case 'wizard_helper':
  258. $sJson = utils::ReadParam('json_obj', '');
  259. $oWizardHelper = WizardHelper::FromJSON($sJson);
  260. $oObj = $oWizardHelper->GetTargetObject();
  261. $sClass = $oWizardHelper->GetTargetClass();
  262. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  263. {
  264. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  265. $defaultValue = $oAttDef->GetDefaultValue();
  266. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  267. $oObj->Set($sAttCode, $defaultValue);
  268. }
  269. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  270. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  271. {
  272. $sId = $oWizardHelper->GetIdForField($sAttCode);
  273. if ($sId != '')
  274. {
  275. // It may happen that the field we'd like to update does not
  276. // exist in the form. For example, if the field should be hidden/read-only
  277. // in the current state of the object
  278. $value = $oObj->Get($sAttCode);
  279. $displayValue = $oObj->GetEditValue($sAttCode);
  280. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  281. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  282. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  283. // Make sure that we immediatly validate the field when we reload it
  284. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  285. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  286. }
  287. }
  288. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  289. break;
  290. // DisplayBlock
  291. case 'ajax':
  292. if ($sFilter != "")
  293. {
  294. $sExtraParams = stripslashes(utils::ReadParam('extra_params', ''));
  295. $aExtraParams = array();
  296. if (!empty($sExtraParams))
  297. {
  298. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  299. }
  300. if ($sEncoding == 'oql')
  301. {
  302. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  303. }
  304. else
  305. {
  306. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  307. }
  308. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  309. $aExtraParams['display_limit'] = true;
  310. $aExtraParams['truncated'] = true;
  311. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  312. }
  313. else
  314. {
  315. $oPage->p("Invalid query (empty filter).");
  316. }
  317. break;
  318. case 'displayCSVHistory':
  319. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  320. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  321. break;
  322. case 'details':
  323. $key = utils::ReadParam('id', 0);
  324. $oFilter = new DBObjectSearch($sClass);
  325. $oFilter->AddCondition('id', $key, '=');
  326. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  327. $oDisplayBlock->RenderContent($oPage);
  328. break;
  329. case 'pie_chart':
  330. $sGroupBy = utils::ReadParam('group_by', '');
  331. if ($sFilter != '')
  332. {
  333. if ($sEncoding == 'oql')
  334. {
  335. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  336. }
  337. else
  338. {
  339. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  340. }
  341. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  342. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  343. }
  344. else
  345. {
  346. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  347. }
  348. break;
  349. case 'open_flash_chart':
  350. $aParams = utils::ReadParam('params', array());
  351. if ($sFilter != '')
  352. {
  353. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  354. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  355. $oDisplayBlock->RenderContent($oPage, $aParams);
  356. }
  357. else
  358. {
  359. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  360. }
  361. break;
  362. case 'modal_details':
  363. $key = utils::ReadParam('id', 0);
  364. $oFilter = new DBObjectSearch($sClass);
  365. $oFilter->AddCondition('id', $key, '=');
  366. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  367. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  368. $oDisplayBlock->RenderContent($oPage);
  369. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  370. break;
  371. case 'link':
  372. $sClass = utils::ReadParam('sclass', 'logInfra');
  373. $sAttCode = utils::ReadParam('attCode', 'name');
  374. //$sOrg = utils::ReadParam('org_id', '');
  375. $sName = utils::ReadParam('q', '');
  376. $iMaxCount = utils::ReadParam('max', 30);
  377. $iCount = 0;
  378. $oFilter = new DBObjectSearch($sClass);
  379. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  380. //$oFilter->AddCondition('org_id', $sOrg, '=');
  381. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  382. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  383. {
  384. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  385. $iCount++;
  386. }
  387. break;
  388. case 'create':
  389. case 'create_menu':
  390. $sClass = utils::ReadParam('class', '');
  391. $sFilter = utils::ReadParam('filter', '');
  392. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  393. break;
  394. case 'combo_options':
  395. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  396. $oSet = new CMDBObjectSet($oFilter);
  397. while( $oObj = $oSet->fetch())
  398. {
  399. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  400. }
  401. break;
  402. case 'display_document':
  403. $id = utils::ReadParam('id', '');
  404. $sField = utils::ReadParam('field', '');
  405. if (!empty($sClass) && !empty($id) && !empty($sField))
  406. {
  407. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  408. }
  409. break;
  410. case 'download_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, 'attachement');
  416. }
  417. break;
  418. case 'search_form':
  419. $sClass = utils::ReadParam('className', '');
  420. $sRootClass = utils::ReadParam('baseClass', '');
  421. $currentId = utils::ReadParam('currentId', '');
  422. $sAction = utils::ReadParam('action', '');
  423. $oFilter = new DBObjectSearch($sClass);
  424. $oSet = new CMDBObjectSet($oFilter);
  425. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass, 'action' => $sAction));
  426. $oPage->add($sHtml);
  427. break;
  428. case 'set_pref':
  429. $sCode = utils::ReadPostedParam('code', '');
  430. $sValue = utils::ReadPostedParam('value', '');
  431. appUserPreferences::SetPref($sCode, $sValue);
  432. break;
  433. case 'erase_all_pref':
  434. // Can be useful in case a user got some corrupted prefs...
  435. appUserPreferences::ClearPreferences();
  436. break;
  437. case 'on_form_cancel':
  438. // Called when a creation/modification form is cancelled by the end-user
  439. // Let's take this opportunity to inform the plug-ins so that they can perform some cleanup
  440. $iTransactionId = utils::ReadParam('transaction_id', 0);
  441. $sTempId = session_id().'_'.$iTransactionId;
  442. foreach (MetaModel::EnumPlugins('iApplicationObjectExtension') as $oExtensionInstance)
  443. {
  444. $oExtensionInstance->OnFormCancel($sTempId);
  445. }
  446. break;
  447. default:
  448. $oPage->p("Invalid query.");
  449. }
  450. $oPage->output();
  451. }
  452. catch (Exception $e)
  453. {
  454. echo $e->GetMessage();
  455. IssueLog::Error($e->getMessage());
  456. }
  457. /**
  458. * Downloads a document to the browser, either as 'inline' or 'attachment'
  459. *
  460. * @param WebPage $oPage The web page for the output
  461. * @param string $sClass Class name of the object
  462. * @param mixed $id Identifier of the object
  463. * @param string $sAttCode Name of the attribute containing the document to download
  464. * @param string $sContentDisposition Either 'inline' or 'attachment'
  465. * @return none
  466. */
  467. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  468. {
  469. try
  470. {
  471. $oObj = MetaModel::GetObject($sClass, $id);
  472. if (is_object($oObj))
  473. {
  474. $oDocument = $oObj->Get($sAttCode);
  475. if (is_object($oDocument))
  476. {
  477. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  478. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  479. $oPage->add($oDocument->GetData());
  480. }
  481. }
  482. }
  483. catch(Exception $e)
  484. {
  485. $oPage->p($e->getMessage());
  486. }
  487. }
  488. ?>