@@ 19-125 (lines=107) @@ | ||
16 | /** |
|
17 | * @method Blog Blog() |
|
18 | */ |
|
19 | class BlogCategoriesWidget extends Widget |
|
20 | { |
|
21 | /** |
|
22 | * @var string |
|
23 | */ |
|
24 | private static $title = 'Categories'; |
|
25 | ||
26 | /** |
|
27 | * @var string |
|
28 | */ |
|
29 | private static $cmsTitle = 'Blog Categories'; |
|
30 | ||
31 | /** |
|
32 | * @var string |
|
33 | */ |
|
34 | private static $description = 'Displays a list of blog categories.'; |
|
35 | ||
36 | /** |
|
37 | * @var array |
|
38 | */ |
|
39 | private static $db = array( |
|
40 | 'Limit' => 'Int', |
|
41 | 'Order' => 'Varchar', |
|
42 | 'Direction' => 'Varchar', |
|
43 | ); |
|
44 | ||
45 | /** |
|
46 | * @var array |
|
47 | */ |
|
48 | private static $has_one = array( |
|
49 | 'Blog' => Blog::class, |
|
50 | ); |
|
51 | ||
52 | /** |
|
53 | * {@inheritdoc} |
|
54 | */ |
|
55 | public function getCMSFields() |
|
56 | { |
|
57 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
58 | $fields[] = DropdownField::create( |
|
59 | 'BlogID', |
|
60 | _t('BlogCategoriesWidget.Blog', 'Blog'), |
|
61 | Blog::get()->map() |
|
62 | ); |
|
63 | ||
64 | $fields[] = NumericField::create( |
|
65 | 'Limit', |
|
66 | _t('BlogCategoriesWidget.Limit.Label', 'Limit'), |
|
67 | 0 |
|
68 | ) |
|
69 | ->setDescription( |
|
70 | _t( |
|
71 | 'BlogCategoriesWidget.Limit.Description', |
|
72 | 'Limit the number of categories shown by this widget (set to 0 to show all categories).' |
|
73 | ) |
|
74 | ) |
|
75 | ->setMaxLength(3); |
|
76 | ||
77 | $fields[] = DropdownField::create( |
|
78 | 'Order', |
|
79 | _t('BlogCategoriesWidget.Sort.Label', 'Sort'), |
|
80 | array('Title' => 'Title', 'Created' => 'Created', 'LastEdited' => 'Updated') |
|
81 | ) |
|
82 | ->setDescription( |
|
83 | _t('BlogCategoriesWidget.Sort.Description', 'Change the order of categories shown by this widget.') |
|
84 | ); |
|
85 | ||
86 | $fields[] = DropdownField::create( |
|
87 | 'Direction', |
|
88 | _t('BlogCategoriesWidget.Direction.Label', 'Direction'), |
|
89 | array('ASC' => 'Ascending', 'DESC' => 'Descending') |
|
90 | ) |
|
91 | ->setDescription( |
|
92 | _t( |
|
93 | 'BlogCategoriesWidget.Direction.Description', |
|
94 | 'Change the direction of ordering of categories shown by this widget.' |
|
95 | ) |
|
96 | ); |
|
97 | }); |
|
98 | ||
99 | return parent::getCMSFields(); |
|
100 | } |
|
101 | ||
102 | /** |
|
103 | * @return DataList |
|
104 | */ |
|
105 | public function getCategories() |
|
106 | { |
|
107 | $blog = $this->Blog(); |
|
108 | ||
109 | if (!$blog) { |
|
110 | return array(); |
|
111 | } |
|
112 | ||
113 | $query = $blog->Categories(); |
|
114 | ||
115 | if ($this->Limit) { |
|
116 | $query = $query->limit(Convert::raw2sql($this->Limit)); |
|
117 | } |
|
118 | ||
119 | if ($this->Order && $this->Direction) { |
|
120 | $query = $query->sort(Convert::raw2sql($this->Order), Convert::raw2sql($this->Direction)); |
|
121 | } |
|
122 | ||
123 | return $query; |
|
124 | } |
|
125 | } |
|
126 | ||
127 | ||
128 |
@@ 19-125 (lines=107) @@ | ||
16 | /** |
|
17 | * @method Blog Blog() |
|
18 | */ |
|
19 | class BlogTagsWidget extends Widget |
|
20 | { |
|
21 | /** |
|
22 | * @var string |
|
23 | */ |
|
24 | private static $title = 'Tags'; |
|
25 | ||
26 | /** |
|
27 | * @var string |
|
28 | */ |
|
29 | private static $cmsTitle = 'Blog Tags'; |
|
30 | ||
31 | /** |
|
32 | * @var string |
|
33 | */ |
|
34 | private static $description = 'Displays a list of blog tags.'; |
|
35 | ||
36 | /** |
|
37 | * @var array |
|
38 | */ |
|
39 | private static $db = array( |
|
40 | 'Limit' => 'Int', |
|
41 | 'Order' => 'Varchar', |
|
42 | 'Direction' => 'Varchar', |
|
43 | ); |
|
44 | ||
45 | /** |
|
46 | * @var array |
|
47 | */ |
|
48 | private static $has_one = array( |
|
49 | 'Blog' => Blog::class |
|
50 | ); |
|
51 | ||
52 | /** |
|
53 | * {@inheritdoc} |
|
54 | */ |
|
55 | public function getCMSFields() |
|
56 | { |
|
57 | $this->beforeUpdateCMSFields(function (Fieldlist $fields) { |
|
58 | $fields[] = DropdownField::create( |
|
59 | 'BlogID', |
|
60 | _t('BlogTagsWidget.Blog', 'Blog'), |
|
61 | Blog::get()->map() |
|
62 | ); |
|
63 | ||
64 | $fields[] = NumericField::create( |
|
65 | 'Limit', |
|
66 | _t('BlogTagsWidget.Limit.Label', 'Limit'), |
|
67 | 0 |
|
68 | ) |
|
69 | ->setDescription( |
|
70 | _t( |
|
71 | 'BlogTagsWidget.Limit.Description', |
|
72 | 'Limit the number of tags shown by this widget (set to 0 to show all tags).' |
|
73 | ) |
|
74 | ) |
|
75 | ->setMaxLength(3); |
|
76 | ||
77 | $fields[] = DropdownField::create( |
|
78 | 'Order', |
|
79 | _t('BlogTagsWidget.Sort.Label', 'Sort'), |
|
80 | array('Title' => 'Title', 'Created' => 'Created', 'LastEdited' => 'Updated') |
|
81 | ) |
|
82 | ->setDescription( |
|
83 | _t('BlogTagsWidget.Sort.Description', 'Change the order of tags shown by this widget.') |
|
84 | ); |
|
85 | ||
86 | $fields[] = DropdownField::create( |
|
87 | 'Direction', |
|
88 | _t('BlogTagsWidget.Direction.Label', 'Direction'), |
|
89 | array('ASC' => 'Ascending', 'DESC' => 'Descending') |
|
90 | ) |
|
91 | ->setDescription( |
|
92 | _t( |
|
93 | 'BlogTagsWidget.Direction.Description', |
|
94 | 'Change the direction of ordering of tags shown by this widget.' |
|
95 | ) |
|
96 | ); |
|
97 | }); |
|
98 | ||
99 | return parent::getCMSFields(); |
|
100 | } |
|
101 | ||
102 | /** |
|
103 | * @return DataList |
|
104 | */ |
|
105 | public function getTags() |
|
106 | { |
|
107 | $blog = $this->Blog(); |
|
108 | ||
109 | if (!$blog) { |
|
110 | return array(); |
|
111 | } |
|
112 | ||
113 | $query = $blog->Tags(); |
|
114 | ||
115 | if ($this->Limit) { |
|
116 | $query = $query->limit(Convert::raw2sql($this->Limit)); |
|
117 | } |
|
118 | ||
119 | if ($this->Order && $this->Direction) { |
|
120 | $query = $query->sort(Convert::raw2sql($this->Order), Convert::raw2sql($this->Direction)); |
|
121 | } |
|
122 | ||
123 | return $query; |
|
124 | } |
|
125 | } |
|
126 |