-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhome.php
More file actions
139 lines (125 loc) · 3.71 KB
/
home.php
File metadata and controls
139 lines (125 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace KVSun\KVSAPI;
use \shgysk8zer0\Core\{PDO};
use \PDOStatement;
use \stdClass;
final class Home extends Abstracts\Content
{
use Traits\Images;
/**
* Type of content
* @var string
*/
const TYPE = 'home';
/**
* Default content
* @var Array
*/
const DEFAULTS = [
'categories' => null,
];
/**
* Creates an instance of Home containing categories and articles
* @param PDO $pdo Instance of database connection
* @param String $url URL to retrieve data for (this should always be '/')
* @param Array $categories Array of categories to get data for
* @param integer $count Max number of results
*/
public function __construct(
PDO $pdo,
String $url = null,
Array $categories,
Int $count = 16
)
{
$this->_categories = $categories;
$this->_count = $count;
$this->_init($pdo, $url);
}
/**
* Required method to get query to execute
* @return String SQL query
*/
protected function _getSQL(): String
{
return "SELECT
`posts`.`title`,
`posts`.`author`,
`posts`.`img`,
`posts`.`url`,
`posts`.`posted`,
`posts`.`isFree`,
`posts`.`keywords`,
`posts`.`description`,
`posts`.`draft`,
`categories`.`url-name` AS `catURL`,
`categories`.`icon`,
`categories`.`parent`,
`categories`.`name` AS `category`
FROM `posts`
JOIN `categories` ON `categories`.`id` = `posts`.`cat-id`
WHERE `categories`.`url-name` = :cat
ORDER BY `posted` DESC, `posts`.`sort` DESC
LIMIT {$this->_count};";
}
/**
* Required method for setting data
* @param PDOStatement $stm A prepared statment using `\PDO::prepare`
*/
protected function _setData(PDOStatement $stm)
{
$cats = new stdClass();
foreach ($this->_categories as $cat) {
$stm->bindParam(':cat', $cat);
$stm->execute();
$results = $stm->fetchAll(PDO::FETCH_CLASS) ?? [];
array_reduce($results, [$this, '_reduceSections'], $cats);
}
$this->_set('categories', $cats);
}
/**
* Maps data from table to more complex objects
* @param stdClass $sections Class containing all sections
* @param StdClass $post Individual post containing full data from table
* @return stdClass $sections with $post data appended, possibly creating a new section
*/
private function _reduceSections(stdClass $sections, StdClass $post): stdClass
{
$sec_name = $post->category;
// If section is not an object in $sections, create the object
// and give it properties that belong on sections/categories
if (! isset($sections->{$sec_name})) {
$sections->{$sec_name} = new stdClass();
$sections->{$sec_name}->posts = [];
$sections->{$sec_name}->icon = $post->icon;
$sections->{$sec_name}->catURL = $post->catURL;
$sections->{$sec_name}->parent = $post->parent;
}
// Section will exist in $sections now, so add individual post data
// to the array, of posts for the section
$sections->{$sec_name}->posts[] = (object)[
'title' => $post->title,
'author' => $post->author,
'description' => $post->description,
'keywords' => $this->_getKeywords($post->keywords ?? ''),
'img' => $post->img,
'url' => "{$post->catURL}/{$post->url}",
'posted' => $post->posted,
'isFree' => $post->isFree === '1',
'isDraft' => $post->draft === '1',
'image' => $this->_getImage(intval($post->img)),
];
return $sections;
}
/**
* Converts keywords from string to array (trimmed)
* @param String $keywords "keywords 1, keyword2, ..."
* @return Array ["keywords 1", "keyword2", ...]
*/
protected function _getKeywords(String $keywords): Array
{
return empty($keywords)
? []
: array_map('trim', array_filter(explode(',', $keywords)));
}
}