Completed
Push — master ( d6d0c3...7ff1b8 )
by Jean-Christophe
03:01
created
Ajax/common/traits/JqueryAjaxTrait.php 2 patches
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 
16 16
 	protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>';
17 17
 
18
-	abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true);
18
+	abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false, $immediatly=true);
19 19
 	protected function addLoading(&$retour, $responseElement) {
20 20
 		$loading_notifier='<div class="ajax-loader">';
21 21
 		if ($this->ajaxLoader=='') {
@@ -28,47 +28,47 @@  discard block
 block discarded – undo
28 28
 		$retour.="\t\t$({$responseElement}).prepend('{$loading_notifier}');\n";
29 29
 	}
30 30
 
31
-	public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
32
-		return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$immediatly);
31
+	public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true, $jqueryDone="html", $immediatly=false) {
32
+		return $this->_ajax("get", $url, $params, $responseElement, $jsCallback, $attr, $hasLoader, $jqueryDone, $immediatly);
33 33
 	}
34
-	public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
35
-		return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$jqueryDone,$immediatly);
34
+	public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true, $jqueryDone="html", $immediatly=false) {
35
+		return $this->_ajax("post", $url, $params, $responseElement, $jsCallback, $attr, $hasLoader, $jqueryDone, $immediatly);
36 36
 	}
37 37
 
38
-	protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
39
-		if(JString::isNull($params)){$params="{}";}
38
+	protected function _ajax($method, $url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true, $jqueryDone="html", $immediatly=false) {
39
+		if (JString::isNull($params)) {$params="{}"; }
40 40
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
41 41
 		$retour=$this->_getAjaxUrl($url, $attr);
42 42
 		$responseElement=$this->_getResponseElement($responseElement);
43 43
 		$retour.="var self=this;\n";
44
-		if($hasLoader===true){
44
+		if ($hasLoader===true) {
45 45
 			$this->addLoading($retour, $responseElement);
46 46
 		}
47 47
 		$retour.="$.".$method."(url,".$params.").done(function( data ) {\n";
48
-		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$jsCallback)."});\n";
48
+		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone, $jsCallback)."});\n";
49 49
 		if ($immediatly)
50 50
 			$this->jquery_code_for_compile[]=$retour;
51 51
 			return $retour;
52 52
 	}
53 53
 
