Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions Sources/Actions/Profile/GroupMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ class GroupMembership implements ActionInterface
*/
public string $change_type;

/*********************
* Internal properties
*********************/

/**
* @var array
*
* Info about groups that this member is part of or can become part of.
*/
protected array $current_and_assignable_groups;

/****************
* Public methods
****************/
Expand Down Expand Up @@ -103,7 +114,7 @@ public function show(): void
}, $open_requests);

// Show the assignable groups in the templates.
foreach (Profile::$member->current_and_assignable_groups as $id => $group) {
foreach ($this->current_and_assignable_groups as $id => $group) {
// Skip "Regular Members" for now.
if ($id == 0) {
continue;
Expand Down Expand Up @@ -173,12 +184,12 @@ public function save(): void
// Which groups can they be assigned to?
$this->loadCurrentAndAssignableGroups();

if (!isset(Profile::$member->current_and_assignable_groups[$new_group_id])) {
if (!isset($this->current_and_assignable_groups[$new_group_id])) {
ErrorHandler::fatalLang('cannot_manage_membergroups', false);
}

// Just for improved legibility...
$new_group_info = Profile::$member->current_and_assignable_groups[$new_group_id];
$new_group_info = $this->current_and_assignable_groups[$new_group_id];

// Can't request a non-requestable group.
if ($this->change_type == 'request' && $new_group_info['type'] != 2) {
Expand Down Expand Up @@ -269,7 +280,7 @@ protected function __construct()
*/
protected function loadCurrentAndAssignableGroups(): void
{
if (isset(Profile::$member->current_and_assignable_groups)) {
if (isset($this->current_and_assignable_groups)) {
return;
}

Expand Down Expand Up @@ -306,7 +317,7 @@ function ($a, $b) {
},
);

Profile::$member->current_and_assignable_groups = $current_and_assignable_groups;
$this->current_and_assignable_groups = $current_and_assignable_groups;
}

/**
Expand All @@ -319,16 +330,16 @@ protected function canEditPrimary(?int $new_group_id = null): bool

// Hidden groups cannot be primary groups.
if (isset($new_group_id)) {
$can_edit_primary = Profile::$member->current_and_assignable_groups[$new_group_id]->can_be_primary;
$can_edit_primary = $this->current_and_assignable_groups[$new_group_id]->can_be_primary;
} else {
$possible_primary_groups = array_filter(Profile::$member->current_and_assignable_groups, fn($group) => !empty($group->can_be_primary));
$possible_primary_groups = array_filter($this->current_and_assignable_groups, fn($group) => !empty($group->can_be_primary));

$can_edit_primary = !empty($possible_primary_groups);
}

// Changing the primary group means turning the current primary group
// into an additional group. So, is that possible?
$can_edit_primary &= Profile::$member->group_id == Group::REGULAR || Profile::$member->current_and_assignable_groups[Profile::$member->group_id]->can_be_additional;
$can_edit_primary &= Profile::$member->group_id == Group::REGULAR || $this->current_and_assignable_groups[Profile::$member->group_id]->can_be_additional;

return (bool) $can_edit_primary;
}
Expand Down
50 changes: 35 additions & 15 deletions Sources/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class Profile extends User implements \ArrayAccess
*
* @param bool $force_reload Whether to reload the data.
*/
public function loadStandardFields(bool $force_reload = false)
public function loadStandardFields(bool $force_reload = false): void
{
// Don't load this twice!
if (!empty($this->standard_fields) && !$force_reload) {
Expand Down Expand Up @@ -1133,8 +1133,10 @@ public function loadCustomFields(string $area = 'summary'): void
$this->custom_fields_required = $this->custom_fields_required || $cf_def['show_reg'] == 2;
}

Utils::$context['custom_fields'] = &$this->custom_fields;
Utils::$context['custom_fields_required'] = &$this->custom_fields_required;
if ($this === self::$member) {
Utils::$context['custom_fields'] = &$this->custom_fields;
Utils::$context['custom_fields_required'] = &$this->custom_fields_required;
}

IntegrationHook::call('integrate_load_custom_profile_fields', [$this->id, $area]);
}
Expand All @@ -1147,8 +1149,13 @@ public function loadCustomFields(string $area = 'summary'): void
* theme, where "default theme" means whichever theme is used for guests.
* Default: false.
*/
public function loadThemeOptions(bool $default_only = false)
public function loadThemeOptions(bool $default_only = false): void
{
// This only applies to self::$member.
if ($this !== self::$member) {
return;
}

// Get this member's current theme options.
if ($default_only && $this->theme != (Config::$modSettings['theme_guests'] ?? 1)) {
$temp = $this->data['options'] ?? [];
Expand Down Expand Up @@ -1207,6 +1214,11 @@ public function loadAvatarData(): bool
*/
public function loadSignatureData(): bool
{
// This only applies to self::$member.
if ($this !== self::$member) {
return false;
}

// Signature limits.
list($sig_limits, $sig_bbc) = explode(':', Config::$modSettings['signature_settings']);
$sig_limits = explode(',', $sig_limits);
Expand Down Expand Up @@ -1305,18 +1317,20 @@ public function loadAssignableGroups(): bool
}

// For the templates.
Utils::$context['member_groups'] = [
0 => [
'id' => 0,
'name' => Lang::getTxt('no_primary_membergroup', file: 'Profile'),
'is_primary' => $this->data['id_group'] == 0,
'can_be_additional' => false,
'can_be_primary' => true,
],
];
if ($this === self::$member) {
Utils::$context['member_groups'] = [
0 => [
'id' => 0,
'name' => Lang::getTxt('no_primary_membergroup', file: 'Profile'),
'is_primary' => $this->data['id_group'] == 0,
'can_be_additional' => false,
'can_be_primary' => true,
],
];

// Do not use array merge here, does not maintain key association.
Utils::$context['member_groups'] += $this->assignable_groups;
// Do not use array merge here, does not maintain key association.
Utils::$context['member_groups'] += $this->assignable_groups;
}

return true;
}
Expand All @@ -1326,9 +1340,15 @@ public function loadAssignableGroups(): bool
*
* @param array $fields The profile fields to display. Each item should
* correspond to an item in the $this->standard_fields array.
* @throws \LogicException if called on an object that is not self::$member.
*/
public function setupContext(array $fields): void
{
if ($this !== self::$member) {
// Complain loudly about this programmer error.
throw new \LogicException('Called ' . __METHOD__ . ' for a profile that is not ' . __CLASS__ . '::$member');
}

// Some default bits.
Utils::$context['profile_prehtml'] = '';
Utils::$context['profile_posthtml'] = '';
Expand Down
Loading