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