Completed
Push — master ( 541414...c44f15 )
by Jean-Christophe
03:02
created

JsUtilsAjaxTrait::_add_event()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Ajax\common\traits;
4
5
use Ajax\service\AjaxTransition;
6
use Ajax\service\Javascript;
7
use Ajax\service\JString;
8
9
/**
10
 * @author jc
11
 * @property array $jquery_code_for_compile
12
 */
13
trait JsUtilsAjaxTrait {
14
15
	protected $ajaxTransition;
16
	protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>';
17
18
	abstract public function getUrl($url);
19
	abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true);
20
21
	protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) {
22
		if(JString::isNull($params)){$params="{}";}
23
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
24
		$retour=$this->_getAjaxUrl($url, $attr);
25
		$responseElement=$this->_getResponseElement($responseElement);
26
		$retour.="var self=this;\n";
27
		if($hasLoader===true){
28
			$this->addLoading($retour, $responseElement);
29
		}
30
		$retour.="$.".$method."(url,".$params.").done(function( data ) {\n";
31
		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n";
32
		if ($immediatly)
33
			$this->jquery_code_for_compile[]=$retour;
34
			return $retour;
35
	}
36
37
38
39
	protected function _getAjaxUrl($url,$attr){
40
		$url=$this->_correctAjaxUrl($url);
41
		$retour="url='".$url."';";
42
		$slash="/";
43
		if(JString::endswith($url, "/")===true)
44
			$slash="";
45
			if(JString::isNotNull($attr)){
46
				if ($attr==="value")
47
					$retour.="url=url+'".$slash."'+$(this).val();\n";
48
				elseif ($attr==="html")
49
					$retour.="url=url+'".$slash."'+$(this).html();\n";
50
				elseif($attr!==null && $attr!=="")
51
					$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
52
			}
53
			return $retour;
54
	}
55
56
	protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){
57
		$retour="";$call=null;
58
		if ($responseElement!=="") {
59
			if(isset($ajaxTransition)){
60
				$call=$this->setAjaxDataCall($ajaxTransition);
61
			}elseif(isset($this->ajaxTransition)){
62
				$call=$this->ajaxTransition;
63
			}
64
			if(\is_callable($call))
65
				$retour="\t".$call($responseElement,$jqueryDone).";\n";
66
				else
67
					$retour="\t$({$responseElement}).{$jqueryDone}( data );\n";
68
		}
69
		$retour.="\t".$jsCallback."\n";
70
		return $retour;
71
	}
72
73
	protected function _getResponseElement($responseElement){
74
		if ($responseElement!=="") {
75
			$responseElement=Javascript::prep_value($responseElement);
76
		}
77
		return $responseElement;
78
	}
79
80
	protected function _correctAjaxUrl($url) {
81
		if ($url!=="/" && JString::endsWith($url, "/")===true)
82
			$url=substr($url, 0, strlen($url)-1);
83
			if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
84
				$url=$this->getUrl($url);
85
			}
86
			return $url;
87
	}
88
89
	protected function addLoading(&$retour, $responseElement) {
90
		$loading_notifier='<div class="ajax-loader">';
91
		if ($this->ajaxLoader=='') {
92
			$loading_notifier.="Loading...";
93
		} else {
94
			$loading_notifier.=$this->ajaxLoader;
95
		}
96
		$loading_notifier.='</div>';
97
		$retour.="$({$responseElement}).empty();\n";
98
		$retour.="\t\t$({$responseElement}).prepend('{$loading_notifier}');\n";
99
	}
100
101
	protected function setAjaxDataCall($params){
102
		$result=null;
103
		if(!\is_callable($params)){
104
			$result=function ($responseElement,$jqueryDone="html") use($params){
105
				return AjaxTransition::{$params}($responseElement,$jqueryDone);
106
			};
107
		}
108
		return $result;
109
	}
110
111
	public function setAjaxLoader($loader) {
112
		$this->ajaxLoader=$loader;
113
	}
114
115
	/**
116
	 * Performs an ajax GET request
117
	 * @param string $url The url of the request
118
	 * @param string $params JSON parameters
119
	 * @param string $responseElement selector of the HTML element displaying the answer
120
	 * @param string $jsCallback javascript code to execute after the request
121
	 * @param boolean $hasLoader true for showing ajax loader. default : true
122
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
123
	 * @param string|callable $ajaxTransition
124
	 */
