ajax.render.php 12 KB

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