3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Content;
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;
30 use Friendica\Model\Contact;
33 * This class handles methods related to the forum functionality
38 * Function to list all forums a user is connected with
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
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
53 public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
56 $params = ['order' => ['last-item' => true]];
58 $params = ['order' => ['name']];
62 'contact-type' => Contact::TYPE_COMMUNITY,
63 'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB],
70 $condition = DBA::mergeConditions($condition, ["`platform` != ?", 'peertube']);
73 $condition = DBA::mergeConditions($condition, ['manually-approve' => false]);
77 $condition = DBA::mergeConditions($condition, ['hidden' => false]);
82 $fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
83 $contacts = DBA::select('account-user-view', $fields, $condition, $params);
88 while ($contact = DBA::fetch($contacts)) {
90 'url' => $contact['url'],
91 'name' => $contact['name'],
92 'id' => $contact['id'],
93 'micro' => $contact['micro'],
94 'thumb' => $contact['thumb'],
97 DBA::close($contacts);
106 * Sidebar widget to show subcribed friendica forums. If activated
107 * in the settings, it appears at the notwork page sidebar
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"
113 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
114 * @throws \ImagickException
116 public static function widget(string $baseurl, int $uid, int $cid = 0)
120 //sort by last updated item
123 $contacts = self::getList($uid, $lastitem, true, true);
124 $total = count($contacts);
125 $visible_forums = 10;
127 if (DBA::isResult($contacts)) {
132 foreach ($contacts as $contact) {
133 $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
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)),
147 $tpl = Renderer::getMarkupTemplate('widget_forumlist.tpl');
149 $o .= Renderer::replaceMacros(
152 '$title' => DI::l10n()->t('Forums'),
153 '$forums' => $entries,
154 '$link_desc' => DI::l10n()->t('External link to forum'),
156 '$visible_forums' => $visible_forums,
157 '$showless' => DI::l10n()->t('show less'),
158 '$showmore' => DI::l10n()->t('show more')]
166 * Format forumlist as contact block
168 * This function is used to show the forumlist in
169 * the advanced profile.
171 * @param int $uid The ID of the User
173 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
174 * @throws \ImagickException
176 public static function profileAdvanced($uid)
178 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
185 // place holder in case somebody wants configurability
188 //don't sort by last updated item
191 $contacts = self::getList($uid, $lastitem, false, false);
194 foreach ($contacts as $contact) {
195 $o .= HTML::micropro($contact, true, 'forumlist-profile-advanced');
197 if ($total_shown == $show_total) {
206 * count unread forum items
208 * Count unread items of connected forums and private groups
212 * 'name' => contact/forum name
213 * 'count' => counted unseen forum items
216 public static function countUnseenItems()
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()
230 return DBA::toArray($stmtContacts);