-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
getoptions.class.php
66 lines (56 loc) · 2.05 KB
/
getoptions.class.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
<?php
class msProductGetOptionsProcessor extends modObjectProcessor
{
public $classKey = 'msProductOption';
public function process()
{
$query = trim($this->getProperty('query'));
$start = (int)$this->getProperty('start', 0);
$limit = (int)$this->getProperty('limit', 10);
$key = preg_replace('#^options-(.*?)#', '$1', $this->getProperty('key'));
$values = [];
$c = $this->modx->newQuery('msProductOption');
$c->sortby('value', 'ASC');
$c->select('value');
$c->groupby('value');
$c->where(['key' => $key]);
$c->limit(0);
if (!empty($query)) {
$c->where(['value:LIKE' => "%{$query}%"]);
}
if ($c->prepare() && $c->stmt->execute()) {
if ($tmp = $c->stmt->fetchAll(PDO::FETCH_COLUMN)) {
$values = $tmp;
}
}
if ($exclude = json_decode($this->getProperty('exclude'), true)) {
$values = array_diff($values, $exclude);
}
$values = $this->prepareValues($values, $query);
$count = count($values);
$values = array_slice($values, $start, $limit);
return $this->outputArray($values, $count);
}
public function prepareValues($values, $query = '')
{
if ($words = array_diff(array_map('trim', explode('|', $query)), [''])) {
$search = [];
foreach ($words as $word) {
$s = preg_quote($word, '\\');
$found = preg_grep("!{$s}!usi", $values);
if (is_array($found) && !preg_grep("!^{$s}$!si", $found)) {
array_unshift($found, $word);
}
$search = $found ? array_merge($search, $found) : $search;
}
$values = $search;
}
$values = array_keys(array_flip($values));
$values = array_diff($values, ['']);
foreach ($values as $id => $value) {
$values[$id] = ['value' => $value];
}
return $values;
}
}
return 'msProductGetOptionsProcessor';