1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Products\Page; |
4
|
|
|
|
5
|
|
|
use Bummzack\SortableFile\Forms\SortableUploadField; |
6
|
|
|
use Dynamic\Products\Model\Brochure; |
7
|
|
|
use SilverStripe\Assets\Image; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
12
|
|
|
use SilverStripe\Forms\TextField; |
13
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
14
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
15
|
|
|
|
16
|
|
|
class Product extends \Page |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private static $table_name = 'Product'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $db = [ |
|
|
|
|
27
|
|
|
'Code' => 'Varchar(100)', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $many_many = [ |
|
|
|
|
34
|
|
|
'Images' => Image::class, |
35
|
|
|
'Brochures' => Brochure::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
42
|
|
|
'Images' => [ |
43
|
|
|
'SortOrder' => 'Int', |
44
|
|
|
], |
45
|
|
|
'Brochures' => [ |
46
|
|
|
'SortOrder' => 'Int', |
47
|
|
|
], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
private static $owns = [ |
|
|
|
|
54
|
|
|
'Images', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private static $allowed_children = []; |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \SilverStripe\Forms\FieldList |
64
|
|
|
*/ |
65
|
|
|
public function getCMSFields() |
66
|
|
|
{ |
67
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
68
|
|
|
$fields->insertBefore( |
69
|
|
|
'Content', |
70
|
|
|
TextField::create('Code', 'Product code') |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
// Images tab |
74
|
|
|
$images = SortableUploadField::create('Images') |
75
|
|
|
->setSortColumn('SortOrder') |
76
|
|
|
->setIsMultiUpload(true) |
77
|
|
|
->setAllowedFileCategories('image') |
78
|
|
|
->setFolderName('Uploads/Products/Images'); |
79
|
|
|
|
80
|
|
|
$fields->addFieldsToTab('Root.Images', [ |
81
|
|
|
$images, |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
if ($this->ID) { |
85
|
|
|
// Brochures |
86
|
|
|
$config = GridFieldConfig_RecordEditor::create(); |
87
|
|
|
$config->addComponents([ |
88
|
|
|
new GridFieldOrderableRows('SortOrder'), |
89
|
|
|
new GridFieldAddExistingSearchButton() |
90
|
|
|
]) |
91
|
|
|
->removeComponentsByType([ |
92
|
|
|
GridFieldAddExistingAutocompleter::class |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$brochures = GridField::create( |
96
|
|
|
'Brochures', |
97
|
|
|
'Brochures', |
98
|
|
|
$this->Brochures()->sort('SortOrder'), |
|
|
|
|
99
|
|
|
$config |
100
|
|
|
); |
101
|
|
|
$fields->addFieldsToTab('Root.Files.Brochures', array( |
102
|
|
|
$brochures, |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return parent::getCMSFields(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function getImage() |
114
|
|
|
{ |
115
|
|
|
if ($this->Images()->exists()) { |
|
|
|
|
116
|
|
|
$image = $this->Images()->first(); |
117
|
|
|
if ($image->ImageID > 0) { |
118
|
|
|
return $image->Image(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function getThumbnail() |
129
|
|
|
{ |
130
|
|
|
if ($image = $this->getImage()) { |
131
|
|
|
if ($thumb = Image::get()->byID($image->ID)) { |
132
|
|
|
return $thumb->CMSThumbnail(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|