125
	private function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) {
126
		return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$ajaxTransition,$immediatly);
127
	}
128
129
	/**
130
	 * Performs an ajax GET request
131
	 * @param string $url The url of the request
132
	 * @param string $params JSON parameters
133
	 * @param string $responseElement selector of the HTML element displaying the answer
134
	 * @param string $jsCallback javascript code to execute after the request
135
	 * @param boolean $hasLoader true for showing ajax loader. default : true
136
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
137
	 * @param string|callable $ajaxTransition
138
	 */
139
	public function get($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) {
140
		return $this->_get($url,$params,$responseElement,$jsCallback,null,$hasLoader,$jqueryDone,$ajaxTransition,true);
141
	}
142
143
	/**
144
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
145
	 * @param string $url the request url
146
	 * @param string $params JSON parameters
147
	 * @param string $method Method used
148
	 * @param string $jsCallback javascript code to execute after the request
149
	 * @param string $attr
150
	 * @param string $context
151
	 * @param boolean $immediatly
152
	 */
153
	private function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) {
154
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
155
		$retour=$this->_getAjaxUrl($url, $attr);
156
		$retour.="$.{$method}(url,".$params.").done(function( data ) {\n";
157
		$retour.="\tdata=$.parseJSON(data);for(var key in data){"
158
				."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n";
159
				$retour.="\t".$jsCallback."\n".
160
						"\t$(document).trigger('jsonReady',[data]);\n".
161
						"});\n";
162
				if ($immediatly)
163
					$this->jquery_code_for_compile[]=$retour;
164
		return $retour;
165
	}
166
167
	/**
168
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
169
	 * @param string $url the request url
170
	 * @param string $params JSON parameters
171
	 * @param string $method Method used
172
	 * @param string $jsCallback javascript code to execute after the request
173
	 * @param string $context
174
	 * @param boolean $immediatly
175
	 */
176
	public function json($url, $method="get", $params="{}", $jsCallback=NULL,$context="document",$immediatly=false) {
177
		return $this->_json($url,$method,$params,$jsCallback,NULL,$context,$immediatly);
178
	}
179
180
	/**
181
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element
182
	 * @param string $element
183
	 * @param string $event
184
	 * @param string $url the request address
185
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
186
	 */
187 View Code Duplication
	public function jsonOn($event,$element, $url,$parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
		$preventDefault=true;
189
		$stopPropagation=true;
190
		$jsCallback=null;
191
		$attr="id";
192
		$method="get";
193
		$context="document";
194
		$params="{}";
195
		$immediatly=true;
196
		extract($parameters);
197
		return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation,$immediatly);
198
	}
199
200
	/**
201
	 * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name
202
	 * @param string $url the request url
203
	 * @param string $params Paramètres passés au format JSON
204
	 * @param string $method Method used
205
	 * @param string $jsCallback javascript code to execute after the request
206
	 * @param string $context jquery DOM element, array container.
207
	 */
208
	public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL,$context=NULL) {
209
		return $this->json($url, $method, $params, $jsCallback, $context,false);
210
	}
211
212
	/**
213
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
214
	 * @param string $url the request url
215
	 * @param string $params The JSON parameters
216
	 * @param string $method Method used
217
	 * @param string $jsCallback javascript code to execute after the request
218
	 * @param string $context jquery DOM element, array container.
219
	 * @param string $rowClass the css class for the new element
220
	 * @param boolean $immediatly
221
	 */
