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