ajax.render.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 'addObjects':
  47. require_once(APPROOT.'/application/uilinkswizard.class.inc.php');
  48. $sClass = utils::ReadParam('class', '', 'get');
  49. $sLinkedClass = utils::ReadParam('linkedClass', '');
  50. $sLinkageAttr = utils::ReadParam('linkageAttr', '');
  51. $iObjectId = utils::ReadParam('objectId', '');
  52. $oLinksWizard = new UILinksWizard($sClass, $sLinkageAttr, $iObjectId, $sLinkedClass);
  53. $oLinksWizard->DisplayAddForm($oPage);
  54. break;
  55. // ui.linkswidget
  56. case 'searchObjectsToAdd':
  57. $sRemoteClass = utils::ReadParam('sRemoteClass', '');
  58. $sAttCode = utils::ReadParam('sAttCode', '');
  59. $iInputId = utils::ReadParam('iInputId', '');
  60. $sSuffix = utils::ReadParam('sSuffix', '');
  61. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  62. $aAlreadyLinked = utils::ReadParam('aAlreadyLinked', array());
  63. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  64. $oWidget->SearchObjectsToAdd($oPage, $sRemoteClass, $aAlreadyLinked);
  65. break;
  66. // ui.extkeywidget
  67. case 'searchObjectsToSelect':
  68. $sTargetClass = utils::ReadParam('sRemoteClass', '');
  69. $sAttCode = utils::ReadParam('sAttCode', '');
  70. $iInputId = utils::ReadParam('iInputId', '');
  71. $sSuffix = utils::ReadParam('sSuffix', '');
  72. $sJson = utils::ReadParam('json', '');
  73. $oWizardHelper = WizardHelper::FromJSON($sJson);
  74. $oObj = $oWizardHelper->GetTargetObject();
  75. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array('this' => $oObj));
  76. $oWidget = new UIExtKeyWidget($sAttCode, $sClass, '', $aAllowedValues, $oObj->Get($sAttCode), $iInputId, false, $sSuffix, '');
  77. $oWidget->SearchObjectsToSelect($oPage, $sTargetClass);
  78. break;
  79. // ui.extkeywidget
  80. case 'objectCreationForm':
  81. $sTargetClass = utils::ReadParam('sRemoteClass', '');
  82. $sAttCode = utils::ReadParam('sAttCode', '');
  83. $iInputId = utils::ReadParam('iInputId', '');
  84. $sSuffix = utils::ReadParam('sSuffix', '');
  85. $sJson = utils::ReadParam('json', '');
  86. $oWizardHelper = WizardHelper::FromJSON($sJson);
  87. $oObj = $oWizardHelper->GetTargetObject();
  88. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array('this' => $oObj));
  89. $oWidget = new UIExtKeyWidget($sAttCode, $sClass, '', $aAllowedValues, $oObj->Get($sAttCode), $iInputId, false, $sSuffix, '');
  90. $oWidget->GetObjectCreationForm($oPage);
  91. break;
  92. // ui.extkeywidget
  93. case 'doCreateObject':
  94. $sTargetClass = utils::ReadParam('sRemoteClass', '');
  95. $sAttCode = utils::ReadParam('sAttCode', '');
  96. $iInputId = utils::ReadParam('iInputId', '');
  97. $sSuffix = utils::ReadParam('sSuffix', '');
  98. $sJson = utils::ReadParam('json', '');
  99. $oWizardHelper = WizardHelper::FromJSON($sJson);
  100. $oObj = $oWizardHelper->GetTargetObject();
  101. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, array('this' => $oObj));
  102. // The iInputId of the autocomplete is the prefix for the form used to create the target object
  103. $oWidget = new UIExtKeyWidget($sAttCode, $sClass, '', $aAllowedValues, null, $iInputId, false, $sSuffix, $oWizardHelper->GetFormPrefix());
  104. $aResult = $oWidget->DoCreateObject($oPage);
  105. echo json_encode($aResult);
  106. break;
  107. // ui.extkeywidget
  108. case 'getObjectName':
  109. $sTargetClass = utils::ReadParam('sTargetClass', '');
  110. $sAttCode = utils::ReadParam('sAttCode', '');
  111. $iInputId = utils::ReadParam('iInputId', '');
  112. $iObjectId = utils::ReadParam('iObjectId', '');
  113. $sSuffix = utils::ReadParam('sSuffix', '');
  114. $oWidget = new UIExtKeyWidget($sAttCode, $sClass, '', array(), '', $iInputId, $sSuffix, '');
  115. $sName = $oWidget->GetObjectName($iObjectId);
  116. echo json_encode(array('name' => $sName));
  117. break;
  118. // ui.linkswidget
  119. case 'doAddObjects':
  120. $sAttCode = utils::ReadParam('sAttCode', '');
  121. $iInputId = utils::ReadParam('iInputId', '');
  122. $sSuffix = utils::ReadParam('sSuffix', '');
  123. $bDuplicates = (utils::ReadParam('bDuplicates', 'false') == 'false') ? false : true;
  124. $aLinkedObjectIds = utils::ReadParam('selectObject', array());
  125. $oWidget = new UILinksWidget($sClass, $sAttCode, $iInputId, $sSuffix, $bDuplicates);
  126. $oWidget->DoAddObjects($oPage, $aLinkedObjectIds);
  127. break;
  128. case 'wizard_helper_preview':
  129. $sJson = utils::ReadParam('json_obj', '');
  130. $oWizardHelper = WizardHelper::FromJSON($sJson);
  131. $oObj = $oWizardHelper->GetTargetObject();
  132. $oObj->DisplayBareProperties($oPage);
  133. break;
  134. case 'wizard_helper':
  135. $sJson = utils::ReadParam('json_obj', '');
  136. $oWizardHelper = WizardHelper::FromJSON($sJson);
  137. $oObj = $oWizardHelper->GetTargetObject();
  138. $sClass = $oWizardHelper->GetTargetClass();
  139. foreach($oWizardHelper->GetFieldsForDefaultValue() as $sAttCode)
  140. {
  141. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  142. $defaultValue = $oAttDef->GetDefaultValue();
  143. $oWizardHelper->SetDefaultValue($sAttCode, $defaultValue);
  144. $oObj->Set($sAttCode, $defaultValue);
  145. }
  146. $sFormPrefix = $oWizardHelper->GetFormPrefix();
  147. foreach($oWizardHelper->GetFieldsForAllowedValues() as $sAttCode)
  148. {
  149. $sId = $oWizardHelper->GetIdForField($sAttCode);
  150. if ($sId != '')
  151. {
  152. // It may happen that the field we'd like to update does not
  153. // exist in the form. For example, if the field should be hidden/read-only
  154. // in the current state of the object
  155. $value = $oObj->Get($sAttCode);
  156. $displayValue = $oObj->GetEditValue($sAttCode);
  157. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  158. $iFlags = MetaModel::GetAttributeFlags($sClass, $oObj->GetState(), $sAttCode);
  159. $sHTMLValue = cmdbAbstractObject::GetFormElementForField($oPage, $sClass, $sAttCode, $oAttDef, $value, $displayValue, $sId, '', $iFlags, array('this' => $oObj, 'formPrefix' => $sFormPrefix));
  160. // Make sure that we immediatly validate the field when we reload it
  161. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  162. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  163. }
  164. }
  165. $oPage->add_script("oWizardHelper{$sFormPrefix}.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper{$sFormPrefix}.UpdateFields();\n");
  166. break;
  167. case 'ajax':
  168. if ($sFilter != "")
  169. {
  170. $sExtraParams = stripslashes(utils::ReadParam('extra_params', ''));
  171. $aExtraParams = array();
  172. if (!empty($sExtraParams))
  173. {
  174. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  175. }
  176. if ($sEncoding == 'oql')
  177. {
  178. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  179. }
  180. else
  181. {
  182. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  183. }
  184. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  185. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  186. }
  187. else
  188. {
  189. $oPage->p("Invalid query (empty filter).");
  190. }
  191. break;
  192. case 'displayCSVHistory':
  193. $bShowAll = (utils::ReadParam('showall', 'false') == 'true');
  194. BulkChange::DisplayImportHistory($oPage, true, $bShowAll);
  195. break;
  196. case 'details':
  197. $key = utils::ReadParam('id', 0);
  198. $oFilter = new DBObjectSearch($sClass);
  199. $oFilter->AddCondition('id', $key, '=');
  200. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  201. $oDisplayBlock->RenderContent($oPage);
  202. break;
  203. case 'preview':
  204. $key = utils::ReadParam('id', 0);
  205. $oFilter = new DBObjectSearch($sClass);
  206. $oFilter->AddCondition('id', $key, '=');
  207. $oDisplayBlock = new DisplayBlock($oFilter, 'preview', false);
  208. $oDisplayBlock->RenderContent($oPage);
  209. break;
  210. case 'pie_chart':
  211. $sGroupBy = utils::ReadParam('group_by', '');
  212. if ($sFilter != '')
  213. {
  214. if ($sEncoding == 'oql')
  215. {
  216. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  217. }
  218. else
  219. {
  220. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  221. }
  222. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  223. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  224. }
  225. else
  226. {
  227. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  228. }
  229. break;
  230. case 'open_flash_chart':
  231. $aParams = utils::ReadParam('params', array());
  232. if ($sFilter != '')
  233. {
  234. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  235. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  236. $oDisplayBlock->RenderContent($oPage, $aParams);
  237. }
  238. else
  239. {
  240. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  241. }
  242. break;
  243. case 'modal_details':
  244. $key = utils::ReadParam('id', 0);
  245. $oFilter = new DBObjectSearch($sClass);
  246. $oFilter->AddCondition('id', $key, '=');
  247. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  248. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  249. $oDisplayBlock->RenderContent($oPage);
  250. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  251. break;
  252. case 'ui.linkswidget':
  253. /*
  254. $sClass = utils::ReadParam('sclass', 'bizContact');
  255. $sAttCode = utils::ReadParam('attCode', 'name');
  256. $sOrg = utils::ReadParam('org_id', '');
  257. $sName = utils::ReadParam('q', '');
  258. $iMaxCount = utils::ReadParam('max', 30);
  259. UILinksWidget::Autocomplete($oPage, $sClass, $sAttCode, $sName, $iMaxCount);
  260. */
  261. break;
  262. case 'ui.linkswidget.linkedset':
  263. /*
  264. $sClass = utils::ReadParam('sclass', 'bizContact');
  265. $sJSONSet = stripslashes(utils::ReadParam('sset', ''));
  266. $sExtKeyToMe = utils::ReadParam('sextkeytome', '');
  267. $sExtKeyToRemote = utils::ReadParam('sextkeytoremote', '');
  268. $iObjectId = utils::ReadParam('id', -1);
  269. UILinksWidget::RenderSet($oPage, $sClass, $sJSONSet, $sExtKeyToMe, $sExtKeyToRemote, $iObjectId);
  270. $iFieldId = utils::ReadParam('myid', '-1');
  271. $oPage->add_ready_script("$('#{$iFieldId}').trigger('validate');");
  272. */
  273. break;
  274. case 'autocomplete':
  275. $key = utils::ReadParam('id', 0);
  276. $sClass = utils::ReadParam('sclass', 'bizContact');
  277. $sAttCode = utils::ReadParam('attCode', 'name');
  278. $sOrg = utils::ReadParam('org_id', '');
  279. $sName = utils::ReadParam('q', '');
  280. $iMaxCount = utils::ReadParam('max', 30);
  281. $aArgs = array();
  282. if (!empty($key))
  283. {
  284. if ($oThis = MetaModel::GetObject($sClass, $key))
  285. {
  286. $aArgs['*this*'] = $oThis;
  287. $aArgs['this'] = $oThis;
  288. }
  289. }
  290. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs, $sName);
  291. $iCount = 0;
  292. foreach($aAllowedValues as $key => $value)
  293. {
  294. $oPage->add($value."|".$key."\n");
  295. }
  296. break;
  297. case 'link':
  298. $sClass = utils::ReadParam('sclass', 'logInfra');
  299. $sAttCode = utils::ReadParam('attCode', 'name');
  300. //$sOrg = utils::ReadParam('org_id', '');
  301. $sName = utils::ReadParam('q', '');
  302. $iMaxCount = utils::ReadParam('max', 30);
  303. $iCount = 0;
  304. $oFilter = new DBObjectSearch($sClass);
  305. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  306. //$oFilter->AddCondition('org_id', $sOrg, '=');
  307. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  308. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  309. {
  310. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  311. $iCount++;
  312. }
  313. break;
  314. case 'create':
  315. case 'create_menu':
  316. $sClass = utils::ReadParam('class', '');
  317. $sFilter = utils::ReadParam('filter', '');
  318. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  319. break;
  320. case 'combo_options':
  321. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  322. $oSet = new CMDBObjectSet($oFilter);
  323. while( $oObj = $oSet->fetch())
  324. {
  325. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  326. }
  327. break;
  328. case 'display_document':
  329. $id = utils::ReadParam('id', '');
  330. $sField = utils::ReadParam('field', '');
  331. if (!empty($sClass) && !empty($id) && !empty($sField))
  332. {
  333. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  334. }
  335. break;
  336. case 'download_document':
  337. $id = utils::ReadParam('id', '');
  338. $sField = utils::ReadParam('field', '');
  339. if (!empty($sClass) && !empty($id) && !empty($sField))
  340. {
  341. DownloadDocument($oPage, $sClass, $id, $sField, 'attachement');
  342. }
  343. break;
  344. case 'search_form':
  345. $sClass = utils::ReadParam('className', '');
  346. $sRootClass = utils::ReadParam('baseClass', '');
  347. $currentId = utils::ReadParam('currentId', '');
  348. $oFilter = new DBObjectSearch($sClass);
  349. $oSet = new CMDBObjectSet($oFilter);
  350. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass));
  351. $oPage->add($sHtml);
  352. break;
  353. case 'set_pref':
  354. $sCode = utils::ReadPostedParam('code', '');
  355. $sValue = utils::ReadPostedParam('value', '');
  356. appUserPreferences::SetPref($sCode, $sValue);
  357. break;
  358. case 'erase_all_pref':
  359. // Can be useful in case a user got some corrupted prefs...
  360. appUserPreferences::ClearPreferences();
  361. break;
  362. default:
  363. $oPage->p("Invalid query.");
  364. }
  365. $oPage->output();
  366. }
  367. catch (Exception $e)
  368. {
  369. echo $e->GetMessage();
  370. IssueLog::Error($e->getMessage());
  371. }
  372. /**
  373. * Downloads a document to the browser, either as 'inline' or 'attachment'
  374. *
  375. * @param WebPage $oPage The web page for the output
  376. * @param string $sClass Class name of the object
  377. * @param mixed $id Identifier of the object
  378. * @param string $sAttCode Name of the attribute containing the document to download
  379. * @param string $sContentDisposition Either 'inline' or 'attachment'
  380. * @return none
  381. */
  382. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  383. {
  384. try
  385. {
  386. $oObj = MetaModel::GetObject($sClass, $id);
  387. if (is_object($oObj))
  388. {
  389. $oDocument = $oObj->Get($sAttCode);
  390. if (is_object($oDocument))
  391. {
  392. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  393. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  394. $oPage->add($oDocument->GetData());
  395. }
  396. }
  397. }
  398. catch(Exception $e)
  399. {
  400. $oPage->p($e->getMessage());
  401. }
  402. }
  403. ?>