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