forked from ezze/ezze-elfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElFinderWidget.php
201 lines (177 loc) · 6.06 KB
/
ElFinderWidget.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
Yii::import("ext.ezzeelfinder.ElFinderConnectorAction");
/**
* A widget to integrate ElFinder uploader.
*
* @author Dmitriy Pushkov <ezze@ezze.org>
* @version 0.0.5
*/
class ElFinderWidget extends CWidget
{
/**
* jQuery selector for a container to place ElFinder in.
*
* @var string
*/
public $selector = "#elfinder";
/**
* ElFinder client's configuration options as described here:
* https://github.com/Studio-42/elFinder/wiki/Client-configuration-options
*
* Please note that these options must be passed as an array and will be
* converted to JSON object automatically.
*
* @var array
*/
public $clientOptions = array();
/**
* A route to ElFinder connector's action.
*
* This action must be implemented as a reference to {@link ElFinderConnectorAction} class.
*
* @var string
*/
public $connectorRoute = null;
/**
* ElFinder connector's configuration options as described here:
* https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options
*
* @var array
*/
public $connectorOptions = array();
/**
* Used to store an asset URL of ElFinder's initialization script.
* This script is located in "js" directory of the extension.
*
* @var string
*/
protected $_initJsUrl = null;
/**
* Used to store an asset URL of ElFinder's scripts, images and CSS files.
* These files are located in "assets" directory of the extension.
*
* @var string
*/
protected $_elFinderAssetsUrl = null;
/**
* Used to store an asset URL of ElFinder's CSS directory.
*
* @var string
*/
protected $_elFinderCssUrl = null;
/**
* Used to store an asset URL of ElFinder's scripts directory.
*
* @var string
*/
protected $_elFinderJsUrl = null;
/**
* Used to store JSON object's string retrieved from {@link clientOptions}.
*
* @var string
*/
protected $_jsonClientOptions = null;
/**
* Retrieves an asset URL of ElFinder's initialization script.
*
* @return string
*/
public function getInitJsUrl()
{
if ($this->_initJsUrl === null)
{
$this->_initJsUrl = Yii::app()->getAssetManager()->publish(
dirname(__FILE__) . "/js/elFinderInit.js"
);
}
return $this->_initJsUrl;
}
/**
* Retrieves an asset URL of ElFinder's files directory.
*
* @return string
*/
public function getElFinderAssetsUrl()
{
if ($this->_elFinderAssetsUrl === null)
{
$this->_elFinderAssetsUrl = Yii::app()->getAssetManager()->publish(
dirname(__FILE__) . "/assets"
);
}
return $this->_elFinderAssetsUrl;
}
/**
* Retrieves an asset URL of ElFinder's CSS directory.
*
* @return string
*/
public function getElFinderCssUrl()
{
if ($this->_elFinderCssUrl === null)
$this->_elFinderCssUrl = $this->elFinderAssetsUrl . "/css";
return $this->_elFinderCssUrl;
}
/**
* Retrieves an asset URL of ElFinder's scripts directory.
*
* @return string
*/
public function getElFinderJsUrl()
{
if ($this->_elFinderJsUrl === null)
$this->_elFinderJsUrl = $this->elFinderAssetsUrl . "/js";
return $this->_elFinderJsUrl;
}
/**
* Retrieves a string representation of JSON object with ElFinder's
* client configuration options.
*
* @return string
*/
public function getJsonClientOptions()
{
if ($this->_jsonClientOptions === null)
$this->_jsonClientOptions = json_encode($this->clientOptions);
return $this->_jsonClientOptions;
}
/**
* Initializes the widget preparing an URL to ElFinder's connector.
*/
public function init()
{
parent::init();
if ($this->connectorRoute !== null && is_string($this->connectorRoute)
&& $this->connectorOptions !== null && is_array($this->connectorOptions))
{
$connectorOptionsSerialized = serialize($this->connectorOptions);
$connectorOptionsEncoded = base64_encode($connectorOptionsSerialized);
$this->clientOptions['url'] = Yii::app()->createUrl($this->connectorRoute, array(
ElFinderConnectorAction::GET_PARAM_ELFINDER_CONNECTOR_OPTIONS => $connectorOptionsEncoded
));
}
$this->registerScripts();
}
protected function registerScripts()
{
// Registering required scripts and CSS files
Yii::app()->clientScript->registerCoreScript("jquery");
Yii::app()->clientScript->registerCoreScript("jquery.ui");
Yii::app()->clientScript->registerCssFile(Yii::app()->clientScript->getCoreScriptUrl()
. "/jui/css/base/jquery-ui.css");
Yii::app()->clientScript->registerCssFile($this->elFinderCssUrl . "/elfinder.min.css");
Yii::app()->clientScript->registerScriptFile($this->elFinderJsUrl . "/elfinder.min.js");
Yii::app()->clientScript->registerCssFile($this->elFinderCssUrl . "/theme.css");
// If client's language is set then also registering language script
if (isset($this->clientOptions['lang']) && $this->clientOptions['lang'])
Yii::app()->clientScript->registerScriptFile($this->elFinderJsUrl . "/i18n/elfinder." . $this->clientOptions['lang'] . ".js");
// Registering ElFinder's initialization script
Yii::app()->clientScript->registerScriptFile($this->initJsUrl);
// Registering a script to run initialization script when page's DOM will be built
$initScript = "jQuery(document).ready(function() {
EzzeElFinder.init(\"" . $this->selector . "\", " . $this->jsonClientOptions . ")
});";
$initScriptId = "elFinderInit" . preg_replace("/[^0-9]/", "", microtime(false)) . rand();
Yii::app()->clientScript->registerScript($initScriptId, $initScript, CClientScript::POS_READY);
}
}