11<?php
2+
23namespace NSWDPC \Elemental \Models \Image ;
34
45use DNADesign \Elemental \Models \BaseElement ;
56use SilverStripe \AssetAdmin \Forms \UploadField ;
67use SilverStripe \Assets \Image ;
78use SilverStripe \Forms \DropdownField ;
89use SilverStripe \Forms \TextareaField ;
10+ use SilverStripe \Forms \CheckboxField ;
911
1012/**
1113 * ElementImage adds an image with some config
14+ * @property ?string $Width
15+ * @property ?string $Height
16+ * @property ?string $Caption
17+ * @property int $ImageID
18+ * @method \SilverStripe\Assets\Image Image()
19+ * @property bool $HideCaption
1220 */
1321class ElementImage extends BaseElement
1422{
15- private static $ icon = "font-icon-image " ;
23+ private static string $ icon = "font-icon-image " ;
24+
25+ private static string $ table_name = "ElementImage " ;
26+
27+ private static string $ title = "Image " ;
28+
29+ private static string $ description = "Display an image " ;
30+
31+ private static string $ singular_name = "Image " ;
32+
33+ private static string $ plural_name = "Images " ;
34+
35+ private static array $ allowed_file_types = ["jpg " , "jpeg " , "gif " , "png " , "webp " ];
1636
17- private static $ table_name = " ElementImage " ;
37+ public const WIDTH_FULL = ' full ' ;
1838
19- private static $ title = "Image " ;
20- private static $ description = "Display an image " ;
39+ public const WIDTH_CONTAINER = 'container ' ;
2140
22- private static $ singular_name = "Image " ;
23- private static $ plural_name = "Images " ;
41+ public const HEIGHT_SMALL = 'small ' ;
2442
25- private static $ allowed_file_types = [ " jpg " , " jpeg " , " gif " , " png " , " webp " ] ;
43+ public const HEIGHT_MEDIUM = ' medium ' ;
2644
27- const WIDTH_FULL = 'full ' ;
28- const WIDTH_CONTAINER = 'container ' ;
45+ public const HEIGHT_LARGE = 'large ' ;
2946
30- const HEIGHT_SMALL = 'small ' ;
31- const HEIGHT_MEDIUM = 'medium ' ;
32- const HEIGHT_LARGE = 'large ' ;
33- const HEIGHT_ORIGINAL = 'original ' ;
47+ public const HEIGHT_ORIGINAL = 'original ' ;
3448
49+ #[\Override]
3550 public function getType ()
3651 {
37- return _t (__CLASS__ . ".BlockType " , "Image " );
52+ return _t (self ::class . ".BlockType " , "Image " );
3853 }
3954
40- private static $ db = [
55+ private static array $ db = [
4156 "Width " => "Varchar " ,
4257 "Height " => "Varchar " ,
4358 'Caption ' => 'Text ' ,
59+ "HideCaption " => "Boolean "
4460 ];
4561
46- private static $ has_one = [
62+ private static array $ has_one = [
4763 "Image " => Image::class,
4864 ];
4965
50- private static $ summary_fields = [
66+ private static array $ defaults = [
67+ "HideCaption " => 0
68+ ];
69+
70+ private static array $ summary_fields = [
5171 "Image.CMSThumbnail " => "Image " ,
5272 "Title " => "Title " ,
5373 ];
5474
55- private static $ owns = ["Image " ];
75+ private static array $ owns = ["Image " ];
5676
57- public function getAllowedFileTypes ()
77+ public function ShowCaption (): bool
78+ {
79+ return $ this ->HideCaption == 0 ;
80+ }
81+
82+ public function getAllowedFileTypes (): array
5883 {
5984 $ types = $ this ->config ()->get ("allowed_file_types " );
6085 if (empty ($ types )) {
6186 $ types = ['jpg ' , 'jpeg ' , 'gif ' , 'png ' , 'webp ' ];
6287 }
63- $ types = array_unique ( $ types );
64- return $ types ;
88+
89+ return array_unique ( $ types) ;
6590 }
6691
92+ #[\Override]
6793 public function getCMSFields ()
6894 {
6995
70- $ this ->beforeUpdateCMSFields (function ($ fields ) {
96+ $ this ->beforeUpdateCMSFields (function ($ fields ): void {
7197 $ fields ->addFieldsToTab ("Root.Main " , [
7298 DropdownField::create (
7399 "Width " ,
74- _t (__CLASS__ . ".WIDTH " , "Width " ),
100+ _t (self ::class . ".WIDTH " , "Width " ),
75101 [
76- self ::WIDTH_CONTAINER => _t (__CLASS__ . ".CONTAINER_WIDTH " , "Content width " ),
77- self ::WIDTH_FULL => _t (__CLASS__ . ".BROWSER_WIDTH " , "Browser width " )
102+ self ::WIDTH_CONTAINER => _t (self ::class . ".CONTAINER_WIDTH " , "Content width " ),
103+ self ::WIDTH_FULL => _t (self ::class . ".BROWSER_WIDTH " , "Browser width " )
78104 ]
79105 ),
80106 DropdownField::create (
81107 "Height " ,
82- _t (__CLASS__ . ".HEIGHT " , "Height " ),
108+ _t (self ::class . ".HEIGHT " , "Height " ),
83109 [
84- self ::HEIGHT_SMALL => _t (__CLASS__ . ".HEIGHT_SMALL " , "Small " ),
85- self ::HEIGHT_MEDIUM => _t (__CLASS__ . ".HEIGHT_MEDIUM " , "Medium " ),
86- self ::HEIGHT_LARGE => _t (__CLASS__ . ".HEIGHT_LARGE " , "Large " ),
87- self ::HEIGHT_ORIGINAL => _t (__CLASS__ . ".HEIGHT_ORIGINAL " , "Original " )
110+ self ::HEIGHT_SMALL => _t (self ::class . ".HEIGHT_SMALL " , "Small " ),
111+ self ::HEIGHT_MEDIUM => _t (self ::class . ".HEIGHT_MEDIUM " , "Medium " ),
112+ self ::HEIGHT_LARGE => _t (self ::class . ".HEIGHT_LARGE " , "Large " ),
113+ self ::HEIGHT_ORIGINAL => _t (self ::class . ".HEIGHT_ORIGINAL " , "Original " )
88114 ]
89115 ),
90116 UploadField::create (
91117 "Image " ,
92- _t (__CLASS__ . ".SLIDE_IMAGE " , "Image " )
118+ _t (self ::class . ".SLIDE_IMAGE " , "Image " )
93119 )
94120 ->setAllowedExtensions ($ this ->getAllowedFileTypes ())
95121 ->setIsMultiUpload (false )
96122 ->setDescription (
97123 _t (
98- __CLASS__ . "ALLOWED_FILE_TYPES " ,
124+ self ::class . "ALLOWED_FILE_TYPES " ,
99125 "Allowed file types: {types} " ,
100126 [
101127 'types ' => implode (", " , $ this ->getAllowedFileTypes ())
@@ -104,7 +130,11 @@ public function getCMSFields()
104130 ),
105131 TextareaField::create (
106132 'Caption ' ,
107- _t (__CLASS__ . ".CAPTION " , "Caption " )
133+ _t (self ::class . ".CAPTION " , "Caption " )
134+ ),
135+ CheckboxField::create (
136+ "HideCaption " ,
137+ _t (self ::class . ".HIDE_CAPTION " , "Hide Caption " )
108138 )
109139 ]);
110140 });
0 commit comments