Completed
Push — master ( c7a943...2e4ffb )
by Jean-Christophe
02:55
created

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