-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.php
113 lines (95 loc) · 3.37 KB
/
Widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace plathir\upload;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use Yii;
class Widget extends InputWidget {
public $uploadParameter = 'uploadfile';
public $label = '';
public $uploadUrl;
public $previewUrl;
public $KeyFolder;
public $tempPreviewUrl;
public $noPhotoImage;
public $extensions = 'jpeg, jpg, png, gif, pdf, mov, txt, pdf';
public $maxUploads = 10;
public $autoSubmit = false;
public $maxSize = 3145728; // 3MB
public $galleryType = false;
/**
* @inheritdoc
*/
public function init() {
parent::init();
self::registerTranslations();
if ($this->uploadUrl === null) {
throw new InvalidConfigException(Yii::t('upload', 'MISSING_ATTRIBUTE', ['attribute' => 'uploadUrl']));
} else {
$this->uploadUrl = rtrim(Yii::getAlias($this->uploadUrl), '/') . '/';
}
if ($this->KeyFolder) {
$this->previewUrl = $this->previewUrl . '/' . $this->KeyFolder;
}
if ($this->label == '') {
$this->label = Yii::t('upload', 'DEFAULT_LABEL');
}
$this->noPhotoImage = '';
if ($this->galleryType == true ) {
$this->extensions = 'jpeg, jpg, png, gif';
}
}
/**
* @inheritdoc
*/
public function run() {
$this->registerClientAssets();
return $this->render('widget', [
'model' => $this->model,
'widget' => $this
]);
}
/**
* Register widget asset.
*/
public function registerClientAssets() {
$view = $this->getView();
$assets = Asset::register($view);
if ($this->noPhotoImage == '') {
$this->noPhotoImage = $assets->baseUrl . '/img/file.png';
}
$settings = [
'url' => $this->uploadUrl,
'name' => $this->uploadParameter,
'previewUrl' => $this->previewUrl,
'tempPreviewUrl' => $this->tempPreviewUrl,
'allowedExtensions' => explode(', ', $this->extensions),
'autoSubmit' => $this->autoSubmit,
'maxUploads' => $this->maxUploads,
'noPhotoImage' => $this->noPhotoImage,
'maxSize' => $this->maxSize / 1024,
'allowedExtensions' => explode(', ', $this->extensions),
'size_error_text' => Yii::t('upload', 'TOO_BIG_ERROR', ['size' => $this->maxSize / (1024 * 1024)]),
'ext_error_text' => Yii::t('upload', 'EXTENSION_ERROR', ['formats' => $this->extensions]),
'galleryType' => $this->galleryType,
];
$view->registerJs(
'jQuery("#' . $this->options['id'] . '").siblings(".upload_box").uploader(' . Json::encode($settings) . ');', $view::POS_READY
);
}
/**
* Register widget translations.
*/
public static function registerTranslations() {
if (!isset(Yii::$app->i18n->translations['upload']) && !isset(Yii::$app->i18n->translations['upload/*'])) {
Yii::$app->i18n->translations['upload'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@plathir/upload/messages',
'forceTranslation' => true,
'fileMap' => [
'upload' => 'upload.php'
]
];
}
}
}