1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class RecentVideosBlock |
5
|
|
|
* |
6
|
|
|
* @property int $Limit |
7
|
|
|
* @property int $VideoPageID |
8
|
|
|
* @method YouTubeIntegrationVideosPage $VideoPage |
9
|
|
|
*/ |
10
|
|
|
class RecentVideosBlock extends Block |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private static $singular_name = 'Recent Videos Block'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private static $plural_name = 'Recent Videos Blocks'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $db = array( |
26
|
|
|
'Limit' => 'Int', |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $has_one = array( |
33
|
|
|
'VideoPage' => 'YouTubeIntegrationVideosPage', |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $defaults = array( |
40
|
|
|
'Limit' => 3, |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var |
45
|
|
|
*/ |
46
|
|
|
private $recent_videos; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return FieldList |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function getCMSFields() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$fields = singleton('Block')->getCMSFields(); |
54
|
|
|
|
55
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
56
|
|
|
NumericField::create('Limit'), |
57
|
|
|
)); |
58
|
|
|
|
59
|
|
|
$fields->addFieldToTab( |
60
|
|
|
'Root.Main', |
61
|
|
|
DropdownField::create('VideoPageID', 'Featured Video Page', YouTubeIntegrationVideosPage::get()->map()) |
62
|
|
|
->setEmptyString('') |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return $fields; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param null $member |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function canCreate($member = null) |
73
|
|
|
{ |
74
|
|
|
return parent::canCreate($member); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param null $member |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function canView($member = null) |
82
|
|
|
{ |
83
|
|
|
return parent::canView($member); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function getRecentVideos() |
90
|
|
|
{ |
91
|
|
|
if (!$this->recent_videos) { |
92
|
|
|
$this->setRecentVideos(); |
93
|
|
|
} |
94
|
|
|
return $this->recent_videos; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setRecentVideos() |
101
|
|
|
{ |
102
|
|
|
$this->recent_videos = ($this->VideoPageID != 0) |
103
|
|
|
? $this->VideoPage()->getVideosList()->limit($this->Limit) |
|
|
|
|
104
|
|
|
: SilverStripeYouTubeVideo::get()->limit($this->Limit); |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
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.