ajax.render.php 13 KB

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