]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/Group.php
Merge remote-tracking branch 'upstream/develop' into item-view
[friendica.git] / src / Model / Contact / Group.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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
26 /**
27  * This class provides information about contact groups based on the "group_member" table.
28  */
29 class Group
30 {
31         /**
32          * Returns a list of contacts belonging in a group
33          *
34          * @param int $gid
35          * @return array
36          * @throws \Exception
37          */
38         public static function getById(int $gid)
39         {
40                 $return = [];
41
42                 if (intval($gid)) {
43                         $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
44                                 FROM `contact`
45                                 INNER JOIN `group_member`
46                                         ON `contact`.`id` = `group_member`.`contact-id`
47                                 WHERE `gid` = ?
48                                 AND `contact`.`uid` = ?
49                                 AND NOT `contact`.`self`
50                                 AND NOT `contact`.`deleted`
51                                 AND NOT `contact`.`blocked`
52                                 AND NOT `contact`.`pending`
53                                 ORDER BY `contact`.`name` ASC',
54                                 $gid,
55                                 local_user()
56                         );
57
58                         if (DBA::isResult($stmt)) {
59                                 $return = DBA::toArray($stmt);
60                         }
61                 }
62
63                 return $return;
64         }
65
66         /**
67          * Returns ungrouped contact count or list for user
68          *
69          * Returns either the total number of ungrouped contacts for the given user
70          * id or a paginated list of ungrouped contacts.
71          *
72          * @param int $uid uid
73          * @return array
74          * @throws \Exception
75          */
76         public static function listUngrouped(int $uid)
77         {
78                 return q("SELECT *
79                            FROM `contact`
80                            WHERE `uid` = %d
81                            AND NOT `self`
82                            AND NOT `deleted`
83                            AND NOT `blocked`
84                            AND NOT `pending`
85                            AND NOT `failed`
86                            AND `id` NOT IN (
87                                 SELECT DISTINCT(`contact-id`)
88                                 FROM `group_member`
89                                 INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
90                                 WHERE `group`.`uid` = %d
91                            )", intval($uid), intval($uid));
92         }
93
94         /**
95          * Remove a contact from all groups
96          *
97          * @param integer $contact_id
98          *
99          * @return boolean Success
100          */
101         public static function removeContact(int $contact_id)
102         {
103                 return DBA::delete('group_member', ['contact-id' => $contact_id]);
104         }
105 }