YiiWheels
  • Package
  • Class
  • Tree

Packages

  • None
  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • fileupload
      • fileuploader
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhBasicFileUpload
  • WhFileUpload
  1 <?php
  2 /**
  3  * WhFileUpload widget class
  4  *
  5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  6  * @copyright Copyright &copy; 2amigos.us 2013-
  7  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  8  * @package YiiWheels.widgets.fileupload
  9  * @uses YiiStrap.helpers.TbArray
 10  */
 11 Yii::import('bootstrap.helpers.TbArray');
 12 Yii::import('zii.widgets.jui.CJuiInputWidget');
 13 
 14 class WhFileUpload extends CJuiInputWidget
 15 {
 16     /**
 17      * the url to the upload handler
 18      * @var string
 19      */
 20     public $url;
 21 
 22     /**
 23      * set to true to use multiple file upload
 24      * @var boolean
 25      */
 26     public $multiple = true;
 27 
 28     /**
 29      * The upload template id to display files available for upload
 30      * defaults to null, meaning using the built-in template
 31      */
 32     public $uploadTemplate;
 33 
 34     /**
 35      * The template id to display files available for download
 36      * defaults to null, meaning using the built-in template
 37      */
 38     public $downloadTemplate;
 39 
 40     /**
 41      * Wheter or not to preview image files before upload
 42      */
 43     public $previewImages = true;
 44 
 45     /**
 46      * Wheter or not to add the image processing pluging
 47      */
 48     public $imageProcessing = true;
 49 
 50     /**
 51      * @var string name of the form view to be rendered
 52      */
 53     public $formView = 'yiiwheels.widgets.fileupload.views.form';
 54 
 55     /**
 56      * @var string name of the upload view to be rendered
 57      */
 58     public $uploadView = 'yiiwheels.widgets.fileupload.views.upload';
 59 
 60     /**
 61      * @var string name of the download view to be rendered
 62      */
 63     public $downloadView = 'yiiwheels.widgets.fileupload.views.download';
 64 
 65     /**
 66      * @var string name of the view to display images at bootstrap-slideshow
 67      */
 68     public $previewImagesView = 'yiiwheels.widgets.fileupload.views.gallery';
 69 
 70     /**
 71      * Widget initialization
 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      * Generates the required HTML and Javascript
 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      * Registers and publishes required scripts
122      */
123     public function registerClientScript()
124     {
125 
126         /* publish assets dir */
127         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
128         $assetsUrl = $this->getAssetsUrl($path);
129 
130         /* @var $cs CClientScript */
131         $cs = Yii::app()->getClientScript();
132 
133         $cs->registerCssFile($assetsUrl . '/css/jquery.fileupload-ui.css');
134 
135         // Upgrade widget factory
136         // @todo remove when jquery.ui 1.9+ is fully integrated into stable Yii versions
137         $cs->registerScriptFile($assetsUrl . '/js/vendor/jquery.ui.widget.js', CClientScript::POS_END);
138 
139         //The Templates plugin is included to render the upload/download listings
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             // gallery :)
146             $this->getYiiWheels()->registerAssetCss("bootstrap-image-gallery.min.css");
147             $this->getYiiWheels()->registerAssetJs("bootstrap-image-gallery.min.js", CClientScript::POS_END);
148         }
149         //The Iframe Transport is required for browsers without support for XHR file uploads
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         // The File Upload image processing plugin
154         if ($this->imageProcessing) {
155             $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-ip.js', CClientScript::POS_END);
156         }
157         // The File Upload file processing plugin
158         if ($this->previewImages) {
159             $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-fp.js', CClientScript::POS_END);
160         }
161         // locale
162         $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-locale.js', CClientScript::POS_END);
163 
164         //The File Upload user interface plugin
165         $cs->registerScriptFile($assetsUrl . '/js/jquery.fileupload-ui.js', CClientScript::POS_END);
166 
167         /* initialize plugin */
168         $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
169 
170         $this->getApi()->registerPlugin('fileupload', $selector, $this->options);
171     }
172 
173 }
174 
YiiWheels API documentation generated by ApiGen 2.8.0