|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Filters; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Builder; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | +use ProcessMaker\Models\ProcessRequestToken; |
| 8 | +use ProcessMaker\Models\User; |
| 9 | + |
| 10 | +class Filter |
| 11 | +{ |
| 12 | + const TYPE_PARTICIPANTS = 'Participants'; |
| 13 | + |
| 14 | + const TYPE_STATUS = 'Status'; |
| 15 | + |
| 16 | + const TYPE_FIELD = 'Field'; |
| 17 | + |
| 18 | + const TYPE_PROCESS = 'Process'; |
| 19 | + |
| 20 | + public string|null $subjectValue; |
| 21 | + |
| 22 | + public string $subjectType; |
| 23 | + |
| 24 | + public string $operator; |
| 25 | + |
| 26 | + public $value; |
| 27 | + |
| 28 | + public array $or; |
| 29 | + |
| 30 | + public static function filter(Builder $query, string $filterDefinitions) |
| 31 | + { |
| 32 | + $filterDefinitions = json_decode($filterDefinitions, true); |
| 33 | + $query->where(function ($query) use ($filterDefinitions) { |
| 34 | + foreach ($filterDefinitions as $filter) { |
| 35 | + (new self($filter))->addToQuery($query); |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + public function __construct($definition) |
| 41 | + { |
| 42 | + $this->subjectType = $definition['subject']['type']; |
| 43 | + $this->subjectValue = Arr::get($definition, 'subject.value'); |
| 44 | + $this->operator = $definition['operator']; |
| 45 | + $this->value = $definition['value']; |
| 46 | + $this->or = Arr::get($definition, 'or', []); |
| 47 | + } |
| 48 | + |
| 49 | + public function addToQuery(Builder $query) |
| 50 | + { |
| 51 | + if (!empty($this->or)) { |
| 52 | + $query->where(function ($query) { |
| 53 | + $this->apply($query); |
| 54 | + }); |
| 55 | + } else { |
| 56 | + $this->apply($query); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private function apply($query) |
| 61 | + { |
| 62 | + if ($valueAliasMethod = $this->valueAliasMethod()) { |
| 63 | + $this->valueAliasAdapter($valueAliasMethod, $query); |
| 64 | + } elseif ($this->subjectType === self::TYPE_PROCESS) { |
| 65 | + $this->filterByProcessId($query); |
| 66 | + } else { |
| 67 | + $this->applyQueryBuilderMethod($query); |
| 68 | + } |
| 69 | + |
| 70 | + if (!empty($this->or)) { |
| 71 | + $query->orWhere(function ($orQuery) { |
| 72 | + foreach ($this->or as $or) { |
| 73 | + (new self($or))->addToQuery($orQuery); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private function applyQueryBuilderMethod($query) |
| 80 | + { |
| 81 | + $method = $this->method(); |
| 82 | + |
| 83 | + if (in_array($method, ['whereIn', 'whereBetween', 'whereJsonContains'])) { |
| 84 | + $query->$method( |
| 85 | + $this->subject(), |
| 86 | + $this->value(), |
| 87 | + ); |
| 88 | + } else { |
| 89 | + $query->$method( |
| 90 | + $this->subject(), |
| 91 | + $this->operator(), |
| 92 | + $this->value(), |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private function operator() |
| 98 | + { |
| 99 | + if ($this->operator === 'contains' || $this->operator === 'starts_with') { |
| 100 | + return 'like'; |
| 101 | + } |
| 102 | + |
| 103 | + return $this->operator; |
| 104 | + } |
| 105 | + |
| 106 | + private function method() |
| 107 | + { |
| 108 | + switch($this->operator) { |
| 109 | + case 'in': |
| 110 | + $method = 'whereIn'; |
| 111 | + if ($this->isJsonData()) { |
| 112 | + $method = 'whereJsonContains'; |
| 113 | + } |
| 114 | + break; |
| 115 | + case 'between': |
| 116 | + $method = 'whereBetween'; |
| 117 | + break; |
| 118 | + default: |
| 119 | + $method = 'where'; |
| 120 | + } |
| 121 | + |
| 122 | + return $method; |
| 123 | + } |
| 124 | + |
| 125 | + private function isJsonData() |
| 126 | + { |
| 127 | + return $this->subjectType === self::TYPE_FIELD && str_starts_with($this->subjectValue, 'data.'); |
| 128 | + } |
| 129 | + |
| 130 | + private function subject() |
| 131 | + { |
| 132 | + if ($this->isJsonData()) { |
| 133 | + return str_replace('.', '->', $this->subjectValue); |
| 134 | + } |
| 135 | + |
| 136 | + if ($this->subjectType === self::TYPE_PARTICIPANTS) { |
| 137 | + return 'user_id'; |
| 138 | + } |
| 139 | + |
| 140 | + if ($this->subjectType === self::TYPE_PROCESS) { |
| 141 | + return 'process_id'; |
| 142 | + } |
| 143 | + |
| 144 | + return $this->subjectValue; |
| 145 | + } |
| 146 | + |
| 147 | + private function value() |
| 148 | + { |
| 149 | + if ($this->operator === 'contains') { |
| 150 | + return '%' . $this->value . '%'; |
| 151 | + } |
| 152 | + |
| 153 | + if ($this->operator === 'starts_with') { |
| 154 | + return $this->value . '%'; |
| 155 | + } |
| 156 | + |
| 157 | + return $this->value; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Forward Status and Participant subjects to PMQL methods on the models. |
| 162 | + * |
| 163 | + * For now, we only need Participants and Status because Request and Requester |
| 164 | + * are columns on the tables (process_request_id and user_id). |
| 165 | + */ |
| 166 | + private function valueAliasMethod() |
| 167 | + { |
| 168 | + $method = null; |
| 169 | + |
| 170 | + switch ($this->subjectType) { |
| 171 | + case self::TYPE_PARTICIPANTS: |
| 172 | + $method = 'valueAliasParticipant'; |
| 173 | + break; |
| 174 | + case self::TYPE_STATUS: |
| 175 | + $method = 'valueAliasStatus'; |
| 176 | + break; |
| 177 | + } |
| 178 | + |
| 179 | + return $method; |
| 180 | + } |
| 181 | + |
| 182 | + private function valueAliasAdapter(string $method, Builder $query) |
| 183 | + { |
| 184 | + $operator = $this->operator(); |
| 185 | + if ($operator === 'in') { |
| 186 | + $operator = '='; |
| 187 | + } |
| 188 | + $values = (array) $this->value(); |
| 189 | + |
| 190 | + $expression = (object) ['operator' => $operator]; |
| 191 | + |
| 192 | + $model = $query->getModel(); |
| 193 | + |
| 194 | + if ($method === 'valueAliasParticipant') { |
| 195 | + $values = $this->convertUserIdsToUsernames($values); |
| 196 | + } |
| 197 | + |
| 198 | + foreach ($values as $i => $value) { |
| 199 | + if ($i === 0) { |
| 200 | + $query->where($model->$method($value, $expression)); |
| 201 | + } else { |
| 202 | + $query->orWhere($model->$method($value, $expression)); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private function convertUserIdsToUsernames($values) |
| 208 | + { |
| 209 | + return array_map(function ($value) { |
| 210 | + return User::find($value)?->username; |
| 211 | + }, $values); |
| 212 | + } |
| 213 | + |
| 214 | + private function filterByProcessId(Builder $query) |
| 215 | + { |
| 216 | + if ($query->getModel() instanceof ProcessRequestToken) { |
| 217 | + $query->whereIn('process_request_id', function ($query) { |
| 218 | + $query->select('id')->from('process_requests') |
| 219 | + ->whereIn('process_id', (array) $this->value()); |
| 220 | + }); |
| 221 | + } else { |
| 222 | + $this->applyQueryBuilderMethod($query); |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments