-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathWildcard.php
267 lines (230 loc) · 9.44 KB
/
Wildcard.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* This file is part of the Sprog package.
*
* @author (c) Thomas Blum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sprog;
class Wildcard
{
public static function getOpenTag()
{
return \rex_config::get('sprog', 'wildcard_open_tag', '{{ ');
}
public static function getCloseTag()
{
return \rex_config::get('sprog', 'wildcard_close_tag', ' }}');
}
public static function getRegexp($value = '.*?')
{
return '@(?<complete>'.preg_quote(trim(self::getOpenTag())).'\s*(?<wildcard>'.$value.')\s*((\|(?<filter>\s*[a-z]+)\(?(?<arguments>.*?)?\)?))?\s*'.preg_quote(trim(self::getCloseTag())).')@';
}
public static function isClangSwitchMode()
{
return (\rex_config::get('sprog', 'wildcard_clang_switch', '1') == 1) ? true : false;
}
/**
* Returns the replaced wildcard.
*
* @param string $wildcard
* @param int $clang_id
*
* @return string
*/
public static function get($wildcard, $clang_id = null)
{
if (trim($wildcard) == '') {
return $wildcard;
}
if (!$clang_id) {
$clang_id = \rex_clang::getCurrentId();
}
$clangBase = \rex_config::get('sprog', 'clang_base');
if (isset($clangBase[$clang_id])) {
$clang_id = $clangBase[$clang_id];
}
$sql = \rex_sql::factory();
$sql->setQuery('SELECT `replace` FROM '.\rex::getTable('sprog_wildcard').' WHERE clang_id = :clang_id AND `wildcard` = :wildcard', ['clang_id' => $clang_id, 'wildcard' => trim($wildcard)]);
if ($sql->getRows() == 1 && trim($sql->getValue('replace')) != '') {
return self::replace($wildcard, $sql->getValue('replace'));
}
return false;
}
/**
* Returns the replaced content.
*
* @param string $content
* @param int $clang_id
*
* @return string
*/
public static function parse($content, $clang_id = null)
{
if (trim($content) == '') {
return $content;
}
preg_match_all(self::getRegexp(), $content, $matches, PREG_SET_ORDER);
if (count($matches) < 1) {
return $content;
}
if (!$clang_id) {
$clang_id = \rex_clang::getCurrentId();
}
$clangBase = \rex_config::get('sprog', 'clang_base');
if (isset($clangBase[$clang_id])) {
$clang_id = $clangBase[$clang_id];
}
$sql = \rex_sql::factory();
$items = $sql->getArray('SELECT `wildcard`, `replace` FROM '.\rex::getTable('sprog_wildcard').' WHERE clang_id = :clang_id', ['clang_id' => $clang_id]);
if (count($items) < 1) {
return $content;
}
$wildcards = [];
foreach ($items as $item) {
$wildcards[$item['wildcard']] = $item['replace'];
}
$filters = \rex::getProperty('SPROG_FILTER', []);
$search = [];
$replace = [];
foreach ($matches as $match) {
$value = $match['complete'];
if (isset($wildcards[$match['wildcard']])) {
$value = $wildcards[$match['wildcard']];
}
if (isset($match['filter']) && isset($filters[trim($match['filter'])])) {
$filter = $filters[trim($match['filter'])];
$arguments = isset($match['arguments']) ? $match['arguments'] : '';
$value = $filter->fire($value, $arguments);
} else {
$value = self::replace($match['wildcard'], $value);
}
$search[] = $match['complete'];
$replace[] = $value;
}
return str_replace($search, $replace, $content);
}
public static function getMissingWildcards()
{
$wildcards = [];
if (\rex_addon::get('structure')->isAvailable() && \rex_plugin::get('structure', 'content')->isAvailable()) {
$sql = \rex_sql::factory();
// Slices der Artikel durchsuchen
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$fields = [
's.value' => range('1', '20'),
];
$searchFields = [];
$selectFields = [];
foreach ($fields as $field => $numbers) {
$concatFields = [];
foreach ($numbers as $number) {
$concatFields[] = $field.$number;
$searchFields[] = $field.$number.' RLIKE '.$sql->escape(preg_quote(trim(self::getOpenTag())).'.*'.preg_quote(trim(self::getCloseTag())));
}
$selectFields[] = 'CONCAT_WS("|", '.implode(',', $concatFields).') AS subject';
}
$fields = $searchFields;
$sql_query = ' SELECT s.article_id AS id,
s.clang_id,
s.ctype_id,
'.implode(', ', $selectFields).'
FROM '.\rex::getTable('article_slice').' AS s
LEFT JOIN
'.\rex::getTable('article').' AS a
ON (s.article_id = a.id AND s.clang_id = a.clang_id)
WHERE '.implode(' OR ', $fields).'
';
$sql->setDebug(false);
$sql->setQuery($sql_query);
if ($sql->getRows() >= 1) {
$items = $sql->getArray();
foreach ($items as $item) {
preg_match_all(self::getRegexp(), $item['subject'], $matchesSubject, PREG_SET_ORDER);
foreach ($matchesSubject as $match) {
$wildcard = $match['wildcard'];
$wildcards[$wildcard]['wildcard'] = $wildcard;
$wildcards[$wildcard]['url'] = \rex_url::backendController(
[
'page' => 'content/edit',
'article_id' => $item['id'],
'mode' => 'edit',
'clang' => $item['clang_id'],
'ctype' => $item['ctype_id'],
]
);
}
}
}
// Alle bereits angelegten Platzhalter entfernen
if (count($wildcards)) {
$sql = \rex_sql::factory();
$sql->setDebug(false);
$sql->setQuery('
SELECT wildcard
FROM '.\rex::getTable('sprog_wildcard').'
WHERE clang_id = :clang_id',
['clang_id' => \rex_clang::getStartId()]
);
if ($sql->getRows() >= 1) {
$items = $sql->getArray();
foreach ($items as $item) {
if (isset($wildcards[$item['wildcard']])) {
unset($wildcards[$item['wildcard']]);
}
}
}
}
return $wildcards;
}
return false;
}
public static function getMissingWildcardsAsTable()
{
$missingWildcards = self::getMissingWildcards();
if (count($missingWildcards)) {
$content = '';
$content .= '
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="rex-table-icon"></th>
<th>'.\rex_addon::get('sprog')->i18n('wildcard').'</th>
<th class="rex-table-action" colspan="2">'.\rex_addon::get('sprog')->i18n('function').'</th>
</tr>
</thead>
<tbody>
';
foreach ($missingWildcards as $name => $params) {
$content .= '
<tr>
<td class="rex-table-icon"><i class="rex-icon rex-icon-refresh"></i></td>
<td data-title="'.\rex_addon::get('sprog')->i18n('wildcard').'">'.$name.'</td>
<td class="rex-table-action"><a href="'.\rex_url::currentBackendPage(['func' => 'add', 'wildcard_name' => $params['wildcard']]).'"><i class="rex-icon rex-icon-edit"></i> '.\rex_addon::get('sprog')->i18n('function_add').'</a></td>
<td class="rex-table-action"><a href="'.$params['url'].'"><i class="rex-icon rex-icon-article"></i> '.\rex_addon::get('sprog')->i18n('wildcard_go_to_the_article').'</a></td>
</tr>';
}
$content .= '
</tbody>
</table>';
$fragment = new \rex_fragment();
$fragment->setVar('title', \rex_addon::get('sprog')->i18n('wildcard_caption_missing', \rex_addon::get('structure')->i18n('title_structure')), false);
$fragment->setVar('content', $content, false);
$content = $fragment->parse('core/page/section.php');
return $content;
}
}
/**
* Returns the replaced wildcard.
*
* @param string $wildcard
*
* @return string
*/
protected static function replace($wildcard, $replace)
{
return nl2br($replace);
}
}