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'.
|
|
|
|
*
|
|
|
|
* Example Usage:
|
|
|
|
* <code>
|
|
|
|
* class Article extends DataObject {
|
|
|
|
* static $has_one = array('MyImage' => 'Image');
|
|
|
|
* }
|
|
|
|
* // use in your form constructor etc.
|
|
|
|
* $myField = new SimpleImageField('MyImage');
|
|
|
|
* </code>
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-files
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-05-25 06:12:06 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
class SimpleImageField extends FileField {
|
2010-05-25 06:03:46 +02:00
|
|
|
/**
|
|
|
|
* @deprecated 2.5
|
|
|
|
*/
|
2008-10-01 20:33:30 +02:00
|
|
|
public $allowedExtensions = array('jpg','gif','png');
|
|
|
|
|
2010-05-25 06:12:06 +02:00
|
|
|
function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = null) {
|
|
|
|
parent::__construct($name, $title, $value, $form, $rightTitle, $folderName);
|
|
|
|
|
2010-05-25 06:03:46 +02: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-16 06:37:58 +01:00
|
|
|
$html .= "<img src=\"".$imageField->Thumbnail()->getURL()."\" />";
|
2008-10-14 23:46:52 +02:00
|
|
|
} else if($imageField->CMSThumbnail()) {
|
2009-12-16 06:37:58 +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-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;
|
|
|
|
if($record) $imageField = $record->$fieldName();
|
|
|
|
$field = "<div class=\"simpleimage\">";
|
|
|
|
if($imageField && $imageField->exists()) {
|
2010-04-13 01:38:37 +02:00
|
|
|
if($imageField->hasMethod('Thumbnail')) $field .= "<img src=\"".$imageField->Thumbnail()->URL."\" />";
|
|
|
|
elseif($imageField->CMSThumbnail()) $field .= "<img src=\"".$imageField->CMSThumbnail()->URL."\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
else {} // This shouldn't be called but it sometimes is for some reason, so we don't do anything
|
|
|
|
}else{
|
2007-10-25 04:47:45 +02:00
|
|
|
$field .= "<label>" . _t('SimpleImageField.NOUPLOAD', 'No Image Uploaded') . "</label>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
$field .= "</div>";
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|