Completed
Push — master ( b9a8b8...3e8704 )
by Jean-Christophe
03:43
created

_JsUtils::compile()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 11
rs 8.8571
cc 5
eloc 8
nc 4
nop 3
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\CDNBootstrap;
10
use Phalcon\DiInterface;
11
use Phalcon\Version;
12
use Phalcon\Di\InjectionAwareInterface;
13
use Ajax\service\JArray;
14
15
/**
16
 * JQuery Phalcon library
17
 *
18
 * @author jcheron
19
 * @version 1.002
20
 * @license Apache 2 http://www.apache.org/licenses/
21
 */
22
/**
23
 * JsUtils Class : Phalcon service to be injected
24
 */
25
abstract class _JsUtils implements InjectionAwareInterface {
26
	protected $_di;
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
		$this->_di=$di;
53
		if ($this->js!=null&&$di!=null)
54
			$this->js->setDi($di);
55
	}
56
57
	/**
58
	 *
59
	 * @param JqueryUI $ui
60
	 * @return \Ajax\JqueryUI
61
	 */
62 View Code Duplication
	public function ui($ui=NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
63
		if ($ui!==NULL) {
64
			$this->_ui=$ui;
65
			if ($this->js!=null) {
66
				$this->js->ui($ui);
67
				$ui->setJs($this);
68
			}
69
			$bs=$this->bootstrap();
70
			if (isset($bs)) {
71
				$this->conflict();
72
			}
73
		}
74
		return $this->_ui;
75
	}
76
77
	/**
78
	 *
79
	 * @param Bootstrap $bootstrap
80
	 * @return \Ajax\Bootstrap
81
	 */
82 View Code Duplication
	public function bootstrap($bootstrap=NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
83
		if ($bootstrap!==NULL) {
84
			$this->_bootstrap=$bootstrap;
85
			if ($this->js!=null) {
86
				$this->js->bootstrap($bootstrap);
87
				$bootstrap->setJs($this);
88
			}
89
			$ui=$this->ui();
90
			if (isset($ui)) {
91
				$this->conflict();
92
			}
93
		}
94
		return $this->_bootstrap;
95
	}
96
97
	/**
98
	 *
99
	 * @param Semantic $semantic
100
	 * @return \Ajax\Semantic
101
	 */
102
	public function semantic($semantic=NULL) {
103
		if ($semantic!==NULL) {
104
			$this->_semantic=$semantic;
105
			if ($this->js!=null) {
106
				$this->js->semantic($semantic);
107
				$semantic->setJs($this);
108
			}
109
			$ui=$this->ui();
110
			if (isset($ui)) {
111
				$this->conflict();
112
			}
113
		}
114
		return $this->_semantic;
115
	}
116
117
	protected function conflict() {
118
		$this->js->_addToCompile("var btn = $.fn.button.noConflict();$.fn.btn = btn;");
119
	}
120
121
	/**
122
	 *
123
	 * @param \Ajax\config\Config $config
124
	 * @return \Ajax\config\Config
125
	 */
126
	public function config($config=NULL) {
127
		if ($config===NULL) {
128
			if ($this->config===NULL) {
129
				$this->config=new DefaultConfig();
130
			}
131
		} elseif (is_array($config)) {
132
			$this->config=new Config($config);
133
		} elseif ($config instanceof Config) {
134
			$this->config=$config;
135
		}
136
		return $this->config;
137
	}
138
139
	public function getDi() {
140
		return $this->_di;
141
	}
142
143
	public function setAjaxLoader($loader) {
144
		$this->js->_setAjaxLoader($loader);
145
	}
146
147
	public function __construct($params=array()) {
148
		$defaults=array (
149
				'driver' => 'Jquery',
150
				'debug' => true
151
		);
152
		foreach ( $defaults as $key => $val ) {
153
			if (isset($params[$key])&&$params[$key]!=="") {
154
				$defaults[$key]=$params[$key];
155
			}
156
		}
157
		extract($defaults);
158
		$this->js=new Jquery($defaults);
159
		$this->cdns=array ();
160
	}
161
162
	public function addToCompile($jsScript) {
163
		$this->js->_addToCompile($jsScript);
164
	}
165
	// --------------------------------------------------------------------
166
	// Event Code
167
	// --------------------------------------------------------------------
168
	/**
169
	 * Outputs a javascript library blur event
170
	 *
171
	 * @param string $element element to attach the event to
172
	 * @param string $js code to execute
173
	 * @return string
174
	 */
175
	public function blur($element='this', $js='') {
176
		return $this->js->_blur($element, $js);
177
	}
178
	// --------------------------------------------------------------------
179
	/**
180
	 * Outputs a javascript library change event
181
	 *
182
	 * @param string $element element to attach the event to
183
	 * @param string $js code to execute
184
	 * @return string
185
	 */
186
	public function change($element='this', $js='') {
187
		return $this->js->_change($element, $js);
188
	}
189
	// --------------------------------------------------------------------
190
	/**
191
	 * Outputs a javascript library click event
192
	 *
193
	 * @param string $element element to attach the event to
194
	 * @param string $js code to execute
195
	 * @param boolean $ret_false or not to return false
196
	 * @return string
197
	 */
