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