Move ProfileFieldRepository::updateCollectionFromForm()

This commit is contained in:
Philipp 2021-10-10 20:39:35 +02:00
parent 6f692b857b
commit f403851946
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
9 changed files with 320 additions and 177 deletions

View file

@ -21,7 +21,6 @@
namespace Friendica\Repository;
use Friendica\BaseModel;
use Friendica\BaseRepository;
use Friendica\Core\L10n;
use Friendica\Database\Database;
@ -55,56 +54,6 @@ class ProfileField extends BaseRepository
$this->l10n = $l10n;
}
/**
* @param array $data
*
* @return \Friendica\Profile\ProfileField\Entity\ProfileField
*/
protected function create(array $data)
{
return new Model\ProfileField($this->dba, $this->logger, $this->permissionSet, $data);
}
/**
* @param array $condition
*
* @return \Friendica\Profile\ProfileField\Entity\ProfileField
* @throws \Friendica\Network\HTTPException\NotFoundException
*/
public function selectFirst(array $condition)
{
return parent::selectFirst($condition);
}
/**
* @param array $condition
* @param array $params
*
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception
*/
public function select(array $condition = [], array $params = [])
{
return parent::select($condition, $params);
}
/**
* @param array $condition
* @param array $params
* @param int|null $min_id
* @param int|null $max_id
* @param int $limit
*
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception
*/
public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
{
return parent::selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
}
/**
* @param array $fields
*
@ -119,102 +68,6 @@ class ProfileField extends BaseRepository
return parent::insert($fields);
}
/**
* @param \Friendica\Profile\ProfileField\Entity\ProfileField $model
*
* @return bool
* @throws \Exception
*/
public function update(BaseModel $model)
{
$model->edited = DateTimeFormat::utcNow();
return parent::update($model);
}
/**
* @param int $uid User Id
* @param \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields Collection of existing profile fields
* @param array $profileFieldInputs Array of profile field form inputs indexed by profile field id
* @param array $profileFieldOrder List of profile field id in order
*
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception
*/
public function updateCollectionFromForm(int $uid, \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder)
{
// Returns an associative array of id => order values
$profileFieldOrder = array_flip($profileFieldOrder);
// Creation of the new field
if (!empty($profileFieldInputs['new']['label'])) {
$psid = $this->permissionSet->selectOrCreate($this->permissionSetFactory->createFromString(
$uid,
$profileFieldInputs['new']['contact_allow'] ?? '',
$profileFieldInputs['new']['group_allow'] ?? '',
$profileFieldInputs['new']['contact_deny'] ?? '',
$profileFieldInputs['new']['group_deny'] ?? ''
))->id;
$newProfileField = $this->insert([
'uid' => $uid,
'label' => $profileFieldInputs['new']['label'],
'value' => $profileFieldInputs['new']['value'],
'psid' => $psid,
'order' => $profileFieldOrder['new'],
]);
$profileFieldInputs[$newProfileField->id] = $profileFieldInputs['new'];
$profileFieldOrder[$newProfileField->id] = $profileFieldOrder['new'];
$profileFields[] = $newProfileField;
}
unset($profileFieldInputs['new']);
unset($profileFieldOrder['new']);
// Prunes profile field whose label has been emptied
$profileFields = $profileFields->filter(function (\Friendica\Profile\ProfileField\Entity\ProfileField $profileField) use (&$profileFieldInputs, &$profileFieldOrder) {
$keepModel = !isset($profileFieldInputs[$profileField->id]) || !empty($profileFieldInputs[$profileField->id]['label']);
if (!$keepModel) {
unset($profileFieldInputs[$profileField->id]);
unset($profileFieldOrder[$profileField->id]);
$this->delete($profileField);
}
return $keepModel;
});
// Regenerates the order values if items were deleted
$profileFieldOrder = array_flip(array_keys($profileFieldOrder));
// Update existing profile fields from form values
$profileFields = $profileFields->map(function (\Friendica\Profile\ProfileField\Entity\ProfileField $profileField) use ($uid, &$profileFieldInputs, &$profileFieldOrder) {
if (isset($profileFieldInputs[$profileField->id]) && isset($profileFieldOrder[$profileField->id])) {
$psid = $this->permissionSet->selectOrCreate($this->permissionSetFactory->createFromString(
$uid,
$profileFieldInputs[$profileField->id]['contact_allow'] ?? '',
$profileFieldInputs[$profileField->id]['group_allow'] ?? '',
$profileFieldInputs[$profileField->id]['contact_deny'] ?? '',
$profileFieldInputs[$profileField->id]['group_deny'] ?? ''
))->id;
$profileField->permissionSetId = $psid;
$profileField->label = $profileFieldInputs[$profileField->id]['label'];
$profileField->value = $profileFieldInputs[$profileField->id]['value'];
$profileField->order = $profileFieldOrder[$profileField->id];
unset($profileFieldInputs[$profileField->id]);
unset($profileFieldOrder[$profileField->id]);
}
return $profileField;
});
return $profileFields;
}
/**
* Migrates a legacy profile to the new slimmer profile with extra custom fields.
* Multi profiles are converted to ACl-protected custom fields and deleted.