Completed
Push — master ( 2ffc14...238349 )
by Jean-Christophe
02:59
created

JsUtilsAjaxTrait::_post()   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 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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