2020-01-20 01:21:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Repository;
|
|
|
|
|
|
|
|
use Friendica\BaseModel;
|
|
|
|
use Friendica\BaseRepository;
|
|
|
|
use Friendica\Collection;
|
2020-01-23 16:01:11 +01:00
|
|
|
use Friendica\Core\L10n;
|
2020-01-14 04:22:02 +01:00
|
|
|
use Friendica\Database\Database;
|
2020-01-19 15:43:59 +01:00
|
|
|
use Friendica\Database\DBA;
|
2020-01-20 01:21:30 +01:00
|
|
|
use Friendica\Model;
|
2020-01-23 16:01:11 +01:00
|
|
|
use Friendica\Util\ACLFormatter;
|
2020-01-20 01:21:30 +01:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2020-01-14 04:22:02 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-01-20 01:21:30 +01:00
|
|
|
|
|
|
|
class ProfileField extends BaseRepository
|
|
|
|
{
|
|
|
|
protected static $table_name = 'profile_field';
|
|
|
|
|
|
|
|
protected static $model_class = Model\ProfileField::class;
|
|
|
|
|
|
|
|
protected static $collection_class = Collection\ProfileFields::class;
|
|
|
|
|
2020-01-14 04:22:02 +01:00
|
|
|
/** @var PermissionSet */
|
|
|
|
private $permissionSet;
|
2020-01-23 16:01:11 +01:00
|
|
|
/** @var ACLFormatter */
|
|
|
|
private $aclFormatter;
|
|
|
|
/** @var L10n */
|
|
|
|
private $l10n;
|
2020-01-14 04:22:02 +01:00
|
|
|
|
2020-01-23 16:01:11 +01:00
|
|
|
public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet, ACLFormatter $aclFormatter, L10n $l10n)
|
2020-01-14 04:22:02 +01:00
|
|
|
{
|
|
|
|
parent::__construct($dba, $logger);
|
|
|
|
|
|
|
|
$this->permissionSet = $permissionSet;
|
2020-01-23 16:01:11 +01:00
|
|
|
$this->aclFormatter = $aclFormatter;
|
|
|
|
$this->l10n = $l10n;
|
2020-01-14 04:22:02 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 01:21:30 +01:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @return Model\ProfileField
|
|
|
|
*/
|
|
|
|
protected function create(array $data)
|
|
|
|
{
|
2020-01-14 04:22:02 +01:00
|
|
|
return new Model\ProfileField($this->dba, $this->logger, $this->permissionSet, $data);
|
2020-01-20 01:21:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $condition
|
|
|
|
* @return Model\ProfileField
|
|
|
|
* @throws \Friendica\Network\HTTPException\NotFoundException
|
|
|
|
*/
|
|
|
|
public function selectFirst(array $condition)
|
|
|
|
{
|
|
|
|
return parent::selectFirst($condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $condition
|
|
|
|
* @param array $params
|
|
|
|
* @return 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 $max_id
|
|
|
|
* @param int|null $since_id
|
|
|
|
* @param int $limit
|
|
|
|
* @return Collection\ProfileFields
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
|
|
|
|
{
|
|
|
|
return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $uid Field owner user Id
|
|
|
|
* @return Collection\ProfileFields
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function selectByUserId(int $uid)
|
|
|
|
{
|
|
|
|
return $this->select(
|
|
|
|
['uid' => $uid],
|
|
|
|
['order' => ['order']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve all custom profile field a given contact is able to access to, including public profile fields.
|
|
|
|
*
|
|
|
|
* @param int $cid Private contact id, must be owned by $uid
|
|
|
|
* @param int $uid Field owner user id
|
|
|
|
* @return Collection\ProfileFields
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function selectByContactId(int $cid, int $uid)
|
|
|
|
{
|
2020-01-14 04:22:02 +01:00
|
|
|
$permissionSets = $this->permissionSet->selectByContactId($cid, $uid);
|
|
|
|
|
|
|
|
$psids = $permissionSets->column('id');
|
2020-01-20 01:21:30 +01:00
|
|
|
|
|
|
|
// Includes public custom fields
|
|
|
|
$psids[] = 0;
|
|
|
|
|
|
|
|
return $this->select(
|
|
|
|
['uid' => $uid, 'psid' => $psids],
|
|
|
|
['order' => ['order']]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $fields
|
|
|
|
* @return Model\ProfileField|bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function insert(array $fields)
|
|
|
|
{
|
|
|
|
$fields['created'] = DateTimeFormat::utcNow();
|
|
|
|
$fields['edited'] = DateTimeFormat::utcNow();
|
|
|
|
|
|
|
|
return parent::insert($fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Model\ProfileField $model
|
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function update(BaseModel $model)
|
|
|
|
{
|
|
|
|
$model->edited = DateTimeFormat::utcNow();
|
|
|
|
|
|
|
|
return parent::update($model);
|
|
|
|
}
|
2020-01-14 04:22:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $uid User Id
|
|
|
|
* @param 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 Collection\ProfileFields
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function updateCollectionFromForm(int $uid, 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->getIdFromACL(
|
|
|
|
$uid,
|
2020-01-23 16:01:11 +01:00
|
|
|
$this->aclFormatter->toString($profileFieldInputs['new']['contact_allow'] ?? ''),
|
|
|
|
$this->aclFormatter->toString($profileFieldInputs['new']['group_allow'] ?? ''),
|
|
|
|
$this->aclFormatter->toString($profileFieldInputs['new']['contact_deny'] ?? ''),
|
|
|
|
$this->aclFormatter->toString($profileFieldInputs['new']['group_deny'] ?? '')
|
2020-01-14 04:22:50 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$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 (Model\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
|
2020-01-23 16:01:11 +01:00
|
|
|
$profileFields = $profileFields->map(function (Model\ProfileField $profileField) use ($uid, &$profileFieldInputs, &$profileFieldOrder) {
|
2020-01-14 04:22:50 +01:00
|
|
|
if (isset($profileFieldInputs[$profileField->id]) && isset($profileFieldOrder[$profileField->id])) {
|
|
|
|
$psid = $this->permissionSet->getIdFromACL(
|
|
|
|
$uid,
|
2020-01-23 16:01:11 +01:00
|
|
|
$this->aclFormatter->toString($profileFieldInputs[$profileField->id]['contact_allow'] ?? ''),
|
|
|
|
$this->aclFormatter->toString($profileFieldInputs[$profileField->id]['group_allow'] ?? ''),
|
|
|
|
$this->aclFormatter->toString($profileFieldInputs[$profileField->id]['contact_deny'] ?? ''),
|
|
|
|
$this->aclFormatter->toString($profileFieldInputs[$profileField->id]['group_deny'] ?? '')
|
2020-01-14 04:22:50 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$profileField->psid = $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;
|
|
|
|
}
|
2020-01-19 15:43:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Migrates a legacy profile to the new slimmer profile with extra custom fields.
|
|
|
|
* Multi profiles are converted to ACl-protected custom fields and deleted.
|
|
|
|
*
|
|
|
|
* @param array $profile Profile table row
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2020-01-23 20:00:35 +01:00
|
|
|
public function migrateFromLegacyProfile(array $profile)
|
2020-01-19 15:43:59 +01:00
|
|
|
{
|
|
|
|
// Already processed, aborting
|
|
|
|
if ($profile['is-default'] === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$profile['is-default']) {
|
|
|
|
$contacts = Model\Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'profile-id' => $profile['id']]);
|
|
|
|
if (!count($contacts)) {
|
|
|
|
// No contact visibility selected defaults to user-only permission
|
|
|
|
$contacts = Model\Contact::selectToArray(['id'], ['uid' => $profile['uid'], 'self' => true]);
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:01:11 +01:00
|
|
|
$allow_cid = $this->aclFormatter->toString(array_column($contacts, 'id'));
|
2020-01-19 15:43:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 16:01:11 +01:00
|
|
|
$psid = $this->permissionSet->getIdFromACL($profile['uid'], $allow_cid ?? '');
|
2020-01-19 15:43:59 +01:00
|
|
|
|
|
|
|
$order = 1;
|
|
|
|
|
|
|
|
$custom_fields = [
|
2020-01-23 16:01:11 +01:00
|
|
|
'hometown' => $this->l10n->t('Hometown:'),
|
|
|
|
'gender' => $this->l10n->t('Gender:'),
|
|
|
|
'marital' => $this->l10n->t('Marital Status:'),
|
|
|
|
'with' => $this->l10n->t('With:'),
|
|
|
|
'howlong' => $this->l10n->t('Since:'),
|
|
|
|
'sexual' => $this->l10n->t('Sexual Preference:'),
|
|
|
|
'politic' => $this->l10n->t('Political Views:'),
|
|
|
|
'religion' => $this->l10n->t('Religious Views:'),
|
|
|
|
'likes' => $this->l10n->t('Likes:'),
|
|
|
|
'dislikes' => $this->l10n->t('Dislikes:'),
|
|
|
|
'about' => $this->l10n->t('About:'),
|
|
|
|
'summary' => $this->l10n->t('Summary'),
|
|
|
|
'music' => $this->l10n->t('Musical interests'),
|
|
|
|
'book' => $this->l10n->t('Books, literature'),
|
|
|
|
'tv' => $this->l10n->t('Television'),
|
|
|
|
'film' => $this->l10n->t('Film/dance/culture/entertainment'),
|
|
|
|
'interest' => $this->l10n->t('Hobbies/Interests'),
|
|
|
|
'romance' => $this->l10n->t('Love/romance'),
|
|
|
|
'work' => $this->l10n->t('Work/employment'),
|
|
|
|
'education' => $this->l10n->t('School/education'),
|
|
|
|
'contact' => $this->l10n->t('Contact information and Social Networks'),
|
2020-01-19 15:43:59 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($custom_fields as $field => $label) {
|
|
|
|
if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) {
|
2020-01-23 16:01:11 +01:00
|
|
|
$this->insert([
|
2020-01-19 15:43:59 +01:00
|
|
|
'uid' => $profile['uid'],
|
|
|
|
'psid' => $psid,
|
|
|
|
'order' => $order++,
|
|
|
|
'label' => trim($label, ':'),
|
|
|
|
'value' => $profile[$field],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$profile[$field] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($profile['is-default']) {
|
|
|
|
$profile['profile-name'] = null;
|
|
|
|
$profile['is-default'] = null;
|
2020-01-23 16:01:11 +01:00
|
|
|
$this->dba->update('profile', $profile, ['id' => $profile['id']]);
|
2020-01-19 15:43:59 +01:00
|
|
|
} elseif (!empty($profile['id'])) {
|
2020-01-23 16:01:11 +01:00
|
|
|
$this->dba->delete('profile', ['id' => $profile['id']]);
|
2020-01-19 15:43:59 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-20 01:21:30 +01:00
|
|
|
}
|