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