forked from iutbay/yii2-kcfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCFinderInputWidget.php
139 lines (114 loc) · 3.65 KB
/
KCFinderInputWidget.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace iutbay\yii2kcfinder;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use yii\widgets\InputWidget;
use yii\bootstrap\Modal;
use iutbay\yii2fontawesome\FontAwesome;
/**
* KCFinder Input Widget.
* @author Kevin LEVRON <[email protected]>
*/
class KCFinderInputWidget extends KCFinder
{
/**
* Button label
* @var string
*/
public $buttonLabel = 'Add Media';
/**
* Button options
* @var array
*/
public $buttonOptions = [];
/**
* Modal title
* @var string
*/
public $modalTitle = 'Media Manager';
/**
* Main template
* @var array
*/
public $template = '{button}{thumbs}';
/**
* Thumb template
* @var array
*/
public $thumbTemplate = '<li class="sortable"><div class="remove"><span class="fa fa-trash"></span></div><img src="{thumbSrc}" /><input type="hidden" name="{inputName}" value="{inputValue}"></li>';
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->clientOptions['thumbsDir'] = $this->kcfOptions['thumbsDir'];
$this->clientOptions['thumbsSelector'] = '#' . $this->getThumbsId();
$this->clientOptions['thumbTemplate'] = $this->thumbTemplate;
$this->buttonOptions['id'] = $this->getButtonId();
Html::addCssClass($this->options, 'form-control');
Html::addCssClass($this->buttonOptions, 'btn btn-default');
}
/**
* Renders the widget.
*/
public function run()
{
$this->registerClientScript();
$button = Html::button(FontAwesome::icon('picture-o') . ' ' . $this->buttonLabel, $this->buttonOptions);
if ($this->iframe) {
$button.= Modal::widget([
'id' => $this->getIFrameModalId(),
'header' => Html::tag('h4', $this->modalTitle, ['class' => 'modal-title']),
'size' => Modal::SIZE_LARGE,
'options' => [
'class' => 'kcfinder-modal',
],
]);
}
$thumbs = '';
if ($this->hasModel() && is_array($this->model->{$this->attribute})) {
$images = $this->model->{$this->attribute};
foreach ($images as $path) {
$thumbs.= strtr($this->thumbTemplate, [
'{thumbSrc}' => $this->getThumbSrc($path),
'{inputName}' => $this->getInputName(),
'{inputValue}' => $path,
]);
}
}
$thumbs = Html::tag('ul', $thumbs, ['id' => $this->getThumbsId(), 'class' => 'kcf-thumbs']);
echo Html::tag('div', strtr($this->template, [
'{button}' => $button,
'{thumbs}' => $thumbs,
]), ['class' => 'kcf-input-group']);
}
/**
* Registers the needed JavaScript.
*/
public function registerClientScript()
{
$view = $this->getView();
KCFinderWidgetAsset::register($view);
$this->clientOptions['kcfUrl'] = Yii::$app->assetManager->getPublishedUrl((new KCFinderAsset)->sourcePath);
if ($this->iframe) {
$this->clientOptions['iframeModalId'] = $this->getIFrameModalId();
}
$clientOptions = Json::encode($this->clientOptions);
$view->registerJs("jQuery('#{$this->buttonOptions['id']}').KCFinderInputWidget($clientOptions)");
}
public function getButtonId()
{
return $this->getId() . '-button';
}
public function getThumbsId()
{
return $this->getId() . '-thumbs';
}
public function getIFrameModalId()
{
return $this->getId() . '-iframe';
}
}