1 <?php
2 3 4 5 6 7 8 9 10 11
12 Yii::import('bootstrap.helpers.TbArray');
13 Yii::import('bootstrap.helpers.TbHtml');
14
15 class WhGoogleFonts extends CInputWidget
16 {
17 18 19 20 21 22 23 24
25 public $pluginOptions = array();
26
27 28 29
30 public $useHelperSelectBox = false;
31
32 33 34
35 public $helperOptions = array();
36
37
38 39 40 41
42 public function init()
43 {
44
45 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
46
47 TbHtml::addCssClass('bfh-googlefonts', $this->htmlOptions);
48 }
49
50 51 52
53 public function run()
54 {
55 $this->renderField();
56 $this->registerClientScript();
57 }
58
59 60 61
62 public function renderField()
63 {
64 list($name, $id) = $this->resolveNameID();
65
66 TbArray::defaultValue('id', $id, $this->htmlOptions);
67 TbArray::defaultValue('name', $name, $this->htmlOptions);
68
69 if ($this->useHelperSelectBox) {
70 $select = Yii::createComponent(CMap::mergeArray($this->helperOptions, array(
71 'class' => 'yiiwheels.widgets.formhelpers.WhSelectBox',
72 'htmlOptions' => $this->htmlOptions,
73 'model' => $this->model,
74 'attribute' => $this->attribute,
75 'name' => $this->name,
76 'value' => $this->value,
77 'wrapperOptions' => array(
78 'class' => 'bfh-googlefonts',
79 'data-family' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value,
80 'data-subsets' => isset($this->pluginOptions['subsets'])
81 ? $this->pluginOptions['subsets']
82 : null,
83 'data-families' => isset($this->pluginOptions['families'])
84 ? $this->pluginOptions['families']
85 : null
86 )
87 )));
88 $select->init();
89 $select->run();
90 } else {
91 $this->htmlOptions['data-family'] = $this->hasModel()
92 ? $this->model->{$this->attribute}
93 : $this->value;
94 $this->htmlOptions['data-subsets'] = isset($this->pluginOptions['subsets'])
95 ? $this->pluginOptions['subsets']
96 : null;
97 $this->htmlOptions['data-families'] = isset($this->pluginOptions['families'])
98 ? $this->pluginOptions['families']
99 : null;
100 if ($this->hasModel()) {
101 echo CHtml::activeDropDownList($this->model, $this->attribute, array(), $this->htmlOptions);
102 } else {
103 echo CHtml::dropDownList($name, $this->value, array(), $this->htmlOptions);
104 }
105 }
106 }
107
108 109 110
111 public function registerClientScript()
112 {
113
114 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
115 $assetsUrl = $this->getAssetsUrl($path);
116
117
118 $cs = Yii::app()->getClientScript();
119
120 $cs->registerCssFile($assetsUrl . '/css/bootstrap-formhelpers.css');
121
122 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-googlefonts.codes.js');
123 $cs->registerScriptFile($assetsUrl . '/js/bootstrap-formhelpers-googlefonts.js');
124
125
126 if (!$this->useHelperSelectBox) {
127 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
128 $this->getApi()->registerPlugin('bfhgooglefonts', $selector, $this->pluginOptions);
129 }
130 }
131 }