Completed
Push — master ( a91c54...ab06fe )
by Jean-Christophe
03:07
created

JsUtilsAjaxTrait::jsonArrayDeferred()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 7
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,$responseElement="",$parameters=[]) {
22
		$jsCallback=null;
23
		$attr="id";
24
		$hasLoader=true;
25
		$immediatly=false;
26
		$jqueryDone="html";
27
		$ajaxTransition=null;
28
		$params=null;
29
		$headers=null;
30
		$jsCondition=null;
31
		extract($parameters);
32
33
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
34
		$retour=$this->_getAjaxUrl($url, $attr);
35
		$responseElement=$this->_getResponseElement($responseElement);
36
		$retour.="var self=this;\n";
37
		if($hasLoader===true){
38
			$this->addLoading($retour, $responseElement);
39
		}
40
41
		$ajaxParameters=["url"=>"url","method"=>"'".\strtoupper($method)."'"];
42
		if(isset($params)){
43
			$ajaxParameters["data"]=self::_correctParams($params);
44
		}
45
		if(isset($headers)){
46
			$ajaxParameters["headers"]=$this->_correctParams($headers);
47
		}
48
		$this->createAjaxParameters($ajaxParameters, $parameters);
49
		$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data, textStatus, jqXHR ) {\n";
50
		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n";
51
		$retour=$this->_addJsCondition($jsCondition,$retour);
52
		if ($immediatly)
53
			$this->jquery_code_for_compile[]=$retour;
54
		return $retour;
55
	}
56
57
	protected function createAjaxParameters(&$original,$parameters){
58
		$validParameters=["dataType"=>"'%value%'","beforeSend"=>"function(jqXHR,settings){%value%}","complete"=>"function(jqXHR){%value%}"];
59
		foreach ($validParameters as $param=>$mask){
60
			if(isset($parameters[$param])){
61
				$original[$param]=\str_replace("%value%", $parameters[$param], $mask);
62
			}
63
		}
64
	}
65
66
	protected function implodeAjaxParameters($ajaxParameters){
67
		$s = ''; foreach ($ajaxParameters as $k=>$v) { if ($s !== '') { $s .= ','; } $s .= "'{$k}':{$v}"; }
68
		return $s;
69
	}
70
71
	protected function _addJsCondition($jsCondition,$jsSource){
72
		if(isset($jsCondition)){
73
			return "if(".$jsCondition."){\n".$jsSource."\n}";
74
		}
75
		return $jsSource;
76
	}
77
78
79
	protected function _getAjaxUrl($url,$attr){
80
		$url=$this->_correctAjaxUrl($url);
81
		$retour="url='".$url."';";
82
		$slash="/";
83
		if(JString::endswith($url, "/")===true){
84
			$slash="";
85
		}
86
		if(JString::isNotNull($attr)){
87
			if ($attr==="value"){
88
				$retour.="url=url+'".$slash."'+$(this).val();\n";
89
			}elseif ($attr==="html"){
90
				$retour.="url=url+'".$slash."'+$(this).html();\n";
91
			}elseif(\substr($attr, 0,3)==="js:"){
92
				$retour.="url=url+'".$slash."'+".\substr($attr, 3).";\n";
93
			}elseif($attr!==null && $attr!=="")
94
				$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
95
		}
96
		return $retour;
97
	}
98
99
	protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){
100
		$retour="";$call=null;
101
		if ($responseElement!=="") {
102
			if(isset($ajaxTransition)){
103
				$call=$this->setAjaxDataCall($ajaxTransition);
104
			}elseif(isset($this->ajaxTransition)){
105
				$call=$this->ajaxTransition;
106
			}
107
			if(\is_callable($call))
108
				$retour="\t".$call($responseElement,$jqueryDone).";\n";
109
			else
110
				$retour="\t$({$responseElement}).{$jqueryDone}( data );\n";
111
		}
112
		$retour.="\t".$jsCallback."\n";
113
		return $retour;
114
	}
115
116
	protected function _getResponseElement($responseElement){
117
		if ($responseElement!=="") {
118
			$responseElement=Javascript::prep_value($responseElement);
119
		}
120
		return $responseElement;
121
	}