198
	public function click($element='this', $js='', $ret_false=TRUE) {
199
		return $this->js->_click($element, $js, $ret_false);
200
	}
201
202
	/**
203
	 * Outputs a javascript library contextmenu event
204
	 *
205
	 * @param string $element element to attach the event to
206
	 * @param string $js code to execute
207
	 * @return string
208
	 */
209
	public function contextmenu($element='this', $js='') {
210
		return $this->js->_contextmenu($element, $js);
211
	}
212
213
	// --------------------------------------------------------------------
214
	/**
215
	 * Outputs a javascript library dblclick event
216
	 *
217
	 * @param string $element element to attach the event to
218
	 * @param string $js code to execute
219
	 * @return string
220
	 */
221
	public function dblclick($element='this', $js='') {
222
		return $this->js->_dblclick($element, $js);
223
	}
224
	// --------------------------------------------------------------------
225
	/**
226
	 * Outputs a javascript library error event
227
	 *
228
	 * @param string $element element to attach the event to
229
	 * @param string $js code to execute
230
	 * @return string
231
	 */
232
	public function error($element='this', $js='') {
233
		return $this->js->_error($element, $js);
234
	}
235
	// --------------------------------------------------------------------
236
	/**
237
	 * Outputs a javascript library focus event
238
	 *
239
	 * @param string $element element to attach the event to
240
	 * @param string $js code to execute
241
	 * @return string
242
	 */
243
	public function focus($element='this', $js='') {
244
		return $this->js->_add_event($element, $js, "focus");
245
	}
246
	// --------------------------------------------------------------------
247
	/**
248
	 * Outputs a javascript library hover event
249
	 *
250
	 * @param string $element
251
	 * @param string $over code for mouse over
252
	 * @param string $out code for mouse out
253
	 * @return string
254
	 */
255
	public function hover($element='this', $over, $out) {
256
		return $this->js->_hover($element, $over, $out);
257
	}
258
	// --------------------------------------------------------------------
259
	/**
260
	 * Outputs a javascript library keydown event
261
	 *
262
	 * @param string $element element to attach the event to
263
	 * @param string $js code to execute
264
	 * @return string
265
	 */
266
	public function keydown($element='this', $js='') {
267
		return $this->js->_keydown($element, $js);
268
	}
269
	// --------------------------------------------------------------------
270
	/**
271
	 * Outputs a javascript library keypress event
272
	 *
273
	 * @param string $element element to attach the event to
274
	 * @param string $js code to execute
275
	 * @return string
276
	 */
277
	public function keypress($element='this', $js='') {
278
		return $this->js->_keypress($element, $js);
279
	}
280
	// --------------------------------------------------------------------
281
	/**
282
	 * Outputs a javascript library keydown event
283
	 *
284
	 * @param string $element element to attach the event to
285
	 * @param string $js code to execute
286
	 * @return string
287
	 */
288
	public function keyup($element='this', $js='') {
289
		return $this->js->_keyup($element, $js);
290
	}
291
	// --------------------------------------------------------------------
292
	/**
293
	 * Outputs a javascript library load event
294
	 *
295
	 * @param string $element element to attach the event to
296
	 * @param string $js code to execute
297
	 * @return string
298
	 */
299
	public function load($element='this', $js='') {
300
		return $this->js->_load($element, $js);
301
	}
302
	// --------------------------------------------------------------------
303
	/**
304
	 * Outputs a javascript library mousedown event
305
	 *
306
	 * @param string $element element to attach the event to
307
	 * @param string $js code to execute
308
	 * @return string
309
	 */
310
	public function mousedown($element='this', $js='') {
311
		return $this->js->_mousedown($element, $js);
312
	}
313
	// --------------------------------------------------------------------
314
	/**
315
	 * Outputs a javascript library mouseout event
316
	 *
317
	 * @param string $element element to attach the event to
318
	 * @param string $js code to execute
319
	 * @return string
320
	 */
321
	public function mouseout($element='this', $js='') {
322
		return $this->js->_mouseout($element, $js);
323
	}
324
	// --------------------------------------------------------------------
325
	/**
326
	 * Outputs a javascript library mouseover event
327
	 *
328
	 * @param string $element element to attach the event to
329
	 * @param string $js code to execute
330
	 * @return string
331
	 */
332
	public function mouseover($element='this', $js='') {
333
		return $this->js->_mouseover($element, $js);
334
	}
335
	// --------------------------------------------------------------------
336
	/**
337
	 * Outputs a javascript library mouseup event
338
	 *
339
	 * @param string $element element to attach the event to
340
	 * @param string $js code to execute
341
	 * @return string
342
	 */
343
	public function mouseup($element='this', $js='') {
344
		return $this->js->_mouseup($element, $js);
345
	}
346
	// --------------------------------------------------------------------
347
	/**
348
	 * Outputs the called javascript to the screen
349
	 *
350
	 * @param string $js code to output
351
	 * @return string
352
	 */
353
	public function output($js) {
354
		return $this->js->_output($js);
355
	}
