]> git.mxchange.org Git - friendica.git/blob - src/Content/ForumManager.php
Merge pull request #5167 from VVelox/develop
[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\App;
9 use Friendica\Content\Feature;
10 use Friendica\Core\L10n;
11 use Friendica\Core\System;
12 use Friendica\Model\Contact;
13 use Friendica\Database\DBM;
14 use dba;
15
16 require_once 'include/dba.php';
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          */
38         public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
39         {
40                 $forumlist = [];
41
42                 $order = (($showhidden) ? '' : ' AND NOT `hidden` ');
43                 $order .= (($lastitem) ? ' ORDER BY `last-item` DESC ' : ' ORDER BY `name` ASC ');
44                 $select = '`forum` ';
45                 if ($showprivate) {
46                         $select = '(`forum` OR `prv`)';
47                 }
48
49                 $contacts = dba::p(
50                         "SELECT `contact`.`id`, `contact`.`url`, `contact`.`name`, `contact`.`micro`, `contact`.`thumb`
51                         FROM `contact`
52                                 WHERE `network`= 'dfrn' AND $select AND `uid` = ?
53                                 AND NOT `blocked` AND NOT `pending` AND NOT `archive`
54                                 AND `success_update` > `failure_update`
55                         $order ",
56                         $uid
57                 );
58
59                 if (!$contacts) {
60                         return($forumlist);
61                 }
62
63                 while ($contact = dba::fetch($contacts)) {
64                         $forumlist[] = [
65                                 'url'   => $contact['url'],
66                                 'name'  => $contact['name'],
67                                 'id'    => $contact['id'],
68                                 'micro' => $contact['micro'],
69                                 'thumb' => $contact['thumb'],
70                         ];
71                 }
72                 dba::close($contacts);
73
74                 return($forumlist);
75         }
76
77
78         /**
79          * @brief Forumlist widget
80          *
81          * Sidebar widget to show subcribed friendica forums. If activated
82          * in the settings, it appears at the notwork page sidebar
83          *
84          * @param int $uid The ID of the User
85          * @param int $cid The contact id which is used to mark a forum as "selected"
86          * @return string
87          */
88         public static function widget($uid, $cid = 0)
89         {
90                 if (! intval(Feature::isEnabled(local_user(), 'forumlist_widget'))) {
91                         return;
92                 }
93
94                 $o = '';
95
96                 //sort by last updated item
97                 $lastitem = true;
98
99                 $contacts = self::getList($uid, $lastitem, true, true);
100                 $total = count($contacts);
101                 $visible_forums = 10;
102
103                 if (DBM::is_result($contacts)) {
104                         $id = 0;
105
106                         foreach ($contacts as $contact) {
107                                 $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
108
109                                 $entry = [
110                                         'url' => 'network?f=&cid=' . $contact['id'],
111                                         'external_url' => Contact::magicLink($contact['url']),
112                                         'name' => $contact['name'],
113                                         'cid' => $contact['id'],
114                                         'selected'      => $selected,
115                                         'micro' => System::removedBaseUrl(proxy_url($contact['micro'], false, PROXY_SIZE_MICRO)),
116                                         'id' => ++$id,
117                                 ];
118                                 $entries[] = $entry;
119                         }
120
121                         $tpl = get_markup_template('widget_forumlist.tpl');
122
123                         $o .= replace_macros(
124                                 $tpl,
125                                 [
126                                         '$title'        => L10n::t('Forums'),
127                                         '$forums'       => $entries,
128                                         '$link_desc'    => L10n::t('External link to forum'),
129                                         '$total'        => $total,
130                                         '$visible_forums' => $visible_forums,
131                                         '$showmore'     => L10n::t('show more')]
132                         );
133                 }
134
135                 return $o;
136         }
137
138         /**
139          * @brief Format forumlist as contact block
140          *
141          * This function is used to show the forumlist in
142          * the advanced profile.
143          *
144          * @param int $uid The ID of the User
145          * @return string
146          */
147         public static function profileAdvanced($uid)
148         {
149                 $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
150                 if (! $profile) {
151                         return;
152                 }
153
154                 $o = '';
155
156                 // place holder in case somebody wants configurability
157                 $show_total = 9999;
158
159                 //don't sort by last updated item
160                 $lastitem = false;
161
162                 $contacts = self::getList($uid, $lastitem, false, false);
163
164                 $total_shown = 0;
165                 $forumlist = '';
166                 foreach ($contacts as $contact) {
167                         $forumlist .= micropro($contact, false, 'forumlist-profile-advanced');
168                         $total_shown ++;
169                         if ($total_shown == $show_total) {
170                                 break;
171                         }
172                 }
173
174                 if (count($contacts) > 0) {
175                         $o .= $forumlist;
176                         return $o;
177                 }
178         }
179
180         /**
181          * @brief count unread forum items
182          *
183          * Count unread items of connected forums and private groups
184          *
185          * @return array
186          *      'id' => contact id
187          *      'name' => contact/forum name
188          *      'count' => counted unseen forum items
189          */
190         public static function countUnseenItems()
191         {
192                 $r = q(
193                         "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `item`
194                                 INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id`
195                                 WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` AND `item`.`unseen`
196                                 AND `contact`.`network`= 'dfrn' AND (`contact`.`forum` OR `contact`.`prv`)
197                                 AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
198                                 AND NOT `contact`.`pending` AND NOT `contact`.`archive`
199                                 AND `contact`.`success_update` > `failure_update`
200                                 GROUP BY `contact`.`id` ",
201                         intval(local_user())
202                 );
203
204                 return $r;
205         }
206 }