1 <?php
2 3 4 5 6 7 8 9 10
11 class FileIFrameField extends FileField {
12
13 public static $allowed_actions = array (
14 'iframe',
15 'EditFileForm',
16 'DeleteFileForm'
17 );
18
19 20 21 22 23 24
25 protected $canUploadNewFile = true;
26
27 28 29 30 31 32
33 public $AutoresizeMaxWidth = 0;
34
35 36 37 38 39 40
41 public $AutoresizeMaxHeight = 0;
42
43 44 45 46
47 public function setCanUploadNewFile($can) {
48 $this->canUploadNewFile = $can;
49 }
50
51 52 53 54
55 public function dataClass() {
56 if($this->form && $this->form->getRecord()) {
57 $class = $this->form->getRecord()->has_one($this->Name());
58 return ($class) ? $class : 'File';
59 } else {
60 return 'File';
61 }
62 }
63
64 65 66
67 public function Field() {
68 if ($this->isReadonly()) {
69 return ($this->AttachedFile() && $this->AttachedFile()->ID) ? $this->AttachedFile()->CMSThumbnail() : '';
70 }
71
72 Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/base/jquery.ui.all.css');
73 Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
74 Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
75 Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery-ui-1.8rc3.custom.js');
76
77
78 if($this->form->getRecord() && $this->form->getRecord()->exists()) {
79 $record = $this->form->getRecord();
80 if(Object::has_extension('SiteTree', 'Translatable') && $record->Locale){
81 $iframe = "iframe?locale={$record->Locale}&m=".time();
82 }else{
83 $iframe = "iframe?m=".time();
84 }
85
86 return $this->createTag (
87 'iframe',
88 array (
89 'name' => $this->Name() . '_iframe',
90 'src' => Controller::join_links($this->Link(), $iframe),
91 'style' => 'height: 152px; width: 100%; border: none;'
92 )
93 ) . $this->createTag (
94 'input',
95 array (
96 'type' => 'hidden',
97 'id' => $this->ID(),
98 'name' => $this->Name() . 'ID',
99 'value' => $this->attrValue()
100 )
101 );
102 }
103
104 $this->setValue(sprintf(_t (
105 'FileIFrameField.ATTACHONCESAVED', '%ss can be attached once you have saved the record for the first time.'
106 ), $this->FileTypeName()));
107
108 return FormField::field();
109 }
110
111 112 113 114 115
116 public function AttachedFile() {
117 return $this->form->getRecord() ? $this->form->getRecord()->{$this->Name()}() : null;
118 }
119
120 121 122
123 public function iframe() {
124
125 Requirements::clear();
126 Requirements::add_i18n_javascript('sapphire/javascript/lang');
127 Requirements::javascript(THIRDPARTY_DIR . '/prototype/prototype.js');
128 Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
129 Requirements::javascript('sapphire/javascript/FileIFrameField.js');
130
131 Requirements::css('cms/css/typography.css');
132 Requirements::css('sapphire/css/FileIFrameField.css');
133
134 return $this->renderWith('FileIFrameField');
135 }
136
137 138 139
140 public function EditFileForm() {
141 $uploadFile = _t('FileIFrameField.FROMCOMPUTER', 'From your Computer');
142 $selectFile = _t('FileIFrameField.FROMFILESTORE', 'From the File Store');
143
144 if($this->AttachedFile() && $this->AttachedFile()->ID) {
145 $title = sprintf(_t('FileIFrameField.REPLACE', 'Replace %s'), $this->FileTypeName());
146 } else {
147 $title = sprintf(_t('FileIFrameField.ATTACH', 'Attach %s'), $this->FileTypeName());
148 }
149
150 $fileSources = array();
151 if(singleton($this->dataClass())->canCreate()) {
152 if($this->canUploadNewFile) {
153 $fileSources["new//$uploadFile"] = new FileField('Upload', '');
154 }
155 }
156
157 $tdf = new TreeDropdownField('ExistingFile', '', 'File');
158 $folderName = 'assets/' . $this->FolderName . '/';
159 $fileFolder = DataObject::get_one('Folder', "Filename = '{$folderName}'");
160 if ($fileFolder) {
161 $tdf->setValue($fileFolder->ID);
162 }
163 $fileSources["existing//$selectFile"] = $tdf;
164
165 $fields = new FieldSet (
166 new HeaderField('EditFileHeader', $title),
167 new SelectionGroup('FileSource', $fileSources)
168 );
169
170
171 if(isset($_GET['locale'])) {
172 $fields->push(new HiddenField('locale', '', $_GET['locale']));
173 }
174
175 return new Form (
176 $this,
177 'EditFileForm',
178 $fields,
179 new FieldSet(
180 new FormAction('save', $title)
181 )
182 );
183 }
184
185 public function save($data, $form) {
186
187 if (
188 !isset($data['FileSource'])
189 || ($data['FileSource'] == 'new' && (!isset($_FILES['Upload']) || !$_FILES['Upload']))
190 || ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile']))
191 ) {
192 $form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required');
193 Director::redirectBack();
194 return;
195 }
196
197 $desiredClass = $this->dataClass();
198
199
200 if($data['FileSource'] == 'new') {
201 if (class_exists('ImageAutoResize')) {
202 $oldMaxSizes = ImageAutoResize::get_max_size();
203 $maxWidth = ($this->AutoresizeMaxWidth > 0) ? $this->AutoresizeMaxWidth : $oldMaxSizes['Width'];
204 $maxHeight = ($this->AutoresizeMaxHeight > 0) ? $this->AutoresizeMaxHeight : $oldMaxSizes['Height'];
205 ImageAutoResize::set_max_size($maxWidth, $maxHeight);
206 }
207
208 $fileObject = Object::create($desiredClass);
209
210 $this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName);
211
212 if($this->upload->isError()) {
213 Director::redirectBack();
214 return;
215 }
216
217 $this->form->getRecord()->{$this->Name() . 'ID'} = $fileObject->ID;
218
219 $fileObject->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
220 $fileObject->write();
221 if (class_exists('ImageAutoResize')) {
222 ImageAutoResize::set_max_size($oldMaxSizes['Width'], $oldMaxSizes['Height']);
223 }
224 }
225
226
227 if($data['FileSource'] == 'existing') {
228 $fileObject = DataObject::get_by_id('File', $data['ExistingFile']);
229
230
231 if(!$fileObject || ($fileObject instanceof Folder && $desiredClass != 'Folder')) {
232 Director::redirectBack();
233 return;
234 }
235
236 $this->form->getRecord()->{$this->Name() . 'ID'} = $fileObject->ID;
237
238 if(!$fileObject instanceof $desiredClass) {
239 $fileObject->ClassName = $desiredClass;
240 $fileObject->write();
241 }
242 }
243
244 $this->form->getRecord()->write();
245 Director::redirectBack();
246 }
247
248 249 250
251 public function DeleteFileForm() {
252 $form = new Form (
253 $this,
254 'DeleteFileForm',
255 new FieldSet (
256 new HiddenField('DeleteFile', null, false)
257 ),
258 new FieldSet (
259 $deleteButton = new FormAction (
260 'delete', sprintf(_t('FileIFrameField.DELETE', 'Delete %s'), $this->FileTypeName())
261 )
262 )
263 );
264
265 $deleteButton->addExtraClass('delete');
266 return $form;
267 }
268
269 public function delete($data, $form) {
270
271 if(isset($data['DeleteFile']) && $data['DeleteFile']) {
272 $file = DataObject::get_by_id('File', $this->form->getRecord()->{$this->Name() . 'ID'});
273
274 if($file) {
275 $file->delete();
276 }
277 }
278
279
280 $this->form->getRecord()->{$this->Name() . 'ID'} = 0;
281 $this->form->getRecord()->write();
282
283 Director::redirectBack();
284 }
285
286 287 288 289 290
291 public function FileTypeName() {
292 return _t('FileIFrameField.FILE', 'File');
293 }
294
295 }
296
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-