]> git.mxchange.org Git - friendica.git/blobdiff - src/Repository/ProfileField.php
Merge pull request #10160 from annando/issue-10156
[friendica.git] / src / Repository / ProfileField.php
index 3a97e237178fef13624b12f1881dbdb40595d45d..bf7ac69d6131f28ed101c33dcd4d042dc2156805 100644 (file)
@@ -1,13 +1,34 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Repository;
 
 use Friendica\BaseModel;
 use Friendica\BaseRepository;
 use Friendica\Collection;
+use Friendica\Core\L10n;
 use Friendica\Database\Database;
-use Friendica\DI;
+use Friendica\Database\DBA;
 use Friendica\Model;
+use Friendica\Util\ACLFormatter;
 use Friendica\Util\DateTimeFormat;
 use Psr\Log\LoggerInterface;
 
@@ -21,12 +42,18 @@ class ProfileField extends BaseRepository
 
        /** @var PermissionSet */
        private $permissionSet;
+       /** @var ACLFormatter */
+       private $aclFormatter;
+       /** @var L10n */
+       private $l10n;
 
-       public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet)
+       public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet, ACLFormatter $aclFormatter, L10n $l10n)
        {
                parent::__construct($dba, $logger);
 
                $this->permissionSet = $permissionSet;
+               $this->aclFormatter = $aclFormatter;
+               $this->l10n = $l10n;
        }
 
        /**
@@ -60,17 +87,17 @@ class ProfileField extends BaseRepository
        }
 
        /**
-        * @param array $condition
-        * @param array $params
+        * @param array    $condition
+        * @param array    $params
+        * @param int|null $min_id
         * @param int|null $max_id
-        * @param int|null $since_id
-        * @param int $limit
+        * @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)
+       public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
        {
-               return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
+               return parent::selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
        }
 
        /**
@@ -144,8 +171,6 @@ class ProfileField extends BaseRepository
         */
        public function updateCollectionFromForm(int $uid, Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder)
        {
-               $aclFormatter = DI::aclFormatter();
-
                // Returns an associative array of id => order values
                $profileFieldOrder = array_flip($profileFieldOrder);
 
@@ -153,10 +178,10 @@ class ProfileField extends BaseRepository
                if (!empty($profileFieldInputs['new']['label'])) {
                        $psid = $this->permissionSet->getIdFromACL(
                                $uid,
-                               $aclFormatter->toString($profileFieldInputs['new']['contact_allow'] ?? ''),
-                               $aclFormatter->toString($profileFieldInputs['new']['group_allow'] ?? ''),
-                               $aclFormatter->toString($profileFieldInputs['new']['contact_deny'] ?? ''),
-                               $aclFormatter->toString($profileFieldInputs['new']['group_deny'] ?? '')
+                               $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'] ?? '')
                        );
 
                        $newProfileField = $this->insert([
@@ -193,14 +218,14 @@ class ProfileField extends BaseRepository
                $profileFieldOrder = array_flip(array_keys($profileFieldOrder));
 
                // Update existing profile fields from form values
-               $profileFields = $profileFields->map(function (Model\ProfileField $profileField) use ($uid, $aclFormatter, &$profileFieldInputs, &$profileFieldOrder) {
+               $profileFields = $profileFields->map(function (Model\ProfileField $profileField) use ($uid, &$profileFieldInputs, &$profileFieldOrder) {
                        if (isset($profileFieldInputs[$profileField->id]) && isset($profileFieldOrder[$profileField->id])) {
                                $psid = $this->permissionSet->getIdFromACL(
                                        $uid,
-                                       $aclFormatter->toString($profileFieldInputs[$profileField->id]['contact_allow'] ?? ''),
-                                       $aclFormatter->toString($profileFieldInputs[$profileField->id]['group_allow'] ?? ''),
-                                       $aclFormatter->toString($profileFieldInputs[$profileField->id]['contact_deny'] ?? ''),
-                                       $aclFormatter->toString($profileFieldInputs[$profileField->id]['group_deny'] ?? '')
+                                       $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'] ?? '')
                                );
 
                                $profileField->psid  = $psid;
@@ -217,4 +242,78 @@ class ProfileField extends BaseRepository
 
                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.
+        *
+        * @param array $profile Profile table row
+        * @throws \Exception
+        */
+       public function migrateFromLegacyProfile(array $profile)
+       {
+               // 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]);
+                       }
+
+                       $allow_cid = $this->aclFormatter->toString(array_column($contacts, 'id'));
+               }
+
+               $psid = $this->permissionSet->getIdFromACL($profile['uid'], $allow_cid ?? '');
+
+               $order = 1;
+
+               $custom_fields = [
+                       'hometown'  => $this->l10n->t('Hometown:'),
+                       '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:'),
+                       'pdesc'     => $this->l10n->t('Title/Description:'),
+                       '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'),
+               ];
+
+               foreach ($custom_fields as $field => $label) {
+                       if (!empty($profile[$field]) && $profile[$field] > DBA::NULL_DATE && $profile[$field] > DBA::NULL_DATETIME) {
+                               $this->insert([
+                                       '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;
+                       $this->dba->update('profile', $profile, ['id' => $profile['id']]);
+               } elseif (!empty($profile['id'])) {
+                       $this->dba->delete('profile', ['id' => $profile['id']]);
+               }
+       }
 }