ajax.render.php 13 KB

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