-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListFilesWidget.php
86 lines (71 loc) · 2.2 KB
/
ListFilesWidget.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
<?php
namespace plathir\upload;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
use yii\base\Widget;
use Yii;
class ListFilesWidget extends Widget {
public $KeyFolder;
public $previewUrl;
public $attribute;
public $model;
public $previewImages = true;
public $id;
/**
* @inheritdoc
*/
public function init() {
parent::init();
self::registerTranslations();
if ($this->model === null) {
throw new InvalidConfigException('Please specify the "model" property.');
}
if ($this->previewUrl === null) {
throw new InvalidConfigException(Yii::t('upload', 'MISSING_ATTRIBUTE', ['attribute' => 'previewUrl']));
} else {
$this->previewUrl = rtrim(Yii::getAlias($this->previewUrl), '/') . '/';
}
if ($this->KeyFolder) {
$this->previewUrl = rtrim(Yii::getAlias($this->previewUrl), '/') . '/';
$this->previewUrl = $this->previewUrl . $this->KeyFolder . '/' ;
}
}
/**
* @inheritdoc
*/
public function run() {
$this->registerClientAssets();
return $this->render('list_widget', [
'model' => $this->model,
'widget' => $this
]);
}
/**
* Register widget asset.
*/
public function registerClientAssets() {
$view = $this->getView();
$assets = Asset::register($view);
$settings = [
'preview' => $this->previewImages,
];
$view->registerJs(
'jQuery(".flist").listfiles(' . 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'
]
];
}
}
}