222
	private function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$attr="id",$immediatly=false) {
223
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
224
		$retour=$this->_getAjaxUrl($url, $attr);
225
		if($context===null){
226
			$parent="$('".$maskSelector."').parent()";
227
			$newElm = "$('#'+newId)";
228
		}else{
229
			$parent=$context;
230
			$newElm = $context.".find('#'+newId)";
231
		}
232
		$appendTo="\t\tnewElm.appendTo(".$parent.");\n";
233
		$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n";
234
		$retour.=$parent.".find('._json').remove();";
235
		$retour.="\tdata=$.parseJSON(data);$.each(data, function(index, value) {\n"."\tvar created=false;var maskElm=$('".$maskSelector."').first();maskElm.hide();"."\tvar newId=(maskElm.attr('id') || 'mask')+'-'+index;"."\tvar newElm=".$newElm.";\n"."\tif(!newElm.length){\n"."\t\tnewElm=maskElm.clone();
236
		newElm.attr('id',newId);\n;newElm.addClass('{$rowClass}').removeClass('_jsonArrayModel');\nnewElm.find('[id]').each(function(){ var newId=$(this).attr('id')+'-'+index;$(this).attr('id',newId).removeClass('_jsonArrayChecked');});\n";
237
		$retour.= $appendTo;
238
		$retour.="\t}\n"."\tfor(var key in value){\n"."\t\t\tvar html = $('<div />').append($(newElm).clone()).html();\n"."\t\t\tif(html.indexOf('__'+key+'__')>-1){\n"."\t\t\t\tcontent=$(html.split('__'+key+'__').join(value[key]));\n"."\t\t\t\t$(newElm).replaceWith(content);newElm=content;\n"."\t\t\t}\n"."\t\tvar sel='[data-id=\"'+key+'\"]';if($(sel,newElm).length){\n"."\t\t\tvar selElm=$(sel,newElm);\n"."\t\t\t if(selElm.is('[value]')) { selElm.attr('value',value[key]);selElm.val(value[key]);} else { selElm.html(value[key]); }\n"."\t\t}\n"."}\n"."\t$(newElm).show(true);"."\n"."\t$(newElm).removeClass('hide');"."});\n";
239
		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
240
		$retour.="\t".$jsCallback."\n"."});\n";
241
		if ($immediatly)
242
			$this->jquery_code_for_compile[]=$retour;
243
		return $retour;
244
	}
245
246
	/**
247
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
248
	 * @param string $url the request url
249
	 * @param string $params The JSON parameters
250
	 * @param string $method Method used
251
	 * @param string $jsCallback javascript code to execute after the request
252
	 * @param string $rowClass the css class for the new element
253
	 * @param string $context jquery DOM element, array container.
254
	 * @param boolean $immediatly
255
	 */
256
	public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$immediatly=false) {
257
		return $this->_jsonArray($maskSelector, $url,$method,$params,$jsCallback,$rowClass,$context,NULL,$immediatly);
258
	}
259
260
	/**
261
	 * Peforms an ajax request delayed and receives a JSON array data types by copying and assigning them to the DOM elements with the same name
262
	 * @param string $maskSelector the selector of the element to clone
263
	 * @param string $url the request url
264
	 * @param string $params JSON parameters
265
	 * @param string $method Method used
266
	 * @param string $jsCallback javascript code to execute after the request
267
	 * @param string $rowClass the css class for the new element
268
	 * @param string $context jquery DOM element, array container.
269
	 */
270
	public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL) {
271
		return $this->jsonArray($maskSelector, $url, $method, $params, $jsCallback,$rowClass,$context,false);
272
	}
273
274
	/**
275
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element
276
	 * @param string $element
277
	 * @param string $event
278
	 * @param string $url the request url
279
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true)
280
	 */
281 View Code Duplication
	public function jsonArrayOn($event,$element,$maskSelector, $url,$parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
		$preventDefault=true;
283
		$stopPropagation=true;
284
		$jsCallback=null;
285
		$attr="id";
286
		$method="get";
287
		$context = null;
288
		$params="{}";
289
		$immediatly=true;
290
		$rowClass="_json";
291
		extract($parameters);
292
		return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $rowClass, $context,$attr), $event, $preventDefault, $stopPropagation,$immediatly);
293
	}
294
295
	/**
296
	 * Prepares a Get ajax request
297
	 * To use on an event
298
	 * @param string $url The url of the request
299
	 * @param string $params JSON parameters
300
	 * @param string $responseElement selector of the HTML element displaying the answer
301
	 * @param string $jsCallback javascript code to execute after the request
302
	 * @param string $attr the html attribute added to the request
303
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
304
	 * @param string|callable $ajaxTransition
305
	 */
306
	public function getDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL,$attr="id",$jqueryDone="html",$ajaxTransition=null) {
307
		return $this->_get($url, $params,$responseElement,$jsCallback,$attr,false,$jqueryDone,$ajaxTransition);
308
	}
309
310
	/**
311
	 * Performs a get to $url on the event $event on $element
312
	 * and display it in $responseElement
313
	 * @param string $event
314
	 * @param string $element
315
	 * @param string $url The url of the request
316
	 * @param string $responseElement The selector of the HTML element displaying the answer
317
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html")
318
	 */
