Usage * * * class Article extends DataObject { * static $has_one = array('MyImage' => 'Image'); * } * // use in your form constructor etc. * $myField = new SimpleImageField('MyImage'); * * * Usage within a controller * * First add your $has_one relationship: * * * static $has_one = array( * 'FileName' => 'FileType' * ); * * (i.e. Image for a FileType) * * Then add your Field into your form: * * * function Form() { * return new Form($this, "Form", new FieldList( * new SimpleImageField ( * $name = "FileTypeID", * $title = "Upload your FileType" * ) * ), new FieldList( * * // 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/'); * } * * * Your file should be now in the uploads directory * * @package forms * @subpackage fields-files */ class SimpleImageField extends FileField { 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); $this->getValidator()->setAllowedExtensions(array('jpg','gif','png')); } function Field() { if($this->form) $record = $this->form->getRecord(); $fieldName = $this->name; if(isset($record)&&$record) { $imageField = $record->$fieldName(); } else { $imageField = ""; } $html = "
"; if($imageField && $imageField->exists()) { $html .= '
'; if($imageField->hasMethod('Thumbnail') && $imageField->Thumbnail()) { $html .= "Thumbnail()->getURL()."\" />"; } else if($imageField->CMSThumbnail()) { $html .= "CMSThumbnail()->getURL()."\" />"; } $html .= '
'; } $html .= $this->createTag("input", array( "type" => "file", "name" => $this->name, "id" => $this->id(), "tabindex" => $this->getTabIndex(), 'disabled' => $this->disabled ) ); $html .= $this->createTag("input", array( "type" => "hidden", "name" => "MAX_FILE_SIZE", "value" => $this->getValidator()->getAllowedMaxFileSize(), "tabindex" => $this->getTabIndex() ) ); $html .= "
"; return $html; } /** * Returns a readonly version of this field */ function performReadonlyTransformation() { $field = new SimpleImageField_Disabled($this->name, $this->title, $this->value); $field->setForm($this->form); $field->setReadonly(true); return $field; } } /** * Disabled version of {@link SimpleImageField}. * @package forms * @subpackage fields-files */ class SimpleImageField_Disabled extends FormField { protected $disabled = true; protected $readonly = true; function Field() { $record = $this->form->getRecord(); $fieldName = $this->name; $field = "
"; 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 .= "Thumbnail()->URL."\" />"; elseif($imageField->CMSThumbnail()) $field .= "CMSThumbnail()->URL."\" />"; else {} // This shouldn't be called but it sometimes is for some reason, so we don't do anything }else{ $field .= ""; } } $field .= "
"; return $field; } }