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