forum_page()   F
last analyzed

Complexity

Conditions 19
Paths 6480

Size

Total Lines 128
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 78
nc 6480
nop 3
dl 0
loc 128
rs 0.3499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2019 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// forum page: display 'New thread' button and list of threads
20
21
require_once('../inc/util.inc');
22
require_once('../inc/time.inc');
23
require_once('../inc/forum.inc');
24
require_once('../inc/pm.inc');
25
26
// show a forum.
27
// $user is null if not logged in
28
//
29
function forum_page($forum, $user, $msg=null) {
30
    global $forum_sort_styles;
31
32
    if (DISABLE_FORUMS) {
33
        if (!$user || !is_admin($user)) {
34
            error_page("Forums are disabled");
35
        }
36
    }
37
    $sort_style = get_int("sort", true);
38
    $start = get_int("start", true);
39
    if (!$start) $start = 0;
40
41
    $subs = [];
42
    if ($user) {
43
        BoincForumPrefs::lookup($user);
44
        $subs = BoincSubscription::enum("userid=$user->id");
45
    }
46
47
    if (!is_forum_visible_to_user($forum, $user)) {
48
        if ($user) {
49
            remove_subscriptions_forum($user->id, $forum->id);
50
        }
51
        error_page(tra("Not visible to you"));
52
    }
53
54
    if (!$sort_style) {
55
        // get the sort style either from the logged in user or a cookie
56
        if ($user){
57
            $sort_style = $user->prefs->forum_sorting;
58
        } else {
59
            list($sort_style, $thread_style) = parse_forum_cookie();
60
        }
61
    } else {
62
        // set the sort style
63
        if ($user){
64
            $user->prefs->forum_sorting = $sort_style;
65
            $user->prefs->update("forum_sorting=$sort_style");
66
        } else {
67
            list($old_style, $thread_style) = parse_forum_cookie();
68
            send_cookie(
69
                'sorting', implode("|", array($sort_style, $thread_style)), true
70
            );
71
        }
72
    }
73
74
    switch ($forum->parent_type) {
75
    case 0:
76
        $category = BoincCategory::lookup_id($forum->category);
77
        page_head(sprintf("%s '%s'", tra("Forum"), $forum->title));
78
        if ($msg) echo "<p>$msg</p>\n";
79
        show_forum_header($user);
80
        echo forum_title($category, $forum, NULL);
81
        break;
82
    case 1:
83
        $team = BoincTeam::lookup_id($forum->category);
84
        page_head(tra("Team message board for %1", $team->name));
85
        if ($msg) echo "<p>$msg</p>\n";
86
        show_forum_header($user);
87
        echo team_forum_title($forum);
88
        break;
89
    }
90
91
    echo '
92
        <p>
93
        <form action="forum_forum.php" method="get" class="form-inline">
94
        <input type="hidden" name="id" value="'.$forum->id.'">
95
        <table width="100%" cellspacing="0" cellpadding="0">
96
        <tr valign="top">
97
        <td colspan=2>
98
    ';
99
100
    switch (user_can_create_thread($user, $forum)) {
101
    case 'yes':
102
        show_button(
103
            "forum_post.php?id=$forum->id", tra("New thread"),
104
            tra("Add a new thread to this forum")
105
        );
106
        break;
107
    case 'login':
108
        echo "To add a thread, you must <a href=login_form.php>log in</a>.";
109
        break;
110
    }
111
112
    if ($user) {
113
        if (is_subscribed(-$forum->id, $subs)) {
114
            BoincNotify::delete_aux(sprintf(
115
                'userid=%d and type=%d and opaque=%d',
116
                $user->id,
117
                NOTIFY_SUBSCRIBED_FORUM,
118
                $forum->id
119
            ));
120
            show_button_small(
121
                "forum_forum.php?id=$forum->id&action=unsubscribe",
122
                'Unsubscribe',
123
                'Unsubscribe from this forum'
124
            );
125
        } else {
126
            show_button_small(
127
                "forum_forum.php?id=$forum->id&action=subscribe",
128
                'Subscribe',
129
                'Click to get notified when there are new threads in this forum'
130
            );
131
        }
132
    }
133
134
    echo '</td>
135
        <td valign=top align="right">
136
        <div class="form-group">
137
    ';
138
    echo select_from_array("sort", $forum_sort_styles, $sort_style);
139
    echo sprintf('
140
        <input class="btn btn-sm" %s type="submit" value="Sort">
141
        </div>
142
        </td>
143
        </tr>
144
        </table>
145
        </form>
146
        <p></p> ',
147
        button_style()
148
    );
149
150
    show_forum_threads($forum, $start, $sort_style, $user, $subs);
151
152
    echo "
153
        <p>".
154
        tra("This message board is available as an %1 RSS feed %2", "<a href=forum_rss.php?forumid=$forum->id&setup=1>", "<img src=img/feed_logo.png></a>");
155
156
    page_tail();
157
}
158
159
// Show the threads for the given forum
160
// starting from $start,
161
// using the given $sort_style (as defined in forum.php)
162
// $user is logged-in user, or null
163
// If $user is not null, $subs is list of their subscriptions
164
//
165
function show_forum_threads($forum, $start, $sort_style, $user, $subs) {
166
    $page_nav = page_links(
167
        "forum_forum.php?id=$forum->id&amp;sort=$sort_style",
168
        $forum->threads,
169
        THREADS_PER_PAGE,
170
        $start
171
    );
172
    echo $page_nav;
173
    start_table('table-striped');
174
    row_heading_array(
175
        array(
176
            "",
177
            tra("Threads"),
178
            tra("Posts"),
179
            tra("Author"),
180
            tra("Views"),
181
            "<nobr>".tra("Last post")."</nobr>"
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
182
        ),
183
        array("", "width=35%", "", "", "", "")
184
185
    );
186
187
    $sticky_first = !$user || !$user->prefs->ignore_sticky_posts;
188
189
    // Show hidden threads if logged in user is a moderator
190
    //
191
    $show_hidden = is_moderator($user, $forum);
192
    $threads = get_forum_threads(
193
        $forum->id, $start, THREADS_PER_PAGE,
194
        $sort_style, $show_hidden, $sticky_first
195
    );
196
197
    // Run through the list of threads, displaying each of them
198
    //
199
    foreach ($threads as $thread) {
200
        $owner = BoincUser::lookup_id($thread->owner);
201
        if (!$owner) continue;
202
        if (!$show_hidden && is_banished($owner)) continue;
203
        $unread = thread_is_unread($user, $thread);
204
205
        //if ($thread->status==1){
206
            // This is an answered helpdesk thread
207
        if ($user && is_subscribed($thread->id, $subs)) {
208
            echo '<tr class="bg-info">';
209
        } else {
210
            // Just a standard thread.
211
            echo '<tr>';
212
        }
213
214
        echo "<td width=\"1%\"><nobr>";
215
        if ($thread->hidden) {
216
            show_image(IMAGE_HIDDEN, tra("This thread is hidden"), tra("hidden"));
217
        } else if ($unread) {
218
            if ($thread->sticky) {
219
                if ($thread->locked) {
220
                    show_image(NEW_IMAGE_STICKY_LOCKED, tra("This thread is sticky and locked, and you haven't read it yet"), tra("sticky/locked/unread"));
221
                } else {
222
                    show_image(NEW_IMAGE_STICKY, tra("This thread is sticky and you haven't read it yet"), tra("sticky/unread"));
223
                }
224
            } else {
225
                if ($thread->locked) {
226
                    show_image(NEW_IMAGE_LOCKED, tra("You haven't read this thread yet, and it's locked"), tra("unread/locked"));
227
                } else {
228
                    show_image(NEW_IMAGE, tra("You haven't read this thread yet"), tra("unread"));
229
                }
230
            }
231
        } else {
232
            if ($thread->sticky) {
233
                if ($thread->locked) {
234
                    show_image(IMAGE_STICKY_LOCKED, tra("This thread is sticky and locked"), tra("sticky/locked"));
235
                } else {
236
                    show_image(IMAGE_STICKY, tra("This thread is sticky"), tra("sticky"));
237
                }
238
            } else {
239
                if ($thread->locked) {
240
                    show_image(IMAGE_LOCKED, tra("This thread is locked"), tra("locked"));
241
                } else {
242
                    show_image(IMAGE_POST, tra("You read this thread"), tra("read"));
243
                }
244
            }
245
        }
246
        echo "</nobr></td>";
247
248
        $title = cleanup_title($thread->title);
249
        //$titlelength = 9999;
250
        //if (strlen($title) > $titlelength) {
251
        //    $title = substr($title, 0, $titlelength)."...";
252
        //}
253
        echo "<td><a href=\"forum_thread.php?id=$thread->id\">$title</a><br></td>";
254
255
        echo '
256
            <td>'.($thread->replies+1).'</td>
257
            <td>'.user_links($owner, BADGE_HEIGHT_SMALL).'</td>
258
            <td>'.$thread->views.'</td>
259
            <td>'.time_diff_str($thread->timestamp, time()).'</td>
260
            </tr>
261
        ';
262
        flush();
263
    }
264
    end_table();
265
    echo "<br>$page_nav";    // show page links
266
}
267
268
$id = get_int("id");
269
$forum = BoincForum::lookup_id($id);
270
if (!$forum) error_page("forum ID not found");
271
$user = get_logged_in_user(false);
272
$action = get_str('action', true);
273
274
if ($action == 'subscribe') {
275
    $user = get_logged_in_user();
276
    BoincSubscription::replace($user->id, -$id);
277
    forum_page($forum, $user, 'You are now subscribed to this forum.');
278
} else if ($action == 'unsubscribe') {
279
    $user = get_logged_in_user();
280
    BoincSubscription::delete($user->id, -$id);
281
    forum_page($forum, $user, 'You are now unsubscribed from this forum.');
282
} else {
283
    forum_page($forum, $user);
284
}
285
286
?>
287