1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Elements\Elements; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use Dynamic\Elements\Model\Testimonial; |
7
|
|
|
use Dynamic\Elements\Model\TestimonialCategory; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
10
|
|
|
use SilverStripe\Forms\ListboxField; |
11
|
|
|
use SilverStripe\ORM\DB; |
12
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
13
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ElementTestimonials |
17
|
|
|
* @package Dynamic\Elements\Elements |
18
|
|
|
* |
19
|
|
|
* @property int $Limit |
20
|
|
|
* @property string $Content |
21
|
|
|
*/ |
22
|
|
|
class ElementTestimonials extends BaseElement |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private static $icon = 'font-icon-chat'; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private static $singular_name = 'Testimonials Element'; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private static $plural_name = 'Testimonials Elements'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $table_name = 'ElementTestimonials'; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private static $db = [ |
|
|
|
|
48
|
|
|
'Limit' => 'Int', |
49
|
|
|
'Content' => 'HTMLText', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private static $many_many = [ |
|
|
|
|
56
|
|
|
'TestimonialCategories' => TestimonialCategory::class, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
private static $defaults = [ |
|
|
|
|
63
|
|
|
'Limit' => 3, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return DBHTMLText |
|
|
|
|
68
|
|
|
*/ |
69
|
1 |
|
public function getSummary() |
70
|
|
|
{ |
71
|
1 |
|
if ($this->TestimonialCategories()->count() > 0) { |
|
|
|
|
72
|
1 |
|
$ct = 0; |
73
|
1 |
|
foreach ($this->TestimonialCategories() as $category) { |
74
|
1 |
|
$ct += $category->Testimonials()->count(); |
75
|
|
|
} |
76
|
1 |
|
if ($ct == 1) { |
|
|
|
|
77
|
|
|
$label = ' testimonial'; |
78
|
|
|
} else { |
79
|
1 |
|
$label = ' testimonials'; |
80
|
|
|
} |
81
|
1 |
|
return DBField::create_field( |
82
|
1 |
|
'HTMLText', |
83
|
1 |
|
$ct . $label |
84
|
1 |
|
)->Summary(20); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function provideBlockSchema() |
92
|
|
|
{ |
93
|
|
|
$blockSchema = parent::provideBlockSchema(); |
94
|
|
|
$blockSchema['content'] = $this->getSummary(); |
95
|
|
|
return $blockSchema; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
1 |
|
public function getType() |
102
|
|
|
{ |
103
|
1 |
|
return _t(__CLASS__ . '.BlockType', 'Testimonials'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return FieldList |
108
|
|
|
*/ |
109
|
|
|
public function getCMSFields() |
110
|
|
|
{ |
111
|
1 |
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
112
|
1 |
|
$fields->dataFieldByName('Content') |
113
|
1 |
|
->setRows(8); |
114
|
|
|
|
115
|
1 |
|
$fields->dataFieldByName('Limit') |
116
|
1 |
|
->setTitle('Number of testimonials to show'); |
117
|
|
|
|
118
|
1 |
|
if ($this->exists()) { |
119
|
1 |
|
$fields->removeByName([ |
120
|
1 |
|
'TestimonialCategories' |
121
|
|
|
]); |
122
|
|
|
|
123
|
1 |
|
$fields->addFieldToTab('Root.Main', |
124
|
1 |
|
'Limit', |
|
|
|
|
125
|
1 |
|
ListboxField::create( |
126
|
1 |
|
'TestimonialCategories', |
127
|
1 |
|
'Categories', |
128
|
1 |
|
TestimonialCategory::get()->map()->toArray() |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
} |
132
|
1 |
|
}); |
133
|
|
|
|
134
|
1 |
|
return parent::getCMSFields(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
1 |
|
public function getTestimonialsList() |
141
|
|
|
{ |
142
|
1 |
|
$random = DB::get_conn()->random(); |
143
|
1 |
|
$testimonials = Testimonial::get(); |
144
|
|
|
|
145
|
1 |
|
$categories = $this->TestimonialCategories(); |
146
|
1 |
|
if ($categories->count() > 0) { |
147
|
1 |
|
$testimonials = $testimonials->filterAny(['TestimonialCategories.ID' => $categories->column()]); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
$testimonials = $testimonials->sort($random); |
151
|
1 |
|
if (0 < $this->Limit) { |
152
|
1 |
|
$testimonials = $testimonials->limit($this->Limit); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return $testimonials; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|