Completed
Push — master ( e00726...1c8254 )
by Craig
07:10
created

AbstractSearchable::setTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\SearchModule;
13
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\QueryBuilder;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Zikula\Common\Translator\TranslatorTrait;
18
use Zikula\Core\AbstractModule;
19
20
abstract class AbstractSearchable
21
{
22
    use TranslatorTrait;
23
24
    const SEARCHABLE = 'searchable';
25
26
    /**
27
     * @var string The module name
28
     */
29
    protected $name;
30
31
    /**
32
     * @var EntityManager;
33
     */
34
    protected $entityManager;
35
36
    /**
37
     * @var ContainerInterface
38
     */
39
    protected $container;
40
41
    /**
42
     * @var array
43
     */
44
    private $errors = [];
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param ContainerInterface $container
50
     * @param AbstractModule $bundle
51
     */
52
    public function __construct(ContainerInterface $container, AbstractModule $bundle)
53
    {
54
        $this->container = $container;
55
        $this->entityManager = $container->get('doctrine')->getManager();
56
        $this->name = $bundle->getName();
57
        $this->setTranslator($this->container->get('translator.default'));
58
    }
59
60
    public function setTranslator($translator)
61
    {
62
        $this->translator = $translator;
63
    }
64
65
    /**
66
     * get the UI options for search form
67
     *
68
     * @param boolean $active if the module should be checked as active
69
     * @param array|null $modVars module form vars as previously set
70
     * @return string
71
     */
72
    abstract public function getOptions($active, $modVars = null);
73
74
    /**
75
     * Get the search results
76
     *
77
     * @param array $words array of words to search for
78
     * @param string $searchType AND|OR|EXACT
79
     * @param array|null $modVars module form vars passed though
80
     * @return array
81
     */
82
    abstract public function getResults(array $words, $searchType = 'AND', $modVars = null);
83
84
    /**
85
     * Construct a QueryBuilder Where orX|andX Expr instance
86
     *
87
     * @param QueryBuilder $qb
88
     * @param array $words the words to query for
89
     * @param array $fields
90
     * @param string $searchtype AND|OR|EXACT
91
     * @return null|\Doctrine\ORM\Query\Expr\Composite
92
     */
93
    public function formatWhere(QueryBuilder $qb, array $words, array $fields, $searchtype = 'AND')
94
    {
95
        if (empty($words) || empty($fields)) {
96
            return null;
97
        }
98
        $method = ($searchtype == 'OR') ? 'orX' : 'andX';
99
        /** @var $where \Doctrine\ORM\Query\Expr\Composite */
100
        $where = $qb->expr()->$method();
101
        $i = 1;
102
        foreach ($words as $word) {
103
            $subWhere = $qb->expr()->orX();
104
            foreach ($fields as $field) {
105
                $expr = $qb->expr()->like($field, "?$i");
106
                $subWhere->add($expr);
107
                $qb->setParameter($i, '%' . $word . '%');
108
                $i++;
109
            }
110
            $where->add($subWhere);
111
        }
112
113
        return $where;
114
    }
115
116
    /**
117
     * @param array $error
118
     */
119
    public function addError($error)
120
    {
121
        $this->errors[] = $this->name . ': ' . $error;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getErrors()
128
    {
129
        return $this->errors;
130
    }
131
132
    /**
133
     * @return ContainerInterface
134
     */
135
    public function getContainer()
136
    {
137
        return $this->container;
138
    }
139
}
140