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 {
|
|
|
|
|
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
|
|
|
|
* @param boolean $can
|
|
|
|
*/
|
|
|
|
public function setCanUploadNewFile($can) {
|
|
|
|
$this->canUploadNewFile = $can;
|
|
|
|
}
|
2009-10-21 04:26:01 +02:00
|
|
|
|
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() {
|
2009-05-18 00:06:59 +02:00
|
|
|
Requirements::css(THIRDPARTY_DIR . '/jquery/themes/default/ui.all.css');
|
|
|
|
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
2009-11-21 03:22:30 +01:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery-latest.js');
|
2009-05-18 00:06:59 +02:00
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/ui/ui.core.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/ui/ui.dialog.js');
|
|
|
|
|
|
|
|
if($this->form->getRecord() && $this->form->getRecord()->exists()) {
|
|
|
|
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() {
|
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:22:30 +01:00
|
|
|
Requirements::javascript('jsparty/jquery/jquery-latest.js');
|
2009-05-18 00:06:59 +02:00
|
|
|
Requirements::javascript('sapphire/javascript/FileIFrameField.js');
|
|
|
|
|
|
|
|
Requirements::css('cms/css/typography.css');
|
|
|
|
Requirements::css('sapphire/css/FileIFrameField.css');
|
|
|
|
|
|
|
|
return $this->renderWith('FileIFrameField');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-10-21 04:23:14 +02:00
|
|
|
$fileSources["existing//$selectFile"] = new TreeDropdownField('ExistingFile', '', $this->dataClass());
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
return new Form (
|
|
|
|
$this,
|
|
|
|
'EditFileForm',
|
|
|
|
new FieldSet (
|
|
|
|
new HeaderField('EditFileHeader', $title),
|
|
|
|
new SelectionGroup('FileSource', $fileSources)
|
|
|
|
),
|
|
|
|
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']))
|
|
|
|
) {
|
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);
|
|
|
|
|
|
|
|
$this->upload->setAllowedExtensions($this->allowedExtensions);
|
|
|
|
$this->upload->setAllowedMaxFileSize($this->allowedMaxFileSize);
|
|
|
|
|
|
|
|
$this->upload->loadIntoFile($_FILES['Upload'], $fileObject, $this->folderName);
|
|
|
|
|
|
|
|
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',
|
|
|
|
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;
|
|
|
|
}
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|