356
	// --------------------------------------------------------------------
357
	/**
358
	 * Outputs a javascript library mouseup event
359
	 *
360
	 * @param string $js code to execute
361
	 * @return string
362
	 */
363
	public function ready($js) {
364
		return $this->js->_document_ready($js);
365
	}
366
	// --------------------------------------------------------------------
367
	/**
368
	 * Outputs a javascript library resize event
369
	 *
370
	 * @param string $element element to attach the event to
371
	 * @param string $js code to execute
372
	 * @return string
373
	 */
374
	public function resize($element='this', $js='') {
375
		return $this->js->_resize($element, $js);
376
	}
377
	// --------------------------------------------------------------------
378
	/**
379
	 * Outputs a javascript library scroll event
380
	 *
381
	 * @param string $element element to attach the event to
382
	 * @param string $js code to execute
383
	 * @return string
384
	 */
385
	public function scroll($element='this', $js='') {
386
		return $this->js->_scroll($element, $js);
387
	}
388
	// --------------------------------------------------------------------
389
	/**
390
	 * Outputs a javascript library unload event
391
	 *
392
	 * @param string $element element to attach the event to
393
	 * @param string $js code to execute
394
	 * @return string
395
	 */
396
	public function unload($element='this', $js='') {
397
		return $this->js->_unload($element, $js);
398
	}
399
	// --------------------------------------------------------------------
400
	// Effects
401
	// --------------------------------------------------------------------
402
	/**
403
	 * add class to element
404
	 *
405
	 * @param string $element
406
	 * @param string $class to add
407
	 * @param boolean $immediatly defers the execution if set to false
408
	 * @return string
409
	 */
410
	public function addClass($element='this', $class='', $immediatly=false) {
411
		return $this->js->_genericCallValue('addClass',$element, $class, $immediatly);
412
	}
413
414
	/**
415
	 * Insert content, specified by the parameter, after each element in the set of matched elements
416
	 * @param string $to
417
	 * @param string $element
418
	 * @param boolean $immediatly defers the execution if set to false
419
	 * @return string
420
	 */
421
	public function after($to, $element, $immediatly=false){
422
		return $this->js->_genericCallElement('after',$to, $element, $immediatly);
423
	}
424
425
	/**
426
	 * Insert content, specified by the parameter, before each element in the set of matched elements
427
	 * @param string $to
428
	 * @param string $element
429
	 * @param boolean $immediatly defers the execution if set to false
430
	 * @return string
431
	 */
432
	public function before($to, $element, $immediatly=false){
433
		return $this->js->_genericCallElement('before',$to, $element, $immediatly);
434
	}
435
436
	/**
437
	 * Get or set the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.
438
	 * @param string $element
439
	 * @param string $attributeName
440
	 * @param string $value
441
	 * @param boolean $immediatly defers the execution if set to false
442
	 */
443
	public function attr($element='this', $attributeName='value', $value='', $immediatly=false) {
444
		return $this->js->_attr($element, $attributeName, $value, $immediatly);
445
	}
446
447
	/**
448
	 * Get or set the value of the first element in the set of matched elements or set one or more attributes for every matched element.
449
	 * @param string $element
450
	 * @param string $value
451
	 * @param boolean $immediatly defers the execution if set to false
452
	 */
453
	public function val($element='this',$value='',$immediatly=false){
454
		return $this->js->_genericCallValue('val',$element,$value,$immediatly);
455
	}
456
457
	/**
458
	 * Get or set the html of an attribute for the first element in the set of matched elements.
459
	 * @param string $element
460
	 * @param string $value
461
	 * @param boolean $immediatly defers the execution if set to false
462
	 */
463
	public function html($element='this', $value='', $immediatly=false) {
464
		return $this->js->_genericCallValue('html',$element, $value, $immediatly);
465
	}
466
	// --------------------------------------------------------------------
467
	/**
468
	 * Outputs a javascript library animate event
469
	 *
470
	 * @param string $element element
471
	 * @param array $params
472
	 * @param string $speed One of 'slow', 'normal', 'fast', or time in milliseconds
473
	 * @param string $extra
474
	 * @param boolean $immediatly defers the execution if set to false
475
	 * @return string
476
	 */
477
	public function animate($element='this', $params=array(), $speed='', $extra='', $immediatly=false) {
478
		return $this->js->_animate($element, $params, $speed, $extra, $immediatly);
479
	}
480
481
	/**
482
	 * Insert content, specified by the parameter $element, to the end of each element in the set of matched elements $to.
483
	 * @param string $to
484
	 * @param string $element
485
	 * @param boolean $immediatly defers the execution if set to false
486
	 * @return string
487
	 */
488
	public function append($to, $element, $immediatly=false) {
489
		return $this->js->_genericCallElement('append',$to, $element, $immediatly);
490
	}
491
492
	/**
493
	 * Insert content, specified by the parameter $element, to the beginning of each element in the set of matched elements $to.
494
	 * @param string $to
495
	 * @param string $element
496
	 * @param boolean $immediatly defers the execution if set to false
497
	 * @return string
498
	 */
