-
Notifications
You must be signed in to change notification settings - Fork 985
/
FilesController.php
163 lines (150 loc) · 4.94 KB
/
FilesController.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
<?php
namespace ls\controllers;
use \Yii;
use CFileHelper, CClientScript, CHtml;
use \elFinder, elFinderConnector;
class FilesController extends Controller
{
/**
* This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action.
* @param CAction $action the action to be executed.
* @return boolean whether the action should be executed.
*/
protected function beforeAction($action)
{
$result = parent::beforeAction($action);
foreach (App()->log->routes as $route) {
if ($route instanceof \CWebLogRoute) {
$route->enabled = false;
}
}
return $result;
}
/**
* Shows the file manager.
* @param bool|false $dialog
* @param null $surveyId
*/
public function actionManage($dialog = false, $context, $key)
{
$this->renderText($this->widget(\FileManagerWidget::class, [
'context' => $context,
'key' => $key
], true));
}
/**
* The browse function used by elFinder.
*/
public function actionBrowse($context, $key) {
switch ($context) {
case 'survey':
$roots = [$this->getRootForSurvey($key)];
break;
case 'template':
$roots = $this->getRootForTemplate($key);
break;
default:
throw new \CHttpException(404, "Unknown context");
}
$finder = new elFinder([
'debug' => true,
'roots' => $roots,
'mimeDetect' => 'internal',
]);
$connector = new elFinderConnector($finder, false);
$connector->run();
}
/**
* @param int $surveyId
* @return array
* @todo Add permission check and set appropriate rights.
*/
protected function getRootForSurvey($surveyId) {
$relative = "/upload/surveys/$surveyId";
$dir = Yii::getPathOfAlias('publicPath') . $relative;
if (!is_dir($dir)) {
mkdir($dir);
}
return [
'alias' => "ls\models\Survey ({$surveyId})",
'driver' => 'LocalFileSystem',
'autoload' => true,
'path' => $dir,
'URL' => App()->publicUrl . $relative,
'icon' => "{$this->getAssetsUrl()}/img/volume_icon_local.png",
'accessControl' => 'access',
'attributes' => [
[
// hide anything else
'pattern' => '!^/\..*$!',
'hidden' => true
]
]
];
}
/**
* @param int $surveyId
* @return array
* @todo Add permission check and set appropriate rights.
*/
protected function getRootForTemplate($template) {
$result = [];
if (true || !\ls\models\Template::isStandardTemplate($template)) {
$dir = \ls\models\Template::getTemplatePath($template);
$url = \ls\models\Template::getTemplateURL($template);
$result[] = [
'alias' => "ls\models\Template ({$template})",
'driver' => 'LocalFileSystem',
'autoload' => true,
'path' => $dir,
'URL' => App()->baseUrl . $url,
'mimeMap' => [
"pstpl:text/plain" => "text/html",
"css:text/plain" => "text/css"
],
'icon' => "{$this->getAssetsUrl()}/img/volume_icon_local.png",
'accessControl' => 'access',
'attributes' => [
[
// hide anything else
'pattern' => '!^/\..*$!',
'hidden' => true
]
]
];
}
return $result;
}
protected function getAssetsUrl() {
$dir = __DIR__ . '/../vendor/studio-42/elfinder/';
$url = App()->assetManager->getPublishedUrl($dir);
return $url;
}
protected function getRoots()
{
$result = [];
// Get accessible surveys.
foreach (\ls\models\Survey::model()->accessible()->findAll() as $survey) {
$result[] = $this->getRootForSurvey($survey->primaryKey);
}
// vdd($result);
// $result[] = [
// 'alias' => "All surveys",
// 'driver' => 'LocalFileSystem',
//// 'path' => Yii::getPathOfAlias('webroot'),
//// 'URL' => '/files'
// 'path' => Yii::getPathOfAlias('webroot') . "/upload/surveys",
// 'URL' => App()->baseUrl . "/upload/surveys",
// 'attributes' => [
// [
// // hide anything else
// 'pattern' => '!^/\..*$!',
// 'hidden' => true
// ]
// ]
// ];
// vdd($result);
return $result;
}
}