1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\traits; |
4
|
|
|
|
5
|
|
|
use Ajax\service\JString; |
6
|
|
|
use Ajax\service\PhalconUtils; |
7
|
|
|
trait JqueryAjaxTrait { |
8
|
|
|
|
9
|
|
|
protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
10
|
|
|
|
11
|
|
|
public abstract function _prep_value($value); |
12
|
|
|
public abstract function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true); |
13
|
|
|
protected function addLoading(&$retour, $responseElement) { |
14
|
|
|
$loading_notifier='<div class="ajax-loader">'; |
15
|
|
|
if ($this->ajaxLoader=='') { |
16
|
|
|
$loading_notifier.="Loading..."; |
17
|
|
|
} else { |
18
|
|
|
$loading_notifier.=$this->ajaxLoader; |
19
|
|
|
} |
20
|
|
|
$loading_notifier.='</div>'; |
21
|
|
|
$retour.="$({$responseElement}).empty();\n"; |
22
|
|
|
$retour.="\t\t$({$responseElement}).prepend('{$loading_notifier}');\n"; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
26
|
|
|
return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly); |
27
|
|
|
} |
28
|
|
|
public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
29
|
|
|
return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
33
|
|
|
if(JString::isNull($params)){$params="{}";} |
34
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
35
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
36
|
|
|
$responseElement=$this->_getResponseElement($responseElement); |
37
|
|
|
$retour.="var self=this;\n"; |
38
|
|
|
if($hasLoader===true){ |
39
|
|
|
$this->addLoading($retour, $responseElement); |
40
|
|
|
} |
41
|
|
|
$retour.="$.".$method."(url,".$params.").done(function( data ) {\n"; |
42
|
|
|
$retour.=$this->_getOnAjaxDone($responseElement, $jsCallback)."});\n"; |
43
|
|
|
if ($immediatly) |
44
|
|
|
$this->jquery_code_for_compile[]=$retour; |
|
|
|
|
45
|
|
|
return $retour; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function _getAjaxUrl($url,$attr){ |
49
|
|
|
$url=$this->_correctAjaxUrl($url); |
50
|
|
|
$retour="url='".$url."';\n"; |
51
|
|
|
$slash="/"; |
52
|
|
|
if(PhalconUtils::endsWith($url, "/")===true) |
53
|
|
|
$slash=""; |
54
|
|
|
if(JString::isNotNull($attr)){ |
55
|
|
|
if ($attr=="value") |
56
|
|
|
$retour.="url=url+'".$slash."'+$(this).val();\n"; |
57
|
|
|
else if($attr!=null && $attr!=="") |
58
|
|
|
$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n"; |
59
|
|
|
} |
60
|
|
|
return $retour; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function _getOnAjaxDone($responseElement,$jsCallback){ |
64
|
|
|
$retour=""; |
65
|
|
|
if ($responseElement!=="") { |
66
|
|
|
$retour="\t$({$responseElement}).html( data );\n"; |
67
|
|
|
} |
68
|
|
|
$retour.="\t".$jsCallback."\n"; |
69
|
|
|
return $retour; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function _getResponseElement($responseElement){ |
73
|
|
|
if ($responseElement!=="") { |
74
|
|
|
$responseElement=$this->_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->jsUtils->getUrl($url); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
return $url; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name |
90
|
|
|
* @param string $url the request address |
91
|
|
|
* @param string $params Paramètres passés au format JSON |
92
|
|
|
* @param string $method Method use |
93
|
|
|
* @param string $jsCallback javascript code to execute after the request |
94
|
|
|
* @param boolean $immediatly |
95
|
|
|
*/ |
96
|
|
|
public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) { |
97
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
98
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
99
|
|
|
$retour.="$.{$method}(url,".$params.").done(function( data ) {\n"; |
100
|
|
|
$retour.="\tdata=$.parseJSON(data);for(var key in data){" |
101
|
|
|
."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n"; |
102
|
|
|
$retour.="\t".$jsCallback."\n". |
103
|
|
|
"\t$(document).trigger('jsonReady',[data]);\n". |
104
|
|
|
"});\n"; |
105
|
|
|
if ($immediatly) |
106
|
|
|
$this->jquery_code_for_compile[]=$retour; |
107
|
|
|
return $retour; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
112
|
|
|
* @param string $element |
113
|
|
|
* @param string $event |
114
|
|
|
* @param string $url the request address |
115
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function _jsonOn($event,$element, $url,$parameters=array()) { |
|
|
|
|
118
|
|
|
$preventDefault=true; |
119
|
|
|
$stopPropagation=true; |
120
|
|
|
$jsCallback=null; |
121
|
|
|
$attr="id"; |
122
|
|
|
$method="get"; |
123
|
|
|
$context="document"; |
124
|
|
|
$params="{}"; |
125
|
|
|
$immediatly=true; |
126
|
|
|
extract($parameters); |
127
|
|
|
return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation,$immediatly); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Makes an ajax request and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
132
|
|
|
* @param string $url the request address |
133
|
|
|
* @param string $params Paramètres passés au format JSON |
134
|
|
|
* @param string $method Method use |
135
|
|
|
* @param string $jsCallback javascript code to execute after the request |
136
|
|
|
* @param string $context jquery DOM element, array container. |
137
|
|
|
* @param boolean $immediatly |
138
|
|
|
*/ |
139
|
|
|
public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) { |
140
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
141
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
142
|
|
|
if($context===null){ |
143
|
|
|
$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n"; |
144
|
|
|
$newElm = "$('#'+newId)"; |
145
|
|
|
}else{ |
146
|
|
|
$appendTo="\t\tnewElm.appendTo(".$context.");\n"; |
147
|
|
|
$newElm = $context.".find('#'+newId)"; |
148
|
|
|
} |
149
|
|
|
$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n"; |
150
|
|
|
$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();newElm.attr('id',newId);\n"; |
151
|
|
|
$retour.= $appendTo; |
152
|
|
|
$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"; |
153
|
|
|
$retour.="\t$(document).trigger('jsonReady',[data]);\n"; |
154
|
|
|
$retour.="\t".$jsCallback."\n"."});\n"; |
155
|
|
|
if ($immediatly) |
156
|
|
|
$this->jquery_code_for_compile[]=$retour; |
157
|
|
|
return $retour; |
158
|
|
|
} |
159
|
|
|
/** |
160
|
|
|
* Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
161
|
|
|
* @param string $element |
162
|
|
|
* @param string $event |
163
|
|
|
* @param string $url the request address |
164
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null) |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) { |
|
|
|
|
167
|
|
|
$preventDefault=true; |
168
|
|
|
$stopPropagation=true; |
169
|
|
|
$jsCallback=null; |
170
|
|
|
$attr="id"; |
171
|
|
|
$method="get"; |
172
|
|
|
$context = null; |
173
|
|
|
$params="{}"; |
174
|
|
|
$immediatly=true; |
175
|
|
|
extract($parameters); |
176
|
|
|
return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation,$immediatly); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) { |
180
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
181
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
182
|
|
|
$retour.="\nvar params=$('#".$form."').serialize();\n"; |
183
|
|
|
$responseElement=$this->_getResponseElement($responseElement); |
184
|
|
|
$retour.="var self=this;\n"; |
185
|
|
|
if($hasLoader===true){ |
186
|
|
|
$this->addLoading($retour, $responseElement); |
187
|
|
|
} |
188
|
|
|
$retour.="$.post(url,params).done(function( data ) {\n"; |
189
|
|
|
$retour.=$this->_getOnAjaxDone($responseElement, $jsCallback)."});\n"; |
190
|
|
|
|
191
|
|
|
if ($validation) { |
192
|
|
|
$retour="$('#".$form."').validate({submitHandler: function(form) { |
193
|
|
|
".$retour." |
194
|
|
|
}});\n"; |
195
|
|
|
$retour.="$('#".$form."').submit();\n"; |
196
|
|
|
} |
197
|
|
|
if ($immediatly) |
198
|
|
|
$this->jquery_code_for_compile[]=$retour; |
199
|
|
|
return $retour; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Effectue un get vers $url sur l'évènement $event de $element en passant les paramètres $params |
204
|
|
|
* puis affiche le résultat dans $responseElement |
205
|
|
|
* @param string $element |
206
|
|
|
* @param string $event |
207
|
|
|
* @param string $url |
208
|
|
|
* @param string $params queryString parameters (JSON format). default : {} |
209
|
|
|
* @param string $responseElement |
210
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
|
|
|
213
|
|
|
$preventDefault=true; |
214
|
|
|
$stopPropagation=true; |
215
|
|
|
$jsCallback=null; |
216
|
|
|
$attr="id"; |
217
|
|
|
$hasLoader=true; |
218
|
|
|
$immediatly=true; |
219
|
|
|
extract($parameters); |
220
|
|
|
return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres $params |
225
|
|
|
* puis affiche le résultat dans $responseElement |
226
|
|
|
* @param string $element |
227
|
|
|
* @param string $event |
228
|
|
|
* @param string $url |
229
|
|
|
* @param string $params queryString parameters (JSON format). default : {} |
230
|
|
|
* @param string $responseElement |
231
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true) |
232
|
|
|
*/ |
233
|
|
View Code Duplication |
public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
|
|
|
234
|
|
|
$preventDefault=true; |
235
|
|
|
$stopPropagation=true; |
236
|
|
|
$jsCallback=null; |
237
|
|
|
$attr="id"; |
238
|
|
|
$hasLoader=true; |
239
|
|
|
$immediatly=true; |
240
|
|
|
extract($parameters); |
241
|
|
|
return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Effectue un post vers $url sur l'évènement $event de $element en passant les paramètres du formulaire $form |
246
|
|
|
* puis affiche le résultat dans $responseElement |
247
|
|
|
* @param string $element |
248
|
|
|
* @param string $event |
249
|
|
|
* @param string $url |
250
|
|
|
* @param string $form |
251
|
|
|
* @param string $responseElement |
252
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true) |
253
|
|
|
*/ |
254
|
|
View Code Duplication |
public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) { |
|
|
|
|
255
|
|
|
$preventDefault=true; |
256
|
|
|
$stopPropagation=true; |
257
|
|
|
$validation=false; |
258
|
|
|
$jsCallback=null; |
259
|
|
|
$attr="id"; |
260
|
|
|
$hasLoader=true; |
261
|
|
|
$immediatly=true; |
262
|
|
|
extract($parameters); |
263
|
|
|
return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly); |
264
|
|
|
} |
265
|
|
|
} |
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: