From d6307b30c3893f3c8b28cf07b66267eeb249e708 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Tue, 18 Aug 2020 11:23:15 +0200 Subject: [PATCH] [update] introduce BC way for intuitive filtering of fields --- src/Model.php | 22 ++++++++++++++-------- src/Persistence/Sql/Query.php | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Model.php b/src/Model.php index 9b2a14978..c318ecf21 100644 --- a/src/Model.php +++ b/src/Model.php @@ -625,15 +625,21 @@ public function isDirty(string $field): bool * * @return Field[] */ - public function getFields($filters = null): array + public function getFields($filters = null, bool $onlyFields = null): array { if ($filters === null) { - return $this->fields; + return $onlyFields ? $this->getFields(self::FIELD_FILTER_ONLY_FIELDS) : $this->fields; } elseif (!is_array($filters)) { $filters = [$filters]; } - return array_filter($this->fields, function (Field $field, $name) use ($filters) { + $onlyFields = $onlyFields ?? true; + + return array_filter($this->fields, function (Field $field, $name) use ($filters, $onlyFields) { + if ($onlyFields && !$this->isOnlyFieldsField($field->short_name)) { + return false; + } + foreach ($filters as $filter) { if ($this->fieldMatchesFilter($field, $filter)) { return true; @@ -648,13 +654,13 @@ protected function fieldMatchesFilter(Field $field, string $filter): bool { switch ($filter) { case self::FIELD_FILTER_SYSTEM: - return $this->isOnlyFieldsField($field->short_name) && $field->system; + return $field->system; case self::FIELD_FILTER_NOT_SYSTEM: - return $this->isOnlyFieldsField($field->short_name) && !$field->system; + return !$field->system; case self::FIELD_FILTER_EDITABLE: - return $this->isOnlyFieldsField($field->short_name) && $field->isEditable(); + return $field->isEditable(); case self::FIELD_FILTER_VISIBLE: - return $this->isOnlyFieldsField($field->short_name) && $field->isVisible(); + return $field->isVisible(); case self::FIELD_FILTER_ONLY_FIELDS: return $this->isOnlyFieldsField($field->short_name); case self::FIELD_FILTER_PERSIST: @@ -1649,7 +1655,7 @@ public function export(array $fields = null, $key_field = null, $typecast_data = // prepare array with field names if ($fields === null) { - $fields = array_keys($this->getFields(self::FIELD_FILTER_PERSIST)); + $fields = array_keys($this->getFields(self::FIELD_FILTER_PERSIST, false)); } // add key_field to array if it's not there diff --git a/src/Persistence/Sql/Query.php b/src/Persistence/Sql/Query.php index 2d58033d2..4a57e2c69 100644 --- a/src/Persistence/Sql/Query.php +++ b/src/Persistence/Sql/Query.php @@ -78,7 +78,7 @@ protected function initSelect($fields = null): void } if (!is_array($fields)) { - $fields = array_keys($this->model->getFields(Model::FIELD_FILTER_PERSIST)); + $fields = array_keys($this->model->getFields(Model::FIELD_FILTER_PERSIST, false)); } foreach ($fields as $fieldName) {