2012-05-24 00:33:40 +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
|
|
|
*
|
2012-02-03 00:59:04 +01:00
|
|
|
* @deprecated 3.0 Use UploadField
|
|
|
|
*
|
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
|
2011-08-14 13:54:13 +02:00
|
|
|
*
|
|
|
|
* @param boolean
|
2009-10-21 07:54:12 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCanUploadNewFile() {
|
2011-08-14 13:54:13 +02:00
|
|
|
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()) {
|
2011-10-29 13:07:40 +02:00
|
|
|
$class = $this->form->getRecord()->has_one($this->getName());
|
2009-10-21 04:23:14 +02:00
|
|
|
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
|
|
|
*/
|
2012-04-11 07:33:36 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-02-03 00:59:04 +01:00
|
|
|
Deprecation::notice('3.0', 'Use UploadField');
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
|
|
|
|
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/javascript/lang');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_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 (
|
2011-10-29 13:07:40 +02:00
|
|
|
'name' => $this->getName() . '_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(),
|
2011-10-29 13:07:40 +02:00
|
|
|
'name' => $this->getName() . 'ID',
|
2009-05-18 00:06:59 +02:00
|
|
|
'value' => $this->attrValue()
|
|
|
|
)
|
|
|
|
);
|
2011-03-23 05:12:25 +01:00
|
|
|
} else {
|
2012-05-01 21:44:54 +02:00
|
|
|
return _t(
|
|
|
|
'FileIFrameField.ATTACHONCESAVED',
|
|
|
|
'{type}s can be attached once you have saved the record for the first time.',
|
|
|
|
array('type' => $this->FileTypeName())
|
|
|
|
);
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to retreive a File object that has already been attached to this forms data record
|
|
|
|
*
|
|
|
|
* @return File|null
|
|
|
|
*/
|
|
|
|
public function AttachedFile() {
|
2011-10-29 13:07:40 +02:00
|
|
|
return $this->form->getRecord() ? $this->form->getRecord()->{$this->getName()}() : null;
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function iframe() {
|
|
|
|
// clear the requirements added by any parent controllers
|
|
|
|
Requirements::clear();
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/javascript/lang');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/FileIFrameField.js');
|
2009-05-18 00:06:59 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/FileIFrameField.css');
|
2009-05-18 00:06:59 +02:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
return $this->renderWith('FileIframeField_iframe');
|
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) {
|
2012-05-01 21:44:54 +02:00
|
|
|
$title = _t('FileIFrameField.REPLACE', 'Replace {type}', array('type' => $this->FileTypeName()));
|
2009-05-18 00:06:59 +02:00
|
|
|
} else {
|
2012-05-01 21:44:54 +02:00
|
|
|
$title = _t('FileIFrameField.ATTACH', 'Attach {type}', array('type' => $this->FileTypeName()));
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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');
|
2012-05-24 00:33:40 +02:00
|
|
|
$form->getController()->redirectBack();
|
2009-05-18 00:06:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-05-24 00:33:40 +02:00
|
|
|
|
2009-10-21 04:23:14 +02:00
|
|
|
$desiredClass = $this->dataClass();
|
2012-05-24 00:33:40 +02:00
|
|
|
$controller = $this->form->getController();
|
|
|
|
|
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');
|
2012-05-24 00:33:40 +02:00
|
|
|
$controller->redirectBack();
|
2010-11-24 07:23:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
if($this->upload->isError()) {
|
2012-05-24 00:33:40 +02:00
|
|
|
$controller->redirectBack();
|
2009-05-18 00:06:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-29 13:07:40 +02:00
|
|
|
$this->form->getRecord()->{$this->getName() . 'ID'} = $fileObject->ID;
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
$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')) {
|
2012-05-24 00:33:40 +02:00
|
|
|
$controller->redirectBack();
|
2009-05-18 00:06:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-10-29 13:07:40 +02:00
|
|
|
$this->form->getRecord()->{$this->getName() . 'ID'} = $fileObject->ID;
|
2009-05-18 00:06:59 +02:00
|
|
|
|
|
|
|
if(!$fileObject instanceof $desiredClass) {
|
|
|
|
$fileObject->ClassName = $desiredClass;
|
|
|
|
$fileObject->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->form->getRecord()->write();
|
2012-05-24 00:33:40 +02:00
|
|
|
$controller->redirectBack();
|
2009-05-18 00:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 (
|
2012-05-01 21:44:54 +02:00
|
|
|
'delete', _t('FileIFrameField.DELETE', 'Delete {type}', array('type' => $this->FileTypeName()))
|
2009-05-18 00:06:59 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$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']) {
|
2011-10-29 13:07:40 +02:00
|
|
|
$file = DataObject::get_by_id('File', $this->form->getRecord()->{$this->getName() . '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
|
|
|
}
|
2012-05-24 00:33:40 +02:00
|
|
|
|
2009-05-18 00:06:59 +02:00
|
|
|
// then un-attach file from this record
|
2011-10-29 13:07:40 +02:00
|
|
|
$this->form->getRecord()->{$this->getName() . 'ID'} = 0;
|
2009-05-18 00:06:59 +02:00
|
|
|
$this->form->getRecord()->write();
|
2012-05-24 00:33:40 +02:00
|
|
|
|
|
|
|
$this->form->getController()->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
|
|
|
}
|