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\Database\DBA;
29 use Friendica\Model\Contact;
32 * This class handles methods related to the forum functionality
37 * Function to list all forums a user is connected with
39 * @param int $uid of the profile owner
40 * @param boolean $lastitem Sort by lastitem
41 * @param boolean $showhidden Show frorums which are not hidden
42 * @param boolean $showprivate Show private groups
46 * 'name' => forum name
47 * 'id' => number of the key from the array
48 * 'micro' => contact photo in format micro
49 * 'thumb' => contact photo in format thumb
52 public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
55 $params = ['order' => ['last-item' => true]];
57 $params = ['order' => ['name']];
61 'contact-type' => Contact::TYPE_COMMUNITY,
62 'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB],
69 $condition = DBA::mergeConditions($condition, ["`platform` != ?", 'peertube']);
72 $condition = DBA::mergeConditions($condition, ['manually-approve' => false]);
76 $condition = DBA::mergeConditions($condition, ['hidden' => false]);
81 $fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
82 $contacts = DBA::select('account-user-view', $fields, $condition, $params);
87 while ($contact = DBA::fetch($contacts)) {
89 'url' => $contact['url'],
90 'name' => $contact['name'],
91 'id' => $contact['id'],
92 'micro' => $contact['micro'],
93 'thumb' => $contact['thumb'],
96 DBA::close($contacts);
105 * Sidebar widget to show subcribed friendica forums. If activated
106 * in the settings, it appears at the notwork page sidebar
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 forum as "selected"
112 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
113 * @throws \ImagickException
115 public static function widget(string $baseurl, int $uid, int $cid = 0)
119 //sort by last updated item
122 $contacts = self::getList($uid, $lastitem, true, true);
123 $total = count($contacts);
124 $visible_forums = 10;
126 if (DBA::isResult($contacts)) {
131 foreach ($contacts as $contact) {
132 $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
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)),
146 $tpl = Renderer::getMarkupTemplate('widget_forumlist.tpl');
148 $o .= Renderer::replaceMacros(
151 '$title' => DI::l10n()->t('Forums'),
152 '$forums' => $entries,
153 '$link_desc' => DI::l10n()->t('External link to forum'),
155 '$visible_forums' => $visible_forums,
156 '$showless' => DI::l10n()->t('show less'),
157 '$showmore' => DI::l10n()->t('show more')]
165 * Format forumlist as contact block
167 * This function is used to show the forumlist in
168 * the advanced profile.
170 * @param int $uid The ID of the User
172 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
173 * @throws \ImagickException
175 public static function profileAdvanced($uid)
177 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
184 // place holder in case somebody wants configurability
187 //don't sort by last updated item
190 $contacts = self::getList($uid, $lastitem, false, false);
193 foreach ($contacts as $contact) {
194 $o .= HTML::micropro($contact, true, 'forumlist-profile-advanced');
196 if ($total_shown == $show_total) {
205 * count unread forum items
207 * Count unread items of connected forums and private groups
211 * 'name' => contact/forum name
212 * 'count' => counted unseen forum items
215 public static function countUnseenItems()
217 $stmtContacts = DBA::p(
218 "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `post-user-view`
219 INNER JOIN `contact` ON `post-user-view`.`contact-id` = `contact`.`id`
220 WHERE `post-user-view`.`uid` = ? AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted` AND `post-user-view`.`unseen`
221 AND `contact`.`network` IN (?, ?) AND `contact`.`contact-type` = ?
222 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
223 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
224 AND `contact`.`uid` = ?
225 GROUP BY `contact`.`id`",
226 local_user(), Protocol::DFRN, Protocol::ACTIVITYPUB, Contact::TYPE_COMMUNITY, local_user()
229 return DBA::toArray($stmtContacts);