PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/ajax.function.php

https://github.com/ardowz/Thesis-SideB
PHP | 302 lines | 165 code | 26 blank | 111 comment | 13 complexity | 3d9c3a37ee97da8e6bd47af62fe2687b MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id: ajax.function.php 14684 2011-06-11 06:32:40Z remi $
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2003-2011 by the INDEPNET Development Team.
  7. http://indepnet.net/ http://glpi-project.org
  8. -------------------------------------------------------------------------
  9. LICENSE
  10. This file is part of GLPI.
  11. GLPI is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. GLPI is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with GLPI; if not, write to the Free Software Foundation, Inc.,
  21. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. --------------------------------------------------------------------------
  23. */
  24. /**
  25. * Complete Dropdown system using ajax to get datas
  26. *
  27. * @param $use_ajax Use ajax search system (if not display a standard dropdown)
  28. * @param $relativeurl Relative URL to the root directory of GLPI
  29. * @param $params Parameters to send to ajax URL
  30. * @param $default Default datas t print in case of $use_ajax
  31. * @param $rand Random parameter used
  32. *
  33. **/
  34. function ajaxDropdown($use_ajax, $relativeurl, $params=array(), $default="&nbsp;", $rand=0) {
  35. global $CFG_GLPI, $DB, $LANG;
  36. $initparams = $params;
  37. if ($rand==0) {
  38. $rand = mt_rand();
  39. }
  40. if ($use_ajax) {
  41. ajaxDisplaySearchTextForDropdown($rand);
  42. ajaxUpdateItemOnInputTextEvent("search_$rand", "results_$rand",
  43. $CFG_GLPI["root_doc"].$relativeurl, $params);
  44. }
  45. echo "<span id='results_$rand'>\n";
  46. if (!$use_ajax) {
  47. // Save post datas if exists
  48. $oldpost = array();
  49. if (isset($_POST) && count($_POST)) {
  50. $oldpost = $_POST;
  51. }
  52. $_POST = $params;
  53. $_POST["searchText"] = $CFG_GLPI["ajax_wildcard"];
  54. include (GLPI_ROOT.$relativeurl);
  55. // Restore $_POST datas
  56. if (count($oldpost)) {
  57. $_POST = $oldpost;
  58. }
  59. } else {
  60. echo $default;
  61. }
  62. echo "</span>\n";
  63. echo "<script type='text/javascript'>";
  64. echo "function update_results_$rand() {";
  65. if ($use_ajax) {
  66. ajaxUpdateItemJsCode("results_$rand", $CFG_GLPI['root_doc'].$relativeurl, $initparams, true,
  67. "search_$rand");
  68. } else {
  69. $initparams["searchText"]=$CFG_GLPI["ajax_wildcard"];
  70. ajaxUpdateItemJsCode("results_$rand", $CFG_GLPI['root_doc'].$relativeurl, $initparams, true,"");
  71. }
  72. echo "}";
  73. echo "</script>";
  74. }
  75. /**
  76. * Input text used as search system in ajax system
  77. *
  78. * @param $id ID of the ajax item
  79. * @param $size size of the input text field
  80. *
  81. **/
  82. function ajaxDisplaySearchTextForDropdown($id, $size=4) {
  83. global $CFG_GLPI, $LANG;
  84. echo "<input title=\"".$LANG['buttons'][0]." (".$CFG_GLPI['ajax_wildcard']." ".$LANG['search'][1].")\"
  85. type='text' ondblclick=\"this.value='".
  86. $CFG_GLPI["ajax_wildcard"]."';\" id='search_$id' name='____data_$id' size='$size'>\n";
  87. }
  88. /**
  89. * Javascript code for update an item when a Input text item changed
  90. *
  91. * @param $toobserve id of the Input text to observe
  92. * @param $toupdate id of the item to update
  93. * @param $url Url to get datas to update the item
  94. * @param $parameters Parameters to send to ajax URL
  95. * @param $spinner NOT USED : always spinner - is a spinner displayed when loading ?
  96. *
  97. **/
  98. function ajaxUpdateItemOnInputTextEvent($toobserve, $toupdate, $url, $parameters=array(),
  99. $spinner=true) {
  100. ajaxUpdateItemOnEvent($toobserve, $toupdate, $url, $parameters, array("dblclick", "keyup"),
  101. $spinner);
  102. }
  103. /**
  104. * Javascript code for update an item when a select item changed
  105. *
  106. * @param $toobserve id of the select to observe
  107. * @param $toupdate id of the item to update
  108. * @param $url Url to get datas to update the item
  109. * @param $parameters Parameters to send to ajax URL
  110. * @param $spinner NOT USED : always spinner - is a spinner displayed when loading ?
  111. *
  112. **/
  113. function ajaxUpdateItemOnSelectEvent($toobserve, $toupdate, $url, $parameters=array(),
  114. $spinner=true) {
  115. ajaxUpdateItemOnEvent($toobserve, $toupdate, $url, $parameters, array("change"), $spinner);
  116. }
  117. /**
  118. * Javascript code for update an item when another item changed
  119. *
  120. * @param $toobserve id (or array of id) of the select to observe
  121. * @param $toupdate id of the item to update
  122. * @param $url Url to get datas to update the item
  123. * @param $parameters Parameters to send to ajax URL
  124. * @param $events array of the observed events
  125. * @param $spinner NOT USED : always spinner - is a spinner displayed when loading ?
  126. *
  127. **/
  128. function ajaxUpdateItemOnEvent($toobserve, $toupdate, $url, $parameters=array(),
  129. $events=array("change"), $spinner=true) {
  130. echo "<script type='text/javascript'>";
  131. ajaxUpdateItemOnEventJsCode($toobserve, $toupdate, $url, $parameters, $events, $spinner);
  132. echo "</script>";
  133. }
  134. /**
  135. * Javascript code for update an item when another item changed (Javascript code only)
  136. *
  137. * @param $toobserve id (or array of id) of the select to observe
  138. * @param $toupdate id of the item to update
  139. * @param $url Url to get datas to update the item
  140. * @param $parameters Parameters to send to ajax URL
  141. * @param $events array of the observed events
  142. * @param $spinner NOT USED : always spinner - is a spinner displayed when loading ?
  143. *
  144. **/
  145. function ajaxUpdateItemOnEventJsCode($toobserve, $toupdate, $url, $parameters=array(),
  146. $events=array("change"), $spinner=true) {
  147. if (is_array($toobserve)) {
  148. $zones = $toobserve;
  149. } else {
  150. $zones = array($toobserve);
  151. }
  152. foreach ($zones as $zone) {
  153. foreach ($events as $event) {
  154. echo "
  155. Ext.get('$zone').on(
  156. '$event',
  157. function() {";
  158. ajaxUpdateItemJsCode($toupdate, $url, $parameters, $spinner, $toobserve);
  159. echo "});\n";
  160. }
  161. }
  162. }
  163. /**
  164. * Javascript code for update an item
  165. *
  166. * @param $toupdate id of the item to update
  167. * @param $url Url to get datas to update the item
  168. * @param $parameters Parameters to send to ajax URL
  169. * @param $spinner NOT USED : always spinner - is a spinner displayed when loading ?
  170. * @param $toobserve id of another item used to get value in case of __VALUE__ used
  171. *
  172. **/
  173. function ajaxUpdateItem($toupdate, $url, $parameters=array(), $spinner=true, $toobserve="") {
  174. echo "<script type='text/javascript'>";
  175. ajaxUpdateItemJsCode($toupdate,$url,$parameters,$spinner,$toobserve);
  176. echo "</script>";
  177. }
  178. /**
  179. * Javascript code for update an item (Javascript code only)
  180. *
  181. * @param $toupdate id of the item to update
  182. * @param $url Url to get datas to update the item
  183. * @param $parameters Parameters to send to ajax URL
  184. * @param $spinner NOT USED : always spinner - is a spinner displayed when loading ?
  185. * @param $toobserve id of another item used to get value in case of __VALUE__ used
  186. * array of id to get value in case of __VALUE#__ used
  187. *
  188. **/
  189. function ajaxUpdateItemJsCode($toupdate, $url, $parameters=array(), $spinner=true, $toobserve="") {
  190. // Get it from a Ext.Element object
  191. $out = "Ext.get('$toupdate').load({
  192. url: '$url',
  193. scripts: true";
  194. if (count($parameters)) {
  195. $out .= ",
  196. params:'";
  197. $first = true;
  198. foreach ($parameters as $key => $val) {
  199. if ($first) {
  200. $first = false;
  201. } else {
  202. $out .= "&";
  203. }
  204. $out .= $key."=";
  205. if (is_array($val)) {
  206. $out .= serialize($val);
  207. } else if (preg_match('/^__VALUE(\d+)__$/',$val,$regs)) {
  208. $out .= "'+Ext.get('".$toobserve[$regs[1]]."').getValue()+'";
  209. } else if ($val==="__VALUE__") {
  210. $out .= "'+Ext.get('$toobserve').getValue()+'";
  211. } else {
  212. if (preg_match("/'/",$val)) {
  213. $out .= rawurlencode($val);
  214. } else {
  215. $out .= $val;
  216. }
  217. }
  218. }
  219. echo $out."'\n";
  220. }
  221. echo "});";
  222. }
  223. /**
  224. * Javascript code for update an item (Javascript code only)
  225. *
  226. * @param $options array of options
  227. * - toupdate : array / Update a specific item on select change on dropdown
  228. * (need value_fieldname, to_update, url (see ajaxUpdateItemOnSelectEvent for informations)
  229. * and may have moreparams)
  230. *
  231. **/
  232. function commonDropdownUpdateItem($options) {
  233. if (isset($options["update_item"])
  234. && (is_array($options["update_item"]) || strlen($options["update_item"])>0)) {
  235. if (!is_array($options["update_item"])) {
  236. $data = unserialize(stripslashes($options["update_item"]));
  237. } else {
  238. $data = $options["update_item"];
  239. }
  240. if (is_array($data) && count($data)) {
  241. $paramsupdate = array();
  242. if (isset($data['value_fieldname'])) {
  243. $paramsupdate = array($data['value_fieldname'] => '__VALUE__');
  244. }
  245. if (isset($data["moreparams"])
  246. && is_array($data["moreparams"])
  247. && count($data["moreparams"])) {
  248. foreach ($data["moreparams"] as $key => $val) {
  249. $paramsupdate[$key] = $val;
  250. }
  251. }
  252. ajaxUpdateItemOnSelectEvent("dropdown_".$options["myname"].$options["rand"],
  253. $data['to_update'], $data['url'], $paramsupdate, false);
  254. }
  255. }
  256. }
  257. ?>