Completed
Push — master ( 966ba4...93b0d7 )
by Jean-Christophe
02:34
created

JsUtilsAjaxTrait::_postForm()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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