ajax.render.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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->DisplayBareProperties($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. // Make sure that we immediatly validate the field when we reload it
  112. $oPage->add_ready_script("$('#$sId').trigger('validate');");
  113. $oWizardHelper->SetAllowedValuesHtml($sAttCode, $sHTMLValue);
  114. }
  115. }
  116. $oPage->add("<script type=\"text/javascript\">\noWizardHelper.m_oData=".$oWizardHelper->ToJSON().";\noWizardHelper.UpdateFields();\n</script>\n");
  117. break;
  118. case 'ajax':
  119. if ($sFilter != "")
  120. {
  121. $sExtraParams = stripslashes(utils::ReadParam('extra_params', ''));
  122. $aExtraParams = array();
  123. if (!empty($sExtraParams))
  124. {
  125. $aExtraParams = json_decode(str_replace("'", '"', $sExtraParams), true /* associative array */);
  126. }
  127. if ($sEncoding == 'oql')
  128. {
  129. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  130. }
  131. else
  132. {
  133. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  134. }
  135. $oDisplayBlock = new DisplayBlock($oFilter, $sStyle, false);
  136. $oDisplayBlock->RenderContent($oPage, $aExtraParams);
  137. }
  138. else
  139. {
  140. $oPage->p("Invalid query (empty filter).");
  141. }
  142. break;
  143. case 'details':
  144. $key = utils::ReadParam('id', 0);
  145. $oFilter = $oContext->NewFilter($sClass);
  146. $oFilter->AddCondition('id', $key, '=');
  147. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  148. $oDisplayBlock->RenderContent($oPage);
  149. break;
  150. case 'preview':
  151. $key = utils::ReadParam('id', 0);
  152. $oFilter = $oContext->NewFilter($sClass);
  153. $oFilter->AddCondition('id', $key, '=');
  154. $oDisplayBlock = new DisplayBlock($oFilter, 'preview', false);
  155. $oDisplayBlock->RenderContent($oPage);
  156. break;
  157. case 'pie_chart':
  158. $sGroupBy = utils::ReadParam('group_by', '');
  159. if ($sFilter != '')
  160. {
  161. if ($sEncoding == 'oql')
  162. {
  163. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  164. }
  165. else
  166. {
  167. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  168. }
  169. $oDisplayBlock = new DisplayBlock($oFilter, 'pie_chart_ajax', false);
  170. $oDisplayBlock->RenderContent($oPage, array('group_by' => $sGroupBy));
  171. }
  172. else
  173. {
  174. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  175. }
  176. break;
  177. case 'open_flash_chart':
  178. $aParams = utils::ReadParam('params', array());
  179. if ($sFilter != '')
  180. {
  181. if ($sEncoding == 'oql')
  182. {
  183. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  184. }
  185. else
  186. {
  187. $oFilter = CMDBSearchFilter::unserialize($sFilter);
  188. }
  189. $oDisplayBlock = new DisplayBlock($oFilter, 'open_flash_chart_ajax', false);
  190. $oDisplayBlock->RenderContent($oPage, $aParams);
  191. }
  192. else
  193. {
  194. $oPage->add("<chart>\n<chart_type>3d pie</chart_type><!-- empty filter '$sFilter' --></chart>\n.");
  195. }
  196. break;
  197. case 'modal_details':
  198. $key = utils::ReadParam('id', 0);
  199. $oFilter = $oContext->NewFilter($sClass);
  200. $oFilter->AddCondition('id', $key, '=');
  201. $oPage->Add("<p style=\"width:100%; margin-top:-5px;padding:3px; background-color:#33f; color:#fff;\">Object Details</p>\n");
  202. $oDisplayBlock = new DisplayBlock($oFilter, 'details', false);
  203. $oDisplayBlock->RenderContent($oPage);
  204. $oPage->Add("<input type=\"button\" class=\"jqmClose\" value=\" Close \" />\n");
  205. break;
  206. case 'ui.linkswidget':
  207. $sClass = utils::ReadParam('sclass', 'bizContact');
  208. $sAttCode = utils::ReadParam('attCode', 'name');
  209. $sOrg = utils::ReadParam('org_id', '');
  210. $sName = utils::ReadParam('q', '');
  211. $iMaxCount = utils::ReadParam('max', 30);
  212. UILinksWidget::Autocomplete($oPage, $oContext, $sClass, $sAttCode, $sName, $iMaxCount);
  213. break;
  214. case 'ui.linkswidget.linkedset':
  215. $sClass = utils::ReadParam('sclass', 'bizContact');
  216. $sJSONSet = stripslashes(utils::ReadParam('sset', ''));
  217. $sExtKeyToMe = utils::ReadParam('sextkeytome', '');
  218. $sExtKeyToRemote = utils::ReadParam('sextkeytoremote', '');
  219. $iObjectId = utils::ReadParam('id', -1);
  220. UILinksWidget::RenderSet($oPage, $sClass, $sJSONSet, $sExtKeyToMe, $sExtKeyToRemote, $iObjectId);
  221. break;
  222. case 'autocomplete':
  223. $key = utils::ReadParam('id', 0);
  224. $sClass = utils::ReadParam('sclass', 'bizContact');
  225. $sAttCode = utils::ReadParam('attCode', 'name');
  226. $sOrg = utils::ReadParam('org_id', '');
  227. $sName = utils::ReadParam('q', '');
  228. $iMaxCount = utils::ReadParam('max', 30);
  229. $aArgs = array();
  230. if (!empty($key))
  231. {
  232. if ($oThis = MetaModel::GetObject($sClass, $key))
  233. {
  234. $aArgs['*this*'] = $oThis;
  235. $aArgs['this'] = $oThis;
  236. }
  237. }
  238. $aAllowedValues = MetaModel::GetAllowedValues_att($sClass, $sAttCode, $aArgs, $sName);
  239. $iCount = 0;
  240. foreach($aAllowedValues as $key => $value)
  241. {
  242. $oPage->add($value."|".$key."\n");
  243. }
  244. break;
  245. case 'link':
  246. $sClass = utils::ReadParam('sclass', 'logInfra');
  247. $sAttCode = utils::ReadParam('attCode', 'name');
  248. //$sOrg = utils::ReadParam('org_id', '');
  249. $sName = utils::ReadParam('q', '');
  250. $iMaxCount = utils::ReadParam('max', 30);
  251. $iCount = 0;
  252. $oFilter = $oContext->NewFilter($sClass);
  253. $oFilter->AddCondition($sAttCode, $sName, 'Begins with');
  254. //$oFilter->AddCondition('org_id', $sOrg);
  255. $oSet = new CMDBObjectSet($oFilter, array($sAttCode => true));
  256. while( ($iCount < $iMaxCount) && ($oObj = $oSet->fetch()) )
  257. {
  258. $oPage->add($oObj->GetAsHTML($sAttCode)."|".$oObj->GetKey()."\n");
  259. $iCount++;
  260. }
  261. break;
  262. case 'create':
  263. case 'create_menu':
  264. $sClass = utils::ReadParam('class', '');
  265. $sFilter = utils::ReadParam('filter', '');
  266. menuNode::DisplayCreationForm($oPage, $sClass, $sFilter);
  267. break;
  268. case 'combo_options':
  269. $oFilter = CMDBSearchFilter::FromOQL($sFilter);
  270. $oSet = new CMDBObjectSet($oFilter);
  271. while( $oObj = $oSet->fetch())
  272. {
  273. $oPage->add('<option title="Here is more information..." value="'.$oObj->GetKey().'">'.$oObj->GetDisplayName().'</option>');
  274. }
  275. break;
  276. case 'display_document':
  277. $id = utils::ReadParam('id', '');
  278. $sField = utils::ReadParam('field', '');
  279. if (!empty($sClass) && !empty($id) && !empty($sField))
  280. {
  281. DownloadDocument($oPage, $oContext, $sClass, $id, $sField, 'inline');
  282. }
  283. break;
  284. case 'download_document':
  285. $id = utils::ReadParam('id', '');
  286. $sField = utils::ReadParam('field', '');
  287. if (!empty($sClass) && !empty($id) && !empty($sField))
  288. {
  289. DownloadDocument($oPage, $oContext, $sClass, $id, $sField, 'attachement');
  290. }
  291. break;
  292. case 'search_form':
  293. $sClass = utils::ReadParam('className', '', 'get');
  294. $sRootClass = utils::ReadParam('baseClass', '', 'get');
  295. $currentId = utils::ReadParam('currentId', '', 'get');
  296. $oFilter = $oContext->NewFilter($sClass);
  297. $oSet = new CMDBObjectSet($oFilter);
  298. $sHtml = cmdbAbstractObject::GetSearchForm($oPage, $oSet, array('currentId' => $currentId, 'baseClass' => $sRootClass));
  299. $oPage->add($sHtml);
  300. break;
  301. default:
  302. $oPage->p("Invalid query.");
  303. }
  304. $oPage->output();
  305. /**
  306. * Downloads a document to the browser, either as 'inline' or 'attachment'
  307. *
  308. * @param WebPage $oPage The web page for the output
  309. * @param UserContext $oContext The current User/security context to retreive the objects
  310. * @param string $sClass Class name of the object
  311. * @param mixed $id Identifier of the object
  312. * @param string $sAttCode Name of the attribute containing the document to download
  313. * @param string $sContentDisposition Either 'inline' or 'attachment'
  314. * @return none
  315. */
  316. function DownloadDocument(WebPage $oPage, UserContext $oContext, $sClass, $id, $sAttCode, $sContentDisposition = 'attachement')
  317. {
  318. try
  319. {
  320. $oObj = $oContext->GetObject($sClass, $id);
  321. if (is_object($oObj))
  322. {
  323. $oDocument = $oObj->Get($sAttCode);
  324. if (is_object($oDocument))
  325. {
  326. $oPage->add_header('Content-type: '.$oDocument->GetMimeType());
  327. $oPage->add_header('Content-Disposition: '.$sContentDisposition.'; filename="'.$oDocument->GetFileName().'"');
  328. $oPage->add($oDocument->GetData());
  329. }
  330. }
  331. }
  332. catch(Exception $e)
  333. {
  334. $oPage->p($e->getMessage());
  335. }
  336. }
  337. ?>