1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The home page controller. |
5
|
|
|
*/ |
6
|
|
|
class HomeController extends SiteController |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
private static $popular_blacklist = array( |
10
|
|
|
'silverstripe/framework', |
11
|
|
|
'silverstripe/cms', |
12
|
|
|
'silverstripe/sqlite3', |
13
|
|
|
'silverstripe/postgresql', |
14
|
|
|
'silverstripe/reports', |
15
|
|
|
'silverstripe/siteconfig', |
16
|
|
|
'silverstripe-themes/simple' |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
public static $allowed_actions = array( |
20
|
|
|
'index' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
public function index() |
24
|
|
|
{ |
25
|
|
|
return $this->renderWith(array('Home', 'Page')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function Title() |
29
|
|
|
{ |
30
|
|
|
return 'Home'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function Link() |
34
|
|
|
{ |
35
|
|
|
return Director::baseURL(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param int $limit |
40
|
|
|
* @return DataList|SS_Limitable default to Relative addons |
41
|
|
|
*/ |
42
|
|
|
public function PopularAddons($limit = 10) |
43
|
|
|
{ |
44
|
|
|
return self::RelativePopularAddons($limit); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param int $limit |
49
|
|
|
* @return DataList|SS_Limitable List sorted by absolute downloads |
50
|
|
|
*/ |
51
|
|
|
public static function AbsolutePopularAddons($limit = 10) |
52
|
|
|
{ |
53
|
|
|
return Addon::get() |
54
|
|
|
->sort('Downloads', 'DESC') |
55
|
|
|
->exclude('Name', self::config()->popular_blacklist) |
56
|
|
|
->limit($limit); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function RelativePopularAddons($limit = 10) |
60
|
|
|
{ |
61
|
|
|
$addons = Addon::get() |
62
|
|
|
->exclude(array('Name' => self::config()->popular_blacklist)); |
63
|
|
|
$list = ArrayList::create($addons->toArray()); |
64
|
|
|
/** @var ArrayList $addons */ |
65
|
|
|
$addons = $list->sort('relativePopularity DESC'); |
66
|
|
|
$addons = $addons->limit($limit); |
67
|
|
|
foreach ($addons as $addon) { |
68
|
|
|
$addon->Score = $addon->relativePopularityFormatted() . ' per day'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $addons; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function NewestAddons($limit = 10) |
75
|
|
|
{ |
76
|
|
|
return Addon::get()->sort('Released', 'DESC')->limit($limit); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function RandomAddons($limit = 10) |
80
|
|
|
{ |
81
|
|
|
return Addon::get()->sort(DB::getConn()->random(), 'DESC')->limit($limit); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function NewestVersions($limit = 10) |
85
|
|
|
{ |
86
|
|
|
return AddonVersion::get() |
87
|
|
|
->filter('Development', false) |
88
|
|
|
->sort('Released', 'DESC') |
89
|
|
|
->limit($limit); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function ChartData() |
93
|
|
|
{ |
94
|
|
|
$chartData = array(); |
95
|
|
|
$list = ArrayList::create(array()); |
96
|
|
|
|
97
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
98
|
|
|
$sqlQuery->setFrom('Addon'); |
99
|
|
|
$sqlQuery->setSelect('DATE(Created) as Created'); |
100
|
|
|
$sqlQuery->selectField('COUNT(*)', 'CountInOneDay'); |
101
|
|
|
$sqlQuery->addWhere('"Created" >= DATE_SUB(NOW(), INTERVAL 30 DAY)'); |
102
|
|
|
$sqlQuery->addGroupBy('DATE(Created)'); |
103
|
|
|
|
104
|
|
|
$result = $sqlQuery->execute(); |
105
|
|
|
|
106
|
|
|
if (count($result)) { |
107
|
|
|
foreach ($result as $row) { |
108
|
|
|
$date = date('j M Y', strtotime($row['Created'])); |
109
|
|
|
if (!isset($chartData[$date])) { |
110
|
|
|
$chartData[$date] = $row['CountInOneDay']; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (count($chartData)) { |
116
|
|
|
foreach ($chartData as $x => $y) { |
117
|
|
|
$list->push(ArrayData::create(array( |
118
|
|
|
'XValue' => $x, |
119
|
|
|
'YValue' => $y |
120
|
|
|
))); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $list; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.