122
123
	protected function _correctAjaxUrl($url) {
124
		if ($url!=="/" && JString::endsWith($url, "/")===true)
125
			$url=substr($url, 0, strlen($url)-1);
126
			if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
127
				$url=$this->getUrl($url);
128
			}
129
			return $url;
130
	}
131
132
	public static function _correctParams($params){
133
		if(JString::isNull($params)){
134
			return "";
135
		}
136
		if(\preg_match("@^\{.*?\}$@", $params)){
137
			return '$.param('.$params.')';
138
		}
139
		return $params;
140
	}
141
142
	public static function _implodeParams($parameters){
143
		$allParameters=[];
144
		foreach ($parameters as $params){
145
			if(isset($params))
146
				$allParameters[]=self::_correctParams($params);
147
		}
148
		return \implode("+'&'+", $allParameters);
149
	}
150
151
	protected function addLoading(&$retour, $responseElement) {
152
		$loading_notifier='<div class="ajax-loader">';
153
		if ($this->ajaxLoader==='') {
154
			$loading_notifier.="Loading...";
155
		} else {
156
			$loading_notifier.=$this->ajaxLoader;
157
		}
158
		$loading_notifier.='</div>';
159
		$retour.="$({$responseElement}).empty();\n";
160
		$retour.="\t\t$({$responseElement}).prepend('{$loading_notifier}');\n";
161
	}
162
163
	protected function setAjaxDataCall($params){
164
		$result=null;
165
		if(!\is_callable($params)){
166
			$result=function ($responseElement,$jqueryDone="html") use($params){
167
				return AjaxTransition::{$params}($responseElement,$jqueryDone);
168
			};
169
		}
170
		return $result;
171
	}
172
173
	protected function setDefaultParameters(&$parameters,$default){
174
		foreach ($default as $k=>$v){
175
			if(!isset($parameters[$k]))
176
				$parameters[$k]=$v;
177
		}
178
	}
179
180
	public function setAjaxLoader($loader) {
181
		$this->ajaxLoader=$loader;
182
	}
183
184
	/**
185
	 * Performs an ajax GET request
186
	 * @param string $url The url of the request
187
	 * @param string $responseElement selector of the HTML element displaying the answer
188
	 */
189
	private function _get($url, $responseElement="",$parameters=[]) {
190
		return $this->_ajax("get", $url,$responseElement,$parameters);
191
	}
192
193
	/**
194
	 * Performs an ajax GET request
195
	 * @param string $url The url of the request
196
	 * @param string $responseElement selector of the HTML element displaying the answer
197
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
198
	 */
199
	public function get($url, $responseElement="",$parameters=[]) {
200
		$parameters["immediatly"]=true;
201
		return $this->_get($url,$responseElement,$parameters);
202
	}
203
204
	/**
205
	 * Performs an ajax request
206
	 * @param string $method The http method (get, post, delete, put, head)
207
	 * @param string $url The url of the request
208
	 * @param string $responseElement selector of the HTML element displaying the answer
209
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
210
	 */
211
	public function ajax($method,$url, $responseElement="", $parameters=[]) {
212
		$parameters["immediatly"]=true;
213
		return $this->_ajax($method,$url,$responseElement,$parameters);
214
	}
215
216
	/**
217
	 * Performs a deferred ajax request
218
	 * @param string $method The http method (get, post, delete, put, head)
219
	 * @param string $url The url of the request
220
	 * @param string $responseElement selector of the HTML element displaying the answer
221
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
222
	 */
223
	public function ajaxDeferred($method,$url, $responseElement="", $parameters=[]) {
224
		$parameters["immediatly"]=false;
225
		return $this->_ajax($method,$url,$responseElement,$parameters);
226
	}
227
228
	/**
229
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
230
	 * @param string $url the request url
231
	 * @param string $params JSON parameters
232
	 * @param string $method Method used
233
	 * @param string $jsCallback javascript code to execute after the request
234
	 * @param string $attr
235
	 * @param string $context
236
	 * @param boolean $immediatly
237
	 */
