Skip to content

Commit

Permalink
Populate defaults into exising profile config
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Oct 20, 2021
1 parent f5bdf02 commit e019f64
Showing 1 changed file with 49 additions and 39 deletions.
88 changes: 49 additions & 39 deletions lib/private/Profile/ProfileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,51 +278,61 @@ function (ILinkAction $action) use ($targetUser, $visitingUser) {
}

/**
* @inheritDoc
* Return the default profile config
*/
private function getDefaultProfileConfig(IUser $targetUser, ?IUser $visitingUser): array {
// Contruct the default config for actions
$actionsConfig = [];
foreach ($this->getActions($targetUser, $visitingUser) as $action) {
$actionsConfig[$action->getId()] = [
'displayId' => $action->getDisplayId(),
'visibility' => ProfileConfig::DEFAULT_VISIBILITY,
];
}

// Map of account properties to display IDs
$propertyDisplayMap = [
IAccountManager::PROPERTY_ADDRESS => $this->l10nFactory->get('core')->t('Address'),
IAccountManager::PROPERTY_AVATAR => $this->l10nFactory->get('core')->t('Avatar'),
IAccountManager::PROPERTY_BIOGRAPHY => $this->l10nFactory->get('core')->t('About'),
IAccountManager::PROPERTY_DISPLAYNAME => $this->l10nFactory->get('core')->t('Full name'),
IAccountManager::PROPERTY_HEADLINE => $this->l10nFactory->get('core')->t('Headline'),
IAccountManager::PROPERTY_ORGANISATION => $this->l10nFactory->get('core')->t('Organisation'),
IAccountManager::PROPERTY_ROLE => $this->l10nFactory->get('core')->t('Role'),
IAccountManager::PROPERTY_EMAIL => $this->l10nFactory->get('core')->t('Email'),
IAccountManager::PROPERTY_PHONE => $this->l10nFactory->get('core')->t('Phone'),
IAccountManager::PROPERTY_TWITTER => $this->l10nFactory->get('core')->t('Twitter'),
IAccountManager::PROPERTY_WEBSITE => $this->l10nFactory->get('core')->t('Website'),
];

// Contruct the default config for account properties
$propertiesConfig = [];
foreach ($propertyDisplayMap as $property => $displayId) {
$propertiesConfig[$property] = [
'displayId' => $displayId,
'visibility' => ProfileConfig::DEFAULT_PROPERTY_VISIBILITY[$property] ?: ProfileConfig::DEFAULT_VISIBILITY,
];
}

return array_merge($actionsConfig, $propertiesConfig);
}

/**
* Return the profile config
*/
public function getProfileConfig(IUser $targetUser, ?IUser $visitingUser): array {
$defaultProfileConfig = $this->getDefaultProfileConfig($targetUser, $visitingUser);
try {
$configArray = $this->configMapper->getArray($targetUser->getUID());
$config = $this->configMapper->get($targetUser->getUID());
// Merge defaults with the existing config in case the defaults are missing
$config->setConfigArray(array_merge($defaultProfileConfig, $config->getConfigArray()));
$this->configMapper->update($config);
$configArray = $config->getConfigArray();
} catch (DoesNotExistException $e) {
// Create a new default config if it does not exist
$config = new ProfileConfig();
$config->setUserId($targetUser->getUID());

// Map of account properties to display IDs
$propertyDisplayMap = [
IAccountManager::PROPERTY_ADDRESS => $this->l10nFactory->get('core')->t('Address'),
IAccountManager::PROPERTY_AVATAR => $this->l10nFactory->get('core')->t('Avatar'),
IAccountManager::PROPERTY_BIOGRAPHY => $this->l10nFactory->get('core')->t('About'),
IAccountManager::PROPERTY_DISPLAYNAME => $this->l10nFactory->get('core')->t('Full name'),
IAccountManager::PROPERTY_HEADLINE => $this->l10nFactory->get('core')->t('Headline'),
IAccountManager::PROPERTY_ORGANISATION => $this->l10nFactory->get('core')->t('Organisation'),
IAccountManager::PROPERTY_ROLE => $this->l10nFactory->get('core')->t('Role'),
IAccountManager::PROPERTY_EMAIL => $this->l10nFactory->get('core')->t('Email'),
IAccountManager::PROPERTY_PHONE => $this->l10nFactory->get('core')->t('Phone'),
IAccountManager::PROPERTY_TWITTER => $this->l10nFactory->get('core')->t('Twitter'),
IAccountManager::PROPERTY_WEBSITE => $this->l10nFactory->get('core')->t('Website'),
];

// Contruct the default config for account properties
$propertiesConfig = [];
foreach ($propertyDisplayMap as $property => $displayId) {
$propertiesConfig[$property] = [
'displayId' => $displayId,
'visibility' => ProfileConfig::DEFAULT_PROPERTY_VISIBILITY[$property] ?: ProfileConfig::DEFAULT_VISIBILITY,
];
}

// Contruct the default config for actions
$actionsConfig = [];
/** @var ILinkAction $action */
foreach ($this->getActions($targetUser, $visitingUser) as $action) {
$actionsConfig[$action->getId()] = [
'displayId' => $action->getDisplayId(),
'visibility' => ProfileConfig::DEFAULT_VISIBILITY,
];
}

// Set the default config
$config->setConfigArray(array_merge($propertiesConfig, $actionsConfig));
$config->setConfigArray($defaultProfileConfig);
$this->configMapper->insert($config);
$configArray = $config->getConfigArray();
}
Expand Down

0 comments on commit e019f64

Please sign in to comment.