]> git.mxchange.org Git - friendica.git/blob - src/Content/ForumManager.php
old boot.php functions replaced in /src
[friendica.git] / src / Content / ForumManager.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\Session;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31
32 /**
33  * This class handles methods related to the forum functionality
34  */
35 class ForumManager
36 {
37         /**
38          * Function to list all forums a user is connected with
39          *
40          * @param int     $uid         of the profile owner
41          * @param boolean $lastitem    Sort by lastitem
42          * @param boolean $showhidden  Show frorums which are not hidden
43          * @param boolean $showprivate Show private groups
44          *
45          * @return array
46          *    'url'    => forum url
47          *    'name'    => forum name
48          *    'id'    => number of the key from the array
49          *    'micro' => contact photo in format micro
50          *    'thumb' => contact photo in format thumb
51          * @throws \Exception
52          */
53         public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
54         {
55                 if ($lastitem) {
56                         $params = ['order' => ['last-item' => true]];
57                 } else {
58                         $params = ['order' => ['name']];
59                 }
60
61                 $condition = [
62                         'contact-type' => Contact::TYPE_COMMUNITY,
63                         'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB],
64                         'uid' => $uid, 
65                         'blocked' => false,
66                         'pending' => false,
67                         'archive' => false, 
68                 ];
69
70                 $condition = DBA::mergeConditions($condition, ["`platform` != ?", 'peertube']);
71
72                 if (!$showprivate) {
73                         $condition = DBA::mergeConditions($condition, ['manually-approve' => false]);
74                 }
75
76                 if (!$showhidden) {
77                         $condition = DBA::mergeConditions($condition, ['hidden' => false]);
78                 }
79
80                 $forumlist = [];
81
82                 $fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
83                 $contacts = DBA::select('account-user-view', $fields, $condition, $params);
84                 if (!$contacts) {
85                         return($forumlist);
86                 }
87
88                 while ($contact = DBA::fetch($contacts)) {
89                         $forumlist[] = [
90                                 'url'   => $contact['url'],
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($forumlist);
100         }
101
102
103         /**
104          * Forumlist widget
105          *
106          * Sidebar widget to show subcribed friendica forums. If activated
107          * in the settings, it appears at the notwork 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 forum 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                 $visible_forums = 10;
126
127                 if (DBA::isResult($contacts)) {
128                         $id = 0;
129
130                         $entries = [];
131
132                         foreach ($contacts as $contact) {
133                                 $selected = (($cid == $contact['id']) ? ' forum-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_forumlist.tpl');
148
149                         $o .= Renderer::replaceMacros(
150                                 $tpl,
151                                 [
152                                         '$title'        => DI::l10n()->t('Forums'),
153                                         '$forums'       => $entries,
154                                         '$link_desc'    => DI::l10n()->t('External link to forum'),
155                                         '$total'        => $total,
156                                         '$visible_forums' => $visible_forums,
157                                         '$showless'     => DI::l10n()->t('show less'),
158                                         '$showmore'     => DI::l10n()->t('show more')]
159                         );
160                 }
161
162                 return $o;
163         }
164
165         /**
166          * Format forumlist as contact block
167          *
168          * This function is used to show the forumlist in
169          * the advanced profile.
170          *
171          * @param int $uid The ID of the User
172          * @return string
173          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
174          * @throws \ImagickException
175          */
176         public static function profileAdvanced($uid)
177         {
178                 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
179                 if (!$profile) {
180                         return '';
181                 }
182
183                 $o = '';
184
185                 // place holder in case somebody wants configurability
186                 $show_total = 9999;
187
188                 //don't sort by last updated item
189                 $lastitem = false;
190
191                 $contacts = self::getList($uid, $lastitem, false, false);
192
193                 $total_shown = 0;
194                 foreach ($contacts as $contact) {
195                         $o .= HTML::micropro($contact, true, 'forumlist-profile-advanced');
196                         $total_shown++;
197                         if ($total_shown == $show_total) {
198                                 break;
199                         }
200                 }
201
202                 return $o;
203         }
204
205         /**
206          * count unread forum items
207          *
208          * Count unread items of connected forums and private groups
209          *
210          * @return array
211          *    'id' => contact id
212          *    'name' => contact/forum name
213          *    'count' => counted unseen forum items
214          * @throws \Exception
215          */
216         public static function countUnseenItems()
217         {
218                 $stmtContacts = DBA::p(
219                         "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `post-user-view`
220                                 INNER JOIN `contact` ON `post-user-view`.`contact-id` = `contact`.`id`
221                                 WHERE `post-user-view`.`uid` = ? AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted` AND `post-user-view`.`unseen`
222                                 AND `contact`.`network` IN (?, ?) AND `contact`.`contact-type` = ?
223                                 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
224                                 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
225                                 AND `contact`.`uid` = ?
226                                 GROUP BY `contact`.`id`",
227                         Session::getLocalUser(), Protocol::DFRN, Protocol::ACTIVITYPUB, Contact::TYPE_COMMUNITY, Session::getLocalUser()
228                 );
229
230                 return DBA::toArray($stmtContacts);
231         }
232 }