499
	public function prepend($to, $element, $immediatly=false) {
500
		return $this->js->_genericCallElement('prepend',$to, $element, $immediatly);
501
	}
502
	// --------------------------------------------------------------------
503
	/**
504
	 * Outputs a javascript library hide event
505
	 *
506
	 * @param string - element
507
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
508
	 * @param string - Javascript callback function
509
	 * @param boolean $immediatly defers the execution if set to false
510
	 * @return string
511
	 */
512
	public function fadeIn($element='this', $speed='', $callback='', $immediatly=false) {
513
		return $this->js->_fadeIn($element, $speed, $callback, $immediatly);
514
	}
515
	// --------------------------------------------------------------------
516
	/**
517
	 * Outputs a javascript library hide event
518
	 *
519
	 * @param string - element
520
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
521
	 * @param string - Javascript callback function
522
	 * @param boolean $immediatly defers the execution if set to false
523
	 * @return string
524
	 */
525
	public function fadeOut($element='this', $speed='', $callback='', $immediatly=false) {
526
		return $this->js->_fadeOut($element, $speed, $callback, $immediatly);
527
	}
528
	// --------------------------------------------------------------------
529
	/**
530
	 * Outputs a javascript library slideUp event
531
	 *
532
	 * @param string - element
533
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
534
	 * @param string - Javascript callback function
535
	 * @param boolean $immediatly defers the execution if set to false
536
	 * @return string
537
	 */
538
	public function slideUp($element='this', $speed='', $callback='', $immediatly=false) {
539
		return $this->js->_slideUp($element, $speed, $callback, $immediatly);
540
	}
541
	// --------------------------------------------------------------------
542
	/**
543
	 * Outputs a javascript library removeClass event
544
	 *
545
	 * @param string - element
546
	 * @param string - Class to add
547
	 * @param boolean $immediatly defers the execution if set to false
548
	 * @return string
549
	 */
550
	public function removeClass($element='this', $class='', $immediatly=false) {
551
		return $this->js->_genericCall('removeClass',$element, $class, $immediatly);
0 ignored issues
show
Bug introduced by
The method _genericCall() does not exist on Ajax\Jquery. Did you maybe mean _genericCallValue()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
552
	}
553
	// --------------------------------------------------------------------
554
	/**
555
	 * Outputs a javascript library slideDown event
556
	 *
557
	 * @param string - element
558
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
559
	 * @param string - Javascript callback function
560
	 * @param boolean $immediatly defers the execution if set to false
561
	 * @return string
562
	 */
563
	public function slideDown($element='this', $speed='', $callback='', $immediatly=false) {
564
		return $this->js->_slideDown($element, $speed, $callback, $immediatly);
565
	}
566
	// --------------------------------------------------------------------
567
	/**
568
	 * Outputs a javascript library slideToggle event
569
	 *
570
	 * @param string - element
571
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
572
	 * @param string - Javascript callback function
573
	 * @param boolean $immediatly defers the execution if set to false
574
	 * @return string
575
	 */
576
	public function slideToggle($element='this', $speed='', $callback='', $immediatly=false) {
577
		return $this->js->_slideToggle($element, $speed, $callback, $immediatly);
578
	}
579
	// --------------------------------------------------------------------
580
	/**
581
	 * Outputs a javascript library hide action
582
	 *
583
	 * @param string - element
584
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
585
	 * @param string - Javascript callback function
586
	 * @param boolean $immediatly defers the execution if set to false
587
	 * @return string
588
	 */
589
	public function hide($element='this', $speed='', $callback='', $immediatly=false) {
590
		return $this->js->_hide($element, $speed, $callback, $immediatly);
591
	}
592
	// --------------------------------------------------------------------
593
	/**
594
	 * Outputs a javascript library toggle event
595
	 *
596
	 * @param string - element
597
	 * @param boolean $immediatly defers the execution if set to false
598
	 * @return string
599
	 */
600
	public function toggle($element='this', $immediatly=false) {
601
		return $this->js->_toggle($element, $immediatly);
602
	}
603
	// --------------------------------------------------------------------
604
	/**
605
	 * Outputs a javascript library toggle class event
606
	 *
607
	 * @param string - element
608
	 * @param boolean $immediatly defers the execution if set to false
609
	 * @return string
610
	 */
611
	public function toggleClass($element='this', $class='', $immediatly=false) {
612
		return $this->js->_genericCallValue('toggleClass',$element, $class, $immediatly);
613
	}
614
615
	/**
616
	 * Execute all handlers and behaviors attached to the matched elements for the given event.
617
	 * @param string $element
618
	 * @param string $event
619
	 * @param boolean $immediatly defers the execution if set to false
620
	 */
621
	public function trigger($element='this', $event='click', $immediatly=false) {
622
		return $this->js->_trigger($element, $event, $immediatly);
623
	}
624
	// --------------------------------------------------------------------
625
	/**
626
	 * Outputs a javascript library show event
627
	 *
628
	 * @param string - element
629
	 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
630
	 * @param string - Javascript callback function
631
	 * @param boolean $immediatly defers the execution if set to false
632
	 * @return string
633
	 */
