/
SelectUploadField.php
207 lines (180 loc) · 5.26 KB
/
SelectUploadField.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
202
203
204
205
206
207
<?php
/**
* A composite form field which allows users to select a folder into which files may be uploaded
*
* @package framework
* @subpackage forms
*/
class SelectUploadField extends UploadField {
private static $casting = array(
'DefaultFolderName' => 'Text',
'FolderName' => 'Text',
'DisplayFolderName' => 'Text'
);
private static $url_handlers = array(
'folder/tree/$ID' => 'tree'
);
private static $allowed_actions = array(
'tree'
);
/**
* List of templates for which to disable folder selection.
*
* @config
* @var array
*/
private static $disable_for_templates = array(
'AssetUploadField' // Disable folder selection if this field is used in the AssetAdmin
);
/**
* Set default permission for selecting folders
*
* @var array
* @config
*/
private static $defaultConfig = array(
'canSelectFolder' => true
);
/**
* Folder selector field
*
* @var FolderDropdownField
*/
protected $selectField;
public function __construct($name, $title = null, \SS_List $items = null) {
parent::__construct($name, $title, $items);
$this->addExtraClass('ss-selectupload'); // class, used by js
$this->addExtraClass('ss-selectuploadfield'); // class, used by css for selectuploadfield onl
$this->selectField = FolderDropdownField::create("{$name}/folder")
->addExtraClass('FolderSelector')
->setTitle('Select a folder to upload into');
// If we haven't uploaded to a folder yet, set to the default foldername
if(!$this->selectField->Value()) {
$folderID = $this->folderIDFromPath($this->getDefaultFolderName());
if($folderID) $this->selectField->setValue($folderID);
}
}
public function Field($properties = array()) {
$field = parent::Field($properties);
// Extra requirements
$base = basename(dirname(__DIR__));
Requirements::javascript("{$base}/javascript/SelectUploadField.js");
Requirements::css("{$base}/css/SelectUploadField.css");
return $field;
}
/**
* Get the folder selector field
*
* @return FolderDropdownField
*/
public function FolderSelector() {
return $this->selectField;
}
/**
* Return the subtree for a selected folder id
*
* @param SS_HTTPRequest $request
* @return string HTTP Response
*/
public function tree($request) {
return $this->FolderSelector()->tree($request);
}
public function setForm($form) {
$this->selectField->setForm($form);
return parent::setForm($form);
}
public function Type() {
return 'selectupload upload';
}
/**
* Given a request, ensure that the current field is aware of the selected folder
*
* @param SS_HTTPRequest $request
*/
protected function updateFolderName($request) {
// check if allowed to select folder
if(!$this->canSelectFolder()) return;
// Get path from upload
$folderID = $request->requestVar("{$this->Name}/folder");
$path = $this->folderPathFromID($folderID);
if($path !== false) {
$this->setFolderName($path);
$this->selectField->setValue($folderID);
}
}
/**
* Get path of a folder relative to /assets/ by id.
* This will be a format appropriate for setting setFolderName to
*
* @param int $folderID
* @return string|bool Relative path to the assets directory, or false if not found
*/
protected function folderPathFromID($folderID) {
if(empty($folderID)) return false;
$folder = Folder::get()->byID($folderID);
if(!$folder) return false;
// Translate path
$path = $folder->getFilename();
if(stripos($path, ASSETS_DIR) === 0) {
$path = substr($path, strlen(ASSETS_DIR) + 1);
}
return $path;
}
/**
* Gets the ID of a folder given a path relative to /assets/.
*
* @param string $path
* @return int Folder ID
*/
protected function folderIDFromPath($path) {
$folder = File::find($path);
if($folder) return $folder->ID;
}
public function handleRequest(\SS_HTTPRequest $request, \DataModel $model) {
$this->updateFolderName($request);
return parent::handleRequest($request, $model);
}
/**
* Specify whether the user can select an upload folder.
* String values will be treated as required permission codes
*
* @param boolean|string $canSelectFolder Either a boolean flag, or a required
* permission code
* @return self Self reference
*/
public function setCanSelectFolder($canSelectFolder) {
return $this->setConfig('canSelectFolder', $canSelectFolder);
}
/**
* Determine if the current member is allowed to change the folder
*
* @return boolean
*/
public function canSelectFolder() {
if(!$this->isActive()) return false;
if($this->template && in_array($this->template, self::config()->disable_for_templates)) return false;
// Check config
$can = $this->getConfig('canSelectFolder');
return (is_bool($can)) ? $can : Permission::check($can);
}
public function getFolderName() {
// Ensure that, if this member is allowed, the persistant folder overrides any default set
if($this->canSelectFolder()) {
$path = $this->folderPathFromID($this->selectField->Value());
if($path !== false) return $path;
}
return $this->getDefaultFolderName();
}
/**
* Get the 'default' folder name
*
* @return string
*/
public function getDefaultFolderName() {
return parent::getFolderName();
}
public function getDisplayFolderName() {
$name = $this->getFolderName();
return preg_replace('/\s*\\/\s*/', ' / ', trim($name, '/'));
}
}