ajax.render.php 12 KB

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