1 <?php
2 3 4 5 6 7 8 9 10
11 Yii::import('bootstrap.helpers.TbArray');
12 Yii::import('zii.widgets.jui.CJuiInputWidget');
13
14 class WhFileUpload extends CJuiInputWidget
15 {
16 17 18 19
20 public $url;
21
22 23 24 25
26 public $multiple = true;
27
28 29 30 31
32 public $uploadTemplate;
33
34 35 36 37
38 public $downloadTemplate;
39
40 41 42
43 public $previewImages = true;
44
45 46 47
48 public $imageProcessing = true;
49
50 51 52
53 public $formView = 'yiiwheels.widgets.fileupload.views.form';
54
55 56 57
58 public $uploadView = 'yiiwheels.widgets.fileupload.views.upload';
59
60 61 62
63 public $downloadView = 'yiiwheels.widgets.fileupload.views.download';
64
65 66 67
68 public $previewImagesView = 'yiiwheels.widgets.fileupload.views.gallery';
69
70 71 72
73 public function init()
74 {
75 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
76
77 if ($this->uploadTemplate === null) {
78 $this->uploadTemplate = "#template-upload";
79 }
80
81 if ($this->downloadTemplate === null) {
82 $this->downloadTemplate = "#template-download";
83 }
84
85 if (!isset($this->htmlOptions['enctype'])) {
86 $this->htmlOptions['enctype'] = 'multipart/form-data';
87 }
88 parent::init();
89 }
90
91 92 93
94 public function run()
95 {
96
97 list($name, $id) = $this->resolveNameID();
98
99 $this->htmlOptions['id'] = ($this->hasModel() ? get_class($this->model) : 'fileupload') . '-form';
100
101 $this->options['url'] = $this->url;
102
103 $htmlOptions = array();
104
105 if ($this->multiple) {
106 $htmlOptions["multiple"] = true;
107 }
108
109 $this->render($this->uploadView);
110 $this->render($this->downloadView);
111 $this->render($this->formView, array('model', $this->model, 'name' => $name, 'htmlOptions' => $htmlOptions));
112
113 if ($this->previewImages || $this->imageProcessing) {
114 $this->render($this->previewImagesView);
115 }
116
117 $this->registerClientScript();
118 }
119
120 121 122
123 public function registerClientScript()
124 {
125
126
127 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
128 $assetsUrl = $this->getAssetsUrl($path);
129
130
131 $cs = Yii::app()->getClientScript();
132
133 $cs->registerCssFile($assetsUrl . '/css/jquery.fileupload-ui.css');
134
135
136
137 $cs->registerScriptFile($assetsUrl . '/js/vendor/jquery.ui.widget.js', CClientScript::POS_END);
138
139
140 $cs->registerScriptFile($assetsUrl . '/js/tmpl.min.js', CClientScript::POS_END);
141
142 if ($this->previewImages || $this->imageProcessing) {
143 $cs->registerScriptFile($assetsUrl . '/js/load-image.min.js', CClientScript::POS_END);
144 $cs->registerScriptFile($assetsUrl . '/js/canvas-to-blob.min.js', CClientScript::POS_END);
145
146 $this->getYiiWheels()->registerAssetCss("bootstrap-image-gallery.min.css");
147 $this->getYiiWheels()->registerAssetJs("bootstrap-image-gallery.min.js", CClientScript::POS_END);
148 }
149
150 $cs->registerScriptFile($assetsUrl . '/js/jquery.iframe-transport.js', CClientScript::POS_END);
151 $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload.js', CClientScript::POS_END);
152
153
154 if ($this->imageProcessing) {
155 $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-ip.js', CClientScript::POS_END);
156 }
157
158 if ($this->previewImages) {
159 $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-fp.js', CClientScript::POS_END);
160 }
161
162 $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-locale.js', CClientScript::POS_END);
163
164
165 $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-ui.js', CClientScript::POS_END);
166
167
168 $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
169
170 $this->getApi()->registerPlugin('fileupload', $selector, $this->options);
171 }
172
173 }
174