Changes for default group of people sorting filter (#5113)

* Changes for default group of people sorting filter

* Make "Prioritised User Group" required for selected sorting option "Default"
This commit is contained in:
Yuriy Bakhtin 2021-06-09 15:35:19 +03:00 committed by GitHub
parent 008dc7887a
commit 44a33713aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 7 deletions

View File

@ -10,6 +10,7 @@ namespace humhub\modules\admin\models\forms;
use humhub\modules\user\models\Group;
use humhub\modules\user\models\ProfileField;
use humhub\modules\user\models\ProfileFieldCategory;
use humhub\modules\user\widgets\PeopleCard;
use Yii;
use yii\base\Model;
@ -56,7 +57,9 @@ class PeopleSettingsForm extends Model
['detail2', 'in', 'range' => $this->getDetailKeys()],
['detail3', 'in', 'range' => $this->getDetailKeys()],
['defaultSorting', 'in', 'range' => array_keys(self::getSortingOptions())],
['defaultSortingGroup', 'in', 'range' => array_keys(self::getSortingGroupOptions())],
['defaultSortingGroup', 'required', 'when' => function ($model) {
return $model->defaultSorting == '';
}],
];
}
@ -70,7 +73,7 @@ class PeopleSettingsForm extends Model
'detail2' => Yii::t('AdminModule.user', 'Information 2'),
'detail3' => Yii::t('AdminModule.user', 'Information 3'),
'defaultSorting' => Yii::t('AdminModule.user', 'Default Sorting'),
'defaultSortingGroup' => Yii::t('AdminModule.user', 'Group for sorting option "Default"'),
'defaultSortingGroup' => Yii::t('AdminModule.user', 'Prioritised User Group'),
];
}
@ -80,7 +83,7 @@ class PeopleSettingsForm extends Model
public function attributeHints()
{
return [
'defaultSortingGroup' => Yii::t('AdminModule.user', 'Users of the selected group will be sorted first. "Last login" is used as additional sorting here for users inside the group and for other users and also if no group is selected.'),
'defaultSortingGroup' => Yii::t('AdminModule.user', 'Select a prioritised group whose members are displayed before all others when the sorting option \'Default\' is selected. The users within the group and the users outside the group are additionally sorted by their last login.'),
];
}
@ -142,14 +145,20 @@ class PeopleSettingsForm extends Model
return $keys;
}
public static function getSortingOptions(): array
public static function getSortingOptions($checkDefaultGroup = false): array
{
return [
$options = [
'' => Yii::t('AdminModule.user', 'Default'),
'firstname' => Yii::t('AdminModule.user', 'First name'),
'lastname' => Yii::t('AdminModule.user', 'Last name'),
'lastlogin' => Yii::t('AdminModule.user', 'Last login'),
];
if ($checkDefaultGroup && !self::isDefaultGroupDefined()) {
unset($options['']);
}
return $options;
}
public static function getSortingGroupOptions(): array
@ -165,4 +174,13 @@ class PeopleSettingsForm extends Model
return $options;
}
public static function isDefaultGroupDefined(): bool
{
$defaultSortingGroupId = PeopleCard::config('defaultSortingGroup');
return $defaultSortingGroupId && Group::find()
->where(['id' => $defaultSortingGroupId])
->exists();
}
}

View File

@ -60,7 +60,7 @@ class PeopleFilters extends DirectoryFilters
$this->addFilter('sort', [
'title' => Yii::t('SpaceModule.base', 'Sorting'),
'type' => 'dropdown',
'options' => PeopleSettingsForm::getSortingOptions(),
'options' => PeopleSettingsForm::getSortingOptions(true),
'sortOrder' => 300,
]);
@ -127,7 +127,11 @@ class PeopleFilters extends DirectoryFilters
{
switch ($filter) {
case 'sort':
return PeopleCard::config('defaultSorting');
$defaultSorting = PeopleCard::config('defaultSorting');
if ($defaultSorting == '' && !PeopleSettingsForm::isDefaultGroupDefined()) {
return 'lastlogin';
}
return $defaultSorting;
}
return '';