634
	public function show($element='this', $speed='', $callback='', $immediatly=false) {
635
		return $this->js->_show($element, $speed, $callback, $immediatly);
636
	}
637
638
	/**
639
	 * Allows to attach a condition
640
	 * @param string $condition
641
	 * @param string $jsCodeIfTrue
642
	 * @param string $jsCodeIfFalse
643
	 * @param boolean $immediatly defers the execution if set to false
644
	 */
645
	public function condition($condition, $jsCodeIfTrue, $jsCodeIfFalse=null, $immediatly=false) {
646
		return $this->js->_condition($condition, $jsCodeIfTrue, $jsCodeIfFalse, $immediatly);
647
	}
648
	// --------------------------------------------------------------------
649
	/**
650
	 * gather together all script needing to be output
651
	 *
652
	 * @param View $view
653
	 * @param $view_var
654
	 * @param $script_tags
655
	 * @return string
656
	 */
657
	public function compile($view=NULL, $view_var='script_foot', $script_tags=TRUE) {
658
		$bs=$this->_bootstrap;
659
		if (isset($bs)&&isset($view)) {
660
			$bs->compileHtml($this, $view);
661
		}
662
		$sem=$this->_semantic;
663
		if (isset($sem)&&isset($view)) {
664
			$sem->compileHtml($this, $view);
665
		}
666
		return $this->js->_compile($view, $view_var, $script_tags);
667
	}
668
669
	/**
670
	 * Clears any previous javascript collected for output
671
	 *
672
	 * @return void
673
	 */
674
	public function clear_compile() {
675
		$this->js->_clear_compile();
676
	}
677
	// --------------------------------------------------------------------
678
	/**
679
	 * Outputs a <script> tag with the source as an external js file
680
	 *
681
	 * @param string $external_file
682
	 * @param boolean $relative
683
	 * @return string
684
	 */
685
	public function external($external_file='', $relative=FALSE) {
686
		$assets=$this->_di->get('assets');
687
		$assets->addJs($external_file);
688
		return $assets->outputJs();
689
	}
690
	// --------------------------------------------------------------------
691
	/**
692
	 * Outputs a <script> tag
693
	 *
694
	 * @param string $script
695
	 * @param boolean $cdata If a CDATA section should be added
696
	 * @return string
697
	 */
698 View Code Duplication
	public function inline($script, $cdata=TRUE) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
699
		$str=$this->_open_script();
700
		$str.=($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
701
		$str.=$this->_close_script();
702
		return $str;
703
	}
704
	// --------------------------------------------------------------------
705
	/**
706
	 * Outputs an opening <script>
707
	 *
708
	 * @param string $src
709
	 * @return string
710
	 */
711
	private function _open_script($src='') {
712
		$str='<script type="text/javascript" ';
713
		$str.=($src=='') ? '>' : ' src="'.$src.'">';
714
		return $str;
715
	}
716
	// --------------------------------------------------------------------
717
	/**
718
	 * Outputs an closing </script>
719
	 *
720
	 * @param string $extra
721
	 * @return string
722
	 */
723
	private function _close_script($extra="\n") {
724
		return "</script>$extra";
725
	}
726
727
	// --------------------------------------------------------------------
728
	/**
729
	 * Can be passed a database result or associative array and returns a JSON formatted string
730
	 *
731
	 * @param mixed $result result set or array
732
	 * @param bool $match_array_type match array types (defaults to objects)
733
	 * @return string json formatted string
734
	 */
735
	public function generate_json($result=NULL, $match_array_type=FALSE) {
736
		// JSON data can optionally be passed to this function
737
		// either as a database result object or an array, or a user supplied array
738
		if (!is_null($result)) {
739
			if (is_object($result)) {
740
				$json_result=$result->result_array();
741
			} elseif (is_array($result)) {
742
				$json_result=$result;
743
			} else {
744
				return $this->_prep_args($result);
745
			}
746
		} else {
747
			return 'null';
748
		}
749
		return $this->_create_json($json_result, $match_array_type);
750
	}
751
752
	private function _create_json($json_result, $match_array_type) {
753
		$json=array ();
754
		$_is_assoc=TRUE;
755
		if (!is_array($json_result)&&empty($json_result)) {
756
			show_error("Generate JSON Failed - Illegal key, value pair.");
757
		} elseif ($match_array_type) {
758
			$_is_assoc=$this->_is_associative_array($json_result);
759
		}
760
		foreach ( $json_result as $k => $v ) {
761
			if ($_is_assoc) {
762
				$json[]=$this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
763
			} else {
764
				$json[]=$this->generate_json($v, $match_array_type);
765
			}
766
		}
767
		$json=implode(',', $json);
768
		return $_is_assoc ? "{".$json."}" : "[".$json."]";
769
	}
770
	// --------------------------------------------------------------------
771
	/**
772
	 * Checks for an associative array
773
	 *
774
	 * @param type
775
	 * @return type
776
	 */
777
	public function _is_associative_array($arr) {
778
		foreach ( array_keys($arr) as $key => $val ) {
779
			if ($key!==$val) {
780
				return TRUE;
781
			}
782
		}
783
		return FALSE;
784
	}
