|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @link https://github.com/rkit/fileapi-yii2 |
| 5 | + * @copyright Copyright (c) 2015 Igor Romanov |
| 6 | + * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
| 7 | + */ |
| 8 | + |
| 9 | +namespace rkit\fileapi; |
| 10 | + |
| 11 | +use yii\helpers\Html; |
| 12 | +use yii\helpers\Json; |
| 13 | +use yii\helpers\ArrayHelper; |
| 14 | +use yii\web\JsExpression; |
| 15 | +use yii\widgets\InputWidget; |
| 16 | +use Yii; |
| 17 | + |
| 18 | +/** |
| 19 | + * FileApi |
| 20 | + * Widget for https://github.com/RubaXa/jquery.fileapi/ |
| 21 | + */ |
| 22 | +class Widget extends InputWidget |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var string FileAPI selector |
| 26 | + */ |
| 27 | + public $selector; |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + public $inputName = 'file'; |
| 32 | + /** |
| 33 | + * @var array {@link https://github.com/RubaXa/jquery.fileapi} |
| 34 | + */ |
| 35 | + public $settings = []; |
| 36 | + /** |
| 37 | + * @var string Path to template |
| 38 | + */ |
| 39 | + public $template; |
| 40 | + /** |
| 41 | + * @var array {@link https://github.com/RubaXa/jquery.fileapi/#events} |
| 42 | + */ |
| 43 | + public $callbacks = []; |
| 44 | + /** |
| 45 | + * @var boolean |
| 46 | + */ |
| 47 | + public $preview = true; |
| 48 | + /** |
| 49 | + * @var boolean |
| 50 | + */ |
| 51 | + public $crop = false; |
| 52 | + /** |
| 53 | + * @var array {@link https://github.com/RubaXa/jquery.fileapi/#elementsobject} |
| 54 | + */ |
| 55 | + private $defaultSettings = [ |
| 56 | + 'autoUpload' => true, |
| 57 | + 'elements' => [ |
| 58 | + 'progress' => '[data-fileapi="progress"]', |
| 59 | + 'active' => [ |
| 60 | + 'show' => '[data-fileapi="active.show"]', |
| 61 | + 'hide' => '[data-fileapi="active.hide"]' |
| 62 | + ], |
| 63 | + 'name' => '[data-fileapi="name"]', |
| 64 | + 'preview' => [ |
| 65 | + 'el' => '[data-fileapi="preview"]', |
| 66 | + 'width' => 200, |
| 67 | + 'height' => 200, |
| 68 | + 'keepAspectRatio' => true |
| 69 | + ], |
| 70 | + 'dnd' => [ |
| 71 | + 'el' => '.fileapi-dnd', |
| 72 | + 'hover' => '.fileapi-dnd-active' |
| 73 | + ] |
| 74 | + ] |
| 75 | + ]; |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritdoc |
| 79 | + */ |
| 80 | + public function init() |
| 81 | + { |
| 82 | + parent::init(); |
| 83 | + |
| 84 | + $request = Yii::$app->getRequest(); |
| 85 | + |
| 86 | + if ($request->enableCsrfValidation === true) { |
| 87 | + $this->settings['data'][$request->csrfParam] = $request->getCsrfToken(); |
| 88 | + } |
| 89 | + |
| 90 | + if (!isset($this->settings['url'])) { |
| 91 | + $this->settings['url'] = $request->getUrl(); |
| 92 | + } |
| 93 | + |
| 94 | + if ($this->crop === true) { |
| 95 | + $this->settings['autoUpload'] = false; |
| 96 | + } |
| 97 | + |
| 98 | + $this->settings = ArrayHelper::merge($this->defaultSettings, $this->settings); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @inheritdoc |
| 103 | + */ |
| 104 | + public function run() |
| 105 | + { |
| 106 | + $this->registerScripts(); |
| 107 | + |
| 108 | + if ($this->hasModel()) { |
| 109 | + $input = Html::activeHiddenInput($this->model, $this->attribute, $this->options); |
| 110 | + } else { |
| 111 | + $input = Html::hiddenInput($this->name, $this->value, $this->options); |
| 112 | + } |
| 113 | + |
| 114 | + return $this->render( |
| 115 | + $this->template, |
| 116 | + [ |
| 117 | + 'selector' => $this->getSelector(), |
| 118 | + 'input' => $input, |
| 119 | + 'inputName' => $this->inputName, |
| 120 | + 'value' => $this->hasModel() ? $this->model->{$this->attribute} : $this->value, |
| 121 | + 'preview' => $this->preview, |
| 122 | + 'crop' => $this->crop, |
| 123 | + 'model' => $this->model, |
| 124 | + 'attribute' => $this->attribute |
| 125 | + ] |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + public function getSelector() |
| 133 | + { |
| 134 | + return $this->selector !== null ? $this->selector : 'fileapi-' . $this->options['id']; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Register scripts |
| 139 | + */ |
| 140 | + public function registerScripts() |
| 141 | + { |
| 142 | + $view = $this->getView(); |
| 143 | + |
| 144 | + Asset::register($view); |
| 145 | + if ($this->crop === true) { |
| 146 | + CropAsset::register($view); |
| 147 | + } |
| 148 | + |
| 149 | + $view->registerJs( |
| 150 | + 'jQuery(\'#' . $this->getSelector() . '\').fileapi(' . Json::encode($this->settings) .');' |
| 151 | + ); |
| 152 | + |
| 153 | + $this->registerCallbacks(); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Register widget callbacks |
| 158 | + */ |
| 159 | + protected function registerCallbacks() |
| 160 | + { |
| 161 | + if (!empty($this->callbacks)) { |
| 162 | + $selector = $this->getSelector(); |
| 163 | + $view = $this->getView(); |
| 164 | + foreach ($this->callbacks as $event => $callback) { |
| 165 | + if (is_array($callback)) { |
| 166 | + foreach ($callback as $function) { |
| 167 | + if (!$function instanceof JsExpression) { |
| 168 | + $function = new JsExpression($function); |
| 169 | + } |
| 170 | + $view->registerJs("jQuery('#$selector').on('$event', $function);"); |
| 171 | + } |
| 172 | + } else { |
| 173 | + if (!$callback instanceof JsExpression) { |
| 174 | + $callback = new JsExpression($callback); |
| 175 | + } |
| 176 | + $view->registerJs("jQuery('#$selector').on('$event', $callback);"); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments