ajax.render.php 15 KB

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