1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2016 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\LeadBundle\Entity; |
||
13 | |||
14 | use Mautic\LeadBundle\Segment\OperatorOptions; |
||
15 | |||
16 | trait OperatorListTrait |
||
17 | { |
||
18 | protected $typeOperators = [ |
||
19 | 'text' => [ |
||
20 | 'include' => [ |
||
21 | '=', |
||
22 | '!=', |
||
23 | 'empty', |
||
24 | '!empty', |
||
25 | 'like', |
||
26 | '!like', |
||
27 | 'regexp', |
||
28 | '!regexp', |
||
29 | 'startsWith', |
||
30 | 'endsWith', |
||
31 | 'contains', |
||
32 | ], |
||
33 | ], |
||
34 | 'select' => [ |
||
35 | 'include' => [ |
||
36 | '=', |
||
37 | '!=', |
||
38 | 'empty', |
||
39 | '!empty', |
||
40 | 'regexp', |
||
41 | '!regexp', |
||
42 | 'in', |
||
43 | '!in', |
||
44 | ], |
||
45 | ], |
||
46 | 'bool' => [ |
||
47 | 'include' => [ |
||
48 | '=', |
||
49 | '!=', |
||
50 | ], |
||
51 | ], |
||
52 | 'default' => [ |
||
53 | 'exclude' => [ |
||
54 | 'in', |
||
55 | '!in', |
||
56 | 'date', |
||
57 | ], |
||
58 | ], |
||
59 | 'multiselect' => [ |
||
60 | 'include' => [ |
||
61 | 'in', |
||
62 | '!in', |
||
63 | 'empty', |
||
64 | '!empty', |
||
65 | ], |
||
66 | ], |
||
67 | 'date' => [ |
||
68 | 'exclude' => [ |
||
69 | 'in', |
||
70 | '!in', |
||
71 | ], |
||
72 | ], |
||
73 | 'lookup_id' => [ |
||
74 | 'include' => [ |
||
75 | '=', |
||
76 | '!=', |
||
77 | 'empty', |
||
78 | '!empty', |
||
79 | ], |
||
80 | ], |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * @param null $operator |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getFilterExpressionFunctions($operator = null) |
||
89 | { |
||
90 | $operatorOption = OperatorOptions::getFilterExpressionFunctions(); |
||
91 | |||
92 | return (null === $operator) ? $operatorOption : $operatorOption[$operator]; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string|array|null $type |
||
97 | * @param array $overrideHiddenTypes |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getOperatorsForFieldType($type = null, $overrideHiddenTypes = []) |
||
102 | { |
||
103 | static $processedTypes = []; |
||
104 | |||
105 | if (is_array($type)) { |
||
106 | return $this->getOperatorChoiceList($type, $overrideHiddenTypes); |
||
107 | } elseif (array_key_exists($type, $processedTypes)) { |
||
108 | return $processedTypes[$type]; |
||
109 | } |
||
110 | |||
111 | $this->normalizeType($type); |
||
112 | |||
113 | if (null === $type) { |
||
114 | foreach ($this->typeOperators as $type => $def) { |
||
115 | if (!array_key_exists($type, $processedTypes)) { |
||
116 | $processedTypes[$type] = $this->getOperatorChoiceList($def, $overrideHiddenTypes); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | return $processedTypes; |
||
121 | } |
||
122 | |||
123 | $processedTypes[$type] = $this->getOperatorChoiceList($this->typeOperators[$type], $overrideHiddenTypes); |
||
124 | |||
125 | return $processedTypes[$type]; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param $definition |
||
130 | * @param array $overrideHiddenOperators |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function getOperatorChoiceList($definition, $overrideHiddenOperators = []) |
||
135 | { |
||
136 | static $operatorChoices = []; |
||
137 | if (empty($operatorChoices)) { |
||
138 | $operatorList = $this->getFilterExpressionFunctions(); |
||
139 | $operatorChoices = []; |
||
140 | foreach ($operatorList as $operator => $def) { |
||
141 | if (empty($def['hide']) || in_array($operator, $overrideHiddenOperators)) { |
||
142 | $operatorChoices[$operator] = $def['label']; |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | $choices = $operatorChoices; |
||
148 | if (isset($definition['include'])) { |
||
149 | // Inclusive operators |
||
150 | $choices = array_intersect_key($choices, array_flip($definition['include'])); |
||
151 | } elseif (isset($definition['exclude'])) { |
||
152 | // Exclusive operators |
||
153 | $choices = array_diff_key($choices, array_flip($definition['exclude'])); |
||
154 | } |
||
155 | |||
156 | if (isset($this->translator)) { |
||
157 | foreach ($choices as $value => $label) { |
||
158 | $choices[$value] = $this->translator->trans($label); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return array_flip($choices); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Normalize type operator. |
||
167 | * |
||
168 | * @param $type |
||
169 | */ |
||
170 | protected function normalizeType(&$type) |
||
171 | { |
||
172 | if (null === $type) { |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | if ('boolean' === $type) { |
||
177 | $type = 'bool'; |
||
178 | } elseif (in_array($type, ['country', 'timezone', 'region', 'locale'])) { |
||
179 | $type = 'select'; |
||
180 | } elseif (in_array($type, ['lookup', 'text', 'email', 'url', 'email', 'tel'])) { |
||
181 | $type = 'text'; |
||
182 | } elseif ('datetime' === $type) { |
||
183 | $type = 'date'; |
||
184 | } elseif (!array_key_exists($type, $this->typeOperators)) { |
||
185 | $type = 'default'; |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 |