-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
77 lines (60 loc) · 2.06 KB
/
Plugin.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
<?php
namespace MetadataKeyword;
use MapasCulturais\App;
class Plugin extends \MapasCulturais\Plugin
{
function __construct(array $config = [])
{
$config += [];
parent::__construct($config);
}
function _init()
{
$app = App::i();
$plugin = $this;
$this->buildKeywords();
}
function register()
{
}
/**
* @return void
*/
public function buildKeywords(): void
{
/** @var App $app */
$app = App::i();
$kw_config = $this->config;
$count = 0;
foreach ($kw_config as $slug => $values) {
foreach ($values as $value) {
$slo = $slug . "_" . $count;
$app->hook('repo(<<*>>).getIdsByKeywordDQL.join', function (&$joins, $keyword) use ($value, $slo, $app) {
$class_name = $this->getClassName();
$metadata = $app->getRegisteredMetadata($class_name);
if(!isset($metadata[$value])){
return;
}
$joins .= "
LEFT JOIN
e.__metadata " . $slo . "
WITH
e.id = " . $slo . ".owner AND " . $slo . ".key = '{$value}' ";
});
$app->hook('repo(<<*>>).getIdsByKeywordDQL.where', function (&$where, $keyword) use ($slo, $app, $value) {
$class_name = $this->getClassName();
$metadata = $app->getRegisteredMetadata($class_name);
if(!isset($metadata[$value])){
return;
}
if(trim($where)){
$where .= " OR (unaccent(lower(" . $slo . ".value)) LIKE unaccent(lower(:keyword)))";
}else{
$where .= " (unaccent(lower(" . $slo . ".value)) LIKE unaccent(lower(:keyword)))";
}
});
$count++;
}
}
}
}