54
-	protected function _getAjaxUrl($url,$attr){
54
+	protected function _getAjaxUrl($url, $attr) {
55 55
 		$url=$this->_correctAjaxUrl($url);
56 56
 		$retour="url='".$url."';\n";
57 57
 		$slash="/";
58
-		if(JString::endswith($url, "/")===true)
58
+		if (JString::endswith($url, "/")===true)
59 59
 			$slash="";
60
-		if(JString::isNotNull($attr)){
60
+		if (JString::isNotNull($attr)) {
61 61
 			if ($attr==="value")
62 62
 				$retour.="url=url+'".$slash."'+$(this).val();\n";
63 63
 			elseif ($attr==="html")
64 64
 			$retour.="url=url+'".$slash."'+$(this).html();\n";
65
-			elseif($attr!=null && $attr!=="")
65
+			elseif ($attr!=null && $attr!=="")
66 66
 					$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
67 67
 		}
68 68
 		return $retour;
69 69
 	}
70 70
 
71
-	protected function _getOnAjaxDone($responseElement,$jqueryDone,$jsCallback){
71
+	protected function _getOnAjaxDone($responseElement, $jqueryDone, $jsCallback) {
72 72
 		$retour="";
73 73
 		if ($responseElement!=="") {
74 74
 			$retour="\t$({$responseElement}).{$jqueryDone}( data );\n";
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
 		return $retour;
78 78
 	}
79 79
 
80
-	protected function _getResponseElement($responseElement){
80
+	protected function _getResponseElement($responseElement) {
81 81
 		if ($responseElement!=="") {
82 82
 			$responseElement=Javascript::prep_value($responseElement);
83 83
 		}
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 	protected function _correctAjaxUrl($url) {
88 88
 		if ($url!=="/" && JString::endsWith($url, "/")===true)
89 89
 			$url=substr($url, 0, strlen($url)-1);
90
-		if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
90
+		if (strncmp($url, 'http://', 7)!=0 && strncmp($url, 'https://', 8)!=0) {
91 91
 			$url=$this->jsUtils->getUrl($url);
92 92
 		}
93 93
 		return $url;
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
 	 * @param string $jsCallback javascript code to execute after the request
102 102
 	 * @param boolean $immediatly
103 103
 	 */
104
-	public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) {
104
+	public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document", $immediatly=false) {
105 105
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
106 106
 		$retour=$this->_getAjaxUrl($url, $attr);
107 107
 		$retour.="$.{$method}(url,".$params.").done(function( data ) {\n";
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
 	 * @param string $url the request address
123 123
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
124 124
 	 */
125
-	public function _jsonOn($event,$element, $url,$parameters=array()) {
125
+	public function _jsonOn($event, $element, $url, $parameters=array()) {
126 126
 		$preventDefault=true;
127 127
 		$stopPropagation=true;
128 128
 		$jsCallback=null;
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
 		$params="{}";
133 133
 		$immediatly=true;
134 134
 		extract($parameters);
135
-		return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation,$immediatly);
135
+		return $this->_add_event($element, $this->_json($url, $method, $params, $jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation, $immediatly);
136 136
 	}
137 137
 
138 138
 	/**
@@ -144,19 +144,19 @@  discard block
 block discarded – undo
144 144
 	 * @param string $context jquery DOM element, array container.
145 145
 	 * @param boolean $immediatly
146 146
 	 */
147
-	public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) {
147
+	public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null, $immediatly=false) {
148 148
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
149 149
 		$retour=$this->_getAjaxUrl($url, $attr);
150
-		if($context===null){
150
+		if ($context===null) {
151 151
 			$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n";
152
-			$newElm = "$('#'+newId)";
153
-		}else{
152
+			$newElm="$('#'+newId)";
153
+		} else {
154 154
 			$appendTo="\t\tnewElm.appendTo(".$context.");\n";
155
-			$newElm = $context.".find('#'+newId)";
155
+			$newElm=$context.".find('#'+newId)";
156 156
 		}
157 157
 		$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n";
158 158
 		$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";
159
-		$retour.= $appendTo;
159
+		$retour.=$appendTo;
160 160
 		$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";
161 161
 		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
162 162
 		$retour.="\t".$jsCallback."\n"."});\n";
@@ -171,30 +171,30 @@  discard block
 block discarded – undo
171 171
 	 * @param string $url the request address
172 172
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null)
173 173
 	 */
174
-	public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) {
174
+	public function _jsonArrayOn($event, $element, $maskSelector, $url, $parameters=array()) {
175 175
 		$preventDefault=true;
176 176
 		$stopPropagation=true;
177 177
 		$jsCallback=null;
178 178
 		$attr="id";
179 179
 		$method="get";
180
-		$context = null;
180
+		$context=null;
181 181
 		$params="{}";
182 182
 		$immediatly=true;
183 183
 		extract($parameters);
184
-		return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation,$immediatly);
184
+		return $this->_add_event($element, $this->_jsonArray($maskSelector, $url, $method, $params, $jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation, $immediatly);
185 185
 	}
186 186
 
187
-	public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$jqueryDone="html",$immediatly=false) {
187
+	public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true, $jqueryDone="html", $immediatly=false) {
188 188
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
189 189
 		$retour=$this->_getAjaxUrl($url, $attr);
190 190
 		$retour.="\nvar params=$('#".$form."').serialize();\n";
191 191
 		$responseElement=$this->_getResponseElement($responseElement);
192 192
 		$retour.="var self=this;\n";
193
-		if($hasLoader===true){
193
+		if ($hasLoader===true) {
194 194
 			$this->addLoading($retour, $responseElement);
195 195
 		}
196 196
 		$retour.="$.post(url,params).done(function( data ) {\n";
197
-		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$jsCallback)."});\n";
197
+		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone, $jsCallback)."});\n";
198 198
 
199 199
 		if ($validation) {
200 200
 			$retour="$('#".$form."').validate({submitHandler: function(form) {
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
 	 * @param string $responseElement
218 218
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html")
219 219
 	 */
220
-	public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
220
+	public function _getOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
221 221
 		$preventDefault=true;
222 222
 		$stopPropagation=true;
223 223
 		$jsCallback=null;
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
 		$immediatly=true;
227 227
 		$jqueryDone="html";
228 228
 		extract($parameters);
229
-		return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr,$hasLoader,$jqueryDone), $event, $preventDefault, $stopPropagation,$immediatly);
229
+		return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr, $hasLoader, $jqueryDone), $event, $preventDefault, $stopPropagation, $immediatly);
230 230
 	}
231 231
 
232 232
 	/**
@@ -239,7 +239,7 @@  discard block
 block discarded – undo
239 239
 	 * @param string $responseElement
240 240
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
241 241
 	 */
242
-	public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
242
+	public function _postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
243 243
 		$preventDefault=true;
244 244
 		$stopPropagation=true;
245 245
 		$jsCallback=null;
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
 		$immediatly=true;
249 249
 		$jqueryDone="html";
250 250
 		extract($parameters);
251
-		return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader,$jqueryDone), $event, $preventDefault, $stopPropagation,$immediatly);
251
+		return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr, $hasLoader, $jqueryDone), $event, $preventDefault, $stopPropagation, $immediatly);
252 252
 	}
253 253
 
254 254
 	/**
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 	 * @param string $responseElement
262 262
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
263 263
 	 */
