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