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