@@ 22-93 (lines=72) @@ | ||
19 | * |
|
20 | * @property int $NumberOfPosts |
|
21 | */ |
|
22 | class BlogFeaturedPostsWidget extends Widget |
|
23 | { |
|
24 | /** |
|
25 | * @var string |
|
26 | */ |
|
27 | private static $title = 'Featured Posts'; |
|
28 | ||
29 | /** |
|
30 | * @var string |
|
31 | */ |
|
32 | private static $cmsTitle = 'Featured Posts'; |
|
33 | ||
34 | /** |
|
35 | * @var string |
|
36 | */ |
|
37 | private static $description = 'Displays a list of featured blog posts.'; |
|
38 | ||
39 | /** |
|
40 | * @var array |
|
41 | */ |
|
42 | private static $db = [ |
|
43 | 'NumberOfPosts' => 'Int', |
|
44 | ]; |
|
45 | ||
46 | /** |
|
47 | * @var array |
|
48 | */ |
|
49 | private static $has_one = [ |
|
50 | 'Blog' => Blog::class, |
|
51 | ]; |
|
52 | ||
53 | /** |
|
54 | * @var string |
|
55 | */ |
|
56 | private static $table_name = 'BlogFeaturedPostsWidget'; |
|
57 | ||
58 | /** |
|
59 | * {@inheritdoc} |
|
60 | */ |
|
61 | public function getCMSFields() |
|
62 | { |
|
63 | $this->beforeUpdateCMSFields(function ($fields) { |
|
64 | /** |
|
65 | * @var FieldList $fields |
|
66 | */ |
|
67 | $fields->merge([ |
|
68 | DropdownField::create('BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map()), |
|
69 | NumericField::create('NumberOfPosts', _t(__CLASS__ . '.NumberOfPosts', 'Number of Posts')) |
|
70 | ]); |
|
71 | }); |
|
72 | ||
73 | return parent::getCMSFields(); |
|
74 | } |
|
75 | ||
76 | /** |
|
77 | * @return array|DataList |
|
78 | */ |
|
79 | public function getPosts() |
|
80 | { |
|
81 | $blog = $this->Blog(); |
|
82 | ||
83 | if ($blog) { |
|
84 | return $blog->getBlogPosts() |
|
85 | ->filter('ID:not', Director::get_current_page()->ID) |
|
86 | ->filter('FeaturedInWidget', true) |
|
87 | ->sort('RAND()') |
|
88 | ->limit($this->NumberOfPosts); |
|
89 | } |
|
90 | ||
91 | return []; |
|
92 | } |
|
93 | } |
|
94 |
@@ 22-92 (lines=71) @@ | ||
19 | * |
|
20 | * @property int $NumberOfPosts |
|
21 | */ |
|
22 | class BlogRecentPostsWidget extends Widget |
|
23 | { |
|
24 | /** |
|
25 | * @var string |
|
26 | */ |
|
27 | private static $title = 'Recent Posts'; |
|
28 | ||
29 | /** |
|
30 | * @var string |
|
31 | */ |
|
32 | private static $cmsTitle = 'Recent Posts'; |
|
33 | ||
34 | /** |
|
35 | * @var string |
|
36 | */ |
|
37 | private static $description = 'Displays a list of recent blog posts.'; |
|
38 | ||
39 | /** |
|
40 | * @var array |
|
41 | */ |
|
42 | private static $db = [ |
|
43 | 'NumberOfPosts' => 'Int', |
|
44 | ]; |
|
45 | ||
46 | /** |
|
47 | * @var array |
|
48 | */ |
|
49 | private static $has_one = [ |
|
50 | 'Blog' => Blog::class, |
|
51 | ]; |
|
52 | ||
53 | /** |
|
54 | * @var string |
|
55 | */ |
|
56 | private static $table_name = 'BlogRecentPostsWidget'; |
|
57 | ||
58 | /** |
|
59 | * {@inheritdoc} |
|
60 | */ |
|
61 | public function getCMSFields() |
|
62 | { |
|
63 | $this->beforeUpdateCMSFields(function ($fields) { |
|
64 | /** |
|
65 | * @var FieldList $fields |
|
66 | */ |
|
67 | $fields->merge([ |
|
68 | DropdownField::create('BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map()), |
|
69 | NumericField::create('NumberOfPosts', _t(__CLASS__ . '.NumberOfPosts', 'Number of Posts')) |
|
70 | ]); |
|
71 | }); |
|
72 | ||
73 | return parent::getCMSFields(); |
|
74 | } |
|
75 | ||
76 | /** |
|
77 | * @return array|DataList |
|
78 | */ |
|
79 | public function getPosts() |
|
80 | { |
|
81 | $blog = $this->Blog(); |
|
82 | ||
83 | if ($blog) { |
|
84 | return $blog->getBlogPosts() |
|
85 | ->filter('ID:not', Director::get_current_page()->ID) |
|
86 | ->sort('"PublishDate" DESC') |
|
87 | ->limit($this->NumberOfPosts); |
|
88 | } |
|
89 | ||
90 | return []; |
|
91 | } |
|
92 | } |
|
93 |