Completed
Push — master ( fb7dfb...db4c08 )
by Jean-Christophe
02:42
created

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