]> git.mxchange.org Git - friendica.git/blob - src/Content/ForumManager.php
Add mention support to shared content
[friendica.git] / src / Content / ForumManager.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 use Friendica\Util\Proxy as ProxyUtils;
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_str = "`network` IN (?, ?) AND `uid` = ? AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND ";
62
63                 if ($showprivate) {
64                         $condition_str .= '(`forum` OR `prv`)';
65                 } else {
66                         $condition_str .= '`forum`';
67                 }
68
69                 if (!$showhidden) {
70                         $condition_str .=  ' AND NOT `hidden`';
71                 }
72
73                 $forumlist = [];
74
75                 $fields = ['id', 'url', 'name', 'micro', 'thumb'];
76                 $condition = [$condition_str, Protocol::DFRN, Protocol::ACTIVITYPUB, $uid];
77                 $contacts = DBA::select('contact', $fields, $condition, $params);
78                 if (!$contacts) {
79                         return($forumlist);
80                 }
81
82                 while ($contact = DBA::fetch($contacts)) {
83                         $forumlist[] = [
84                                 'url'   => $contact['url'],
85                                 'name'  => $contact['name'],
86                                 'id'    => $contact['id'],
87                                 'micro' => $contact['micro'],
88                                 'thumb' => $contact['thumb'],
89                         ];
90                 }
91                 DBA::close($contacts);
92
93                 return($forumlist);
94         }
95
96
97         /**
98          * Forumlist widget
99          *
100          * Sidebar widget to show subcribed friendica forums. If activated
101          * in the settings, it appears at the notwork page sidebar
102          *
103          * @param int $uid The ID of the User
104          * @param int $cid The contact id which is used to mark a forum as "selected"
105          * @return string
106          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
107          * @throws \ImagickException
108          */
109         public static function widget($uid, $cid = 0)
110         {
111                 $o = '';
112
113                 //sort by last updated item
114                 $lastitem = true;
115
116                 $contacts = self::getList($uid, $lastitem, true, true);
117                 $total = count($contacts);
118                 $visible_forums = 10;
119
120                 if (DBA::isResult($contacts)) {
121                         $id = 0;
122
123                         $entries = [];
124
125                         foreach ($contacts as $contact) {
126                                 $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
127
128                                 $entry = [
129                                         'url' => 'network?contactid=' . $contact['id'],
130                                         'external_url' => Contact::magicLink($contact['url']),
131                                         'name' => $contact['name'],
132                                         'cid' => $contact['id'],
133                                         'selected'      => $selected,
134                                         'micro' => DI::baseUrl()->remove(ProxyUtils::proxifyUrl($contact['micro'], false, ProxyUtils::SIZE_MICRO)),
135                                         'id' => ++$id,
136                                 ];
137                                 $entries[] = $entry;
138                         }
139
140                         $tpl = Renderer::getMarkupTemplate('widget_forumlist.tpl');
141
142                         $o .= Renderer::replaceMacros(
143                                 $tpl,
144                                 [
145                                         '$title'        => DI::l10n()->t('Forums'),
146                                         '$forums'       => $entries,
147                                         '$link_desc'    => DI::l10n()->t('External link to forum'),
148                                         '$total'        => $total,
149                                         '$visible_forums' => $visible_forums,
150                                         '$showmore'     => DI::l10n()->t('show more')]
151                         );
152                 }
153
154                 return $o;
155         }
156
157         /**
158          * Format forumlist as contact block
159          *
160          * This function is used to show the forumlist in
161          * the advanced profile.
162          *
163          * @param int $uid The ID of the User
164          * @return string
165          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
166          * @throws \ImagickException
167          */
168         public static function profileAdvanced($uid)
169         {
170                 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
171                 if (!$profile) {
172                         return '';
173                 }
174
175                 $o = '';
176
177                 // place holder in case somebody wants configurability
178                 $show_total = 9999;
179
180                 //don't sort by last updated item
181                 $lastitem = false;
182
183                 $contacts = self::getList($uid, $lastitem, false, false);
184
185                 $total_shown = 0;
186                 foreach ($contacts as $contact) {
187                         $o .= HTML::micropro($contact, true, 'forumlist-profile-advanced');
188                         $total_shown++;
189                         if ($total_shown == $show_total) {
190                                 break;
191                         }
192                 }
193
194                 return $o;
195         }
196
197         /**
198          * count unread forum items
199          *
200          * Count unread items of connected forums and private groups
201          *
202          * @return array
203          *    'id' => contact id
204          *    'name' => contact/forum name
205          *    'count' => counted unseen forum items
206          * @throws \Exception
207          */
208         public static function countUnseenItems()
209         {
210                 $stmtContacts = DBA::p(
211                         "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `item`
212                                 INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id`
213                                 WHERE `item`.`uid` = ? AND `item`.`visible` AND NOT `item`.`deleted` AND `item`.`unseen`
214                                 AND `contact`.`network`= 'dfrn' AND (`contact`.`forum` OR `contact`.`prv`)
215                                 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
216                                 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
217                                 GROUP BY `contact`.`id` ",
218                         local_user()
219                 );
220
221                 return DBA::toArray($stmtContacts);
222         }
223 }