ajax.render.php 15 KB

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