238
	private function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) {
239
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
240
		$retour=$this->_getAjaxUrl($url, $attr);
241
		$retour.="$.{$method}(url,".$params.").done(function( data ) {\n";
242
		$retour.="\tdata=$.parseJSON(data);for(var key in data){"
243
				."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n";
244
				$retour.="\t".$jsCallback."\n".
245
						"\t$(document).trigger('jsonReady',[data]);\n".
246
						"});\n";
247
				if ($immediatly)
248
					$this->jquery_code_for_compile[]=$retour;
249
		return $retour;
250
	}
251
252
	/**
253
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
254
	 * @param string $url the request url
255
	 * @param string $params JSON parameters
256
	 * @param string $method Method used
257
	 * @param string $jsCallback javascript code to execute after the request
258
	 * @param string $context
259
	 * @param boolean $immediatly
260
	 */
261
	public function json($url, $method="get", $params="{}", $jsCallback=NULL,$context="document",$immediatly=false) {
262
		return $this->_json($url,$method,$params,$jsCallback,NULL,$context,$immediatly);
263
	}
264
265
	/**
266
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element
267
	 * @param string $element
268
	 * @param string $event
269
	 * @param string $url the request address
270
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
271
	 */
272 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...
273
		$preventDefault=true;
274
		$stopPropagation=true;
275
		$jsCallback=null;
276
		$attr="id";
277
		$method="get";
278
		$context="document";
279
		$params="{}";
280
		$immediatly=true;
281
		extract($parameters);
282
		return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation,$immediatly);
283
	}
284
285
	/**
286
	 * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name
287
	 * @param string $url the request url
288
	 * @param string $params Paramètres passés au format JSON
289
	 * @param string $method Method used
290
	 * @param string $jsCallback javascript code to execute after the request
291
	 * @param string $context jquery DOM element, array container.
292
	 */
293
	public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL,$context=NULL) {
294
		return $this->json($url, $method, $params, $jsCallback, $context,false);
295
	}
296
297
	/**
298
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
299
	 * @param string $url the request url
300
	 * @param string $params The JSON parameters
301
	 * @param string $method Method used
302
	 * @param string $jsCallback javascript code to execute after the request
303
	 * @param string $context jquery DOM element, array container.
304
	 * @param string $rowClass the css class for the new element
305
	 * @param boolean $immediatly
306
	 */
307
	private function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$attr="id",$immediatly=false) {
308
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
309
		$retour=$this->_getAjaxUrl($url, $attr);
310
		if($context===null){
311
			$parent="$('".$maskSelector."').parent()";
312
			$newElm = "$('#'+newId)";
313
		}else{
314
			$parent=$context;
315
			$newElm = $context.".find('#'+newId)";
316
		}
317
		$appendTo="\t\tnewElm.appendTo(".$parent.");\n";
318
		$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n";
319
		$retour.=$parent.".find('._json').remove();";
320
		$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();
321
		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";
322
		$retour.= $appendTo;
323
		$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";
324
		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
325
		$retour.="\t".$jsCallback."\n"."});\n";
326
		if ($immediatly)
327
			$this->jquery_code_for_compile[]=$retour;
328
		return $retour;
329
	}
330
331
	/**
332
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
333
	 * @param string $url the request url
334
	 * @param string $params The JSON parameters
335
	 * @param string $method Method used
336
	 * @param string $jsCallback javascript code to execute after the request
337
	 * @param string $rowClass the css class for the new element
338
	 * @param string $context jquery DOM element, array container.
339
	 * @param boolean $immediatly
340
	 */
341
	public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$immediatly=false) {
342
		return $this->_jsonArray($maskSelector, $url,$method,$params,$jsCallback,$rowClass,$context,NULL,$immediatly);
343
	}
344
345
	/**
346
	 * 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
347
	 * @param string $maskSelector the selector of the element to clone
348
	 * @param string $url the request url
349
	 * @param string $params JSON parameters
350
	 * @param string $method Method used
351
	 * @param string $jsCallback javascript code to execute after the request
352
	 * @param string $rowClass the css class for the new element
353
	 * @param string $context jquery DOM element, array container.
354
	 */
355
	public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL) {
356
		return $this->jsonArray($maskSelector, $url, $method, $params, $jsCallback,$rowClass,$context,false);
357
	}
