2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2009-05-18 00:06:59 +02:00
|
|
|
* 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}
|
2008-09-28 15:08:56 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-files
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class FileIFrameField extends FileField {
|
|
|
|
|
2011-04-16 05:09:30 +02:00
|
|
|
protected $template = 'FileIFrameField';
|
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
public static $allowed_actions = array (
|
|
|
|
'iframe',
|
|
|
|
'EditFileForm',
|
|
|
|
'DeleteFileForm'
|
|
|
|
);
|
|
|
|
|
2009-10-21 07:54:12 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2011-08-14 13:54:13 +02:00
|
|
|
*
|
|
|
|
* @param boolean
|
2009-10-21 07:54:12 +02:00
|
|
|
*/
|
2011-08-14 13:54:13 +02:00
|
|
|
function setCanUploadNewFile($can) {
|
2009-10-21 07:54:12 +02:00
|
|
|
$this->canUploadNewFile = $can;
|
|
|
|
}
|
2009-10-21 04:26:01 +02:00
|
|
|
|
2011-08-14 13:54:13 +02:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function getCanUploadNewFile() {
|
|
|
|
return $this->canUploadNewFile;
|
|
|
|
}
|
|
|
|
|
2009-10-21 04:23:14 +02:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
/**
|
2009-05-18 22:55:48 +02:00
|
|
|
* @return string
|
2009-05-18 00:06:59 +02:00
|
|
|
*/
|
2009-05-18 22:55:48 +02:00
|
|
|
public function Field() {
|
2011-04-25 11:24:55 +02:00
|
|
|
Requirements::css(SAPPHIRE_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
|
2009-05-18 00:06:59 +02:00
|
|
|
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
2009-11-21 03:29:59 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery/jquery.js');
|
2011-02-24 05:07:24 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-ui/jquery-ui.js');
|
2009-05-18 00:06:59 +02:00
|
|
|
|
2010-04-13 03:46:39 +02:00
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
if($this->form->getRecord() && $this->form->getRecord()->exists()) {
|
2010-04-13 03:46:39 +02:00
|
|
|
$record = $this->form->getRecord();
|
2011-03-22 09:30:34 +01:00
|
|
|
if(class_exists('Translatable') && Object::has_extension('SiteTree', 'Translatable') && $record->Locale){
|
2010-04-13 03:46:39 +02:00
|
|
|
$iframe = "iframe?locale=".$record->Locale;
|
|
|
|
}else{
|
|
|
|
$iframe = "iframe";
|
|
|
|
}
|
2010-05-25 06:26:47 +02:00
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
return $this->createTag (
|
|
|
|
'iframe',
|
|
|
|
array (
|
|
|
|
'name' => $this->Name() . '_iframe',
|
2010-04-13 03:46:39 +02:00
|
|
|
'src' => Controller::join_links($this->Link(), $iframe),
|
2009-05-18 00:06:59 +02:00
|
|
|
'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() {
|
2009-10-21 04:23:14 +02:00
|
|
|
return $this->form->getRecord() ? $this->form->getRecord()->{$this->Name()}() : null;
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function iframe() {
|
|
|
|
// clear the requirements added by any parent controllers
|
|
|
|
Requirements::clear();
|
|
|
|
Requirements::add_i18n_javascript('sapphire/javascript/lang');
|
2009-11-21 03:29:59 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery/jquery.js');
|
2009-05-18 00:06:59 +02:00
|
|
|
Requirements::javascript('sapphire/javascript/FileIFrameField.js');
|
|
|
|
|
|
|
|
Requirements::css('sapphire/css/FileIFrameField.css');
|
|
|
|
|
2011-04-16 05:09:30 +02:00
|
|
|
return $this->renderWith($this->template);
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
|
2009-10-21 04:23:14 +02:00
|
|
|
if(singleton($this->dataClass())->canCreate()) {
|
2009-10-21 07:54:12 +02:00
|
|
|
if($this->canUploadNewFile) {
|
2009-10-21 04:26:01 +02:00
|
|
|
$fileSources["new//$uploadFile"] = new FileField('Upload', '');
|
|
|
|
}
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
|
2009-12-16 06:42:15 +01:00
|
|
|
$fileSources["existing//$selectFile"] = new TreeDropdownField('ExistingFile', '', 'File');
|
2010-05-25 06:26:47 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList (
|
2010-05-25 06:26:47 +02:00
|
|
|
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']));
|
|
|
|
}
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
return new Form (
|
|
|
|
$this,
|
|
|
|
'EditFileForm',
|
2010-05-25 06:26:47 +02:00
|
|
|
$fields,
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2009-05-18 00:06:59 +02:00
|
|
|
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']))
|
|
|
|
) {
|
2009-09-11 05:14:55 +02:00
|
|
|
$form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required');
|
2009-05-18 00:06:59 +02:00
|
|
|
Director::redirectBack();
|
|
|
|
return;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-10-21 04:23:14 +02:00
|
|
|
$desiredClass = $this->dataClass();
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
// upload a new file
|
|
|
|
if($data['FileSource'] == 'new') {
|
|
|
|
$fileObject = Object::create($desiredClass);
|
|
|
|
|
2010-11-24 07:23:47 +01:00
|
|
|
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;
|
|
|
|
}
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-10-21 07:54:12 +02:00
|
|
|
// attach an existing file from the assets store
|
2009-05-18 00:06:59 +02:00
|
|
|
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;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
$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',
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList (
|
2009-05-18 00:06:59 +02:00
|
|
|
new HiddenField('DeleteFile', null, false)
|
|
|
|
),
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList (
|
2009-05-18 00:06:59 +02:00
|
|
|
$deleteButton = new FormAction (
|
|
|
|
'delete', sprintf(_t('FileIFrameField.DELETE', 'Delete %s'), $this->FileTypeName())
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$deleteButton->addExtraClass('delete');
|
|
|
|
return $form;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
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'});
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
if($file) {
|
|
|
|
$file->delete();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
// then un-attach file from this record
|
|
|
|
$this->form->getRecord()->{$this->Name() . 'ID'} = 0;
|
|
|
|
$this->form->getRecord()->write();
|
|
|
|
|
|
|
|
Director::redirectBack();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
2009-12-16 06:42:15 +01:00
|
|
|
}
|