Completed
Push — master ( 3f2ff8...3555a5 )
by Jean-Christophe
03:49
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
 	protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>';
16 16
 
17 17
 	public abstract function _prep_value($value);
18
-	public abstract function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true);
18
+	public abstract 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,20 +28,20 @@  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,$immediatly=false) {
32
-		return $this->_ajax("get", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly);
31
+	public function _get($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true, $immediatly=false) {
32
+		return $this->_ajax("get", $url, $params, $responseElement, $jsCallback, $attr, $hasLoader, $immediatly);
33 33
 	}
34
-	public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
35
-		return $this->_ajax("post", $url,$params,$responseElement,$jsCallback,$attr,$hasLoader,$immediatly);
34
+	public function _post($url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true, $immediatly=false) {
35
+		return $this->_ajax("post", $url, $params, $responseElement, $jsCallback, $attr, $hasLoader, $immediatly);
36 36
 	}
37 37
 
38
-	protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
39
-		if(JString::isNull($params)){$params="{}";}
38
+	protected function _ajax($method, $url, $params="{}", $responseElement="", $jsCallback=NULL, $attr="id", $hasLoader=true, $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";
@@ -51,28 +51,28 @@  discard block
 block discarded – undo
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
-				else if($attr!=null && $attr!=="")
63
+				else if ($attr!=null && $attr!=="")
64 64
 					$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
65 65
 		}
66 66
 		return $retour;
67 67
 	}
68 68
 
