-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathListFormatHelper.php
204 lines (155 loc) · 5.67 KB
/
ListFormatHelper.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Bolt\Utils;
use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Repository\ContentRepository;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Tightenco\Collect\Support\Collection;
class ListFormatHelper
{
/** @var Connection */
private $connection;
/** @var string */
private $prefix;
/** @var ContentRepository */
private $contentRepository;
/** @var EntityManagerInterface */
private $em;
/** @var string */
private $backendUrl = '/bolt';
public function __construct(
Connection $connection,
ContentRepository $contentRepository,
EntityManagerInterface $em,
string $tablePrefix = 'bolt_',
string $backendUrl = 'bolt')
{
$this->connection = $connection;
$this->prefix = $tablePrefix;
$this->contentRepository = $contentRepository;
$this->em = $em;
$this->backendUrl = preg_replace('/[^\pL\d,]+/u', '', $backendUrl);
}
public function clearColumns(): bool
{
$query = sprintf('UPDATE %scontent SET title = "", list_format = ""', $this->prefix);
$result = $this->connection->executeQuery($query);
return (bool) $result;
}
public function updateColumns(int $limit = 100): bool
{
$query = sprintf('SELECT COUNT(id) FROM %scontent WHERE title = "" OR title IS NULL OR list_format = "" LIMIT %d', $this->prefix, $limit);
$result = $this->connection->fetchAssociative($query);
printf("%d records to go", $result['COUNT(id)']);
$query = sprintf('SELECT id FROM %scontent WHERE title = "" OR title IS NULL OR list_format = "" LIMIT %d', $this->prefix, $limit);
$rows = $this->connection->fetchAllAssociative($query);
$counter = 0;
foreach ($rows as $row) {
$record = $this->contentRepository->findOneById($row['id']);
$record->setListFormat();
$this->em->persist($record);
$counter++;
if ($counter > 20) {
echo '.';
$this->em->flush();
$counter = 0;
}
}
$this->em->flush();
return true;
}
public function getRelated(Collection $contentType, int $amount, string $order): array
{
$order = $this->fixOrder($order);
$query = sprintf(
'SELECT id, list_format FROM %scontent WHERE content_type = "%s" ORDER BY %s LIMIT %d ',
$this->prefix,
$contentType['slug'],
$order,
$amount
);
$rows = $this->connection->fetchAllAssociative($query);
$options = [];
foreach ($rows as $row) {
$options[] = ['key' => (int) $row['id'], 'value' => $row['list_format']];
}
return $options;
}
public function getMenuLinks(ContentType $contentType, int $amount, string $order): array
{
$order = $this->fixOrder($order);
$query = sprintf(
'SELECT id, title FROM %scontent WHERE content_type = "%s" ORDER BY %s LIMIT %d ',
$this->prefix,
$contentType['slug'],
$order,
$amount
);
$rows = $this->connection->fetchAllAssociative($query);
$options = [];
foreach ($rows as $row) {
$options[] = [
'id' => (int) $row['id'],
'name' => $row['title'],
'link' => sprintf('/%s/edit/%s', $this->backendUrl, $row['id'])
];
}
return $options;
}
public function getSelect(string $contentType, array $params): array
{
$order = $this->fixOrder($params['order']);
$contentTypes = $this->split($contentType);
$amount = (int) $params['limit'];
$query = sprintf(
'SELECT id, list_format FROM %scontent WHERE content_type IN (%s) ORDER BY %s LIMIT %d ',
$this->prefix,
$contentTypes,
$order,
$amount
);
$rows = $this->connection->fetchAllAssociative($query);
$options = [];
foreach ($rows as $row) {
$options[] = ['key' => (int) $row['id'], 'value' => $row['list_format']];
}
return $options;
}
public function fixOrder(string $order)
{
if (mb_strpos($order, '-') === 0) {
$direction = 'DESC';
$order = mb_substr($order, 1);
} elseif (mb_strpos($order, ' DESC') !== false) {
$direction = 'DESC';
$order = str_replace(' DESC', '', $order);
} else {
$order = str_replace(' ASC', '', $order);
$direction = 'ASC';
}
$replacements = [
'created' => 'created_at',
'createdat' => 'created_at',
'datechanged' => 'modified_at',
'datecreated' => 'created_at',
'datepublish' => 'published_at',
'modified' => 'modified_at',
'modifiedat' => 'modified_at',
'published' => 'published_at',
'publishedat' => 'published_at',
'_at_at' => '_at',
];
$order = str_replace(array_keys($replacements), array_values($replacements), mb_strtolower($order));
if (!in_array($order, ['id', 'content_type', 'status', 'created_at', 'published_at', 'modified_at', 'title', 'list_format'])) {
$order = 'title';
}
return $order . ' ' . $direction;
}
private function split($contenttypes): string
{
$parts = explode(',', preg_replace('/^\((.*)\)$/', '$1', $contenttypes));
$result = sprintf('"%s"', implode('", "', $parts));
return $result;
}
}