ajax.render.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.autocompletewidget.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 'details':
  193. $key = utils::ReadParam('id', 0);
  194. $oFilter = new DBObjectSearch($sClass);
  195. $oFilter->AddCondition('id', $key, '=');
  196. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  197. $oDisplayBlock->RenderContent($oPage);
  198. break;
  199. case 'preview':
  200. $key = utils::ReadParam('id', 0);
  201. $oFilter = new DBObjectSearch($sClass);
  202. $oFilter->AddCondition('id', $key, '=');
  203. $oDisplayBlock = new DisplayBlock($oFilter, 'preview', false);
  204. $oDisplayBlock->RenderContent($oPage);
  205. break;
  206. case 'pie_chart':
  207. $sGroupBy = utils::ReadParam('group_by', '');
  208. if ($sFilter != '')
  209. {
  210. if ($sEncoding == 'oql')
  211. {
  212. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  213. }
  214. else
  215. {
  216. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  217. }
  218. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  219. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  220. }
  221. else
  222. {
  223. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  224. }
  225. break;
  226. case 'open_flash_chart':
  227. $aParams = utils::ReadParam('params', array());
  228. if ($sFilter != '')
  229. {
  230. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  231. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  232. $oDisplayBlock->RenderContent($oPage, $aParams);
  233. }
  234. else
  235. {
  236. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  237. }
  238. break;
  239. case 'modal_details':
  240. $key = utils::ReadParam('id', 0);
  241. $oFilter = new DBObjectSearch($sClass);
  242. $oFilter->AddCondition('id', $key, '=');
  243. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  244. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  245. $oDisplayBlock->RenderContent($oPage);
  246. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  247. break;
  248. case 'ui.linkswidget':
  249. /*
  250. $sClass = utils::ReadParam('sclass', 'bizContact');
  251. $sAttCode = utils::ReadParam('attCode', 'name');
  252. $sOrg = utils::ReadParam('org_id', '');
  253. $sName = utils::ReadParam('q', '');
  254. $iMaxCount = utils::ReadParam('max', 30);
  255. UILinksWidget::Autocomplete($oPage, $sClass, $sAttCode, $sName, $iMaxCount);
  256. */
  257. break;
  258. case 'ui.linkswidget.linkedset':
  259. /*
  260. $sClass = utils::ReadParam('sclass', 'bizContact');
  261. $sJSONSet = stripslashes(utils::ReadParam('sset', ''));
  262. $sExtKeyToMe = utils::ReadParam('sextkeytome', '');
  263. $sExtKeyToRemote = utils::ReadParam('sextkeytoremote', '');
  264. $iObjectId = utils::ReadParam('id', -1);
  265. UILinksWidget::RenderSet($oPage, $sClass, $sJSONSet, $sExtKeyToMe, $sExtKeyToRemote, $iObjectId);
  266. $iFieldId = utils::ReadParam('myid', '-1');
  267. $oPage->add_ready_script("$('#{$iFieldId}').trigger('validate');");
  268. */
  269. break;
  270. case 'autocomplete':
  271. $key = utils::ReadParam('id', 0);
  272. $sClass = utils::ReadParam('sclass', 'bizContact');
  273. $sAttCode = utils::ReadParam('attCode', 'name');
  274. $sOrg = utils::ReadParam('org_id', '');
  275. $sName = utils::ReadParam('q', '');
  276. $iMaxCount = utils::ReadParam('max', 30);
  277. $aArgs = array();
  278. if (!empty($key))
  279. {
  280. if ($oThis = MetaModel::GetObject($sClass, $key))
  281. {
  282. $aArgs['*this*'] = $oThis;
  283. $aArgs['this'] = $oThis;
  284. }
  285. }
  286. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs, $sName);
  287. $iCount = 0;
  288. foreach($aAllowedValues as $key => $value)
  289. {
  290. $oPage->add($value."|".$key."\n");
  291. }
  292. break;
  293. case 'link':
  294. $sClass = utils::ReadParam('sclass', 'logInfra');
  295. $sAttCode = utils::ReadParam('attCode', 'name');
  296. //$sOrg = utils::ReadParam('org_id', '');
  297. $sName = utils::ReadParam('q', '');
  298. $iMaxCount = utils::ReadParam('max', 30);
  299. $iCount = 0;
  300. $oFilter = new DBObjectSearch($sClass);
  301. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  302. //$oFilter->AddCondition('org_id', $sOrg, '=');
  303. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  304. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  305. {
  306. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  307. $iCount++;
  308. }
  309. break;
  310. case 'create':
  311. case 'create_menu':
  312. $sClass = utils::ReadParam('class', '');
  313. $sFilter = utils::ReadParam('filter', '');
  314. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  315. break;
  316. case 'combo_options':
  317. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  318. $oSet = new CMDBObjectSet($oFilter);
  319. while( $oObj = $oSet->fetch())
  320. {
  321. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  322. }
  323. break;
  324. case 'display_document':
  325. $id = utils::ReadParam('id', '');
  326. $sField = utils::ReadParam('field', '');
  327. if (!empty($sClass) && !empty($id) && !empty($sField))
  328. {
  329. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  330. }
  331. break;
  332. case 'download_document':
  333. $id = utils::ReadParam('id', '');
  334. $sField = utils::ReadParam('field', '');
  335. if (!empty($sClass) && !empty($id) && !empty($sField))
  336. {
  337. DownloadDocument($oPage, $sClass, $id, $sField, 'attachement');
  338. }
  339. break;
  340. case 'search_form':
  341. $sClass = utils::ReadParam('className', '');
  342. $sRootClass = utils::ReadParam('baseClass', '');
  343. $currentId = utils::ReadParam('currentId', '');
  344. $oFilter = new DBObjectSearch($sClass);
  345. $oSet = new CMDBObjectSet($oFilter);
  346. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass));
  347. $oPage->add($sHtml);
  348. break;
  349. case 'set_pref':
  350. $sCode = utils::ReadPostedParam('code', '');
  351. $sValue = utils::ReadPostedParam('value', '');
  352. appUserPreferences::SetPref($sCode, $sValue);
  353. break;
  354. case 'erase_all_pref':
  355. // Can be useful in case a user got some corrupted prefs...
  356. appUserPreferences::ClearPreferences();
  357. break;
  358. default:
  359. $oPage->p("Invalid query.");
  360. }
  361. $oPage->output();
  362. }
  363. catch (Exception $e)
  364. {
  365. echo $e->GetMessage();
  366. IssueLog::Error($e->getMessage());
  367. }
  368. /**
  369. * Downloads a document to the browser, either as 'inline' or 'attachment'
  370. *
  371. * @param WebPage $oPage The web page for the output
  372. * @param string $sClass Class name of the object
  373. * @param mixed $id Identifier of the object
  374. * @param string $sAttCode Name of the attribute containing the document to download
  375. * @param string $sContentDisposition Either 'inline' or 'attachment'
  376. * @return none
  377. */
  378. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  379. {
  380. try
  381. {
  382. $oObj = MetaModel::GetObject($sClass, $id);
  383. if (is_object($oObj))
  384. {
  385. $oDocument = $oObj->Get($sAttCode);
  386. if (is_object($oDocument))
  387. {
  388. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  389. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  390. $oPage->add($oDocument->GetData());
  391. }
  392. }
  393. }
  394. catch(Exception $e)
  395. {
  396. $oPage->p($e->getMessage());
  397. }
  398. }
  399. ?>