1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
22
23 class SimpleImageField extends FileField {
24 25 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 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 89 90 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 {}
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.
-