1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Elements\Promos\Elements; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use Dynamic\Elements\Promos\Model\PromoObject; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
9
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
11
|
|
|
use SilverStripe\Versioned\GridFieldArchiveAction; |
12
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
13
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ElementPromos |
17
|
|
|
* @package Dynamic\Elements\Promos\Elements |
18
|
|
|
* |
19
|
|
|
* @property string $Content |
20
|
|
|
* |
21
|
|
|
* @method \SilverStripe\ORM\ManyManyList Promos() |
22
|
|
|
*/ |
23
|
|
|
class ElementPromos extends BaseElement |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $icon = 'font-icon-block-banner'; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
private static $singular_name = 'Promos Element'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
private static $plural_name = 'Promos Elements'; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private static $table_name = 'ElementPromos'; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private static $styles = array(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private static $db = [ |
|
|
|
|
54
|
|
|
'Content' => DBHTMLText::class, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private static $many_many = array( |
|
|
|
|
61
|
|
|
'Promos' => PromoObject::class, |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private static $many_many_extraFields = array( |
|
|
|
|
68
|
|
|
'Promos' => array( |
69
|
|
|
'SortOrder' => 'Int', |
70
|
|
|
), |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private static $owns = [ |
|
|
|
|
77
|
|
|
'Promos', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be |
82
|
|
|
* clickable and a GridFieldDetailForm will be used. |
83
|
|
|
* |
84
|
|
|
* @config |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
1 |
|
private static $inline_editable = false; |
|
|
|
|
88
|
1 |
|
|
89
|
1 |
|
/** |
90
|
1 |
|
* @param bool $includerelations |
91
|
|
|
* @return array |
92
|
1 |
|
*/ |
93
|
|
|
public function fieldLabels($includerelations = true) |
94
|
1 |
|
{ |
95
|
1 |
|
$labels = parent::fieldLabels($includerelations); |
96
|
1 |
|
|
97
|
1 |
|
$labels['Content'] = _t(__CLASS__.'.ContentLabel', 'Intro'); |
98
|
1 |
|
$labels['Promos'] = _t(__CLASS__ . '.PromosLabel', 'Promos'); |
99
|
|
|
|
100
|
|
|
return $labels; |
101
|
1 |
|
} |
102
|
1 |
|
|
103
|
1 |
|
/** |
104
|
|
|
* @return FieldList |
105
|
|
|
*/ |
106
|
1 |
|
public function getCMSFields() |
107
|
1 |
|
{ |
108
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
109
|
|
|
$fields->dataFieldByName('Content') |
110
|
1 |
|
->setRows(5); |
111
|
|
|
|
112
|
1 |
|
if ($this->ID) { |
113
|
|
|
/** @var \SilverStripe\Forms\GridField\GridField $promoField */ |
114
|
|
|
$promoField = $fields->dataFieldByName('Promos'); |
115
|
|
|
$fields->removeByName('Promos'); |
116
|
|
|
$config = $promoField->getConfig(); |
117
|
|
|
$config->removeComponentsByType([ |
118
|
1 |
|
GridFieldAddExistingAutocompleter::class, |
119
|
|
|
GridFieldDeleteAction::class, |
|
|
|
|
120
|
1 |
|
GridFieldArchiveAction::class, |
121
|
|
|
])->addComponents( |
122
|
|
|
new GridFieldOrderableRows('SortOrder'), |
123
|
|
|
new GridFieldAddExistingSearchButton() |
124
|
|
|
); |
125
|
|
|
|
126
|
1 |
|
$fields->addFieldsToTab('Root.Main', array( |
127
|
|
|
$promoField, |
128
|
1 |
|
)); |
129
|
|
|
} |
130
|
|
|
}); |
131
|
1 |
|
|
132
|
|
|
return parent::getCMSFields(); |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function getPromoList() |
139
|
|
|
{ |
140
|
|
|
return $this->Promos()->sort('SortOrder'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return DBHTMLText |
145
|
|
|
*/ |
146
|
|
|
public function getSummary() |
147
|
|
|
{ |
148
|
|
|
if ($this->Promos()->count() == 1) { |
149
|
1 |
|
$label = ' promo'; |
150
|
|
|
} else { |
151
|
1 |
|
$label = ' promos'; |
152
|
|
|
} |
153
|
|
|
return DBField::create_field('HTMLText', $this->Promos()->count() . $label)->Summary(20); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
protected function provideBlockSchema() |
160
|
|
|
{ |
161
|
|
|
$blockSchema = parent::provideBlockSchema(); |
162
|
|
|
$blockSchema['content'] = $this->getSummary(); |
163
|
|
|
return $blockSchema; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function getType() |
170
|
|
|
{ |
171
|
|
|
return _t(__CLASS__.'.BlockType', 'Promos'); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|