ajax.render.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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', '', 'get');
  46. $sLinkageAttr = utils::ReadParam('linkageAttr', '', 'get');
  47. $iObjectId = utils::ReadParam('objectId', '', 'get');
  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(), 'get');
  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', '', 'post');
  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. if ($sEncoding == 'oql')
  171. {
  172. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  173. }
  174. else
  175. {
  176. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  177. }
  178. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  179. $oDisplayBlock->RenderContent($oPage, $aParams);
  180. }
  181. else
  182. {
  183. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  184. }
  185. break;
  186. case 'modal_details':
  187. $key = utils::ReadParam('id', 0);
  188. $oFilter = new DBObjectSearch($sClass);
  189. $oFilter->AddCondition('id', $key, '=');
  190. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  191. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  192. $oDisplayBlock->RenderContent($oPage);
  193. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  194. break;
  195. case 'ui.linkswidget':
  196. /*
  197. $sClass = utils::ReadParam('sclass', 'bizContact');
  198. $sAttCode = utils::ReadParam('attCode', 'name');
  199. $sOrg = utils::ReadParam('org_id', '');
  200. $sName = utils::ReadParam('q', '');
  201. $iMaxCount = utils::ReadParam('max', 30);
  202. UILinksWidget::Autocomplete($oPage, $sClass, $sAttCode, $sName, $iMaxCount);
  203. */
  204. break;
  205. case 'ui.linkswidget.linkedset':
  206. /*
  207. $sClass = utils::ReadParam('sclass', 'bizContact');
  208. $sJSONSet = stripslashes(utils::ReadParam('sset', ''));
  209. $sExtKeyToMe = utils::ReadParam('sextkeytome', '');
  210. $sExtKeyToRemote = utils::ReadParam('sextkeytoremote', '');
  211. $iObjectId = utils::ReadParam('id', -1);
  212. UILinksWidget::RenderSet($oPage, $sClass, $sJSONSet, $sExtKeyToMe, $sExtKeyToRemote, $iObjectId);
  213. $iFieldId = utils::ReadParam('myid', '-1');
  214. $oPage->add_ready_script("$('#{$iFieldId}').trigger('validate');");
  215. */
  216. break;
  217. case 'autocomplete':
  218. $key = utils::ReadParam('id', 0);
  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. $aArgs = array();
  225. if (!empty($key))
  226. {
  227. if ($oThis = MetaModel::GetObject($sClass, $key))
  228. {
  229. $aArgs['*this*'] = $oThis;
  230. $aArgs['this'] = $oThis;
  231. }
  232. }
  233. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs, $sName);
  234. $iCount = 0;
  235. foreach($aAllowedValues as $key => $value)
  236. {
  237. $oPage->add($value."|".$key."\n");
  238. }
  239. break;
  240. case 'link':
  241. $sClass = utils::ReadParam('sclass', 'logInfra');
  242. $sAttCode = utils::ReadParam('attCode', 'name');
  243. //$sOrg = utils::ReadParam('org_id', '');
  244. $sName = utils::ReadParam('q', '');
  245. $iMaxCount = utils::ReadParam('max', 30);
  246. $iCount = 0;
  247. $oFilter = new DBObjectSearch($sClass);
  248. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  249. //$oFilter->AddCondition('org_id', $sOrg, '=');
  250. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  251. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  252. {
  253. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  254. $iCount++;
  255. }
  256. break;
  257. case 'create':
  258. case 'create_menu':
  259. $sClass = utils::ReadParam('class', '');
  260. $sFilter = utils::ReadParam('filter', '');
  261. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  262. break;
  263. case 'combo_options':
  264. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  265. $oSet = new CMDBObjectSet($oFilter);
  266. while( $oObj = $oSet->fetch())
  267. {
  268. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetName().'</option>');
  269. }
  270. break;
  271. case 'display_document':
  272. $id = utils::ReadParam('id', '');
  273. $sField = utils::ReadParam('field', '');
  274. if (!empty($sClass) && !empty($id) && !empty($sField))
  275. {
  276. DownloadDocument($oPage, $sClass, $id, $sField, 'inline');
  277. }
  278. break;
  279. case 'download_document':
  280. $id = utils::ReadParam('id', '');
  281. $sField = utils::ReadParam('field', '');
  282. if (!empty($sClass) && !empty($id) && !empty($sField))
  283. {
  284. DownloadDocument($oPage, $sClass, $id, $sField, 'attachement');
  285. }
  286. break;
  287. case 'search_form':
  288. $sClass = utils::ReadParam('className', '');
  289. $sRootClass = utils::ReadParam('baseClass', '');
  290. $currentId = utils::ReadParam('currentId', '');
  291. $oFilter = new DBObjectSearch($sClass);
  292. $oSet = new CMDBObjectSet($oFilter);
  293. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass));
  294. $oPage->add($sHtml);
  295. break;
  296. case 'set_pref':
  297. $sCode = utils::ReadParam('code', '', 'post');
  298. $sValue = utils::ReadParam('value', '', 'post');
  299. appUserPreferences::SetPref($sCode, $sValue);
  300. break;
  301. case 'erase_all_pref':
  302. // Can be useful in case a user got some corrupted prefs...
  303. appUserPreferences::ClearPreferences();
  304. break;
  305. default:
  306. $oPage->p("Invalid query.");
  307. }
  308. $oPage->output();
  309. /**
  310. * Downloads a document to the browser, either as 'inline' or 'attachment'
  311. *
  312. * @param WebPage $oPage The web page for the output
  313. * @param string $sClass Class name of the object
  314. * @param mixed $id Identifier of the object
  315. * @param string $sAttCode Name of the attribute containing the document to download
  316. * @param string $sContentDisposition Either 'inline' or 'attachment'
  317. * @return none
  318. */
  319. function DownloadDocument(WebPage $oPage, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  320. {
  321. try
  322. {
  323. $oObj = MetaModel::GetObject($sClass, $id);
  324. if (is_object($oObj))
  325. {
  326. $oDocument = $oObj->Get($sAttCode);
  327. if (is_object($oDocument))
  328. {
  329. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  330. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  331. $oPage->add($oDocument->GetData());
  332. }
  333. }
  334. }
  335. catch(Exception $e)
  336. {
  337. $oPage->p($e->getMessage());
  338. }
  339. }
  340. ?>