Version: current

      1 <?php
      2 /**
      3  * SimpleImageField provides an easy way of uploading images to {@link Image} has_one relationships.
      4  * These relationships are auto-detected if you name the field accordingly.
      5  * Unlike {@link ImageField}, it doesn't use an iframe.
      6  *
      7  * Restricts the upload size to 2MB by default, and only allows upload
      8  * of files with the extension 'jpg', 'gif' or 'png'.
      9  *
     10  * Example Usage:
     11  * <code>
     12  * class Article extends DataObject {
     13  *  static $has_one = array('MyImage' => 'Image');
     14  * }
     15  * // use in your form constructor etc.
     16  * $myField = new SimpleImageField('MyImage');
     17  * </code>
     18  *
     19  * @package forms
     20  * @subpackage fields-files
     21  */
     22
     23 class SimpleImageField extends FileField {
     24     /**
     25      * @deprecated 2.5
     26      */
     27     public $allowedExtensions = array('jpg','gif','png');
     28
     29     function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = null) {
     30         parent::__construct($name, $title, $value, $form, $rightTitle, $folderName);
     31
     32         $this->getValidator()->setAllowedExtensions(array('jpg','gif','png'));
     33     }
     34
     35     function Field() {
     36         if($this->form) $record = $this->form->getRecord();
     37         $fieldName = $this->name;
     38         if(isset($record)&&$record) {
     39             $imageField = $record->$fieldName();
     40         } else {
     41             $imageField = "";
     42         }
     43
     44         $html = "<div class=\"simpleimage\">";
     45         if($imageField && $imageField->exists()) {
     46             $html .= '<div class="thumbnail">';
     47             if($imageField->hasMethod('Thumbnail') && $imageField->Thumbnail()) {
     48                 $html .= "<img src=\"".$imageField->Thumbnail()->getURL()."\" />";
     49             } else if($imageField->CMSThumbnail()) {
     50                 $html .= "<img src=\"".$imageField->CMSThumbnail()->getURL()."\" />";
     51             }
     52             $html .= '</div>';
     53         }
     54         $html .= $this->createTag("input",
     55             array(
     56                 "type" => "file",
     57                 "name" => $this->name,
     58                 "id" => $this->id(),
     59                 "tabindex" => $this->getTabIndex(),
     60                 'disabled' => $this->disabled
     61             )
     62         );
     63         $html .= $this->createTag("input",
     64             array(
     65                 "type" => "hidden",
     66                 "name" => "MAX_FILE_SIZE",
     67                 "value" => $this->getValidator()->getAllowedMaxFileSize(),
     68                 "tabindex" => $this->getTabIndex()
     69             )
     70         );
     71         $html .= "</div>";
     72
     73         return $html;
     74     }
     75
     76     /**
     77      * Returns a readonly version of this field
     78      */
     79     function performReadonlyTransformation() {
     80         $field = new SimpleImageField_Disabled($this->name, $this->title, $this->value);
     81         $field->setForm($this->form);
     82         $field->setReadonly(true);
     83         return $field;
     84     }
     85 }
     86
     87 /**
     88  * Disabled version of {@link SimpleImageField}.
     89  * @package forms
     90  * @subpackage fields-files
     91  */
     92 class SimpleImageField_Disabled extends FormField {
     93
     94     protected $disabled = true;
     95
     96     protected $readonly = true;
     97
     98     function Field() {
     99         $record = $this->form->getRecord();
    100         $fieldName = $this->name;
    101         if($record) $imageField = $record->$fieldName();
    102         $field = "<div class=\"simpleimage\">";
    103         if($imageField && $imageField->exists()) {
    104           if($imageField->hasMethod('Thumbnail')) $field .= "<img src=\"".$imageField->Thumbnail()->URL."\" />";
    105           elseif($imageField->CMSThumbnail()) $field .= "<img src=\"".$imageField->CMSThumbnail()->URL."\" />";
    106           else {} // This shouldn't be called but it sometimes is for some reason, so we don't do anything
    107         }else{
    108             $field .= "<label>" . _t('SimpleImageField.NOUPLOAD', 'No Image Uploaded') . "</label>";
    109         }
    110         $field .= "</div>";
    111         return $field;
    112     }
    113
    114 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -