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