]> git.mxchange.org Git - friendica.git/blob - src/Content/GroupManager.php
Merge pull request #13203 from annando/styled-url
[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', '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                                 'name'  => $contact['name'],
91                                 'id'    => $contact['id'],
92                                 'micro' => $contact['micro'],
93                                 'thumb' => $contact['thumb'],
94                         ];
95                 }
96                 DBA::close($contacts);
97
98                 return($groupList);
99         }
100
101
102         /**
103          * Group list widget
104          *
105          * Sidebar widget to show subscribed Friendica groups. If activated
106          * in the settings, it appears in the network page sidebar
107          *
108          * @param string $baseurl Base module path
109          * @param int    $uid     The ID of the User
110          * @param int    $cid     The contact id which is used to mark a group as "selected"
111          * @return string
112          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
113          * @throws \ImagickException
114          */
115         public static function widget(string $baseurl, int $uid, int $cid = 0)
116         {
117                 $o = '';
118
119                 //sort by last updated item
120                 $lastitem = true;
121
122                 $contacts = self::getList($uid, $lastitem, true, true);
123                 $total = count($contacts);
124                 $visibleGroups = 10;
125
126                 if (DBA::isResult($contacts)) {
127                         $id = 0;
128
129                         $entries = [];
130
131                         foreach ($contacts as $contact) {
132                                 $selected = (($cid == $contact['id']) ? ' group-selected' : '');
133
134                                 $entry = [
135                                         'url' => $baseurl . '/' . $contact['id'],
136                                         'external_url' => Contact::magicLinkByContact($contact),
137                                         'name' => $contact['name'],
138                                         'cid' => $contact['id'],
139                                         'selected'      => $selected,
140                                         'micro' => DI::baseUrl()->remove(Contact::getMicro($contact)),
141                                         'id' => ++$id,
142                                 ];
143                                 $entries[] = $entry;
144                         }
145
146                         $tpl = Renderer::getMarkupTemplate('widget/group_list.tpl');
147
148                         $o .= Renderer::replaceMacros(
149                                 $tpl,
150                                 [
151                                         '$title'        => DI::l10n()->t('Groups'),
152                                         '$groups'       => $entries,
153                                         '$link_desc'    => DI::l10n()->t('External link to group'),
154                                         '$new_group_page' => 'register/',
155                                         '$total'        => $total,
156                                         '$visible_groups' => $visibleGroups,
157                                         '$showless'     => DI::l10n()->t('show less'),
158                                         '$showmore'     => DI::l10n()->t('show more'),
159                                         '$create_new_group' => DI::l10n()->t('Create new group')]
160                         );
161                 }
162
163                 return $o;
164         }
165
166         /**
167          * Format group list as contact block
168          *
169          * This function is used to show the group list in
170          * the advanced profile.
171          *
172          * @param int $uid The ID of the User
173          * @return string
174          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
175          * @throws \ImagickException
176          */
177         public static function profileAdvanced($uid)
178         {
179                 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
180                 if (!$profile) {
181                         return '';
182                 }
183
184                 $o = '';
185
186                 // placeholder in case somebody wants configurability
187                 $show_total = 9999;
188
189                 //don't sort by last updated item
190                 $lastitem = false;
191
192                 $contacts = self::getList($uid, $lastitem, false, false);
193
194                 $total_shown = 0;
195                 foreach ($contacts as $contact) {
196                         $o .= HTML::micropro($contact, true, 'grouplist-profile-advanced');
197                         $total_shown++;
198                         if ($total_shown == $show_total) {
199                                 break;
200                         }
201                 }
202
203                 return $o;
204         }
205
206         /**
207          * count unread group items
208          *
209          * Count unread items of connected groups and private groups
210          *
211          * @return array
212          *    'id' => contact id
213          *    'name' => contact/group name
214          *    'count' => counted unseen group items
215          * @throws \Exception
216          */
217         public static function countUnseenItems()
218         {
219                 $stmtContacts = DBA::p(
220                         "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `post-user-view`
221                                 INNER JOIN `contact` ON `post-user-view`.`contact-id` = `contact`.`id`
222                                 WHERE `post-user-view`.`uid` = ? AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted` AND `post-user-view`.`unseen`
223                                 AND `contact`.`network` IN (?, ?) AND `contact`.`contact-type` = ?
224                                 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
225                                 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
226                                 AND `contact`.`uid` = ?
227                                 GROUP BY `contact`.`id`",
228                         DI::userSession()->getLocalUserId(), Protocol::DFRN, Protocol::ACTIVITYPUB, Contact::TYPE_COMMUNITY, DI::userSession()->getLocalUserId()
229                 );
230
231                 return DBA::toArray($stmtContacts);
232         }
233 }