1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*## TbToggleButton class file |
4
|
|
|
* |
5
|
|
|
* @author: amr bedair <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
*## Class TbToggleButton |
10
|
|
|
* @see <http://www.bootstrap-switch.org/> |
11
|
|
|
* @package booster.widgets.forms.buttons |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class TbSwitch extends CInputWidget { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var TbActiveForm when created via TbActiveForm, this attribute is set to the form that renders the widget |
18
|
|
|
* @see TbActionForm->inputRow |
19
|
|
|
*/ |
20
|
|
|
public $form; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array the javascript events |
24
|
|
|
* |
25
|
|
|
* Example: |
26
|
|
|
* <pre> |
27
|
|
|
* 'events'=>array( |
28
|
|
|
* 'switchChange'=>'js:function(event, state) { |
29
|
|
|
* console.log(this); // DOM element |
30
|
|
|
* console.log(event); // jQuery event |
31
|
|
|
* console.log(state); // true | false |
32
|
|
|
* }' |
33
|
|
|
* ) |
34
|
|
|
* </pre> |
35
|
|
|
*/ |
36
|
|
|
public $events = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* js widget options |
40
|
|
|
* @see <http://www.bootstrap-switch.org/> options part |
41
|
|
|
* @var array to contain the widget js options |
42
|
|
|
*/ |
43
|
|
|
public $options = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Widget's run function |
47
|
|
|
*/ |
48
|
|
|
public function run() { |
49
|
|
|
|
50
|
|
|
list($name, $id) = $this->resolveNameID(); |
51
|
|
|
|
52
|
|
|
if ($this->hasModel()) { |
53
|
|
|
if ($this->form) { |
54
|
|
|
echo $this->form->checkBox($this->model, $this->attribute, $this->htmlOptions); |
55
|
|
|
} else { |
56
|
|
|
echo CHtml::activeCheckBox($this->model, $this->attribute, $this->htmlOptions); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
echo CHtml::checkBox($name, $this->value, $this->htmlOptions); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->registerClientScript($id); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Registers required css and js files |
67
|
|
|
* |
68
|
|
|
* @param integer $id the id of the toggle button |
69
|
|
|
*/ |
70
|
|
|
protected function registerClientScript($id) { |
71
|
|
|
|
72
|
|
|
$booster = Booster::getBooster(); |
73
|
|
|
$booster->registerPackage('switch'); |
74
|
|
|
$config = CJavaScript::encode($this->options); |
75
|
|
|
|
76
|
|
|
ob_start(); |
77
|
|
|
echo "$('input#$id').bootstrapSwitch({$config})"; |
78
|
|
|
foreach ($this->events as $event => $handler) { |
79
|
|
|
$event = $event.'.bootstrapSwitch'; |
80
|
|
|
if (!$handler instanceof CJavaScriptExpression && strpos($handler, 'js:') === 0) |
|
|
|
|
81
|
|
|
$handler = new CJavaScriptExpression($handler); |
82
|
|
|
echo ".on('{$event}', " . $handler . ")"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $this->getId(), ob_get_clean() . ';'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.