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