264
-	public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) {
264
+	public function _postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) {
265 265
 		$preventDefault=true;
266 266
 		$stopPropagation=true;
267 267
 		$validation=false;
@@ -271,6 +271,6 @@  discard block
 block discarded – undo
271 271
 		$immediatly=true;
272 272
 		$jqueryDone="html";
273 273
 		extract($parameters);
274
-		return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader,$jqueryDone), $event, $preventDefault, $stopPropagation,$immediatly);
274
+		return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr, $hasLoader, $jqueryDone), $event, $preventDefault, $stopPropagation, $immediatly);
275 275
 	}
276 276
 }
277 277
\ No newline at end of file
Please login to merge, or discard this patch.
Braces   +26 added lines, -19 removed lines patch added patch discarded remove patch
@@ -46,8 +46,9 @@  discard block
 block discarded – undo
46 46
 		}
47 47
 		$retour.="$.".$method."(url,".$params.").done(function( data ) {\n";
48 48
 		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$jsCallback)."});\n";
49
-		if ($immediatly)
50
-			$this->jquery_code_for_compile[]=$retour;
49
+		if ($immediatly) {
50
+					$this->jquery_code_for_compile[]=$retour;
51
+		}
51 52
 			return $retour;
52 53
 	}
53 54
 
@@ -55,15 +56,17 @@  discard block
 block discarded – undo
55 56
 		$url=$this->_correctAjaxUrl($url);
56 57
 		$retour="url='".$url."';\n";
57 58
 		$slash="/";
58
-		if(JString::endswith($url, "/")===true)
59
-			$slash="";
59
+		if(JString::endswith($url, "/")===true) {
60
+					$slash="";
61
+		}
60 62
 		if(JString::isNotNull($attr)){
61
-			if ($attr==="value")
62
-				$retour.="url=url+'".$slash."'+$(this).val();\n";
63
-			elseif ($attr==="html")
64
-			$retour.="url=url+'".$slash."'+$(this).html();\n";
65
-			elseif($attr!=null && $attr!=="")
66
-					$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
63
+			if ($attr==="value") {
64
+							$retour.="url=url+'".$slash."'+$(this).val();\n";
65
+			} elseif ($attr==="html") {
66
+						$retour.="url=url+'".$slash."'+$(this).html();\n";
67
+			} elseif($attr!=null && $attr!=="") {
68
+								$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
69
+			}
67 70
 		}
68 71
 		return $retour;
69 72
 	}
@@ -85,8 +88,9 @@  discard block
 block discarded – undo
85 88
 	}
86 89
 
87 90
 	protected function _correctAjaxUrl($url) {
88
-		if ($url!=="/" && JString::endsWith($url, "/")===true)
89
-			$url=substr($url, 0, strlen($url)-1);
91
+		if ($url!=="/" && JString::endsWith($url, "/")===true) {
92
+					$url=substr($url, 0, strlen($url)-1);
93
+		}
90 94
 		if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
91 95
 			$url=$this->jsUtils->getUrl($url);
92 96
 		}
@@ -110,8 +114,9 @@  discard block
 block discarded – undo
110 114
 				$retour.="\t".$jsCallback."\n".
111 115
 						"\t$(document).trigger('jsonReady',[data]);\n".
112 116
 						"});\n";
113
-				if ($immediatly)
114
-					$this->jquery_code_for_compile[]=$retour;
117
+				if ($immediatly) {
118
+									$this->jquery_code_for_compile[]=$retour;
119
+				}
115 120
 					return $retour;
116 121
 	}
117 122
 
@@ -150,7 +155,7 @@  discard block
 block discarded – undo
150 155
 		if($context===null){
151 156
 			$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n";
152 157
 			$newElm = "$('#'+newId)";
153
-		}else{
158
+		} else{
154 159
 			$appendTo="\t\tnewElm.appendTo(".$context.");\n";
155 160
 			$newElm = $context.".find('#'+newId)";
156 161
 		}
@@ -160,8 +165,9 @@  discard block
 block discarded – undo
160 165
 		$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";
161 166
 		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
162 167
 		$retour.="\t".$jsCallback."\n"."});\n";
163
-		if ($immediatly)
164
-			$this->jquery_code_for_compile[]=$retour;
168
+		if ($immediatly) {
169
+					$this->jquery_code_for_compile[]=$retour;
170
+		}
165 171
 			return $retour;
166 172
 	}
167 173
 	/**
@@ -202,8 +208,9 @@  discard block
 block discarded – undo
202 208
 			}});\n";
203 209
 			$retour.="$('#".$form."').submit();\n";
204 210
 		}
205
-		if ($immediatly)
206
-			$this->jquery_code_for_compile[]=$retour;
211
+		if ($immediatly) {
212
+					$this->jquery_code_for_compile[]=$retour;
213
+		}
207 214
 			return $retour;
208 215
 	}
209 216
 
Please login to merge, or discard this patch.