/
FileIFrameField.php
executable file
·266 lines (223 loc) · 7.15 KB
/
FileIFrameField.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* A field that allows you to attach a file to a DataObject without submitting the form it is part of, through the use
* of an iframe.
*
* If all you need is a simple file upload, it is reccomended you use {@link FileField}
*
* @package forms
* @subpackage fields-files
*/
class FileIFrameField extends FileField {
protected $template = 'FileIFrameField';
public static $allowed_actions = array (
'iframe',
'EditFileForm',
'DeleteFileForm'
);
/**
* Flag that controls whether or not new files
* can be uploaded by the user from their local computer.
*
* @var boolean
*/
protected $canUploadNewFile = true;
/**
* Sets whether or not files can be uploaded into the CMS from the user's local computer
* @param boolean $can
*/
public function setCanUploadNewFile($can) {
$this->canUploadNewFile = $can;
}
/**
* The data class that this field is editing.
* @return string Class name
*/
public function dataClass() {
if($this->form && $this->form->getRecord()) {
$class = $this->form->getRecord()->has_one($this->Name());
return ($class) ? $class : 'File';
} else {
return 'File';
}
}
/**
* @return string
*/
public function Field() {
Requirements::css(SAPPHIRE_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
if($this->form->getRecord() && $this->form->getRecord()->exists()) {
$record = $this->form->getRecord();
if(class_exists('Translatable') && Object::has_extension('SiteTree', 'Translatable') && $record->Locale){
$iframe = "iframe?locale=".$record->Locale;
}else{
$iframe = "iframe";
}
return $this->createTag (
'iframe',
array (
'name' => $this->Name() . '_iframe',
'src' => Controller::join_links($this->Link(), $iframe),
'style' => 'height: 152px; width: 100%; border: none;'
)
) . $this->createTag (
'input',
array (
'type' => 'hidden',
'id' => $this->ID(),
'name' => $this->Name() . 'ID',
'value' => $this->attrValue()
)
);
}
$this->setValue(sprintf(_t (
'FileIFrameField.ATTACHONCESAVED', '%ss can be attached once you have saved the record for the first time.'
), $this->FileTypeName()));
return FormField::field();
}
/**
* Attempt to retreive a File object that has already been attached to this forms data record
*
* @return File|null
*/
public function AttachedFile() {
return $this->form->getRecord() ? $this->form->getRecord()->{$this->Name()}() : null;
}
/**
* @return string
*/
public function iframe() {
// clear the requirements added by any parent controllers
Requirements::clear();
Requirements::add_i18n_javascript('sapphire/javascript/lang');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript('sapphire/javascript/FileIFrameField.js');
Requirements::css('sapphire/css/FileIFrameField.css');
return $this->renderWith($this->template);
}
/**
* @return Form
*/
public function EditFileForm() {
$uploadFile = _t('FileIFrameField.FROMCOMPUTER', 'From your Computer');
$selectFile = _t('FileIFrameField.FROMFILESTORE', 'From the File Store');
if($this->AttachedFile() && $this->AttachedFile()->ID) {
$title = sprintf(_t('FileIFrameField.REPLACE', 'Replace %s'), $this->FileTypeName());
} else {
$title = sprintf(_t('FileIFrameField.ATTACH', 'Attach %s'), $this->FileTypeName());
}
$fileSources = array();
if(singleton($this->dataClass())->canCreate()) {
if($this->canUploadNewFile) {
$fileSources["new//$uploadFile"] = new FileField('Upload', '');
}
}
$fileSources["existing//$selectFile"] = new TreeDropdownField('ExistingFile', '', 'File');
$fields = new FieldSet (
new HeaderField('EditFileHeader', $title),
new SelectionGroup('FileSource', $fileSources)
);
// locale needs to be passed through from the iframe source
if(isset($_GET['locale'])) {
$fields->push(new HiddenField('locale', '', $_GET['locale']));
}
return new Form (
$this,
'EditFileForm',
$fields,
new FieldSet(
new FormAction('save', $title)
)
);
}
public function save($data, $form) {
// check the user has entered all the required information
if (
!isset($data['FileSource'])
|| ($data['FileSource'] == 'new' && (!isset($_FILES['Upload']) || !$_FILES['Upload']))
|| ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile']))
) {
$form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required');
Director::redirectBack();
return;
}
$desiredClass = $this->dataClass();
// upload a new file
if($data['FileSource'] == 'new') {
$fileObject = Object::create($desiredClass);
try {
$this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName);
} catch (Exception $e){
$form->sessionMessage(_t('FileIFrameField.DISALLOWEDFILETYPE', 'This filetype is not allowed to be uploaded'), 'bad');
Director::redirectBack();
return;
}
if($this->upload->isError()) {
Director::redirectBack();
return;
}
$this->form->getRecord()->{$this->Name() . 'ID'} = $fileObject->ID;
$fileObject->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
$fileObject->write();
}
// attach an existing file from the assets store
if($data['FileSource'] == 'existing') {
$fileObject = DataObject::get_by_id('File', $data['ExistingFile']);
// dont allow the user to attach a folder by default
if(!$fileObject || ($fileObject instanceof Folder && $desiredClass != 'Folder')) {
Director::redirectBack();
return;
}
$this->form->getRecord()->{$this->Name() . 'ID'} = $fileObject->ID;
if(!$fileObject instanceof $desiredClass) {
$fileObject->ClassName = $desiredClass;
$fileObject->write();
}
}
$this->form->getRecord()->write();
Director::redirectBack();
}
/**
* @return Form
*/
public function DeleteFileForm() {
$form = new Form (
$this,
'DeleteFileForm',
new FieldSet (
new HiddenField('DeleteFile', null, false)
),
new FieldSet (
$deleteButton = new FormAction (
'delete', sprintf(_t('FileIFrameField.DELETE', 'Delete %s'), $this->FileTypeName())
)
)
);
$deleteButton->addExtraClass('delete');
return $form;
}
public function delete($data, $form) {
// delete the actual file, or just un-attach it?
if(isset($data['DeleteFile']) && $data['DeleteFile']) {
$file = DataObject::get_by_id('File', $this->form->getRecord()->{$this->Name() . 'ID'});
if($file) {
$file->delete();
}
}
// then un-attach file from this record
$this->form->getRecord()->{$this->Name() . 'ID'} = 0;
$this->form->getRecord()->write();
Director::redirectBack();
}
/**
* Get the type of file this field is used to attach (e.g. File, Image)
*
* @return string
*/
public function FileTypeName() {
return _t('FileIFrameField.FILE', 'File');
}
}