]> git.mxchange.org Git - friendica.git/blob - src/Model/Contact/Circle.php
Merge pull request #13442 from annando/channel-contac-visibility
[friendica.git] / src / Model / Contact / Circle.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\Content\Widget;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28
29 /**
30  * This class provides information about contact circles based on the "group_member" table.
31  */
32 class Circle
33 {
34         /**
35          * Returns a list of contacts belonging in a circle
36          *
37          * @param int $gid
38          * @return array
39          * @throws \Exception
40          */
41         public static function getById(int $gid): array
42         {
43                 $return = [];
44
45                 if (intval($gid)) {
46                         $networks = Widget::unavailableNetworks();
47                         $sql_values = array_merge([$gid, DI::userSession()->getLocalUserId()], $networks);
48
49                         $stmt = DBA::p('SELECT `circle_member`.`contact-id`, `contact`.*
50                                 FROM `contact`
51                                 INNER JOIN `group_member` AS `circle_member`
52                                         ON `contact`.`id` = `circle_member`.`contact-id`
53                                 WHERE `gid` = ?
54                                 AND `contact`.`uid` = ?
55                                 AND NOT `contact`.`self`
56                                 AND NOT `contact`.`deleted`
57                                 AND NOT `contact`.`blocked`
58                                 AND NOT `contact`.`pending`
59                                 AND NOT `contact`.`network` IN (' . substr(str_repeat('?, ', count($networks)), 0, -2) . ')
60                                 ORDER BY `contact`.`name` ASC',
61                                 $sql_values
62                         );
63
64                         if (DBA::isResult($stmt)) {
65                                 $return = DBA::toArray($stmt);
66                         }
67                 }
68
69                 return $return;
70         }
71
72         /**
73          * Returns uncircled contact count or list for user
74          *
75          * Returns either the total number of uncircled contacts for the given user
76          * id or a paginated list of uncircled contacts.
77          *
78          * @param int $uid uid
79          * @return array
80          * @throws \Exception
81          */
82         public static function listUncircled(int $uid)
83         {
84                 $networks = Widget::unavailableNetworks();
85                 $query = "`uid` = ? AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `failed`
86                         AND NOT `network` IN (" . substr(str_repeat('?, ', count($networks)), 0, -2) . ")
87                         AND `id` NOT IN (SELECT DISTINCT(`contact-id`) FROM `group_member` AS `circle_member` INNER JOIN `group` AS `circle` ON `circle`.`id` = `circle_member`.`gid`
88                         WHERE `circle`.`uid` = ? AND `contact-id` = `contact`.`id`)";
89                 $condition = array_merge([$query], [$uid], $networks, [$uid]);
90
91                 return Contact::selectToArray([], $condition);
92         }
93
94         /**
95          * Remove a contact from all circles
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 }