1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhFineUploader extends CInputWidget
14 {
15 16 17
18 public $uploadAction;
19
20 21 22
23 public $tagName = 'div';
24
25 26 27
28 public $noScriptText;
29
30 31 32
33 public $pluginOptions = array();
34
35 36 37
38 public $events = array();
39
40 41 42
43 public $scenario;
44
45 46 47
48 protected $defaultOptions = array();
49
50 51 52
53 public function init()
54 {
55 if ($this->uploadAction === null) {
56 throw new CException(Yii::t('zii', '"uploadAction" attribute cannot be blank'));
57 }
58 if ($this->noScriptText === null) {
59 $this->noScriptText = Yii::t('zii', "Please enable JavaScript to use file uploader.");
60 }
61
62 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
63
64 $this->initDefaultOptions();
65 }
66
67 68 69
70 public function run()
71 {
72 $this->renderTag();
73 $this->registerClientScript();
74 }
75
76 77 78
79 public function renderTag()
80 {
81 echo CHtml::tag($this->tagName, $this->htmlOptions, '<noscript>' . $this->noScriptText . '</noscript>', true);
82 }
83
84 85 86
87 public function registerClientScript()
88 {
89
90 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
91 $assetsUrl = $this->getAssetsUrl($path);
92
93
94 $cs = Yii::app()->getClientScript();
95
96 $script = YII_DEBUG ? 'jquery.fineuploader-3.2.js' : 'jquery.fineuploader-3.2.min.js';
97
98 $cs->registerCssFile($assetsUrl . '/css/fineuploader.css');
99 $cs->registerScriptFile($assetsUrl . '/js/' . $script);
100
101
102 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
103
104 $this->getApi()->registerPlugin(
105 'fineUploader',
106 $selector,
107 CMap::mergeArray($this->defaultOptions, $this->pluginOptions)
108 );
109 $this->getApi()->registerEvents($selector, $this->events);
110 }
111
112 113 114 115
116 protected function initDefaultOptions()
117 {
118 list($name, $id) = $this->resolveNameID();
119
120 TbArray::defaultValue('id', $id, $this->htmlOptions);
121 TbArray::defaultValue('name', $name, $this->htmlOptions);
122
123
124 $this->defaultOptions = array(
125 'request' => array(
126 'endpoint' => $this->uploadAction,
127 'inputName' => $name,
128 ),
129 'validation' => $this->getValidator(),
130 'messages' => array(
131 'typeError' => Yii::t('zii', '{file} has an invalid extension. Valid extension(s): {extensions}.'),
132 'sizeError' => Yii::t('zii', '{file} is too large, maximum file size is {sizeLimit}.'),
133 'minSizeError' => Yii::t('zii', '{file} is too small, minimum file size is {minSizeLimit}.'),
134 'emptyError:' => Yii::t('zii', '{file} is empty, please select files again without it.'),
135 'noFilesError' => Yii::t('zii', 'No files to upload.'),
136 'onLeave' => Yii::t(
137 'zii',
138 'The files are being uploaded, if you leave now the upload will be cancelled.'
139 )
140 ),
141 );
142 }
143
144 145 146
147 protected function getValidator()
148 {
149 $ret = array();
150 if ($this->hasModel()) {
151 if ($this->scenario !== null) {
152 $originalScenario = $this->model->getScenario();
153 $this->model->setScenario($this->scenario);
154 $validators = $this->model->getValidators($this->attribute);
155 $this->model->setScenario($originalScenario);
156
157 } else {
158 $validators = $this->model->getValidators($this->attribute);
159 }
160
161
162 foreach ($validators as $validator) {
163 if (is_a($validator, 'CFileValidator')) {
164 $ret = array(
165 'allowedExtensions' => explode(',', str_replace(' ', '', $validator->types)),
166 'sizeLimit' => $validator->maxSize,
167 'minSizeLimit' => $validator->minSize,
168 );
169 break;
170 }
171 }
172 }
173 return $ret;
174 }
175 }