]> git.mxchange.org Git - friendica.git/blob - src/Content/ForumManager.php
11847383fe96f3e46983398b1c9afe65abf5d13a
[friendica.git] / src / Content / ForumManager.php
1 <?php
2 /**
3  * @file src/Content/ForumManager.php
4  * @brief ForumManager class with its methods related to forum functionality
5  */
6 namespace Friendica\Content;
7
8 use Friendica\Core\Protocol;
9 use Friendica\Content\Feature;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Util\Proxy as ProxyUtils;
17
18 /**
19  * @brief This class handles methods related to the forum functionality
20  */
21 class ForumManager
22 {
23         /**
24          * @brief Function to list all forums a user is connected with
25          *
26          * @param int     $uid         of the profile owner
27          * @param boolean $lastitem    Sort by lastitem
28          * @param boolean $showhidden  Show frorums which are not hidden
29          * @param boolean $showprivate Show private groups
30          *
31          * @return array
32          *    'url'    => forum url
33          *    'name'    => forum name
34          *    'id'    => number of the key from the array
35          *    'micro' => contact photo in format micro
36          *    'thumb' => contact photo in format thumb
37          * @throws \Exception
38          */
39         public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
40         {
41                 if ($lastitem) {
42                         $params = ['order' => ['last-item' => true]];
43                 } else {
44                         $params = ['order' => ['name']];
45                 }
46
47                 $condition_str = "`network` = ? AND `uid` = ? AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `success_update` > `failure_update` AND ";
48
49                 if ($showprivate) {
50                         $condition_str .= '(`forum` OR `prv`)';
51                 } else {
52                         $condition_str .= '`forum`';
53                 }
54
55                 if (!$showhidden) {
56                         $condition_str .=  ' AND NOT `hidden`';
57                 }
58
59                 $forumlist = [];
60
61                 $fields = ['id', 'url', 'name', 'micro', 'thumb'];
62                 $condition = [$condition_str, Protocol::DFRN, $uid];
63                 $contacts = DBA::select('contact', $fields, $condition, $params);
64                 if (!$contacts) {
65                         return($forumlist);
66                 }
67
68                 while ($contact = DBA::fetch($contacts)) {
69                         $forumlist[] = [
70                                 'url'   => $contact['url'],
71                                 'name'  => $contact['name'],
72                                 'id'    => $contact['id'],
73                                 'micro' => $contact['micro'],
74                                 'thumb' => $contact['thumb'],
75                         ];
76                 }
77                 DBA::close($contacts);
78
79                 return($forumlist);
80         }
81
82
83         /**
84          * @brief Forumlist widget
85          *
86          * Sidebar widget to show subcribed friendica forums. If activated
87          * in the settings, it appears at the notwork page sidebar
88          *
89          * @param int $uid The ID of the User
90          * @param int $cid The contact id which is used to mark a forum as "selected"
91          * @return string
92          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93          * @throws \ImagickException
94          */
95         public static function widget($uid, $cid = 0)
96         {
97                 $o = '';
98
99                 //sort by last updated item
100                 $lastitem = true;
101
102                 $contacts = self::getList($uid, $lastitem, true, true);
103                 $total = count($contacts);
104                 $visible_forums = 10;
105
106                 if (DBA::isResult($contacts)) {
107                         $id = 0;
108
109                         foreach ($contacts as $contact) {
110                                 $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
111
112                                 $entry = [
113                                         'url' => 'network?f=&cid=' . $contact['id'],
114                                         'external_url' => Contact::magicLink($contact['url']),
115                                         'name' => $contact['name'],
116                                         'cid' => $contact['id'],
117                                         'selected'      => $selected,
118                                         'micro' => System::removedBaseUrl(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'        => L10n::t('Forums'),
130                                         '$forums'       => $entries,
131                                         '$link_desc'    => L10n::t('External link to forum'),
132                                         '$total'        => $total,
133                                         '$visible_forums' => $visible_forums,
134                                         '$showmore'     => L10n::t('show more')]
135                         );
136                 }
137
138                 return $o;
139         }
140
141         /**
142          * @brief 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                 $forumlist = '';
171                 foreach ($contacts as $contact) {
172                         $forumlist .= HTML::micropro($contact, false, 'forumlist-profile-advanced');
173                         $total_shown ++;
174                         if ($total_shown == $show_total) {
175                                 break;
176                         }
177                 }
178
179                 if (count($contacts) > 0) {
180                         $o .= $forumlist;
181                         return $o;
182                 }
183         }
184
185         /**
186          * @brief count unread forum items
187          *
188          * Count unread items of connected forums and private groups
189          *
190          * @return array
191          *    'id' => contact id
192          *    'name' => contact/forum name
193          *    'count' => counted unseen forum items
194          * @throws \Exception
195          */
196         public static function countUnseenItems()
197         {
198                 $r = q(
199                         "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `item`
200                                 INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id`
201                                 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` AND `item`.`unseen`
202                                 AND `contact`.`network`= 'dfrn' AND (`contact`.`forum` OR `contact`.`prv`)
203                                 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
204                                 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
205                                 AND `contact`.`success_update` > `failure_update`
206                                 GROUP BY `contact`.`id` ",
207                         intval(local_user())
208                 );
209
210                 return $r;
211         }
212 }