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