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