69
-	protected function _getOnAjaxDone($responseElement,$jsCallback){
69
+	protected function _getOnAjaxDone($responseElement, $jsCallback) {
70 70
 		$retour="";
71 71
 		if ($responseElement!=="") {
72
-			if(JString::startswith($responseElement, "-")){
72
+			if (JString::startswith($responseElement, "-")) {
73 73
 				$responseElement=\substr($responseElement, 0);
74 74
 				$retour="\t$({$responseElement}).replaceWith( data );\n";
75
-			}else{
75
+			} else {
76 76
 				$retour="\t$({$responseElement}).html( data );\n";
77 77
 			}
78 78
 		}
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 		return $retour;
81 81
 	}
82 82
 
83
-	protected function _getResponseElement($responseElement){
83
+	protected function _getResponseElement($responseElement) {
84 84
 		if ($responseElement!=="") {
85 85
 			$responseElement=$this->_prep_value($responseElement);
86 86
 		}
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
 	protected function _correctAjaxUrl($url) {
91 91
 		if ($url!=="/" && JString::endsWith($url, "/")===true)
92 92
 			$url=substr($url, 0, strlen($url)-1);
93
-		if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
93
+		if (strncmp($url, 'http://', 7)!=0 && strncmp($url, 'https://', 8)!=0) {
94 94
 			$url=$this->jsUtils->getUrl($url);
95 95
 		}
96 96
 		return $url;
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
 	 * @param string $jsCallback javascript code to execute after the request
105 105
 	 * @param boolean $immediatly
106 106
 	 */
107
-	public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) {
107
+	public function _json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document", $immediatly=false) {
108 108
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
109 109
 		$retour=$this->_getAjaxUrl($url, $attr);
110 110
 		$retour.="$.{$method}(url,".$params.").done(function( data ) {\n";
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
 	 * @param string $url the request address
126 126
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
127 127
 	 */
128
-	public function _jsonOn($event,$element, $url,$parameters=array()) {
128
+	public function _jsonOn($event, $element, $url, $parameters=array()) {
129 129
 		$preventDefault=true;
130 130
 		$stopPropagation=true;
131 131
 		$jsCallback=null;
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 		$params="{}";
136 136
 		$immediatly=true;
137 137
 		extract($parameters);
138
-		return $this->_add_event($element, $this->_json($url,$method, $params,$jsCallback, $attr,$context), $event, $preventDefault, $stopPropagation,$immediatly);
138
+		return $this->_add_event($element, $this->_json($url, $method, $params, $jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation, $immediatly);
139 139
 	}
140 140
 
141 141
 	/**
@@ -147,19 +147,19 @@  discard block
 block discarded – undo
147 147
 	 * @param string $context jquery DOM element, array container.
148 148
 	 * @param boolean $immediatly
149 149
 	 */
150
-	public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null,$immediatly=false) {
150
+	public function _jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context=null, $immediatly=false) {
151 151
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
152 152
 		$retour=$this->_getAjaxUrl($url, $attr);
153
-		if($context===null){
153
+		if ($context===null) {
154 154
 			$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n";
155
-			$newElm = "$('#'+newId)";
156
-		}else{
155
+			$newElm="$('#'+newId)";
156
+		} else {
157 157
 			$appendTo="\t\tnewElm.appendTo(".$context.");\n";
158
-			$newElm = $context.".find('#'+newId)";
158
+			$newElm=$context.".find('#'+newId)";
159 159
 		}
160 160
 		$retour.="var self = $(this);\n$.{$method}(url,".$params.").done(function( data ) {\n";
161 161
 		$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";
162
-		$retour.= $appendTo;
162
+		$retour.=$appendTo;
163 163
 		$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";
164 164
 		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
165 165
 		$retour.="\t".$jsCallback."\n"."});\n";
@@ -174,26 +174,26 @@  discard block
 block discarded – undo
174 174
 	 * @param string $url the request address
175 175
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get", "context"=>null)
176 176
 	 */
177
-	public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=array()) {
177
+	public function _jsonArrayOn($event, $element, $maskSelector, $url, $parameters=array()) {
178 178
 		$preventDefault=true;
179 179
 		$stopPropagation=true;
180 180
 		$jsCallback=null;
181 181
 		$attr="id";
182 182
 		$method="get";
183
-		$context = null;
183
+		$context=null;
184 184
 		$params="{}";
185 185
 		$immediatly=true;
186 186
 		extract($parameters);
187
-		return $this->_add_event($element, $this->_jsonArray($maskSelector,$url,$method, $params,$jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation,$immediatly);
187
+		return $this->_add_event($element, $this->_jsonArray($maskSelector, $url, $method, $params, $jsCallback, $attr, $context), $event, $preventDefault, $stopPropagation, $immediatly);
188 188
 	}
189 189
 
190
-	public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true,$immediatly=false) {
190
+	public function _postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL, $attr="id", $hasLoader=true, $immediatly=false) {
191 191
 		$jsCallback=isset($jsCallback) ? $jsCallback : "";
192 192
 		$retour=$this->_getAjaxUrl($url, $attr);
193 193
 		$retour.="\nvar params=$('#".$form."').serialize();\n";
194 194
 		$responseElement=$this->_getResponseElement($responseElement);
195 195
 		$retour.="var self=this;\n";
196
-		if($hasLoader===true){
196
+		if ($hasLoader===true) {
197 197
 			$this->addLoading($retour, $responseElement);
198 198
 		}
199 199
 		$retour.="$.post(url,params).done(function( data ) {\n";
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
 	 * @param string $responseElement
221 221
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
222 222
 	 */
223
-	public function _getOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
223
+	public function _getOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
224 224
 		$preventDefault=true;
225 225
 		$stopPropagation=true;
226 226
 		$jsCallback=null;
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
 		$hasLoader=true;
229 229
 		$immediatly=true;
230 230
 		extract($parameters);
231
-		return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly);
231
+		return $this->_add_event($element, $this->_get($url, $params, $responseElement, $jsCallback, $attr, $hasLoader), $event, $preventDefault, $stopPropagation, $immediatly);
232 232
 	}
233 233
 
234 234
 	/**
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
 	 * @param string $responseElement
242 242
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
243 243
 	 */
244
-	public function _postOn($event,$element, $url, $params="{}", $responseElement="", $parameters=array()) {
244
+	public function _postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
245 245
 		$preventDefault=true;
246 246
 		$stopPropagation=true;
247 247
 		$jsCallback=null;
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 		$hasLoader=true;
250 250
 		$immediatly=true;
251 251
 		extract($parameters);
252
-		return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly);
252
+		return $this->_add_event($element, $this->_post($url, $params, $responseElement, $jsCallback, $attr, $hasLoader), $event, $preventDefault, $stopPropagation, $immediatly);
253 253
 	}
254 254
 
255 255
 	/**
@@ -262,7 +262,7 @@  discard block
 block discarded – undo
262 262
 	 * @param string $responseElement
263 263
 	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
264 264
 	 */
265
-	public function _postFormOn($event,$element, $url, $form, $responseElement="", $parameters=array()) {
265
+	public function _postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) {
266 266
 		$preventDefault=true;
267 267
 		$stopPropagation=true;
268 268
 		$validation=false;
@@ -271,6 +271,6 @@  discard block
 block discarded – undo
271 271
 		$hasLoader=true;
272 272
 		$immediatly=true;
273 273
 		extract($parameters);
274
-		return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr,$hasLoader), $event, $preventDefault, $stopPropagation,$immediatly);
274
+		return $this->_add_event($element, $this->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr, $hasLoader), $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   +25 added lines, -18 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, $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,13 +56,15 @@  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
-				else if($attr!=null && $attr!=="")
64
-					$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
63
+			if ($attr=="value") {
64
+							$retour.="url=url+'".$slash."'+$(this).val();\n";
65
+			} else if($attr!=null && $attr!=="") {
66
+									$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
67
+				}
65 68
 		}
66 69
 		return $retour;
67 70
 	}
@@ -72,7 +75,7 @@  discard block
 block discarded – undo
72 75
 			if(JString::startswith($responseElement, "-")){
73 76
 				$responseElement=\substr($responseElement, 0);
74 77
 				$retour="\t$({$responseElement}).replaceWith( data );\n";
75
-			}else{
78
+			} else{
76 79
 				$retour="\t$({$responseElement}).html( data );\n";
77 80
 			}
78 81
 		}
@@ -88,8 +91,9 @@  discard block
 block discarded – undo
88 91
 	}
89 92
 
90 93
 	protected function _correctAjaxUrl($url) {
91
-		if ($url!=="/" && JString::endsWith($url, "/")===true)
92
-			$url=substr($url, 0, strlen($url)-1);
94
+		if ($url!=="/" && JString::endsWith($url, "/")===true) {
95
+					$url=substr($url, 0, strlen($url)-1);
96
+		}
93 97
 		if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) {
94 98
 			$url=$this->jsUtils->getUrl($url);
95 99
 		}
@@ -113,8 +117,9 @@  discard block
 block discarded – undo
113 117
 				$retour.="\t".$jsCallback."\n".
114 118
 						"\t$(document).trigger('jsonReady',[data]);\n".
115 119
 						"});\n";
116
-				if ($immediatly)
117
-					$this->jquery_code_for_compile[]=$retour;
120
+				if ($immediatly) {
121
+									$this->jquery_code_for_compile[]=$retour;
122
+				}
118 123
 					return $retour;
119 124
 	}
120 125
 
@@ -153,7 +158,7 @@  discard block
 block discarded – undo
153 158
 		if($context===null){
154 159
 			$appendTo="\t\tnewElm.appendTo($('".$maskSelector."').parent());\n";
155 160
 			$newElm = "$('#'+newId)";
156
-		}else{
161
+		} else{
157 162
 			$appendTo="\t\tnewElm.appendTo(".$context.");\n";
158 163
 			$newElm = $context.".find('#'+newId)";
159 164
 		}
@@ -163,8 +168,9 @@  discard block
 block discarded – undo
163 168
 		$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";
164 169
 		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
165 170
 		$retour.="\t".$jsCallback."\n"."});\n";
166
-		if ($immediatly)
167
-			$this->jquery_code_for_compile[]=$retour;
171
+		if ($immediatly) {
172
+					$this->jquery_code_for_compile[]=$retour;
173
+		}
168 174
 			return $retour;
169 175
 	}
170 176
 	/**
@@ -205,8 +211,9 @@  discard block
 block discarded – undo
205 211
 			}});\n";
206 212
 			$retour.="$('#".$form."').submit();\n";
207 213
 		}
208
-		if ($immediatly)
209
-			$this->jquery_code_for_compile[]=$retour;
214
+		if ($immediatly) {
215
+					$this->jquery_code_for_compile[]=$retour;
216
+		}
210 217
 			return $retour;
211 218
 	}
212 219
 
Please login to merge, or discard this patch.