785
	// --------------------------------------------------------------------
786
	/**
787
	 * Ensures a standard json value and escapes values
788
	 *
789
	 * @param type
790
	 * @return type
791
	 */
792
	public function _prep_args($result, $is_key=FALSE) {
793
		if (is_null($result)) {
794
			return 'null';
795
		} elseif (is_bool($result)) {
796
			return ($result===TRUE) ? 'true' : 'false';
797
		} elseif (is_string($result)||$is_key) {
798
			return '"'.str_replace(array (
799
					'\\',"\t","\n","\r",'"','/'
800
			), array (
801
					'\\\\','\\t','\\n',"\\r",'\"','\/'
802
			), $result).'"';
803
		} elseif (is_scalar($result)) {
804
			return $result;
805
		}
806
	}
807
808
	/**
809
	 * Performs an ajax GET request
810
	 * @param string $url The url of the request
811
	 * @param string $params JSON parameters
812
	 * @param string $responseElement selector of the HTML element displaying the answer
813
	 * @param string $jsCallback javascript code to execute after the request
814
	 * @param boolean $hasLoader true for showing ajax loader. default : true
815
	 */
816
	public function get($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true) {
817
		return $this->js->_get($url, $params, $responseElement, $jsCallback, NULL, $hasLoader,true);
818
	}
819
820
	/**
821
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
822
	 * @param string $url the request url
823
	 * @param string $params JSON parameters
824
	 * @param string $method Method used
825
	 * @param string $jsCallback javascript code to execute after the request
826
	 * @param boolean $immediatly
827
	 */
828
	public function json($url, $method="get", $params="{}", $jsCallback=NULL, $attr="id", $context="document",$immediatly=false) {
829
		return $this->js->_json($url, $method, $params, $jsCallback, $attr, $context,$immediatly);
830
	}
831
832
	/**
833
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element
834
	 * @param string $element
835
	 * @param string $event
836
	 * @param string $url the request address
837
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
838
	 */
839
	public function jsonOn($event,$element, $url,$parameters=array()) {
840
		return $this->js->_jsonOn($event, $element, $url,$parameters);
841
	}
842
843
	/**
844
	 * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name
845
	 * @param string $url the request url
846
	 * @param string $params Paramètres passés au format JSON
847
	 * @param string $method Method used
848
	 * @param string $jsCallback javascript code to execute after the request
849
	 */
