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