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