850
	public function jsonDeferred($url, $method="get", $params="{}", $jsCallback=NULL) {
851
		return $this->js->_json($url, $method, $params, $jsCallback, NULL, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
852
	}
853
854
	/**
855
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
856
	 * @param string $url the request url
857
	 * @param string $params The JSON parameters
858
	 * @param string $method Method used
859
	 * @param string $jsCallback javascript code to execute after the request
860
	 */
861
	public function jsonArray($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL) {
862
		return $this->js->_jsonArray($maskSelector, $url, $method, $params, $jsCallback, NULL, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
863
	}
864
865
	/**
866
	 * Peforms an ajax request delayed and receives a JSON array data types by copying and assigning them to the DOM elements with the same name
867
	 * @param string $maskSelector the selector of the element to clone
868
	 * @param string $url the request url
869
	 * @param string $params JSON parameters
870
	 * @param string $method Method used
871
	 * @param string $jsCallback javascript code to execute after the request
872
	 */
873
	public function jsonArrayDeferred($maskSelector, $url, $method="get", $params="{}", $jsCallback=NULL) {
874
		return $this->js->_jsonArray($maskSelector, $url, $method, $params, $jsCallback, NULL, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
875
	}
876
877
	/**
878
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element
879
	 * @param string $element
880
	 * @param string $event
881
	 * @param string $url the request url
882
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
883
	 */
884
	public function jsonArrayOn($event,$element,$maskSelector, $url,$parameters=array()) {
885
		return $this->js->_jsonArrayOn($event,$element,$maskSelector, $url, $parameters);
886
	}
887
888
	/**
889
	 * Prepares a Get ajax request
890
	 * To use on an event
891
	 * @param string $url The url of the request
892
	 * @param string $params JSON parameters
893
	 * @param string $responseElement selector of the HTML element displaying the answer
894
	 * @param string $jsCallback javascript code to execute after the request
895
	 * @param string $attr the html attribute added to the request
896
	 */
897
	public function getDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL,$attr="id") {
898
		return $this->js->_get($url, $params, $responseElement, $jsCallback, $attr, false);
899
	}
900
901
	/**
902
	 * Performs a get to $url on the event $event on $element
903
	 * and display it in $responseElement
904
	 * @param string $event
905
	 * @param string $element
906
	 * @param string $url The url of the request
907
	 * @param string $responseElement The selector of the HTML element displaying the answer
908
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
909
	 */
910
	public function getOn($event, $element, $url, $responseElement="", $parameters=array()) {
911
		$params=JArray::getDefaultValue($parameters, "params", "{}");
912
		return $this->js->_getOn($event, $element, $url, $params, $responseElement, $parameters);
913
	}
914
915
	/**
916
	 * Performs a get to $url on the click event on $element
917
	 * and display it in $responseElement
918
	 * @param string $element
919
	 * @param string $url The url of the request
920
	 * @param string $responseElement The selector of the HTML element displaying the answer
921
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
922
	 */
923
	public function getOnClick($element, $url, $responseElement="", $parameters=array()) {
924
		return $this->getOn("click", $element, $url, $responseElement, $parameters);
925
	}
926
927
	/**
928
	 * Makes an ajax post
929
	 * @param string $url the request url
930
	 * @param string $params JSON parameters
931
	 * @param string $responseElement selector of the HTML element displaying the answer
932
	 * @param string $jsCallback javascript code to execute after the request
933
	 * @param boolean $hasLoader true for showing ajax loader. default : true
934
	 */
935
	public function post($url, $responseElement="", $params="{}", $jsCallback=NULL,$hasLoader=true) {
936
		return $this->js->_post($url, $params, $responseElement, $jsCallback, NULL, $hasLoader,true);
937
	}
938
939
	/**
940
	 * Prepares a delayed ajax POST
941
	 * to use on an event
942
	 * @param string $url the request url
943
	 * @param string $params JSON parameters
944
	 * @param string $attr the html attribute added to the request
945
	 * @param string $responseElement selector of the HTML element displaying the answer
946
	 * @param string $jsCallback javascript code to execute after the request
947
	 * @param boolean $hasLoader true for showing ajax loader. default : true
948
	 */
949
	public function postDeferred($url, $responseElement="", $params="{}", $jsCallback=NULL, $attr="id",$hasLoader=true) {
950
		return $this->js->_post($url, $params, $responseElement, $jsCallback, $attr, $hasLoader,false);
951
	}
952
953
	/**
954
	 * Performs a post to $url on the event $event fired on $element and pass the parameters $params
955
	 * Display the result in $responseElement
956
	 * @param string $event
957
	 * @param string $element
958
	 * @param string $url The url of the request
959
	 * @param string $params The parameters to send
960
	 * @param string $responseElement selector of the HTML element displaying the answer
961
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
962
	 */
963
	public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
964
		return $this->js->_postOn($event, $element,  $url, $params, $responseElement, $parameters);
965
	}
966
967
	/**
968
	 * Performs a post to $url on the click event fired on $element and pass the parameters $params
969
	 * Display the result in $responseElement
970
	 * @param string $element
971
	 * @param string $url The url of the request
972
	 * @param string $params The parameters to send
973
	 * @param string $responseElement selector of the HTML element displaying the answer
974
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
975
	 */
976
	public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) {
977
		return $this->postOn("click", $element, $url, $params, $responseElement, $parameters);
978
	}
979
980
	/**
981
	 * Performs a post form with ajax
982
	 * @param string $url The url of the request
983
	 * @param string $form The form HTML id
984
	 * @param string $responseElement selector of the HTML element displaying the answer
985
	 * @param string $jsCallback javascript code to execute after the request
986
	 * @param boolean $hasLoader true for showing ajax loader. default : true
987
	 */
988
	public function postForm($url, $form, $responseElement, $validation=false, $jsCallback=NULL,$hasLoader=true) {
989
		return $this->js->_postForm($url, $form, $responseElement, $validation, $jsCallback, NULL, $hasLoader,true);
990
	}
991
992
	/**
993
	 * Performs a delayed post form with ajax
994
	 * For use on an event
995
	 * @param string $url The url of the request
996
	 * @param string $form The form HTML id
997
	 * @param string $responseElement selector of the HTML element displaying the answer
998
	 * @param string $jsCallback javascript code to execute after the request
999
	 * @param string $attr the html attribute added to the request
1000
	 * @param boolean $hasLoader true for showing ajax loader. default : true
1001
	 */
1002
	public function postFormDeferred($url, $form, $responseElement, $validation=false, $jsCallback=NULL,$attr="id",$hasLoader=true) {
1003
		return $this->js->_postForm($url, $form, $responseElement, $validation, $jsCallback, $attr, $hasLoader,false);
1004
	}
1005
1006
	/**
1007
	 * Performs a post form with ajax in response to an event $event on $element
1008
	 * display the result in $responseElement
1009
	 * @param string $event
1010
	 * @param string $element
1011
	 * @param string $url
1012
	 * @param string $form
1013
	 * @param string $responseElement selector of the HTML element displaying the answer
1014
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
1015
	 */
1016
	public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) {
1017
		return $this->js->_postFormOn($event,$element, $url, $form, $responseElement, $parameters);
1018
	}
1019
1020
	/**
1021
	 * Performs a post form with ajax in response to the click event on $element
1022
	 * display the result in $responseElement
1023
	 * @param string $element
1024
	 * @param string $url
1025
	 * @param string $form
1026
	 * @param string $responseElement selector of the HTML element displaying the answer
1027
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
1028
	 */
1029
	public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) {
1030
		return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters);
1031
	}
