Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entityfilter.php
1<?php
2
4
11
13{
14 abstract public function getDocumentType();
15
16 public function getOrmFilter(ConditionGroup $conditionGroup, ?array $targetDocumentType = null): array
17 {
18 $filter = ['LOGIC' => 'OR'];
19
20 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
21 if (is_null($targetDocumentType))
22 {
23 $targetDocumentType = $this->getDocumentType();
24 }
25
26 $fieldsMap = $documentService->getDocumentFields($targetDocumentType);
27 $i = 0;
28 $filter[$i] = [];
29
31 foreach ($conditionGroup->getItems() as [$condition, $joiner])
32 {
33 $fieldId = $condition->getField();
34 if (!isset($fieldsMap[$fieldId]))
35 {
36 continue;
37 }
38
39 if ($condition->getOperator() === \Bitrix\Bizproc\Activity\Operator\BetweenOperator::getCode())
40 {
41 $betweenFilterResult = $this->getBetweenFilter($fieldsMap[$fieldId] ?? [], $condition);
42 if (!$betweenFilterResult->isSuccess())
43 {
44 continue;
45 }
46
47 $filter[$i][] = $betweenFilterResult->getData()['filter1'];
48 $filter[$i][] = $betweenFilterResult->getData()['filter2'];
49 }
50 elseif ($conditionGroup->isInternalized())
51 {
52 $value = $condition->getValue();
53 }
54 else
55 {
56 $extractionResult = $this->extractValue($fieldsMap[$fieldId], (string)$condition->getValue());
57 if ($extractionResult->isSuccess())
58 {
59 $value = $extractionResult->getData()['extractedValue'];
60 }
61 else
62 {
63 continue;
64 }
65 }
66
67 if ($fieldsMap[$fieldId]['Type'] === FieldType::USER && $value)
68 {
69 $value = \CBPHelper::extractUsers($value, $targetDocumentType);
70 }
71 elseif ($fieldsMap[$fieldId]['Type'] === FieldType::BOOL && isset($value))
72 {
73 $value = \CBPHelper::getBool($value);
74 }
75
76 switch ($condition->getOperator())
77 {
78 case 'empty':
79 $operator = '=';
80 $value = '';
81 break;
82
83 case '!empty':
84 $operator = '!=';
85 $value = '';
86 break;
87
88 case 'in':
89 $operator = '@';
90 break;
91
92 case '!in':
93 $operator = '!@';
94 break;
95
96 case 'contain':
97 $operator = '%';
98 break;
99
100 case '!contain':
101 $operator = '!%';
102 break;
103
104 case '>':
105 case '>=':
106 case '<':
107 case '<=':
108 case '=':
109 case '!=':
110 $operator = $condition->getOperator();
111 break;
112
113 default:
114 $operator = '';
115 break;
116 }
117
118 if (!$operator)
119 {
120 continue;
121 }
122
123 $filter[$i][] = $this->createRowFilter($operator, $condition->getField(), $value);
124
125 if ($joiner === ConditionGroup::JOINER_OR)
126 {
127 $filter[++$i] = [];
128 }
129 }
130
131 return $filter;
132 }
133
134 private function getBetweenFilter(array $property, Condition $condition): Result
135 {
136 $value = $condition->getValue();
137 $value_greater_then = (is_array($value) && isset($value[0]) ? $value[0] : $value);
138 $value_less_then = (is_array($value) && isset($value[1]) ? $value[1] : '');
139
140 $extractionResult1 = $this->extractValue($property, (string)$value_greater_then);
141 if (!$extractionResult1->isSuccess())
142 {
143 return Result::createOk()->addErrors($extractionResult1->getErrors());
144 }
145
146 $extractionResult2 = $this->extractValue($property, (string)$value_less_then);
147 if (!$extractionResult2->isSuccess())
148 {
149 return Result::createOk()->addErrors($extractionResult2->getErrors());
150 }
151
152 $filter1 = $this->createRowFilter('>=', $condition->getField(), $extractionResult1->getData()['extractedValue']);
153 $filter2 = $this->createRowFilter('<=', $condition->getField(), $extractionResult2->getData()['extractedValue']);
154
155 return Result::createOk(['filter1' => $filter1, 'filter2' => $filter2]);
156 }
157
158 private function createRowFilter(string $operator, string $field, $value): array
159 {
160 return [$operator . $field => $value];
161 }
162
163 protected function extractValue(array $fieldProperties, $value): Result
164 {
165 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
166
167 $field = $documentService->getFieldTypeObject($this->getDocumentType(), $fieldProperties);
168
169 $errors = [];
170 $extractionErrors = [];
171 if (!$field)
172 {
173 $errors[] = new Error('Can\'t create field type object');
174 }
175 else
176 {
177 if (!isset($fieldProperties['FieldName']))
178 {
179 $fieldProperties['FieldName'] = 'field_name';
180 }
181
182 $value = $field->extractValue(
183 ['Field' => $fieldProperties['FieldName']],
184 [$fieldProperties['FieldName'] => $value],
185 $extractionErrors,
186 );
187 }
188
189 foreach ($extractionErrors as $singleError)
190 {
191 if (is_array($singleError))
192 {
193 $errors[] = new Error(
194 $singleError['message'] ?? '',
195 $singleError['code'] ?? '',
196 $singleError['parameter'] ?? ''
197 );
198 }
199 }
200
201 return $errors ? Result::createOk()->addErrors($errors) : Result::createOk(['extractedValue' => $value]);
202 }
203
204 public static function extractFilterFromProperties(PropertiesDialog $dialog, array $fieldsMap): Result
205 {
206 $currentValues = $dialog->getCurrentValues();
207 $prefix = $fieldsMap['DynamicFilterFields']['FieldName'] . '_';
208
209 $conditionGroup = ['items' => []];
210
211 foreach ($currentValues[$prefix . 'field'] ?? [] as $index => $fieldName)
212 {
213 $operator = $currentValues[$prefix . 'operator'][$index];
214 if (
215 $operator === \Bitrix\Bizproc\Activity\Operator\BetweenOperator::getCode()
216 && isset($currentValues[$prefix . 'value'][$index], $currentValues[$prefix . 'value'][$index + 1])
217 )
218 {
219 $currentValues[$prefix . 'value'][$index] = [
220 $currentValues[$prefix . 'value'][$index],
221 $currentValues[$prefix . 'value'][$index + 1],
222 ];
223
224 array_splice($currentValues[$prefix . 'value'], $index + 1, 1);
225 }
226
227 $conditionGroup['items'][] = [
228 // condition
229 [
230 'object' => $currentValues[$prefix . 'object'][$index],
231 'field' => $currentValues[$prefix . 'field'][$index],
232 'operator' => $operator,
233 'value' => $currentValues[$prefix . 'value'][$index],
234 ],
235 // joiner
236 $currentValues[$prefix . 'joiner'][$index],
237 ];
238 }
239
240 $result = new Result();
241 $result->setData($conditionGroup);
242
243 return $result;
244 }
245}
static createOk(?array $data=null)
Definition result.php:20
extractValue(array $fieldProperties, $value)