ajax.render.php 14 KB

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