-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassifieds.php
More file actions
159 lines (139 loc) · 3.97 KB
/
classifieds.php
File metadata and controls
159 lines (139 loc) · 3.97 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace KVSun\KVSAPI;
use \shgysk8zer0\Core\{PDO};
use \SplFileObject as CSV;
use \RecursiveDirectoryIterator as DirectoryScanner;
use \PDOStatement;
final class Classifieds extends Abstracts\Content
{
const DEFAULTS = [
'ads' => [],
'categories' => [],
'content' => [],
'title' => 'Classifieds',
'description' => '',
'keywords' => [
'classifieds',
'help wanted',
],
];
const TYPE = 'classifieds';
const ALLOWED_TAGS = '<b><p><div><br><hr>';
const IMG_DIR = '/classifieds/01%20Current%20Graphics/';
const AD_FEED = '/01 Current Graphics/display ad feed.5.1.csv';
const DISP_AD_PATTERN = '/\s*\*+\s*DISPLAY\s+AD\s*\*+\s?/im';
private $_dir = '';
/**
* Create a new instance of Classifieds class
* @param PDO $pdo PDO instance
* @param String $dir Directory containing classifieds HTML files and images
*/
public function __construct(PDO $pdo, String $dir)
{
if (! is_dir($dir)) {
$this->setStatus(500);
throw new \InvalidArgumentException("{$dir} is not a directory");
} else {
$this->_dir = $dir;
$this::_init($pdo, '/classifieds');
}
}
/**
* Returns SQL to create prepared statement from
* @return String SELECT statement
*/
protected function _getSQL(): String
{
return 'SELECT `id`, `name` FROM `classifieds`;';
}
/**
* Set instance data using prepared statement to execute
* @param PDOStatement $stm Statment prepared using `$this::getSql`
*/
protected function _setData(PDOStatement $stm)
{
$stm->execute();
$cats = $stm->fetchAll(PDO::FETCH_CLASS);
$cats = array_reduce($cats, [$this, '_reduceCats'], []);
$this->_set('ads', $this->_parseCSV($this->_dir . self::AD_FEED));
$this->_set('categories', $cats);
$this->_set('content', $this->_scanDir($cats));
}
/**
* Convert categories object into a category code indexed array
* @param Array $cats [$id => $name]
* @param stdClass $cat {"id": $id, "name": $name}
* @return Array $cats with $cat appended to it
*/
private function _reduceCats(Array $cats, \stdClass $cat): Array
{
$cats[$cat->id] = $cat->name;
return $cats;
}
/**
* Scans directory for HTML files listed in $cats
* @param Array $cats [$id => $name,...]
* @return Array Array of HTML for each category
*/
private function _scanDir(Array $cats): Array
{
$scanner = new DirectoryScanner($this->_dir);
$scanner->setFlags(DirectoryScanner::SKIP_DOTS);
$content = [];
while ($scanner->valid()) {
if (
$scanner->isFile()
and $scanner->getExtension() === 'html'
and $scanner->isReadable()
and array_key_exists($scanner->getBasename('.html'), $cats)
) {
$content[$scanner->getBasename('.html')] = $this->_parseHTML($scanner->getPathname());
}
$scanner->next();
}
return $content;
}
private function _parseHTML(String $fname): String
{
$html = file_get_contents($fname);
$html = strip_tags($html, self::ALLOWED_TAGS);
$html = preg_replace(self::DISP_AD_PATTERN, null, $html);
return $html;
}
/**
* Parses a CSV file containing ad data
* @param String $fname /path/to/file.csv
* @return Array Array of ad data
*/
private function _parseCSV(String $fname): Array
{
ini_set('auto_detect_line_endings', true);
$csv = new CSV($fname);
$csv->setFlags(CSV::READ_CSV | CSV::DROP_NEW_LINE | CSV::SKIP_EMPTY);
$rows = [];
if ($csv->valid()) {
$headers = $csv->fgetcsv();
$csv->next();
$header_size = count($headers);
}
while ($csv->valid()) {
$row = $csv->fgetcsv();
$csv->next();
if (empty($row)) {
continue;
}
$row = array_pad($row, $header_size, null);
$row = array_combine($headers, $row);
if (isset($row['Category Code'], $row['Ad Text'], $row['Filename'])) {
if (! array_key_exists($row['Category Code'], $rows)) {
$rows[$row['Category Code']] = [];
}
$rows[$row['Category Code']][] = [
'text' => $row['Ad Text'],
'image' => self::IMG_DIR . rawurlencode($row['Filename']),
];
}
}
return $rows;
}
}