1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax; |
4
|
|
|
|
5
|
|
|
use Ajax\config\DefaultConfig; |
6
|
|
|
use Ajax\config\Config; |
7
|
|
|
use Ajax\lib\CDNJQuery; |
8
|
|
|
use Ajax\lib\CDNGuiGen; |
9
|
|
|
use Ajax\lib\CDNCoreCss; |
10
|
|
|
use Phalcon\DiInterface; |
11
|
|
|
use Phalcon\Version; |
12
|
|
|
use Phalcon\Di\InjectionAwareInterface; |
13
|
|
|
use Ajax\common\traits\JsUtilsEventsTrait; |
14
|
|
|
use Ajax\common\traits\JsUtilsActionsTrait; |
15
|
|
|
use Ajax\common\traits\JsUtilsAjaxTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JQuery Phalcon library |
19
|
|
|
* |
20
|
|
|
* @author jcheron |
21
|
|
|
* @version 1.003 |
22
|
|
|
* @license Apache 2 http://www.apache.org/licenses/ |
23
|
|
|
*/ |
24
|
|
|
/** |
25
|
|
|
* JsUtils Class : Phalcon service to be injected |
26
|
|
|
*/ |
27
|
|
|
abstract class _JsUtils implements InjectionAwareInterface { |
28
|
|
|
use JsUtilsEventsTrait,JsUtilsActionsTrait,JsUtilsAjaxTrait; |
29
|
|
|
|
30
|
|
|
protected $_di; |
31
|
|
|
protected $js; |
32
|
|
|
protected $cdns; |
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @var JqueryUI |
36
|
|
|
*/ |
37
|
|
|
protected $_ui; |
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var Bootstrap |
41
|
|
|
*/ |
42
|
|
|
protected $_bootstrap; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var Semantic |
47
|
|
|
*/ |
48
|
|
|
protected $_semantic; |
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
* @var Config |
52
|
|
|
*/ |
53
|
|
|
protected $config; |
54
|
|
|
|
55
|
|
|
protected function _setDi($di) { |
56
|
|
|
$this->_di=$di; |
57
|
|
|
if ($this->js!=null&&$di!=null) |
58
|
|
|
$this->js->setDi($di); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @param JqueryUI $ui |
64
|
|
|
* @return \Ajax\JqueryUI |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function ui($ui=NULL) { |
|
|
|
|
67
|
|
|
if ($ui!==NULL) { |
68
|
|
|
$this->_ui=$ui; |
69
|
|
|
if ($this->js!=null) { |
70
|
|
|
$this->js->ui($ui); |
71
|
|
|
$ui->setJs($this); |
72
|
|
|
} |
73
|
|
|
$bs=$this->bootstrap(); |
74
|
|
|
if (isset($bs)) { |
75
|
|
|
$this->conflict(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return $this->_ui; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @param Bootstrap $bootstrap |
84
|
|
|
* @return \Ajax\Bootstrap |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function bootstrap($bootstrap=NULL) { |
|
|
|
|
87
|
|
|
if ($bootstrap!==NULL) { |
88
|
|
|
$this->_bootstrap=$bootstrap; |
89
|
|
|
if ($this->js!=null) { |
90
|
|
|
$this->js->bootstrap($bootstrap); |
91
|
|
|
$bootstrap->setJs($this); |
92
|
|
|
} |
93
|
|
|
$ui=$this->ui(); |
94
|
|
|
if (isset($ui)) { |
95
|
|
|
$this->conflict(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return $this->_bootstrap; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
* @param Semantic $semantic |
104
|
|
|
* @return \Ajax\Semantic |
105
|
|
|
*/ |
106
|
|
|
public function semantic($semantic=NULL) { |
107
|
|
|
if ($semantic!==NULL) { |
108
|
|
|
$this->_semantic=$semantic; |
109
|
|
|
if ($this->js!=null) { |
110
|
|
|
$this->js->semantic($semantic); |
111
|
|
|
$semantic->setJs($this); |
112
|
|
|
} |
113
|
|
|
$ui=$this->ui(); |
114
|
|
|
if (isset($ui)) { |
115
|
|
|
$this->conflict(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $this->_semantic; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function conflict() { |
122
|
|
|
$this->js->_addToCompile("var btn = $.fn.button.noConflict();$.fn.btn = btn;"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @param \Ajax\config\Config $config |
128
|
|
|
* @return \Ajax\config\Config |
129
|
|
|
*/ |
130
|
|
|
public function config($config=NULL) { |
131
|
|
|
if ($config===NULL) { |
132
|
|
|
if ($this->config===NULL) { |
133
|
|
|
$this->config=new DefaultConfig(); |
134
|
|
|
} |
135
|
|
|
} elseif (is_array($config)) { |
136
|
|
|
$this->config=new Config($config); |
137
|
|
|
} elseif ($config instanceof Config) { |
138
|
|
|
$this->config=$config; |
139
|
|
|
} |
140
|
|
|
return $this->config; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getDi() { |
144
|
|
|
return $this->_di; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
public function __construct($params=array()) { |
150
|
|
|
$defaults=array ( |
151
|
|
|
'driver' => 'Jquery', |
152
|
|
|
'debug' => true |
153
|
|
|
); |
154
|
|
|
foreach ( $defaults as $key => $val ) { |
155
|
|
|
if (isset($params[$key])&&$params[$key]!=="") { |
156
|
|
|
$defaults[$key]=$params[$key]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
extract($defaults); |
160
|
|
|
$this->js=new Jquery($defaults); |
161
|
|
|
$this->cdns=array (); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function addToCompile($jsScript) { |
165
|
|
|
$this->js->_addToCompile($jsScript); |
166
|
|
|
} |
167
|
|
|
// -------------------------------------------------------------------- |
168
|
|
|
/** |
169
|
|
|
* Outputs the called javascript to the screen |
170
|
|
|
* |
171
|
|
|
* @param string $js code to output |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function output($js) { |
175
|
|
|
return $this->js->_output($js); |
176
|
|
|
} |
177
|
|
|
// -------------------------------------------------------------------- |
178
|
|
|
/** |
179
|
|
|
* Document ready method |
180
|
|
|
* |
181
|
|
|
* @param string $js code to execute |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function ready($js) { |
185
|
|
|
return $this->js->_document_ready($js); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// -------------------------------------------------------------------- |
189
|
|
|
/** |
190
|
|
|
* gather together all script needing to be output |
191
|
|
|
* |
192
|
|
|
* @param View $view |
193
|
|
|
* @param $view_var |
194
|
|
|
* @param $script_tags |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) { |
198
|
|
|
$bs=$this->_bootstrap; |
199
|
|
|
if (isset($bs)&&isset($view)) { |
200
|
|
|
$bs->compileHtml($this, $view); |
201
|
|
|
} |
202
|
|
|
$sem=$this->_semantic; |
203
|
|
|
if (isset($sem)&&isset($view)) { |
204
|
|
|
$sem->compileHtml($this, $view); |
205
|
|
|
} |
206
|
|
|
return $this->js->_compile($view, $view_var, $script_tags); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Clears any previous javascript collected for output |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function clear_compile() { |
215
|
|
|
$this->js->_clear_compile(); |
216
|
|
|
} |
217
|
|
|
// -------------------------------------------------------------------- |
218
|
|
|
/** |
219
|
|
|
* Outputs a <script> tag with the source as an external js file |
220
|
|
|
* |
221
|
|
|
* @param string $external_file |
222
|
|
|
* @param boolean $relative |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
|
public function external($external_file='', $relative=FALSE) { |
226
|
|
|
$assets=$this->_di->get('assets'); |
227
|
|
|
$assets->addJs($external_file); |
228
|
|
|
return $assets->outputJs(); |
229
|
|
|
} |
230
|
|
|
// -------------------------------------------------------------------- |
231
|
|
|
/** |
232
|
|
|
* Outputs a <script> tag |
233
|
|
|
* |
234
|
|
|
* @param string $script |
235
|
|
|
* @param boolean $cdata If a CDATA section should be added |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
View Code Duplication |
public function inline($script, $cdata=TRUE) { |
|
|
|
|
239
|
|
|
$str=$this->_open_script(); |
240
|
|
|
$str.=($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n"; |
241
|
|
|
$str.=$this->_close_script(); |
242
|
|
|
return $str; |
243
|
|
|
} |
244
|
|
|
// -------------------------------------------------------------------- |
245
|
|
|
/** |
246
|
|
|
* Outputs an opening <script> |
247
|
|
|
* |
248
|
|
|
* @param string $src |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
private function _open_script($src='') { |
252
|
|
|
$str='<script type="text/javascript" '; |
253
|
|
|
$str.=($src=='') ? '>' : ' src="'.$src.'">'; |
254
|
|
|
return $str; |
255
|
|
|
} |
256
|
|
|
// -------------------------------------------------------------------- |
257
|
|
|
/** |
258
|
|
|
* Outputs an closing </script> |
259
|
|
|
* |
260
|
|
|
* @param string $extra |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
private function _close_script($extra="\n") { |
264
|
|
|
return "</script>$extra"; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// -------------------------------------------------------------------- |
268
|
|
|
/** |
269
|
|
|
* Can be passed a database result or associative array and returns a JSON formatted string |
270
|
|
|
* |
271
|
|
|
* @param mixed $result result set or array |
272
|
|
|
* @param bool $match_array_type match array types (defaults to objects) |
273
|
|
|
* @return string json formatted string |
274
|
|
|
*/ |
275
|
|
|
public function generate_json($result=NULL, $match_array_type=FALSE) { |
276
|
|
|
// JSON data can optionally be passed to this function |
277
|
|
|
// either as a database result object or an array, or a user supplied array |
278
|
|
|
if (!is_null($result)) { |
279
|
|
|
if (is_object($result)) { |
280
|
|
|
$json_result=$result->result_array(); |
281
|
|
|
} elseif (is_array($result)) { |
282
|
|
|
$json_result=$result; |
283
|
|
|
} else { |
284
|
|
|
return $this->_prep_args($result); |
285
|
|
|
} |
286
|
|
|
} else { |
287
|
|
|
return 'null'; |
288
|
|
|
} |
289
|
|
|
return $this->_create_json($json_result, $match_array_type); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
private function _create_json($json_result, $match_array_type) { |
293
|
|
|
$json=array (); |
294
|
|
|
$_is_assoc=TRUE; |
295
|
|
|
if (!is_array($json_result)&&empty($json_result)) { |
296
|
|
|
show_error("Generate JSON Failed - Illegal key, value pair."); |
297
|
|
|
} elseif ($match_array_type) { |
298
|
|
|
$_is_assoc=$this->_is_associative_array($json_result); |
299
|
|
|
} |
300
|
|
|
foreach ( $json_result as $k => $v ) { |
301
|
|
|
if ($_is_assoc) { |
302
|
|
|
$json[]=$this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type); |
303
|
|
|
} else { |
304
|
|
|
$json[]=$this->generate_json($v, $match_array_type); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
$json=implode(',', $json); |
308
|
|
|
return $_is_assoc ? "{".$json."}" : "[".$json."]"; |
309
|
|
|
} |
310
|
|
|
// -------------------------------------------------------------------- |
311
|
|
|
/** |
312
|
|
|
* Checks for an associative array |
313
|
|
|
* |
314
|
|
|
* @param type |
315
|
|
|
* @return type |
316
|
|
|
*/ |
317
|
|
|
public function _is_associative_array($arr) { |
318
|
|
|
foreach ( array_keys($arr) as $key => $val ) { |
319
|
|
|
if ($key!==$val) { |
320
|
|
|
return TRUE; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
return FALSE; |
324
|
|
|
} |
325
|
|
|
// -------------------------------------------------------------------- |
326
|
|
|
/** |
327
|
|
|
* Ensures a standard json value and escapes values |
328
|
|
|
* |
329
|
|
|
* @param type |
330
|
|
|
* @return type |
331
|
|
|
*/ |
332
|
|
|
public function _prep_args($result, $is_key=FALSE) { |
333
|
|
|
if (is_null($result)) { |
334
|
|
|
return 'null'; |
335
|
|
|
} elseif (is_bool($result)) { |
336
|
|
|
return ($result===TRUE) ? 'true' : 'false'; |
337
|
|
|
} elseif (is_string($result)||$is_key) { |
338
|
|
|
return '"'.str_replace(array ( |
339
|
|
|
'\\',"\t","\n","\r",'"','/' |
340
|
|
|
), array ( |
341
|
|
|
'\\\\','\\t','\\n',"\\r",'\"','\/' |
342
|
|
|
), $result).'"'; |
343
|
|
|
} elseif (is_scalar($result)) { |
344
|
|
|
return $result; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function getCDNs() { |
349
|
|
|
return $this->cdns; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function setCDNs($cdns) { |
353
|
|
|
if (is_array($cdns)===false) { |
354
|
|
|
$cdns=array ( |
355
|
|
|
$cdns |
356
|
|
|
); |
357
|
|
|
} |
358
|
|
|
$this->cdns=$cdns; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
public function genCDNs($template=NULL) { |
362
|
|
|
$hasJQuery=false; |
363
|
|
|
$hasJQueryUI=false; |
364
|
|
|
$hasBootstrap=false; |
365
|
|
|
$hasSemantic=false; |
366
|
|
|
$result=array (); |
367
|
|
|
foreach ( $this->cdns as $cdn ) { |
368
|
|
|
switch(get_class($cdn)) { |
369
|
|
|
case "Ajax\lib\CDNJQuery": |
370
|
|
|
$hasJQuery=true; |
371
|
|
|
$result[0]=$cdn; |
372
|
|
|
break; |
373
|
|
|
case "Ajax\lib\CDNJQuery": |
374
|
|
|
$hasJQueryUI=true; |
375
|
|
|
$result[1]=$cdn; |
376
|
|
|
break; |
377
|
|
|
case "Ajax\lib\CDNCoreCss": |
378
|
|
|
if($cdn->getFramework()==="Bootstrap") |
379
|
|
|
$hasBootstrap=true; |
380
|
|
|
elseif($cdn->getFramework()==="Semantic") |
381
|
|
|
$hasSemantic=true; |
382
|
|
|
if($hasSemantic || $hasBootstrap) |
383
|
|
|
$result[2]=$cdn; |
384
|
|
|
break; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
if ($hasJQuery===false) { |
388
|
|
|
$result[0]=new CDNJQuery("x"); |
389
|
|
|
} |
390
|
|
|
if ($hasJQueryUI===false&&isset($this->_ui)) { |
391
|
|
|
$result[1]=new CDNGuiGen("x", $template); |
392
|
|
|
} |
393
|
|
|
if ($hasBootstrap===false&&isset($this->_bootstrap)) { |
394
|
|
|
$result[2]=new CDNCoreCss("Bootstrap","x"); |
395
|
|
|
} |
396
|
|
|
if ($hasSemantic===false&&isset($this->_semantic)) { |
397
|
|
|
$result[2]=new CDNCoreCss("Semantic","x"); |
398
|
|
|
} |
399
|
|
|
ksort($result); |
400
|
|
|
return implode("\n", $result); |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
if (Version::get()==="1.3.4") { |
404
|
|
|
class JsUtils extends _JsUtils { |
|
|
|
|
405
|
|
|
|
406
|
|
|
public function setDi($di) { |
407
|
|
|
$this->_setDi($di); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} else { |
411
|
|
|
class JsUtils extends _JsUtils { |
|
|
|
|
412
|
|
|
|
413
|
|
|
public function setDi(DiInterface $di) { |
414
|
|
|
$this->_setDi($di); |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.