Fix saving of user profile country field value (#6937)

* Fix saving of user profile country field value

* Search in user profile country full name by keyword

* Update CHANGELOG.md
This commit is contained in:
Yuriy Bakhtin 2024-04-09 16:55:55 +02:00 committed by GitHub
parent fabee82688
commit fbe2bf61c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 1 deletions

View File

@ -5,6 +5,7 @@ HumHub Changelog
-----------------------
- Enh #6899: Fix a missed module config file
- Fix #6913: Fix API tests
- Fix #6919: Fix saving of user profile country field value and enable a searching by country title
1.15.4 (March 20, 2024)
-----------------------

View File

@ -44,6 +44,15 @@ abstract class AbstractActiveQueryContentContainer extends ActiveQuery
*/
abstract protected function getSearchableFields(): array;
/**
* Returns a list of fields with its associative array of values and titles.
* Only values of the fields are stored in DB, so we need to do a searching
* in titles which may be translatable, e.g. counties list.
* If additional tables are needed, they must be added via `joinWith`.
*
* @return array
*/
abstract protected function getSearchableFieldTitles(): array;
/**
* Performs a container text search
@ -98,6 +107,19 @@ abstract class AbstractActiveQueryContentContainer extends ActiveQuery
$conditions[] = array_merge(['OR'], $subConditions);
}
$fieldTitles = $this->getSearchableFieldTitles();
foreach ($fieldTitles as $field => $titles) {
$valueKeys = [];
foreach ($titles as $key => $title) {
if (stripos($title, $keyword) === 0) {
$valueKeys[] = $key;
}
}
if ($valueKeys !== []) {
$conditions[] = ['IN', $field, $valueKeys];
}
}
return $this->andWhere(array_merge(['OR'], $conditions));
}

View File

@ -79,6 +79,14 @@ class ActiveQuerySpace extends AbstractActiveQueryContentContainer
return ['space.name', 'space.description', 'contentcontainer.tags_cached'];
}
/**
* @inerhitdoc
*/
protected function getSearchableFieldTitles(): array
{
return [];
}
/**
* Exclude blocked spaces for the given $user or for the current User
*

View File

@ -12,6 +12,8 @@ use humhub\events\ActiveQueryEvent;
use humhub\modules\admin\permissions\ManageUsers;
use humhub\modules\content\components\AbstractActiveQueryContentContainer;
use humhub\modules\user\models\fieldtype\BaseTypeVirtual;
use humhub\modules\user\models\fieldtype\CountrySelect;
use humhub\modules\user\models\fieldtype\Select;
use humhub\modules\user\models\Group;
use humhub\modules\user\models\GroupUser;
use humhub\modules\user\models\ProfileField;
@ -130,6 +132,30 @@ class ActiveQueryUser extends AbstractActiveQueryContentContainer
return $fields;
}
/**
* @inerhitdoc
*/
protected function getSearchableFieldTitles(): array
{
$this->joinWith('profile')->joinWith('contentContainerRecord');
$fields = [];
$profileFields = ProfileField::find()
->where(['searchable' => 1])
->andWhere(['IN', 'field_type_class', [CountrySelect::class, Select::class]]);
foreach ($profileFields->all() as $profileField) {
/* @var ProfileField $profileField */
$fieldType = $profileField->getFieldType();
if ($fieldType instanceof Select) {
$fields['profile.' . $profileField->internal_name] = $fieldType->getSelectItems();
}
}
return $fields;
}
/**
* Limits the query to a specified user group
*

View File

@ -59,7 +59,7 @@ class CountrySelect extends Select
$isoCodes = Iso3166Codes::$countries;
foreach ($isoCodes as $code => $value) {
$items[Iso3166Codes::country($code)] = Iso3166Codes::country($code);
$items[$code] = Iso3166Codes::country($code);
}
} else {
foreach (explode(",", $this->options) as $code) {