ajax.render.php 13 KB

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