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
|
|
|
* Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be |
75
|
|
|
* clickable and a GridFieldDetailForm will be used. |
76
|
|
|
* |
77
|
|
|
* @config |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
private static $inline_editable = false; |
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return FieldList |
84
|
|
|
*/ |
85
|
|
|
public function getCMSFields() |
86
|
|
|
{ |
87
|
1 |
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
88
|
1 |
|
$fields->dataFieldByName('Content') |
89
|
1 |
|
->setTitle('Description') |
90
|
1 |
|
->setRows(5); |
91
|
|
|
|
92
|
1 |
|
if ($this->ID) { |
93
|
|
|
/** @var \SilverStripe\Forms\GridField\GridField $promoField */ |
94
|
1 |
|
$promoField = $fields->dataFieldByName('Promos'); |
95
|
1 |
|
$fields->removeByName('Promos'); |
96
|
1 |
|
$config = $promoField->getConfig(); |
97
|
1 |
|
$config->removeComponentsByType([ |
98
|
1 |
|
GridFieldAddExistingAutocompleter::class, |
99
|
|
|
GridFieldDeleteAction::class, |
|
|
|
|
100
|
|
|
GridFieldArchiveAction::class, |
101
|
1 |
|
])->addComponents( |
102
|
1 |
|
new GridFieldOrderableRows('SortOrder'), |
103
|
1 |
|
new GridFieldAddExistingSearchButton() |
104
|
|
|
); |
105
|
|
|
|
106
|
1 |
|
$fields->addFieldsToTab('Root.Main', array( |
107
|
1 |
|
$promoField, |
108
|
|
|
)); |
109
|
|
|
} |
110
|
1 |
|
}); |
111
|
|
|
|
112
|
1 |
|
return parent::getCMSFields(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
1 |
|
public function getPromoList() |
119
|
|
|
{ |
120
|
1 |
|
return $this->Promos()->sort('SortOrder'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return DBHTMLText |
125
|
|
|
*/ |
126
|
1 |
|
public function getSummary() |
127
|
|
|
{ |
128
|
1 |
|
if ($this->Promos()->count() == 1) { |
129
|
|
|
$label = ' promo'; |
130
|
|
|
} else { |
131
|
1 |
|
$label = ' promos'; |
132
|
|
|
} |
133
|
1 |
|
return DBField::create_field('HTMLText', $this->Promos()->count() . $label)->Summary(20); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected function provideBlockSchema() |
140
|
|
|
{ |
141
|
|
|
$blockSchema = parent::provideBlockSchema(); |
142
|
|
|
$blockSchema['content'] = $this->getSummary(); |
143
|
|
|
return $blockSchema; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
public function getType() |
150
|
|
|
{ |
151
|
1 |
|
return _t(__CLASS__.'.BlockType', 'Promos'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|