1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\traits; |
4
|
|
|
|
5
|
|
|
use Ajax\service\JArray; |
6
|
|
|
use Ajax\service\AjaxTransition; |
7
|
|
|
use Ajax\service\Javascript; |
8
|
|
|
use Ajax\service\JString; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author jc |
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 and receives the JSON data types by assigning DOM elements with the same name |
145
|
|
|
* @param string $url the request url |
146
|
|
|
* @param string $params JSON parameters |
147
|
|
|
* @param string $method Method used |
148
|
|
|
* @param string $jsCallback javascript code to execute after the request |
149
|
|
|
* @param string $attr |
150
|
|
|
* @param string $context |
151
|
|
|
* @param boolean $immediatly |
152
|
|
|
*/ |
153
|
|
|
private function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
154
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
155
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
156
|
|
|
$retour.="$.{$method}(url,".$params.").done(function( data ) {\n"; |
157
|
|
|
$retour.="\tdata=$.parseJSON(data);for(var key in data){" |
158
|
|
|
."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n"; |
159
|
|
|
$retour.="\t".$jsCallback."\n". |
160
|
|
|
"\t$(document).trigger('jsonReady',[data]);\n". |
161
|
|
|
"});\n"; |
162
|
|
|
if ($immediatly) |
163
|
|
|
$this->jquery_code_for_compile[]=$retour; |
164
|
|
|
return $retour; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
169
|
|
|
* @param string $url the request url |
170
|
|
|
* @param string $params JSON parameters |
171
|
|
|
* @param string $method Method used |
172
|
|
|
* @param string $jsCallback javascript code to execute after the request |
173
|
|
|
* @param string $context |
174
|
|
|
* @param boolean $immediatly |
175
|
|
|
*/ |
176
|
|
|
public function json($url, $method="get", $params="{}", $jsCallback=NULL,$context="document",$immediatly=false) { |
177
|
|
|
return $this->_json($url,$method,$params,$jsCallback,NULL,$context,$immediatly); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
182
|
|
|
* @param string $element |
183
|
|
|
* @param string $event |
184
|
|
|
* @param string $url the request address |
185
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
186
|
|
|
*/ |
187
|
|
View Code Duplication |
public function jsonOn($event,$element, $url,$parameters=array()) { |
|
|
|
|
188
|
|
|
$preventDefault=true; |
189
|
|
|
$stopPropagation=true; |
190
|
|
|
$jsCallback=null; |
191
|
|
|
$attr="id"; |
192
|
|
|
$method="get"; |
193
|
|
|
$context="document"; |
194
|
|
|
$params="{}"; |
195
|
|
|
$immediatly=true; |
196
|
|
|
extract($parameters); |
197
|
|
|
return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation,$immediatly); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
202
|
|
|
* @param string $url the request url |
203
|
|
|
* @param string $params Paramètres passés au format JSON |
204
|
|
|
* @param string $method Method used |
205
|
|
|
* @param string $jsCallback javascript code to execute after the request |
206
|
|
|
* @param string $context jquery DOM element, array container. |
207
|
|
|
*/ |
208
|
|
|
public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL,$context=NULL) { |
209
|
|
|
return $this->json($url, $method, $params, $jsCallback, $context,false); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
214
|
|
|
* @param string $url the request url |
215
|
|
|
* @param string $params The JSON parameters |
216
|
|
|
* @param string $method Method used |
217
|
|
|
* @param string $jsCallback javascript code to execute after the request |
218
|
|
|
* @param string $context jquery DOM element, array container. |
219
|
|
|
* @param string $rowClass the css class for the new element |
220
|
|
|
* @param boolean $immediatly |
221
|
|
|
*/ |
222
|
|
|
private function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$attr="id",$immediatly=false) { |
223
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
224
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
225
|
|
|
if($context===null){ |
226
|
|
|
$parent="$('".$maskSelector."').parent()"; |
227
|
|
|
$newElm = "$('#'+newId)"; |
228
|
|
|
}else{ |
229
|
|
|
$parent=$context; |
230
|
|
|
$newElm = $context.".find('#'+newId)"; |
231
|
|
|
} |
232
|
|
|
$appendTo="\t\tnewElm.appendTo(".$parent.");\n"; |
233
|
|
|
$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n"; |
234
|
|
|
$retour.=$parent.".find('._json').remove();"; |
235
|
|
|
$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(); |
236
|
|
|
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"; |
237
|
|
|
$retour.= $appendTo; |
238
|
|
|
$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"; |
239
|
|
|
$retour.="\t$(document).trigger('jsonReady',[data]);\n"; |
240
|
|
|
$retour.="\t".$jsCallback."\n"."});\n"; |
241
|
|
|
if ($immediatly) |
242
|
|
|
$this->jquery_code_for_compile[]=$retour; |
243
|
|
|
return $retour; |
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 $rowClass the css class for the new element |
253
|
|
|
* @param string $context jquery DOM element, array container. |
254
|
|
|
* @param boolean $immediatly |
255
|
|
|
*/ |
256
|
|
|
public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL,$immediatly=false) { |
257
|
|
|
return $this->_jsonArray($maskSelector, $url,$method,$params,$jsCallback,$rowClass,$context,NULL,$immediatly); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* 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 |
262
|
|
|
* @param string $maskSelector the selector of the element to clone |
263
|
|
|
* @param string $url the request url |
264
|
|
|
* @param string $params JSON parameters |
265
|
|
|
* @param string $method Method used |
266
|
|
|
* @param string $jsCallback javascript code to execute after the request |
267
|
|
|
* @param string $rowClass the css class for the new element |
268
|
|
|
* @param string $context jquery DOM element, array container. |
269
|
|
|
*/ |
270
|
|
|
public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL,$rowClass="_json",$context=NULL) { |
271
|
|
|
return $this->jsonArray($maskSelector, $url, $method, $params, $jsCallback,$rowClass,$context,false); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
276
|
|
|
* @param string $element |
277
|
|
|
* @param string $event |
278
|
|
|
* @param string $url the request url |
279
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
280
|
|
|
*/ |
281
|
|
View Code Duplication |
public function jsonArrayOn($event,$element,$maskSelector, $url,$parameters=array()) { |
|
|
|
|
282
|
|
|
$preventDefault=true; |
283
|
|
|
$stopPropagation=true; |
284
|
|
|
$jsCallback=null; |
285
|
|
|
$attr="id"; |
286
|
|
|
$method="get"; |
287
|
|
|
$context = null; |
288
|
|
|
$params="{}"; |
289
|
|
|
$immediatly=true; |
290
|
|
|
$rowClass="_json"; |
291
|
|
|
extract($parameters); |
292
|
|
|
return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $rowClass, $context,$attr), $event, $preventDefault, $stopPropagation,$immediatly); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Prepares a Get ajax request |
297
|
|
|
* To use on an event |
298
|
|
|
* @param string $url The url of the request |
299
|
|
|
* @param string $params JSON parameters |
300
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
301
|
|
|
* @param string $jsCallback javascript code to execute after the request |
302
|
|
|
* @param string $attr the html attribute added to the request |
303
|
|
|
* @param string $jqueryDone the jquery function call on ajax data. default:html |
304
|
|
|
* @param string|callable $ajaxTransition |
305
|
|
|
*/ |
306
|
|
|
public function getDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL,$attr="id",$jqueryDone="html",$ajaxTransition=null) { |
307
|
|
|
return $this->_get($url, $params,$responseElement,$jsCallback,$attr,false,$jqueryDone,$ajaxTransition); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Performs a get to $url on the event $event on $element |
312
|
|
|
* and display it in $responseElement |
313
|
|
|
* @param string $event |
314
|
|
|
* @param string $element |
315
|
|
|
* @param string $url The url of the request |
316
|
|
|
* @param string $responseElement The selector of the HTML element displaying the answer |
317
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html") |
318
|
|
|
*/ |
319
|
|
|
public function getOn($event, $element, $url, $responseElement="", $parameters=array()) { |
320
|
|
|
$preventDefault=true; |
321
|
|
|
$stopPropagation=true; |
322
|
|
|
$jsCallback=null; |
323
|
|
|
$attr="id"; |
324
|
|
|
$hasLoader=true; |
325
|
|
|
$immediatly=true; |
326
|
|
|
$jqueryDone="html"; |
327
|
|
|
$ajaxTransition=null; |
328
|
|
|
extract($parameters); |
329
|
|
|
$params=JArray::getDefaultValue($parameters, "params", "{}"); |
330
|
|
|
return $this->_add_event($element, $this->_get($url, $params,$responseElement,$jsCallback,$attr, $hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Performs a get to $url on the click event on $element |
335
|
|
|
* and display it in $responseElement |
336
|
|
|
* @param string $element |
337
|
|
|
* @param string $url The url of the request |
338
|
|
|
* @param string $responseElement The selector of the HTML element displaying the answer |
339
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html") |
340
|
|
|
*/ |
341
|
|
|
public function getOnClick($element, $url, $responseElement="", $parameters=array()) { |
342
|
|
|
return $this->getOn("click", $element, $url, $responseElement, $parameters); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
private function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
346
|
|
|
return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$ajaxTransition,$immediatly); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Makes an ajax post |
351
|
|
|
* @param string $url the request url |
352
|
|
|
* @param string $params JSON parameters |
353
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
354
|
|
|
* @param string $jsCallback javascript code to execute after the request |
355
|
|
|
* @param boolean $hasLoader true for showing ajax loader. default : true |
356
|
|
|
* @param string $jqueryDone the jquery function call on ajax data. default:html |
357
|
|
|
* @param string|callable $ajaxTransition |
358
|
|
|
*/ |
359
|
|
|
public function post($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
360
|
|
|
return $this->_post($url, $params, $responseElement, $jsCallback, NULL, $hasLoader,$jqueryDone,$ajaxTransition,true); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Prepares a delayed ajax POST |
365
|
|
|
* to use on an event |
366
|
|
|
* @param string $url the request url |
367
|
|
|
* @param string $params JSON parameters |
368
|
|
|
* @param string $attr the html attribute added to the request |
369
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
370
|
|
|
* @param string $jsCallback javascript code to execute after the request |
371
|
|
|
* @param boolean $hasLoader true for showing ajax loader. default : true |
372
|
|
|
* @param string $jqueryDone the jquery function call on ajax data. default:html |
373
|
|
|
* @param string|callable $ajaxTransition |
374
|
|
|
*/ |
375
|
|
|
public function postDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL, $attr="id",$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
376
|
|
|
return $this->_post($url, $params, $responseElement, $jsCallback, $attr, $hasLoader,$jqueryDone,$ajaxTransition,false); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Performs a post to $url on the event $event fired on $element and pass the parameters $params |
381
|
|
|
* Display the result in $responseElement |
382
|
|
|
* @param string $event |
383
|
|
|
* @param string $element |
384
|
|
|
* @param string $url The url of the request |
385
|
|
|
* @param string $params The parameters to send |
386
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
387
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
388
|
|
|
*/ |
389
|
|
View Code Duplication |
public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
|
|
|
390
|
|
|
$preventDefault=true; |
391
|
|
|
$stopPropagation=true; |
392
|
|
|
$jsCallback=null; |
393
|
|
|
$attr="id"; |
394
|
|
|
$hasLoader=true; |
395
|
|
|
$immediatly=true; |
396
|
|
|
$jqueryDone="html"; |
397
|
|
|
$ajaxTransition=null; |
398
|
|
|
extract($parameters); |
399
|
|
|
return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Performs a post to $url on the click event fired on $element and pass the parameters $params |
404
|
|
|
* Display the result in $responseElement |
405
|
|
|
* @param string $element |
406
|
|
|
* @param string $url The url of the request |
407
|
|
|
* @param string $params The parameters to send |
408
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
409
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
410
|
|
|
*/ |
411
|
|
|
public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) { |
412
|
|
|
return $this->postOn("click", $element, $url, $params, $responseElement, $parameters); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
private function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$ajaxTransition=null,$immediatly=false) { |
416
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
417
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
418
|
|
|
$retour.="\nvar params=$('#".$form."').serialize();\n"; |
419
|
|
|
$responseElement=$this->_getResponseElement($responseElement); |
420
|
|
|
$retour.="var self=this;\n"; |
421
|
|
|
if($hasLoader===true){ |
422
|
|
|
$this->addLoading($retour, $responseElement); |
423
|
|
|
} |
424
|
|
|
$retour.="$.post(url,params).done(function( data ) {\n"; |
425
|
|
|
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback)."});\n"; |
426
|
|
|
|
427
|
|
|
if ($validation) { |
428
|
|
|
$retour="$('#".$form."').validate({submitHandler: function(form) { |
429
|
|
|
".$retour." |
430
|
|
|
}});\n"; |
431
|
|
|
$retour.="$('#".$form."').submit();\n"; |
432
|
|
|
} |
433
|
|
|
if ($immediatly) |
434
|
|
|
$this->jquery_code_for_compile[]=$retour; |
435
|
|
|
return $retour; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Performs a post form with ajax |
440
|
|
|
* @param string $url The url of the request |
441
|
|
|
* @param string $form The form HTML id |
442
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
443
|
|
|
* @param string $jsCallback javascript code to execute after the request |
444
|
|
|
* @param boolean $hasLoader true for showing ajax loader. default : true |
445
|
|
|
* @param string $jqueryDone the jquery function call on ajax data. default:html |
446
|
|
|
* @param string|callable $ajaxTransition |
447
|
|
|
*/ |
448
|
|
|
public function postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL,$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
449
|
|
|
return $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, NULL, $hasLoader,$jqueryDone,$ajaxTransition,true); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Performs a delayed post form with ajax |
454
|
|
|
* For use on an event |
455
|
|
|
* @param string $url The url of the request |
456
|
|
|
* @param string $form The form HTML id |
457
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
458
|
|
|
* @param string $jsCallback javascript code to execute after the request |
459
|
|
|
* @param string $attr the html attribute added to the request |
460
|
|
|
* @param boolean $hasLoader true for showing ajax loader. default : true |
461
|
|
|
* @param string $jqueryDone the jquery function call on ajax data. default:html |
462
|
|
|
* @param string|callable $ajaxTransition |
463
|
|
|
*/ |
464
|
|
|
public function postFormDeferred($url, $form, $responseElement, $validation=false, $jsCallback=NULL,$attr="id",$hasLoader=true,$jqueryDone="html",$ajaxTransition=null) { |
465
|
|
|
return $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr, $hasLoader,$jqueryDone,$ajaxTransition,false); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Performs a post form with ajax in response to an event $event on $element |
470
|
|
|
* display the result in $responseElement |
471
|
|
|
* @param string $event |
472
|
|
|
* @param string $element |
473
|
|
|
* @param string $url |
474
|
|
|
* @param string $form |
475
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
476
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
477
|
|
|
*/ |
478
|
|
|
public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) { |
479
|
|
|
$preventDefault=true; |
480
|
|
|
$stopPropagation=true; |
481
|
|
|
$validation=false; |
482
|
|
|
$jsCallback=null; |
483
|
|
|
$attr="id"; |
484
|
|
|
$hasLoader=true; |
485
|
|
|
$immediatly=true; |
486
|
|
|
$jqueryDone="html"; |
487
|
|
|
$ajaxTransition=null; |
488
|
|
|
extract($parameters); |
489
|
|
|
return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader,$jqueryDone,$ajaxTransition), $event, $preventDefault, $stopPropagation,$immediatly); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Performs a post form with ajax in response to the click event on $element |
494
|
|
|
* display the result in $responseElement |
495
|
|
|
* @param string $element |
496
|
|
|
* @param string $url |
497
|
|
|
* @param string $form |
498
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
499
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null) |
500
|
|
|
*/ |
501
|
|
|
public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) { |
502
|
|
|
return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters); |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: