]> git.mxchange.org Git - friendica.git/blob - src/Content/GroupManager.php
Merge pull request #13646 from annando/page-drop
[friendica.git] / src / Content / GroupManager.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\Content;
23
24 use Friendica\Content\Text\HTML;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30
31 /**
32  * This class handles methods related to the group functionality
33  */
34 class GroupManager
35 {
36         /**
37          * Function to list all groups a user is connected with
38          *
39          * @param int     $uid         of the profile owner
40          * @param boolean $lastitem    Sort by lastitem
41          * @param boolean $showhidden  Show groups which are not hidden
42          * @param boolean $showprivate Show private groups
43          *
44          * @return array
45          *    'url'    => group url
46          *    'name'    => group name
47          *    'id'    => number of the key from the array
48          *    'micro' => contact photo in format micro
49          *    'thumb' => contact photo in format thumb
50          * @throws \Exception
51          */
52         public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
53         {
54                 if ($lastitem) {
55                         $params = ['order' => ['last-item' => true]];
56                 } else {
57                         $params = ['order' => ['name']];
58                 }
59
60                 $condition = [
61                         'contact-type' => Contact::TYPE_COMMUNITY,
62                         'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB],
63                         'uid' => $uid,
64                         'blocked' => false,
65                         'pending' => false,
66                         'archive' => false,
67                 ];
68
69                 $condition = DBA::mergeConditions($condition, ["`platform` != ?", 'peertube']);
70
71                 if (!$showprivate) {
72                         $condition = DBA::mergeConditions($condition, ['manually-approve' => false]);
73                 }
74
75                 if (!$showhidden) {
76                         $condition = DBA::mergeConditions($condition, ['hidden' => false]);
77                 }
78
79                 $groupList = [];
80
81                 $fields = ['id', 'url', 'alias', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
82                 $contacts = DBA::select('account-user-view', $fields, $condition, $params);
83                 if (!$contacts) {
84                         return $groupList;
85                 }
86
87                 while ($contact = DBA::fetch($contacts)) {
88                         $groupList[] = [
89                                 'url'   => $contact['url'],
90                                 'alias' => $contact['alias'],
91                                 'name'  => $contact['name'],
92                                 'id'    => $contact['id'],
93                                 'micro' => $contact['micro'],
94                                 'thumb' => $contact['thumb'],
95                         ];
96                 }
97                 DBA::close($contacts);
98
99                 return($groupList);
100         }
101
102
103         /**
104          * Group list widget
105          *
106          * Sidebar widget to show subscribed Friendica groups. If activated
107          * in the settings, it appears in the network page sidebar
108          *
109          * @param string $baseurl Base module path
110          * @param int    $uid     The ID of the User
111          * @param int    $cid     The contact id which is used to mark a group as "selected"
112          * @return string
113          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
114          * @throws \ImagickException
115          */
116         public static function widget(string $baseurl, int $uid, int $cid = 0)
117         {
118                 $o = '';
119
120                 //sort by last updated item
121                 $lastitem = true;
122
123                 $contacts = self::getList($uid, $lastitem, true, true);
124                 $total = count($contacts);
125                 $visibleGroups = 10;
126
127                 if (DBA::isResult($contacts)) {
128                         $id = 0;
129
130                         $entries = [];
131
132                         foreach ($contacts as $contact) {
133                                 $selected = (($cid == $contact['id']) ? ' group-selected' : '');
134
135                                 $entry = [
136                                         'url' => $baseurl . '/' . $contact['id'],
137                                         'external_url' => Contact::magicLinkByContact($contact),
138                                         'name' => $contact['name'],
139                                         'cid' => $contact['id'],
140                                         'selected'      => $selected,
141                                         'micro' => DI::baseUrl()->remove(Contact::getMicro($contact)),
142                                         'id' => ++$id,
143                                 ];
144                                 $entries[] = $entry;
145                         }
146
147                         $tpl = Renderer::getMarkupTemplate('widget/group_list.tpl');
148
149                         $o .= Renderer::replaceMacros(
150                                 $tpl,
151                                 [
152                                         '$title'        => DI::l10n()->t('Groups'),
153                                         '$groups'       => $entries,
154                                         '$link_desc'    => DI::l10n()->t('External link to group'),
155                                         '$new_group_page' => 'register/',
156                                         '$total'        => $total,
157                                         '$visible_groups' => $visibleGroups,
158                                         '$showless'     => DI::l10n()->t('show less'),
159                                         '$showmore'     => DI::l10n()->t('show more'),
160                                         '$create_new_group' => DI::l10n()->t('Create new group')]
161                         );
162                 }
163
164                 return $o;
165         }
166
167         /**
168          * Format group list as contact block
169          *
170          * This function is used to show the group list in
171          * the advanced profile.
172          *
173          * @param int $uid The ID of the User
174          * @return string
175          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
176          * @throws \ImagickException
177          */
178         public static function profileAdvanced($uid)
179         {
180                 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
181                 if (!$profile) {
182                         return '';
183                 }
184
185                 $o = '';
186
187                 // placeholder in case somebody wants configurability
188                 $show_total = 9999;
189
190                 //don't sort by last updated item
191                 $lastitem = false;
192
193                 $contacts = self::getList($uid, $lastitem, false, false);
194
195                 $total_shown = 0;
196                 foreach ($contacts as $contact) {
197                         $o .= HTML::micropro($contact, true, 'grouplist-profile-advanced');
198                         $total_shown++;
199                         if ($total_shown == $show_total) {
200                                 break;
201                         }
202                 }
203
204                 return $o;
205         }
206
207         /**
208          * count unread group items
209          *
210          * Count unread items of connected groups and private groups
211          *
212          * @return array
213          *    'id' => contact id
214          *    'name' => contact/group name
215          *    'count' => counted unseen group items
216          * @throws \Exception
217          */
218         public static function countUnseenItems()
219         {
220                 $stmtContacts = DBA::p(
221                         "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `post-user-view`
222                                 INNER JOIN `contact` ON `post-user-view`.`contact-id` = `contact`.`id`
223                                 WHERE `post-user-view`.`uid` = ? AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted` AND `post-user-view`.`unseen`
224                                 AND `contact`.`network` IN (?, ?) AND `contact`.`contact-type` = ?
225                                 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
226                                 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
227                                 AND `contact`.`uid` = ?
228                                 GROUP BY `contact`.`id`",
229                         DI::userSession()->getLocalUserId(), Protocol::DFRN, Protocol::ACTIVITYPUB, Contact::TYPE_COMMUNITY, DI::userSession()->getLocalUserId()
230                 );
231
232                 return DBA::toArray($stmtContacts);
233         }
234 }