358
359
	/**
360
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element
361
	 * @param string $element
362
	 * @param string $event
363
	 * @param string $url the request url
364
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true)
365
	 */
366 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...
367
		$preventDefault=true;
368
		$stopPropagation=true;
369
		$jsCallback=null;
370
		$attr="id";
371
		$method="get";
372
		$context = null;
373
		$params="{}";
374
		$immediatly=true;
375
		$rowClass="_json";
376
		extract($parameters);
377
		return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $rowClass, $context,$attr), $event, $preventDefault, $stopPropagation,$immediatly);
378
	}
379
380
	/**
381
	 * Prepares a Get ajax request
382
	 * To use on an event
383
	 * @param string $url The url of the request
384
	 * @param string $responseElement selector of the HTML element displaying the answer
385
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
386
	 */
387
	public function getDeferred($url, $responseElement="", $parameters=[]) {
388
		$parameters["immediatly"]=false;
389
		return $this->_get($url, $responseElement,$parameters);
390
	}
391
392
	/**
393
	 * Performs a get to $url on the event $event on $element
394
	 * and display it in $responseElement
395
	 * @param string $event
396
	 * @param string $element
397
	 * @param string $url The url of the request
398
	 * @param string $responseElement The selector of the HTML element displaying the answer
399
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null)
400
	 */
401
	public function getOn($event, $element, $url, $responseElement="", $parameters=array()) {
402
		$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]);
403
		return $this->_add_event($element, $this->getDeferred($url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]);
404
	}
405
406
	/**
407
	 * Performs an ajax request to $url on the event $event on $element
408
	 * and display it in $responseElement
409
	 * @param string $event
410
	 * @param string $element
411
	 * @param string $url The url of the request
412
	 * @param string $responseElement The selector of the HTML element displaying the answer
413
	 * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null)
414
	 */
415
	public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) {
416
		$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true,"method"=>"get"]);
417
		return $this->_add_event($element, $this->ajaxDeferred($parameters["method"],$url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]);
418
	}
419
420
	/**
421
	 * Performs a get to $url on the click event on $element
422
	 * and display it in $responseElement
423
	 * @param string $element
424
	 * @param string $url The url of the request
425
	 * @param string $responseElement The selector of the HTML element displaying the answer
426
	 * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null)
427
	 */
428
	public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) {
429
		return $this->ajaxOn("click", $element, $url, $responseElement, $parameters);
430
	}
431
432
	/**
433
	 * Performs a get to $url on the click event on $element
434
	 * and display it in $responseElement
435
	 * @param string $element
436
	 * @param string $url The url of the request
437
	 * @param string $responseElement The selector of the HTML element displaying the answer
438
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null)
439
	 */
440
	public function getOnClick($element, $url, $responseElement="", $parameters=array()) {
441
		return $this->getOn("click", $element, $url, $responseElement, $parameters);
442
	}
443
444
	private function _post($url, $params="{}",$responseElement="", $parameters=[]) {
445
		$parameters["params"]=$params;
446
		return $this->_ajax("POST", $url,$responseElement,$parameters);
447
	}
448
449
	/**
450
	 * Makes an ajax post
451
	 * @param string $url the request url
452
	 * @param string $responseElement selector of the HTML element displaying the answer
453
	 * @param string $params JSON parameters
454
	 * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
455
	 */
456
	public function post($url, $params="{}",$responseElement="", $parameters=[]) {
457
		return $this->_post($url, $params,$responseElement, $parameters);
458
	}
459
460
	/**
461
	 * Prepares a delayed ajax POST
462
	 * to use on an event
463
	 * @param string $url the request url
464
	 * @param string $params JSON parameters
465
	 * @param string $responseElement selector of the HTML element displaying the answer
466
	 * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
467
	 */
468
	public function postDeferred($url, $params="{}",$responseElement="", $parameters=[]) {
469
		$parameters["immediatly"]=false;
470
		return $this->_post($url, $params, $responseElement, $parameters);
471
	}
