2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2008-10-01 20:33:30 +02:00
|
|
|
* SimpleImageField provides an easy way of uploading images to {@link Image} has_one relationships.
|
|
|
|
* These relationships are auto-detected if you name the field accordingly.
|
2008-09-28 15:08:56 +02:00
|
|
|
* Unlike {@link ImageField}, it doesn't use an iframe.
|
|
|
|
*
|
2008-10-01 20:33:30 +02:00
|
|
|
* Restricts the upload size to 2MB by default, and only allows upload
|
|
|
|
* of files with the extension 'jpg', 'gif' or 'png'.
|
|
|
|
*
|
2010-07-11 12:44:59 +02:00
|
|
|
* <b>Usage</b>
|
|
|
|
*
|
2008-10-01 20:33:30 +02:00
|
|
|
* <code>
|
|
|
|
* class Article extends DataObject {
|
|
|
|
* static $has_one = array('MyImage' => 'Image');
|
|
|
|
* }
|
|
|
|
* // use in your form constructor etc.
|
|
|
|
* $myField = new SimpleImageField('MyImage');
|
|
|
|
* </code>
|
|
|
|
*
|
2010-07-11 12:44:59 +02:00
|
|
|
* <b>Usage within a controller</b>
|
|
|
|
*
|
|
|
|
* First add your $has_one relationship:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* static $has_one = array(
|
|
|
|
* 'FileName' => 'FileType'
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
* (i.e. Image for a FileType)
|
|
|
|
*
|
|
|
|
* Then add your Field into your form:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* function Form() {
|
2011-05-11 09:51:54 +02:00
|
|
|
* return new Form($this, "Form", new FieldList(
|
2010-07-11 12:44:59 +02:00
|
|
|
* new SimpleImageField (
|
|
|
|
* $name = "FileTypeID",
|
|
|
|
* $title = "Upload your FileType"
|
|
|
|
* )
|
2011-05-11 09:51:54 +02:00
|
|
|
* ), new FieldList(
|
2010-07-11 12:44:59 +02:00
|
|
|
*
|
|
|
|
* // List the action buttons here - doform executes the function 'doform' below
|
|
|
|
* new FormAction("doform", "Submit")
|
|
|
|
*
|
|
|
|
* // List the required fields here
|
|
|
|
* ), new RequiredFields(
|
|
|
|
* "FileTypeID"
|
|
|
|
* ));
|
|
|
|
* }
|
|
|
|
* // Then make sure that the file is saved into the assets area:
|
|
|
|
* function doform($data, $form) {
|
|
|
|
* $file = new File();
|
|
|
|
* $file->loadUploaded($_FILES['FileTypeID']);
|
|
|
|
*
|
|
|
|
* // Redirect to a page thanking people for registering
|
|
|
|
* Director::redirect('thanks-for-your-submission/');
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Your file should be now in the uploads directory
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-files
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-03-04 20:52:19 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
class SimpleImageField extends FileField {
|
2008-10-01 20:33:30 +02:00
|
|
|
|
2012-01-02 17:43:36 +01:00
|
|
|
function __construct($name, $title = null, $value = null) {
|
|
|
|
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments');
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value);
|
2010-03-04 20:52:19 +01:00
|
|
|
|
2010-03-04 05:42:41 +01:00
|
|
|
$this->getValidator()->setAllowedExtensions(array('jpg','gif','png'));
|
|
|
|
}
|
|
|
|
|
2008-04-06 06:05:23 +02:00
|
|
|
function Field() {
|
2009-04-29 01:55:53 +02:00
|
|
|
if($this->form) $record = $this->form->getRecord();
|
2008-04-06 06:05:23 +02:00
|
|
|
$fieldName = $this->name;
|
2009-04-29 01:55:53 +02:00
|
|
|
if(isset($record)&&$record) {
|
2008-04-06 06:05:23 +02:00
|
|
|
$imageField = $record->$fieldName();
|
|
|
|
} else {
|
|
|
|
$imageField = "";
|
|
|
|
}
|
|
|
|
|
2008-10-14 23:46:52 +02:00
|
|
|
$html = "<div class=\"simpleimage\">";
|
|
|
|
if($imageField && $imageField->exists()) {
|
|
|
|
$html .= '<div class="thumbnail">';
|
|
|
|
if($imageField->hasMethod('Thumbnail') && $imageField->Thumbnail()) {
|
2009-12-03 01:38:52 +01:00
|
|
|
$html .= "<img src=\"".$imageField->Thumbnail()->getURL()."\" />";
|
2008-10-14 23:46:52 +02:00
|
|
|
} else if($imageField->CMSThumbnail()) {
|
2009-12-03 01:38:52 +01:00
|
|
|
$html .= "<img src=\"".$imageField->CMSThumbnail()->getURL()."\" />";
|
2008-10-14 23:46:52 +02:00
|
|
|
}
|
|
|
|
$html .= '</div>';
|
|
|
|
}
|
|
|
|
$html .= $this->createTag("input",
|
2008-04-09 13:15:02 +02:00
|
|
|
array(
|
|
|
|
"type" => "file",
|
|
|
|
"name" => $this->name,
|
|
|
|
"id" => $this->id(),
|
2009-04-29 01:55:53 +02:00
|
|
|
"tabindex" => $this->getTabIndex(),
|
|
|
|
'disabled' => $this->disabled
|
2008-04-09 13:15:02 +02:00
|
|
|
)
|
2008-05-09 04:38:09 +02:00
|
|
|
);
|
2008-04-09 13:15:02 +02:00
|
|
|
$html .= $this->createTag("input",
|
2008-10-14 23:46:52 +02:00
|
|
|
array(
|
|
|
|
"type" => "hidden",
|
|
|
|
"name" => "MAX_FILE_SIZE",
|
2010-03-04 05:42:41 +01:00
|
|
|
"value" => $this->getValidator()->getAllowedMaxFileSize(),
|
2008-04-09 13:15:02 +02:00
|
|
|
"tabindex" => $this->getTabIndex()
|
2008-10-14 23:46:52 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$html .= "</div>";
|
|
|
|
|
|
|
|
return $html;
|
2008-04-06 06:05:23 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a readonly version of this field
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$field = new SimpleImageField_Disabled($this->name, $this->title, $this->value);
|
|
|
|
$field->setForm($this->form);
|
2008-08-12 04:58:48 +02:00
|
|
|
$field->setReadonly(true);
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-10 01:34:18 +01:00
|
|
|
/**
|
|
|
|
* Disabled version of {@link SimpleImageField}.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-files
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class SimpleImageField_Disabled extends FormField {
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
protected $disabled = true;
|
|
|
|
|
|
|
|
protected $readonly = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function Field() {
|
|
|
|
$record = $this->form->getRecord();
|
|
|
|
$fieldName = $this->name;
|
2011-08-29 22:21:44 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$field = "<div class=\"simpleimage\">";
|
2011-08-29 22:21:44 +02:00
|
|
|
if($this->value) {
|
|
|
|
// Only the case for DataDifferencer
|
|
|
|
$field .= $this->value;
|
|
|
|
} else {
|
|
|
|
if($record) $imageField = $record->$fieldName();
|
|
|
|
if($imageField && $imageField->exists()) {
|
|
|
|
if($imageField->hasMethod('Thumbnail')) $field .= "<img src=\"".$imageField->Thumbnail()->URL."\" />";
|
|
|
|
elseif($imageField->CMSThumbnail()) $field .= "<img src=\"".$imageField->CMSThumbnail()->URL."\" />";
|
|
|
|
else {} // This shouldn't be called but it sometimes is for some reason, so we don't do anything
|
|
|
|
}else{
|
|
|
|
$field .= "<label>" . _t('SimpleImageField.NOUPLOAD', 'No Image Uploaded') . "</label>";
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
$field .= "</div>";
|
2011-08-29 22:21:44 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|