Completed
Push — master ( ef9455...966ba4 )
by Jean-Christophe
02:39
created

JsUtilsAjaxTrait::ajax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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