HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbFileUpload.php
Go to the documentation of this file.
1 <?php
14 Yii::import('zii.widgets.jui.CJuiInputWidget');
15 
23 class TbFileUpload extends CJuiInputWidget
24 {
29  public $url;
30 
35  public $multiple = false;
36 
42 
48 
52  public $previewImages = true;
53 
57  public $imageProcessing = true;
58 
62  public $formView = 'booster.views.fileupload.form';
63 
67  public $uploadView = 'booster.views.fileupload.upload';
68 
72  public $downloadView = 'booster.views.fileupload.download';
73 
77  public $previewImagesView = 'booster.views.gallery.preview';
78 
82  public function init()
83  {
84  if ($this->uploadTemplate === null) {
85  $this->uploadTemplate = "#template-upload";
86  }
87 
88  if ($this->downloadTemplate === null) {
89  $this->downloadTemplate = "#template-download";
90  }
91 
92  if (!isset($this->htmlOptions['enctype'])) {
93  $this->htmlOptions['enctype'] = 'multipart/form-data';
94  }
95 
96  parent::init();
97  }
98 
102  public function run()
103  {
104 
105  list($name, $id) = $this->resolveNameID();
106 
107  $this->htmlOptions['id'] = $this->id.'-'.($this->hasModel() ? get_class($this->model) : 'fileupload') . '-form';
108 
109  $this->options['url'] = $this->url;
110 
111  // if acceptFileTypes is not set as option, try getting it from models rules
112  if (!isset($this->options['acceptFileTypes'])) {
113  $fileTypes = $this->getFileValidatorProperty($this->model, $this->attribute, 'types');
114  if (isset($fileTypes)) {
115  $fileTypes = (preg_match(':jpg:', $fileTypes) && !preg_match(':jpe:', $fileTypes) ? preg_replace(
116  ':jpg:',
117  'jpe?g',
118  $fileTypes
119  ) : $fileTypes);
120  $this->options['acceptFileTypes'] = 'js:/(\.)(' . preg_replace(':,:', '|', $fileTypes) . ')$/i';
121  }
122  }
123 
124  // if maxFileSize is not set as option, try getting it from models rules
125  if (!isset($this->options['maxFileSize'])) {
126  $fileSize = $this->getFileValidatorProperty($this->model, $this->attribute, 'maxSize');
127  if (isset($fileSize)) {
128  $this->options['maxFileSize'] = $fileSize;
129  }
130  }
131 
132  if ($this->multiple) {
133  $this->htmlOptions["multiple"] = true;
134  }
135 
136  $this->render($this->uploadView);
137  $this->render($this->downloadView);
138  $this->render($this->formView, array('name' => $name, 'htmlOptions' => $this->htmlOptions));
139 
140  if ($this->previewImages || $this->imageProcessing) {
141  $this->render($this->previewImagesView);
142  }
143 
144  $this->registerClientScript($this->htmlOptions['id']);
145  }
146 
152  public function registerClientScript($id)
153  {
154  $booster = Booster::getBooster();
155  $booster->registerAssetCss('fileupload/jquery.fileupload-ui.css');
156 
157  // Upgrade widget factory
158  // @todo remove when jquery.ui 1.9+ is fully integrated into stable Yii versions
159  $booster->registerAssetJs('fileupload/vendor/jquery.ui.widget.js');
160  //The Templates plugin is included to render the upload/download listings
161  $booster->registerAssetJs("fileupload/tmpl.min.js", CClientScript::POS_END);
162 
163  if ($this->previewImages || $this->imageProcessing) {
164  $booster->registerAssetJs("fileupload/load-image.min.js", CClientScript::POS_END);
165  $booster->registerAssetJs("fileupload/canvas-to-blob.min.js", CClientScript::POS_END);
166  // gallery :) and one smile from me ;)
167  $booster->registerAssetCss("bootstrap-image-gallery.min.css");
168  $booster->registerAssetJs("bootstrap-image-gallery.min.js", CClientScript::POS_END);
169  }
170  //The Iframe Transport is required for browsers without support for XHR file uploads
171  $booster->registerAssetJs('fileupload/jquery.iframe-transport.js');
172  $booster->registerAssetJs('fileupload/jquery.fileupload.js');
173  // The File Upload image processing plugin
174  if ($this->imageProcessing) {
175  $booster->registerAssetJs('fileupload/jquery.fileupload-ip.js');
176  }
177  // The File Upload file processing plugin
178  if ($this->previewImages) {
179  $booster->registerAssetJs('fileupload/jquery.fileupload-fp.js');
180  }
181  // locale
182  $booster->registerAssetJs('fileupload/jquery.fileupload-locale.js');
183  //The File Upload user interface plugin
184  $booster->registerAssetJs('fileupload/jquery.fileupload-ui.js');
185 
186  $options = CJavaScript::encode($this->options);
187  Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').fileupload({$options});");
188  }
189 
199  private function getFileValidatorProperty($model = null, $attribute = null, $property = null)
200  {
201  if (!isset($model, $attribute, $property)) {
202  return null;
203  }
204 
205  foreach ($model->getValidators($attribute) as $validator) {
206  if ($validator instanceof CFileValidator) {
207  $ret = $validator->$property;
208  }
209  }
210  return isset($ret) ? $ret : null;
211  }
212 }