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

  • WhFineUploader
  1 <?php
  2 /**
  3  * WhFineUploader widget class
  4  * Inspired by https://github.com/anggiaj/EFineUploader
  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.fileuploader
  9  * @uses YiiStrap.helpers.TbArray
 10  */
 11 Yii::import('bootstrap.helpers.TbArray');
 12 
 13 class WhFineUploader extends CInputWidget
 14 {
 15     /**
 16      * @var string upload action url
 17      */
 18     public $uploadAction;
 19 
 20     /**
 21      * @var string the HTML tag to render the uploader to
 22      */
 23     public $tagName = 'div';
 24 
 25     /**
 26      * @var string text to display if javascript is disabled
 27      */
 28     public $noScriptText;
 29 
 30     /**
 31      * @var array the plugin options
 32      */
 33     public $pluginOptions = array();
 34 
 35     /**
 36      * @var array the events
 37      */
 38     public $events = array();
 39 
 40     /**
 41      * @var string which scenario we get the validation from
 42      */
 43     public $scenario;
 44 
 45     /**
 46      * @var array d
 47      */
 48     protected $defaultOptions = array();
 49 
 50     /**
 51      * @throws CException
 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      * Widget's run method
 69      */
 70     public function run()
 71     {
 72         $this->renderTag();
 73         $this->registerClientScript();
 74     }
 75 
 76     /**
 77      * Renders the tag where the button is going to be rendered
 78      */
 79     public function renderTag()
 80     {
 81         echo CHtml::tag($this->tagName, $this->htmlOptions, '<noscript>' . $this->noScriptText . '</noscript>', true);
 82     }
 83 
 84     /**
 85      * Registers required client script for finuploader
 86      */
 87     public function registerClientScript()
 88     {
 89         /* publish assets dir */
 90         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
 91         $assetsUrl = $this->getAssetsUrl($path);
 92 
 93         /* @var $cs CClientScript */
 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         /* initialize plugin */
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      * Sets up default options for the plugin
114      * - thanks https://github.com/anggiaj
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      * @return array
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             // we are just looking for the first founded CFileValidator
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 }
YiiWheels API documentation generated by ApiGen 2.8.0