]> git.mxchange.org Git - friendica.git/commitdiff
New class for group related contact actions
authorMichael <heluecht@pirati.ca>
Tue, 4 Aug 2020 18:22:19 +0000 (18:22 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 4 Aug 2020 18:22:19 +0000 (18:22 +0000)
include/api.php
src/Model/Contact.php
src/Model/Contact/Group.php [new file with mode: 0644]
src/Module/Group.php

index 86cda0b1bb688e915e150c6e14c7f27e555c108f..869ece0d7c8c301de60bbc73a4dd987298c7be20 100644 (file)
@@ -5271,7 +5271,7 @@ function api_friendica_group_show($type)
        // loop through all groups and retrieve all members for adding data in the user array
        $grps = [];
        foreach ($r as $rr) {
-               $members = Contact::getByGroupId($rr['id']);
+               $members = Contact\Group::getById($rr['id']);
                $users = [];
 
                if ($type == "xml") {
@@ -5596,7 +5596,7 @@ function api_friendica_group_update($type)
        }
 
        // remove members
-       $members = Contact::getByGroupId($gid);
+       $members = Contact\Group::getById($gid);
        foreach ($members as $member) {
                $cid = $member['id'];
                foreach ($users as $user) {
index 08cde31eeb16614ec0937495792a8cb36bfee746..97cf605f16f7756efaa9b88cca98d6c9d56cb56a 100644 (file)
@@ -537,41 +537,6 @@ class Contact
                }
        }
 
-       /**
-        * Returns a list of contacts belonging in a group
-        *
-        * @param int $gid
-        * @return array
-        * @throws \Exception
-        */
-       public static function getByGroupId($gid)
-       {
-               $return = [];
-
-               if (intval($gid)) {
-                       $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
-                               FROM `contact`
-                               INNER JOIN `group_member`
-                                       ON `contact`.`id` = `group_member`.`contact-id`
-                               WHERE `gid` = ?
-                               AND `contact`.`uid` = ?
-                               AND NOT `contact`.`self`
-                               AND NOT `contact`.`deleted`
-                               AND NOT `contact`.`blocked`
-                               AND NOT `contact`.`pending`
-                               ORDER BY `contact`.`name` ASC',
-                               $gid,
-                               local_user()
-                       );
-
-                       if (DBA::isResult($stmt)) {
-                               $return = DBA::toArray($stmt);
-                       }
-               }
-
-               return $return;
-       }
-
        /**
         * Creates the self-contact for the provided user id
         *
@@ -1029,33 +994,6 @@ class Contact
                return $menucondensed;
        }
 
-       /**
-        * Returns ungrouped contact count or list for user
-        *
-        * Returns either the total number of ungrouped contacts for the given user
-        * id or a paginated list of ungrouped contacts.
-        *
-        * @param int $uid uid
-        * @return array
-        * @throws \Exception
-        */
-       public static function getUngroupedList($uid)
-       {
-               return q("SELECT *
-                          FROM `contact`
-                          WHERE `uid` = %d
-                          AND NOT `self`
-                          AND NOT `deleted`
-                          AND NOT `blocked`
-                          AND NOT `pending`
-                          AND `id` NOT IN (
-                               SELECT DISTINCT(`contact-id`)
-                               FROM `group_member`
-                               INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
-                               WHERE `group`.`uid` = %d
-                          )", intval($uid), intval($uid));
-       }
-
        /**
         * Have a look at all contact tables for a given profile url.
         * This function works as a replacement for probing the contact.
@@ -2684,18 +2622,6 @@ class Contact
                return $redirect;
        }
 
-       /**
-        * Remove a contact from all groups
-        *
-        * @param integer $contact_id
-        *
-        * @return boolean Success
-        */
-       public static function removeFromGroups($contact_id)
-       {
-               return DBA::delete('group_member', ['contact-id' => $contact_id]);
-       }
-
        /**
         * Is the contact a forum?
         *
diff --git a/src/Model/Contact/Group.php b/src/Model/Contact/Group.php
new file mode 100644 (file)
index 0000000..bb05d3c
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @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\Model\Contact;
+
+use Friendica\Database\DBA;
+
+/**
+ * This class provides information about contact groups based on the "group_member" table.
+ */
+class Group
+{
+       /**
+        * Returns a list of contacts belonging in a group
+        *
+        * @param int $gid
+        * @return array
+        * @throws \Exception
+        */
+       public static function getById(int $gid)
+       {
+               $return = [];
+
+               if (intval($gid)) {
+                       $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
+                               FROM `contact`
+                               INNER JOIN `group_member`
+                                       ON `contact`.`id` = `group_member`.`contact-id`
+                               WHERE `gid` = ?
+                               AND `contact`.`uid` = ?
+                               AND NOT `contact`.`self`
+                               AND NOT `contact`.`deleted`
+                               AND NOT `contact`.`blocked`
+                               AND NOT `contact`.`pending`
+                               ORDER BY `contact`.`name` ASC',
+                               $gid,
+                               local_user()
+                       );
+
+                       if (DBA::isResult($stmt)) {
+                               $return = DBA::toArray($stmt);
+                       }
+               }
+
+               return $return;
+       }
+
+       /**
+        * Returns ungrouped contact count or list for user
+        *
+        * Returns either the total number of ungrouped contacts for the given user
+        * id or a paginated list of ungrouped contacts.
+        *
+        * @param int $uid uid
+        * @return array
+        * @throws \Exception
+        */
+       public static function getUngrouped(int $uid)
+       {
+               return q("SELECT *
+                          FROM `contact`
+                          WHERE `uid` = %d
+                          AND NOT `self`
+                          AND NOT `deleted`
+                          AND NOT `blocked`
+                          AND NOT `pending`
+                          AND `id` NOT IN (
+                               SELECT DISTINCT(`contact-id`)
+                               FROM `group_member`
+                               INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
+                               WHERE `group`.`uid` = %d
+                          )", intval($uid), intval($uid));
+       }
+
+       /**
+        * Remove a contact from all groups
+        *
+        * @param integer $contact_id
+        *
+        * @return boolean Success
+        */
+       public static function removeContact(int $contact_id)
+       {
+               return DBA::delete('group_member', ['contact-id' => $contact_id]);
+       }
+}
index d5f1fc8ef8430bee958096997dd5b7ae26532355..b7fffd4f11050dabc583d8948c24ab98292b15ff 100644 (file)
@@ -239,7 +239,7 @@ class Group extends BaseModule
                                DI::baseUrl()->redirect('contact');
                        }
 
-                       $members = Model\Contact::getByGroupId($group['id']);
+                       $members = Model\Contact\Group::getById($group['id']);
                        $preselected = [];
 
                        if (count($members)) {
@@ -255,7 +255,7 @@ class Group extends BaseModule
                                        Model\Group::addMember($group['id'], $change);
                                }
 
-                               $members = Model\Contact::getByGroupId($group['id']);
+                               $members = Model\Contact\Group::getById($group['id']);
                                $preselected = [];
                                if (count($members)) {
                                        foreach ($members as $member) {
@@ -316,7 +316,7 @@ class Group extends BaseModule
                }
 
                if ($nogroup) {
-                       $contacts = Model\Contact::getUngroupedList(local_user());
+                       $contacts = Model\Contact\Group::getUngrouped(local_user());
                } else {
                        $contacts_stmt = DBA::select('contact', [],
                                ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false],