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']];
60 $condition_str = "`network` IN (?, ?) AND `uid` = ? AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND ";
63 $condition_str .= '(`forum` OR `prv`)';
65 $condition_str .= '`forum`';
69 $condition_str .= ' AND NOT `hidden`';
74 $fields = ['id', 'url', 'name', 'micro', 'thumb', 'avatar', 'network', 'uid'];
75 $condition = [$condition_str, Protocol::DFRN, Protocol::ACTIVITYPUB, $uid];
76 $contacts = DBA::select('contact', $fields, $condition, $params);
81 while ($contact = DBA::fetch($contacts)) {
83 'url' => $contact['url'],
84 'name' => $contact['name'],
85 'id' => $contact['id'],
86 'micro' => $contact['micro'],
87 'thumb' => $contact['thumb'],
90 DBA::close($contacts);
99 * Sidebar widget to show subcribed friendica forums. If activated
100 * in the settings, it appears at the notwork page sidebar
102 * @param string $baseurl Base module path
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"
106 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
107 * @throws \ImagickException
109 public static function widget(string $baseurl, int $uid, int $cid = 0)
113 //sort by last updated item
116 $contacts = self::getList($uid, $lastitem, true, true);
117 $total = count($contacts);
118 $visible_forums = 10;
120 if (DBA::isResult($contacts)) {
125 foreach ($contacts as $contact) {
126 $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
129 'url' => $baseurl . '/' . $contact['id'],
130 'external_url' => Contact::magicLinkByContact($contact),
131 'name' => $contact['name'],
132 'cid' => $contact['id'],
133 'selected' => $selected,
134 'micro' => DI::baseUrl()->remove(Contact::getMicro($contact)),
140 $tpl = Renderer::getMarkupTemplate('widget_forumlist.tpl');
142 $o .= Renderer::replaceMacros(
145 '$title' => DI::l10n()->t('Forums'),
146 '$forums' => $entries,
147 '$link_desc' => DI::l10n()->t('External link to forum'),
149 '$visible_forums' => $visible_forums,
150 '$showless' => DI::l10n()->t('show less'),
151 '$showmore' => DI::l10n()->t('show more')]
159 * Format forumlist as contact block
161 * This function is used to show the forumlist in
162 * the advanced profile.
164 * @param int $uid The ID of the User
166 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
167 * @throws \ImagickException
169 public static function profileAdvanced($uid)
171 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
178 // place holder in case somebody wants configurability
181 //don't sort by last updated item
184 $contacts = self::getList($uid, $lastitem, false, false);
187 foreach ($contacts as $contact) {
188 $o .= HTML::micropro($contact, true, 'forumlist-profile-advanced');
190 if ($total_shown == $show_total) {
199 * count unread forum items
201 * Count unread items of connected forums and private groups
205 * 'name' => contact/forum name
206 * 'count' => counted unseen forum items
209 public static function countUnseenItems()
211 $stmtContacts = DBA::p(
212 "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `post-user-view`
213 INNER JOIN `contact` ON `post-user-view`.`contact-id` = `contact`.`id`
214 WHERE `post-user-view`.`uid` = ? AND `post-user-view`.`visible` AND NOT `post-user-view`.`deleted` AND `post-user-view`.`unseen`
215 AND `contact`.`network` IN (?, ?) AND `contact`.`contact-type` = ?
216 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
217 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
218 AND `contact`.`uid` = ?
219 GROUP BY `contact`.`id`",
220 local_user(), Protocol::DFRN, Protocol::ACTIVITYPUB, Contact::TYPE_COMMUNITY, local_user()
223 return DBA::toArray($stmtContacts);