/
KCFinder.php
134 lines (118 loc) · 3.53 KB
/
KCFinder.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
<?php
namespace bastarann\yii2kcfinder;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
/**
* KCFinder
* @author Kevin LEVRON <kevin.levron@gmail.com>
*/
class KCFinder extends \yii\widgets\InputWidget
{
/**
* Multiple selection
* @var boolean
*/
public $multiple = false;
/**
* IFrame mode
* @var boolean
*/
public $iframe = true;
/**
* KCFinder dynamic settings (using session)
* @link http://kcfinder.sunhater.com/install#dynamic
* @var array
*/
public $kcfOptions = [];
/**
* KCFinder input parameters
* @link http://kcfinder.sunhater.com/integrate#input
* @var array
*/
public $kcfBrowseOptions = [];
/**
* KCFinder client options
* @var array
*/
public $clientOptions = [];
/**
* KCFinder default dynamic settings
* @link http://kcfinder.sunhater.com/install#dynamic
* @var array
*/
public static $kcfDefaultOptions = [
'disabled' => false,
'denyZipDownload' => true,
'denyUpdateCheck' => true,
'denyExtensionRename' => true,
'theme' => 'default',
'access' => [ // @link http://kcfinder.sunhater.com/install#_access
'files' => [
'upload' => false,
'delete' => false,
'copy' => false,
'move' => false,
'rename' => false,
],
'dirs' => [
'create' => false,
'delete' => false,
'rename' => false,
],
],
'types' => [ // @link http://kcfinder.sunhater.com/install#_types
'files' => [
'type' => '',
],
],
'thumbsDir' => '.thumbs',
'thumbWidth' => 100,
'thumbHeight' => 100,
];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->kcfOptions['uploadURL'] = ArrayHelper::getValue($this->kcfOptions, 'uploadURL', '@web/upload');
$this->kcfOptions['uploadDir'] = ArrayHelper::getValue($this->kcfOptions, 'uploadDir', '@app/web/upload');
$this->kcfOptions['uploadURL'] = Yii::getAlias($this->kcfOptions['uploadURL']);
$this->kcfOptions['uploadDir'] = Yii::getAlias($this->kcfOptions['uploadDir']);
$this->kcfOptions = array_merge(self::$kcfDefaultOptions, $this->kcfOptions);
Yii::$app->session['KCFINDER'] = $this->kcfOptions;
$this->clientOptions['browseOptions'] = $this->kcfBrowseOptions;
$this->clientOptions['uploadURL'] = $this->kcfOptions['uploadURL'];
$this->clientOptions['multiple'] = $this->multiple;
$this->clientOptions['iframe'] = $this->iframe;
$this->clientOptions['inputName'] = $this->getInputName();
}
/**
* @param type $path
* @return string thumb src
*/
public function getThumbSrc($path)
{
$path = str_replace('%', '%25', $path);
return str_replace(
$this->kcfOptions['uploadURL'],
$this->kcfOptions['uploadURL'].'/'.$this->kcfOptions['thumbsDir'],
$path);
}
/**
* @return string input name
*/
public function getInputName()
{
if ($this->hasModel()) {
$inputName = Html::getInputName($this->model, $this->attribute);
$inputName.= $this->multiple ? '[]' : '';
return $inputName;
} else {
return $this->name;
}
}
}