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