1 | <?php |
||||
2 | /** |
||||
3 | * Created by PhpStorm. |
||||
4 | * User: floor12 |
||||
5 | * Date: 31.12.2017 |
||||
6 | * Time: 16:00 |
||||
7 | */ |
||||
8 | |||||
9 | namespace floor12\files\components; |
||||
10 | |||||
11 | use floor12\files\assets\FileInputWidgetAsset; |
||||
12 | use floor12\files\logic\ClassnameEncoder; |
||||
13 | use Yii; |
||||
14 | use yii\helpers\Url; |
||||
15 | use yii\jui\InputWidget; |
||||
16 | use yii\web\View; |
||||
17 | |||||
18 | |||||
19 | class FileInputWidget extends InputWidget |
||||
20 | { |
||||
21 | const MODE_SINGLE = 0; |
||||
22 | const MODE_MULTI = 1; |
||||
23 | |||||
24 | const VIEW_SINGLE = 'singleFileInputWidget'; |
||||
25 | const VIEW_MULTI = 'multiFileInputWidget'; |
||||
26 | |||||
27 | public $uploadButtonText; |
||||
28 | public $cropperHideCancel = 'false'; |
||||
29 | public $uploadButtonClass = "btn btn-default btn-sm btn-upload"; |
||||
30 | |||||
31 | private $block_id; |
||||
32 | private $layout = self::VIEW_SINGLE; |
||||
33 | private $ratio; |
||||
34 | |||||
35 | public function init() |
||||
36 | { |
||||
37 | $this->registerTranslations(); |
||||
38 | $this->block_id = rand(9999999, 999999999); |
||||
39 | |||||
40 | if (!$this->uploadButtonText) |
||||
41 | $this->uploadButtonText = Yii::t('files', 'Upload'); |
||||
42 | |||||
43 | $this->ratio = $this->model->getBehavior('files')->attributes[$this->attribute]['ratio'] ?? null; |
||||
44 | |||||
45 | if ( |
||||
46 | isset($this->model->behaviors['files']->attributes[$this->attribute]['validator']) && |
||||
47 | isset($this->model->behaviors['files']->attributes[$this->attribute]['validator']['yii\validators\FileValidator']) && |
||||
48 | $this->model->behaviors['files']->attributes[$this->attribute]['validator']['yii\validators\FileValidator']->maxFiles > 1 |
||||
49 | ) { |
||||
50 | $mode = self::MODE_MULTI; |
||||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||
51 | $this->layout = self::VIEW_MULTI; |
||||
52 | } |
||||
53 | |||||
54 | parent::init(); |
||||
55 | } |
||||
56 | |||||
57 | public function registerTranslations() |
||||
58 | { |
||||
59 | $i18n = Yii::$app->i18n; |
||||
60 | $i18n->translations['files'] = [ |
||||
61 | 'class' => 'yii\i18n\PhpMessageSource', |
||||
62 | 'sourceLanguage' => 'en-US', |
||||
63 | 'basePath' => '@vendor/floor12/yii2-module-files/src/messages', |
||||
64 | ]; |
||||
65 | } |
||||
66 | |||||
67 | public function run() |
||||
68 | { |
||||
69 | |||||
70 | $uploadRoute = Url::toRoute(['/files/default/upload']); |
||||
71 | $deleteRoute = Url::toRoute(['/files/default/delete']); |
||||
72 | $cropperRoute = Url::toRoute(['/files/default/cropper']); |
||||
73 | $cropRoute = Url::toRoute(['/files/default/crop']); |
||||
74 | $renameRoute = Url::toRoute(['/files/default/rename']); |
||||
75 | $altRoute = Url::toRoute(['/files/default/alt']); |
||||
76 | |||||
77 | $className = new ClassnameEncoder($this->model->classname()); |
||||
0 ignored issues
–
show
The function
yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
78 | |||||
79 | $this->getView()->registerJs("Yii2FilesUploaderSet('files-widget-block_{$this->block_id}','{$className}','{$this->attribute}','{$this->model->scenario}','{$this->name}')", View::POS_READY, $this->block_id); |
||||
80 | $this->getView()->registerJs("yii2UploadRoute = '{$uploadRoute}'", View::POS_BEGIN, 'yii2UploadRoute'); |
||||
81 | $this->getView()->registerJs("yii2CsrfParam = '" . Yii::$app->request->csrfParam . "'", View::POS_BEGIN, 'yii2CsrfFieldName'); |
||||
82 | $this->getView()->registerJs("yii2DeleteRoute = '{$deleteRoute}'", View::POS_BEGIN, 'yii2DeleteRoute'); |
||||
83 | $this->getView()->registerJs("yii2CropperRoute = '{$cropperRoute}'", View::POS_BEGIN, 'yii2DeleteRoute'); |
||||
84 | $this->getView()->registerJs("yii2CropRoute = '{$cropRoute}'", View::POS_BEGIN, 'yii2CropRoute'); |
||||
85 | $this->getView()->registerJs("yii2RenameRoute = '{$renameRoute}'", View::POS_BEGIN, 'yii2RenameRoute'); |
||||
86 | $this->getView()->registerJs("yii2AltRoute = '{$altRoute}'", View::POS_BEGIN, 'yii2AltRoute'); |
||||
87 | $this->getView()->registerJs("yii2FileFormToken = '" . self::generateToken() . "'", View::POS_BEGIN, 'yii2FileFormToken'); |
||||
88 | $this->getView()->registerJs("FileUploadedText = '" . Yii::t('files', 'The file is uploaded') . "'", View::POS_BEGIN, 'FileUploadedText'); |
||||
89 | $this->getView()->registerJs("FileSavedText = '" . Yii::t('files', 'The file is saved') . "'", View::POS_BEGIN, 'FileSavedText'); |
||||
90 | $this->getView()->registerJs("FileRemovedText = '" . Yii::t('files', 'The file is removed') . "'", View::POS_BEGIN, 'FileRemovedText'); |
||||
91 | $this->getView()->registerJs("FilesRemovedText = '" . Yii::t('files', 'The files are removed') . "'", View::POS_BEGIN, 'FilesRemovedText'); |
||||
92 | $this->getView()->registerJs("FileRenamedText = '" . Yii::t('files', 'The file is renamed') . "'", View::POS_BEGIN, 'FileRenamedText'); |
||||
93 | $this->getView()->registerJs("cropperHideCancel = '{$this->cropperHideCancel}'", View::POS_BEGIN, 'cropperHideCancel'); |
||||
94 | |||||
95 | FileInputWidgetAsset::register($this->getView()); |
||||
96 | |||||
97 | return $this->render($this->layout, [ |
||||
98 | 'className' => $this->model->classname(), |
||||
0 ignored issues
–
show
The function
yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
99 | 'uploadButtonText' => $this->uploadButtonText, |
||||
100 | 'uploadButtonClass' => $this->uploadButtonClass, |
||||
101 | 'block_id' => $this->block_id, |
||||
102 | 'scenario' => $this->model->scenario, |
||||
103 | 'attribute' => $this->attribute, |
||||
104 | 'model' => $this->model, |
||||
105 | 'ratio' => $this->ratio, |
||||
106 | 'name' => $this->name, |
||||
107 | 'value' => $this->value |
||||
108 | ]); |
||||
109 | } |
||||
110 | |||||
111 | /** Генератор токена защиты форм |
||||
112 | * @return string |
||||
113 | */ |
||||
114 | public static function generateToken() |
||||
115 | { |
||||
116 | return md5(Yii::$app->getModule('files')->token_salt . Yii::$app->request->userAgent . Yii::$app->name); |
||||
0 ignored issues
–
show
Are you sure
Yii::app->getModule('files')->token_salt of type mixed|null|object can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
117 | } |
||||
118 | } |
||||
119 |