]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/Group.php
Fixed:
[friendica.git] / src / Model / Contact / Group.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model\Contact;
23
24 use Friendica\Database\DBA;
25 use Friendica\Model\Contact;
26
27 /**
28  * This class provides information about contact groups based on the "group_member" table.
29  */
30 class Group
31 {
32         /**
33          * Returns a list of contacts belonging in a group
34          *
35          * @param int $gid
36          * @return array
37          * @throws \Exception
38          */
39         public static function getById(int $gid): array
40         {
41                 $return = [];
42
43                 if (intval($gid)) {
44                         $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
45                                 FROM `contact`
46                                 INNER JOIN `group_member`
47                                         ON `contact`.`id` = `group_member`.`contact-id`
48                                 WHERE `gid` = ?
49                                 AND `contact`.`uid` = ?
50                                 AND NOT `contact`.`self`
51                                 AND NOT `contact`.`deleted`
52                                 AND NOT `contact`.`blocked`
53                                 AND NOT `contact`.`pending`
54                                 ORDER BY `contact`.`name` ASC',
55                                 $gid,
56                                 local_user()
57                         );
58
59                         if (DBA::isResult($stmt)) {
60                                 $return = DBA::toArray($stmt);
61                         }
62                 }
63
64                 return $return;
65         }
66
67         /**
68          * Returns ungrouped contact count or list for user
69          *
70          * Returns either the total number of ungrouped contacts for the given user
71          * id or a paginated list of ungrouped contacts.
72          *
73          * @param int $uid uid
74          * @return array
75          * @throws \Exception
76          */
77         public static function listUngrouped(int $uid)
78         {
79                 return Contact::selectToArray([], ["`uid` = ? AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `failed`
80                         AND `id` NOT IN (SELECT DISTINCT(`contact-id`) FROM `group_member` INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
81                                 WHERE `group`.`uid` = ?)", $uid, $uid]);
82         }
83
84         /**
85          * Remove a contact from all groups
86          *
87          * @param integer $contact_id
88          *
89          * @return boolean Success
90          */
91         public static function removeContact(int $contact_id)
92         {
93                 return DBA::delete('group_member', ['contact-id' => $contact_id]);
94         }
95 }