1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Elements\Blog\Elements; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
6
|
|
|
use Sheadawson\DependentDropdown\Forms\DependentDropdownField; |
7
|
|
|
use SilverStripe\Blog\Model\Blog; |
8
|
|
|
use SilverStripe\Blog\Model\BlogCategory; |
9
|
|
|
use SilverStripe\Blog\Model\BlogPost; |
10
|
|
|
use SilverStripe\Forms\DropdownField; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\RequiredFields; |
13
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
14
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
15
|
|
|
use SilverStripe\ORM\ValidationResult; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ElementBlogPosts |
19
|
|
|
* @package Dynamic\Elements\Elements |
20
|
|
|
* |
21
|
|
|
* @property int $Limit |
22
|
|
|
* @property string $Content |
23
|
|
|
* |
24
|
|
|
* @property int $BlogID |
25
|
|
|
* @property int $CategoryID |
26
|
|
|
* @method Blog Blog() |
27
|
|
|
* @method BlogCategory Category() |
28
|
|
|
*/ |
29
|
|
|
class ElementBlogPosts extends BaseElement |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private static $icon = 'font-icon-block-content'; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private static $singular_name = 'Blog Posts Element'; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private static $plural_name = 'Blog Posts Elements'; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private static $description = 'Show recent blog posts from a featured blog.'; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private static $table_name = 'ElementBlogPosts'; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private static $db = array( |
|
|
|
|
60
|
|
|
'Limit' => 'Int', |
61
|
|
|
'Content' => 'HTMLText', |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
private static $has_one = array( |
|
|
|
|
68
|
|
|
'Blog' => Blog::class, |
69
|
|
|
'Category' => BlogCategory::class, |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private static $defaults = array( |
|
|
|
|
76
|
|
|
'Limit' => 3, |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return DBHTMLText |
81
|
|
|
*/ |
82
|
1 |
|
public function getSummary() |
83
|
|
|
{ |
84
|
1 |
|
return DBField::create_field('HTMLText', $this->Content)->Summary(20); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function provideBlockSchema() |
91
|
|
|
{ |
92
|
|
|
$blockSchema = parent::provideBlockSchema(); |
93
|
|
|
$blockSchema['content'] = $this->getSummary(); |
94
|
|
|
return $blockSchema; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
1 |
|
public function getType() |
101
|
|
|
{ |
102
|
1 |
|
return _t(__CLASS__ . '.BlockType', 'Blog Posts'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return FieldList |
107
|
|
|
*/ |
108
|
|
|
public function getCMSFields() |
109
|
|
|
{ |
110
|
1 |
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
111
|
1 |
|
$fields->dataFieldByName('Content') |
112
|
1 |
|
->setRows(8); |
113
|
|
|
|
114
|
1 |
|
$fields->dataFieldByName('Limit') |
115
|
1 |
|
->setTitle('Articles to show'); |
116
|
|
|
|
117
|
1 |
|
if (class_exists(Blog::class)) { |
118
|
1 |
|
$fields->insertBefore( |
119
|
1 |
|
$fields->dataFieldByName('BlogID') |
120
|
1 |
|
->setTitle('Featured Blog') |
121
|
1 |
|
->setEmptyString('-- select --'), |
122
|
1 |
|
'Limit' |
|
|
|
|
123
|
|
|
); |
124
|
|
|
|
125
|
1 |
|
$dataSource = function ($val) { |
126
|
|
|
if ($val) { |
127
|
|
|
return Blog::get()->byID($val)->Categories()->map('ID', 'Title'); |
128
|
|
|
} |
129
|
|
|
return []; |
130
|
1 |
|
}; |
131
|
|
|
|
132
|
1 |
|
$fields->insertAfter( |
133
|
1 |
|
'BlogID', |
134
|
1 |
|
DependentDropdownField::create('CategoryID', 'Category', $dataSource) |
135
|
1 |
|
->setDepends($fields->dataFieldByName('BlogID')) |
136
|
1 |
|
->setHasEmptyDefault(true) |
137
|
1 |
|
->setEmptyString('-- select --') |
138
|
|
|
); |
139
|
|
|
} |
140
|
1 |
|
}); |
141
|
|
|
|
142
|
1 |
|
return parent::getCMSFields(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
1 |
|
public function getPostsList() |
149
|
|
|
{ |
150
|
1 |
|
if ($this->BlogID) { |
151
|
1 |
|
if ($this->CategoryID) { |
152
|
|
|
return BlogCategory::get()->byID($this->CategoryID)->BlogPosts()->Limit($this->Limit); |
153
|
|
|
} |
154
|
1 |
|
return Blog::get()->byID($this->BlogID)->getBlogPosts()->Limit($this->Limit); |
155
|
|
|
} else { |
156
|
|
|
return BlogPost::get()->sort('PublishDate DESC')->limit($this->Limit); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|