1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12
13 class WhBasicFileUpload extends CInputWidget
14 {
15 16 17 18
19 public $pluginOptions = array();
20
21 22 23
24 public $uploadAction;
25
26 27 28 29
30 public function init()
31 {
32 if ($this->uploadAction === null) {
33 throw new CException(Yii::t('zii', '"uploadAction" attribute cannot be blank'));
34 }
35
36 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
37 }
38
39 40 41
42 public function run()
43 {
44 $this->renderField();
45 $this->registerClientScript();
46 }
47
48 49 50
51 public function renderField()
52 {
53 list($name, $id) = $this->resolveNameID();
54
55 TbArray::defaultValue('id', $id, $this->htmlOptions);
56 TbArray::defaultValue('name', $name, $this->htmlOptions);
57 $this->htmlOptions['data-url'] = $this->uploadAction;
58 $this->pluginOptions['url'] = $this->uploadAction;
59 if ($this->hasModel()) {
60 echo CHtml::activeFileField($this->model, $this->attribute, $this->htmlOptions);
61
62 } else {
63 echo CHtml::fileField($name, $this->value, $this->htmlOptions);
64 }
65 }
66
67 68 69
70 public function registerClientScript()
71 {
72
73 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
74 $assetsUrl = $this->getAssetsUrl($path);
75
76
77 $cs = Yii::app()->getClientScript();
78
79 $cs->registerCssFile($assetsUrl . '/css/jquery.fileupload-ui.css');
80 $cs->registerScriptFile($assetsUrl . '/js/vendor/jquery.ui.widget.js');
81 $cs->registerScriptFile($assetsUrl . '/js/jquery.iframe-transport.js');
82 $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload.js');
83
84
85 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
86
87 $this->getApi()->registerPlugin('fileupload', $selector, $this->pluginOptions);
88 }
89
90 }