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