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