1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Widgets; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\Blog; |
6
|
|
|
use SilverStripe\Core\Convert; |
7
|
|
|
use SilverStripe\Forms\DropdownField; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\NumericField; |
10
|
|
|
use SilverStripe\ORM\DataList; |
11
|
|
|
use SilverStripe\Widgets\Model\Widget; |
12
|
|
|
|
13
|
|
|
if (!class_exists(Widget::class)) { |
14
|
|
|
return; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method Blog Blog() |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.