1032
1033
	/**
1034
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
1035
	 * @param string $element the element
1036
	 * @param string $jqueryCall the JQuery callback
1037
	 * @param mixed $param array or string parameters
1038
	 * @param string $jsCallback javascript code to execute after the jquery call
1039
	 * @return mixed
1040
	 */
1041
	public function doJQuery($element, $jqueryCall, $param="", $jsCallback="") {
1042
		return $this->js->_doJQuery($element, $jqueryCall, $param, $jsCallback, true);
1043
	}
1044
1045
	/**
1046
	 * Calls the JQuery callback $someThing on $element with facultative parameter $param
1047
	 * @param string $element the element
1048
	 * @param string $jqueryCall the JQuery callback
1049
	 * @param mixed $param array or string parameters
1050
	 * @param string $jsCallback javascript code to execute after the jquery call
1051
	 * @return mixed
1052
	 */
1053
	public function doJQueryDeferred($element, $jqueryCall, $param="", $jsCallback="") {
1054
		return $this->js->_doJQuery($element, $jqueryCall, $param, $jsCallback, false);
1055
	}
1056
1057
	/**
1058
	 * Calls the JQuery callback $jqueryCall on $element with facultative parameter $param in response to an event $event
1059
	 * @param string $event
1060
	 * @param string $element
1061
	 * @param string $elementToModify
1062
	 * @param string $jqueryCall
1063
	 * @param string $param
1064
	 * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"jsCallback"=>'',"immediatly"=>true)
1065
	 */
1066 View Code Duplication
	public function doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param="", $parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1067
		$jsCallback="";
1068
		$stopPropagation=false;
1069
		$preventDefault=false;
1070
		$immediatly=true;
1071
		extract($parameters);
1072
		return $this->js->_doJQueryOn($event, $element, $elementToModify, $jqueryCall, $param, $preventDefault, $stopPropagation, $jsCallback,$immediatly);
1073
	}
1074
1075
	/**
1076
	 * Executes the code $js
1077
	 * @param string $js Code to execute
1078
	 * @param boolean $immediatly delayed if false
1079
	 * @return String
1080
	 */
1081
	public function exec($js, $immediatly=false) {
1082
		$script=$this->js->_exec($js, $immediatly);
1083
		return $script;
1084
	}
1085
1086
	/**
1087
	 * Executes the javascript code $js when $event fires on $element
1088
	 * @param string $event
1089
	 * @param string $element
1090
	 * @param string $js Code to execute
1091
	 * @param array $parameters default : array("preventDefault"=>false,"stopPropagation"=>false,"immediatly"=>true)
1092
	 * @return String
1093
	 */
1094 View Code Duplication
	public function execOn($event, $element, $js, $parameters=array()) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1095
		$stopPropagation=false;
1096
		$preventDefault=false;
1097
		$immediatly=true;
1098
		extract($parameters);
1099
		$script=$this->js->_execOn($element, $event, $js, $preventDefault, $stopPropagation,$immediatly);
1100
		return $script;
1101
	}
1102
1103
	public function getCDNs() {
1104
		return $this->cdns;
1105
	}
1106
1107
	public function setCDNs($cdns) {
1108
		if (is_array($cdns)===false) {
1109
			$cdns=array (
1110
					$cdns
1111
			);
1112
		}
1113
		$this->cdns=$cdns;
1114
	}
1115
1116
	public function genCDNs($template=NULL) {
1117
		$hasJQuery=false;
1118
		$hasJQueryUI=false;
1119
		$hasBootstrap=false;
1120
		$result=array ();
1121
		foreach ( $this->cdns as $cdn ) {
1122
			switch(get_class($cdn)) {
1123
				case "Ajax\lib\CDNJQuery":
1124
					$hasJQuery=true;
1125
					$result[0]=$cdn;
1126
					break;
1127
				case "Ajax\lib\CDNJQuery":
1128
					$hasJQueryUI=true;
1129
					$result[1]=$cdn;
1130
					break;
1131
				case "Ajax\lib\CDNBootstrap":
1132
					$hasBootstrap=true;
1133
					$result[2]=$cdn;
1134
					break;
1135
			}
1136
		}
1137
		if ($hasJQuery===false) {
1138
			$result[0]=new CDNJQuery("x");
1139
		}
1140
		if ($hasJQueryUI===false&&isset($this->_ui)) {
1141
			$result[1]=new CDNGuiGen("x", $template);
1142
		}
1143
		if ($hasBootstrap===false&&isset($this->_bootstrap)) {
1144
			$result[2]=new CDNBootstrap("x");
1145
		}
1146
		ksort($result);
1147
		return implode("\n", $result);
1148
	}
1149
}
1150
if (Version::get()==="1.3.4") {
1151
	class JsUtils extends _JsUtils {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1152
1153
		public function setDi($di) {
1154
			$this->_setDi($di);
1155
		}
1156
	}
1157
} else {
1158
	class JsUtils extends _JsUtils {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Ajax\JsUtils has been defined more than once; this definition is ignored, only the first definition in this file (L1151-1156) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
1159
1160
		public function setDi(DiInterface $di) {
1161
			$this->_setDi($di);
1162
		}
1163
	}
1164
}
1165