319 View Code Duplication
	public function getOn($event, $element, $url, $responseElement="", $parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
320
		$preventDefault=true;
321
		$stopPropagation=true;
322
		$jsCallback=null;
323
		$attr="id";
324
		$hasLoader=true;
325
		$immediatly=true;
326
		$jqueryDone="html";
327
		$ajaxTransition=null;
328
		$params="{}";
329
		extract($parameters);
330
		return $this->_add_event($element, $this->_get($url, $params,$responseElement,$jsCallback,$attr, $hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly);
331
	}
332
333
	/**
334
	 * Performs an ajax request to $url on the event $event on $element
335
	 * and display it in $responseElement
336
	 * @param string $event
337
	 * @param string $element
338
	 * @param string $url The url of the request
339
	 * @param string $responseElement The selector of the HTML element displaying the answer
340
	 * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html")
341
	 */
342 View Code Duplication
	public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
343
		$preventDefault=true;
344
		$stopPropagation=true;
345
		$jsCallback=null;
346
		$attr="id";
347
		$method="get";
348
		$hasLoader=true;
349
		$immediatly=true;
350
		$jqueryDone="html";
351
		$ajaxTransition=null;
352
		$params="{}";
353
		extract($parameters);
354
		return $this->_add_event($element, $this->_ajax($method,$url, $params,$responseElement,$jsCallback,$attr, $hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly);
355
	}
356
357
	/**
358
	 * Performs a get to $url on the click event on $element
359
	 * and display it in $responseElement
360
	 * @param string $element
361
	 * @param string $url The url of the request
362
	 * @param string $responseElement The selector of the HTML element displaying the answer
363
	 * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html")
364
	 */
365
	public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) {
366
		return $this->ajaxOn("click", $element, $url, $responseElement, $parameters);
367
	}
368
369
	/**
370
	 * Performs a get to $url on the click event on $element
371
	 * and display it in $responseElement
372
	 * @param string $element
373
	 * @param string $url The url of the request
374
	 * @param string $responseElement The selector of the HTML element displaying the answer
375
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html")
376
	 */
377
	public function getOnClick($element, $url, $responseElement="", $parameters=array()) {
378
		return $this->getOn("click", $element, $url, $responseElement, $parameters);
379
	}
380
381
	private function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) {
382
		return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$ajaxTransition,$immediatly);
383
	}
384
385
	/**
386
	 * Makes an ajax post
387
	 * @param string $url the request url
388
	 * @param string $params JSON parameters
389
	 * @param string $responseElement selector of the HTML element displaying the answer
390
	 * @param string $jsCallback javascript code to execute after the request
391
	 * @param boolean $hasLoader true for showing ajax loader. default : true
392
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
393
	 * @param string|callable $ajaxTransition
394
	 */
395
	public function post($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) {
396
		return $this->_post($url, $params, $responseElement, $jsCallback, NULL, $hasLoader,$jqueryDone,$ajaxTransition,true);
397
	}
398
399
	/**
400
	 * Prepares a delayed ajax POST
401
	 * to use on an event
402
	 * @param string $url the request url
403
	 * @param string $params JSON parameters
404
	 * @param string $attr the html attribute added to the request
405
	 * @param string $responseElement selector of the HTML element displaying the answer
406
	 * @param string $jsCallback javascript code to execute after the request
407
	 * @param boolean $hasLoader true for showing ajax loader. default : true
408
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
409
	 * @param string|callable $ajaxTransition
410
	 */
411
	public function postDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL, $attr="id",$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) {
412
		return $this->_post($url, $params, $responseElement, $jsCallback, $attr, $hasLoader,$jqueryDone,$ajaxTransition,false);
413
	}
414
415
	/**
416
	 * Performs a post to $url on the event $event fired on $element and pass the parameters $params
417
	 * Display the result in $responseElement
418
	 * @param string $event
419
	 * @param string $element
420
	 * @param string $url The url of the request
421
	 * @param string $params The parameters to send
422
	 * @param string $responseElement selector of the HTML element displaying the answer
423
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null)
424
	 */
425 View Code Duplication
	public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
426
		$preventDefault=true;
427
		$stopPropagation=true;
428
		$jsCallback=null;
429
		$attr="id";
430
		$hasLoader=true;
431
		$immediatly=true;
432
		$jqueryDone="html";
433
		$ajaxTransition=null;
434
		extract($parameters);
435
		return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly);
436
	}
