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