472
473
	/**
474
	 * Performs a post to $url on the event $event fired on $element and pass the parameters $params
475
	 * Display the result in $responseElement
476
	 * @param string $event
477
	 * @param string $element
478
	 * @param string $url The url of the request
479
	 * @param string $params The parameters to send
480
	 * @param string $responseElement selector of the HTML element displaying the answer
481
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
482
	 */
483
	public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
484
		$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]);
485
		return $this->_add_event($element, $this->postDeferred($url, $params, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]);
486
	}
487
488
	/**
489
	 * Performs a post to $url on the click event fired on $element and pass the parameters $params
490
	 * Display the result in $responseElement
491
	 * @param string $element
492
	 * @param string $url The url of the request
493
	 * @param string $params The parameters to send
494
	 * @param string $responseElement selector of the HTML element displaying the answer
495
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
496
	 */
497
	public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) {
498
		return $this->postOn("click", $element, $url, $params, $responseElement, $parameters);
499
	}
500
501
	private function _postForm($url, $form, $responseElement, $parameters=[]) {
502
		$params="{}";$validation=false;$jsCallback=NULL;$attr="id";$hasLoader=true;$jqueryDone="html";$ajaxTransition=null;$immediatly=false;$jsCondition=NULL;$headers=NULL;
503
		\extract($parameters);
504
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
505
		$retour=$this->_getAjaxUrl($url, $attr);
506
		$retour.="\nvar params=$('#".$form."').serialize();\n";
507
		if(isset($params)){
508
			$retour.="params+='&'+".self::_correctParams($params).";\n";
509
		}
510
		$responseElement=$this->_getResponseElement($responseElement);
511
		$retour.="var self=this;\n";
512
		if($hasLoader===true){
513
			$this->addLoading($retour, $responseElement);
514
		}
515
		$strHeaders="";
516
		if(isset($headers))
517
			$strHeaders=", 'headers: '".self::_correctParams($headers);
518
		$retour.="$.ajax({'url':url,'data':params,'method':'POST'".$strHeaders."}).done(function( data ) {\n";
519
		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n";
520
521
		if ($validation) {
522
			$retour="$('#".$form."').validate({submitHandler: function(form) {
523
			".$retour."
524
			}});\n";
525
			$retour.="$('#".$form."').submit();\n";
526
		}
527
		$retour=$this->_addJsCondition($jsCondition, $retour);
528
		if ($immediatly)
529
			$this->jquery_code_for_compile[]=$retour;
530
		return $retour;
531
	}
532
533
	/**
534
	 * Performs a post form with ajax
535
	 * @param string $url The url of the request
536
	 * @param string $form The form HTML id
537
	 * @param string $responseElement selector of the HTML element displaying the answer
538
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
539
	 */
540
	public function postForm($url, $form, $responseElement, $parameters=[]) {
541
		$parameters["immediatly"]=true;
542
		return $this->_postForm($url, $form, $responseElement, $parameters);
543
	}
544
545
	/**
546
	 * Performs a delayed post form with ajax
547
	 * For use on an event
548
	 * @param string $url The url of the request
549
	 * @param string $form The form HTML id
550
	 * @param string $responseElement selector of the HTML element displaying the answer
551
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null)
552
	 */
553
	public function postFormDeferred($url, $form, $responseElement, $parameters=[]) {
554
		$parameters["immediatly"]=false;
555
		return $this->_postForm($url, $form, $responseElement, $parameters);
556
	}
557
558
	/**
559
	 * Performs a post form with ajax in response to an event $event on $element
560
	 * display the result in $responseElement
561
	 * @param string $event
562
	 * @param string $element
563
	 * @param string $url
564
	 * @param string $form
565
	 * @param string $responseElement selector of the HTML element displaying the answer
566
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null)
567
	 */
568
	public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) {
569
		$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]);
570
		return $this->_add_event($element, $this->postFormDeferred($url, $form, $responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]);
571
	}
572
573
	/**
574
	 * Performs a post form with ajax in response to the click event on $element
575
	 * display the result in $responseElement
576
	 * @param string $element
577
	 * @param string $url
578
	 * @param string $form
579
	 * @param string $responseElement selector of the HTML element displaying the answer
580
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null)
581
	 */
582
	public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) {
583
		return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters);
584
	}
585
}
586