437
438
	/**
439
	 * Performs a post to $url on the click event fired on $element and pass the parameters $params
440
	 * Display the result in $responseElement
441
	 * @param string $element
442
	 * @param string $url The url of the request
443
	 * @param string $params The parameters to send
444
	 * @param string $responseElement selector of the HTML element displaying the answer
445
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null)
446
	 */
447
	public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) {
448
		return $this->postOn("click", $element, $url, $params, $responseElement, $parameters);
449
	}
450
451
	private function _postForm($url, $form, $responseElement, $params=null,$validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) {
452
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
453
		$retour=$this->_getAjaxUrl($url, $attr);
454
		$retour.="\nvar params=$('#".$form."').serialize();\n";
455
		if(isset($params)){
456
			$retour.="params+='&'+$.param(".$params.");\n";
457
		}
458
		$responseElement=$this->_getResponseElement($responseElement);
459
		$retour.="var self=this;\n";
460
		if($hasLoader===true){
461
			$this->addLoading($retour, $responseElement);
462
		}
463
		$retour.="$.post(url,params).done(function( data ) {\n";
464
		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n";
465
466
		if ($validation) {
467
			$retour="$('#".$form."').validate({submitHandler: function(form) {
468
			".$retour."
469
			}});\n";
470
			$retour.="$('#".$form."').submit();\n";
471
		}
472
		if ($immediatly)
473
			$this->jquery_code_for_compile[]=$retour;
474
		return $retour;
475
	}
476
477
	/**
478
	 * Performs a post form with ajax
479
	 * @param string $url The url of the request
480
	 * @param string $form The form HTML id
481
	 * @param string $responseElement selector of the HTML element displaying the answer
482
	 * @param string $params
483
	 * @param string $jsCallback javascript code to execute after the request
484
	 * @param boolean $hasLoader true for showing ajax loader. default : true
485
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
486
	 * @param string|callable $ajaxTransition
487
	 */
488
	public function postForm($url, $form, $responseElement, $params=NULL,$validation=false, $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) {
489
		return $this->_postForm($url, $form, $responseElement, $params,$validation, $jsCallback, NULL, $hasLoader,$jqueryDone,$ajaxTransition,true);
490
	}
491
492
	/**
493
	 * Performs a delayed post form with ajax
494
	 * For use on an event
495
	 * @param string $url The url of the request
496
	 * @param string $form The form HTML id
497
	 * @param string $responseElement selector of the HTML element displaying the answer
498
	 * @param string $params
499
	 * @param boolean $validation
500
	 * @param string $jsCallback javascript code to execute after the request
501
	 * @param string $attr the html attribute added to the request
502
	 * @param boolean $hasLoader true for showing ajax loader. default : true
503
	 * @param string $jqueryDone the jquery function call on ajax data. default:html
504
	 * @param string|callable $ajaxTransition
505
	 */
506
	public function postFormDeferred($url, $form, $responseElement, $params=NULL,$validation=false, $jsCallback=NULL,$attr="id",$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) {
507
		return $this->_postForm($url, $form, $responseElement, $params,$validation, $jsCallback, $attr, $hasLoader,$jqueryDone,$ajaxTransition,false);
508
	}
509
510
	/**
511
	 * Performs a post form with ajax in response to an event $event on $element
512
	 * display the result in $responseElement
513
	 * @param string $event
514
	 * @param string $element
515
	 * @param string $url
516
	 * @param string $form
517
	 * @param string $responseElement selector of the HTML element displaying the answer
518
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null)
519
	 */
520
	public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) {
521
		$preventDefault=true;
522
		$stopPropagation=true;
523
		$validation=false;
524
		$jsCallback=null;
525
		$params=null;
526
		$attr="id";
527
		$hasLoader=true;
528
		$immediatly=true;
529
		$jqueryDone="html";
530
		$ajaxTransition=null;
531
		extract($parameters);
532
		return $this->_add_event($element, $this->_postForm($url, $form, $responseElement,$params, $validation, $jsCallback, $attr,$hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly);
533
	}
534
535
	/**
536
	 * Performs a post form with ajax in response to the click event on $element
537
	 * display the result in $responseElement
538
	 * @param string $element
539
	 * @param string $url
540
	 * @param string $form
541
	 * @param string $responseElement selector of the HTML element displaying the answer
542
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null)
543
	 */
544
	public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) {
545
		return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters);
546
	}
547
}
548