Completed
Push — master ( 66731e...5478d6 )
by Michael
02:20
created

newbb.php ➔ b_marquee_newbb()   D

Complexity

Conditions 13
Paths 26

Size

Total Lines 88
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 64
nc 26
nop 3
dl 0
loc 88
rs 4.9922
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
/**
3
 * ****************************************************************************
4
 * marquee - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard (http://www.herve-thouzard.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright          Hervé Thouzard (http://www.herve-thouzard.com)
15
 * @license            http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package            marquee
17
 * @author             Hervé Thouzard (http://www.herve-thouzard.com)
18
 * @version            $Id $
19
 * ****************************************************************************
20
 *
21
 * @param $limit
22
 * @param $dateFormat
23
 * @param $itemsSize
24
 *
25
 * @return array
26
 */
27
28
// Script to list recent posts from Newbb 1 & 2
29
function b_marquee_newbb($limit, $dateFormat, $itemsSize)
30
{
31
    include_once XOOPS_ROOT_PATH . '/modules/marquee/include/functions.php';
32
    $block = array();
33
34
    $moduleHandler = xoops_getHandler('module');
35
    $newbb          = $moduleHandler->getByDirname('newbb');
36
    $newbbVersion  = (int)$newbb->getInfo('version');
37
38
    if ($newbbVersion >= 2) {
39
        $order          = 't.topic_time';
40
        $forumHandler  = xoops_getModuleHandler('forum', 'newbb');
41
        $moduleHandler = xoops_getHandler('module');
42
        $newbb          = $moduleHandler->getByDirname('newbb');
43
44
        if (!isset($newbbConfig)) {
0 ignored issues
show
Bug introduced by
The variable $newbbConfig seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
45
            $configHandler = xoops_getHandler('config');
46
            $newbbConfig    = &$configHandler->getConfigsByCat(0, $newbb->getVar('mid'));
47
        }
48
49
        if (!isset($access_forums)) {
0 ignored issues
show
Bug introduced by
The variable $access_forums seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
50
            $access_forums = $forumHandler->getForums(0, 'access'); // get all accessible forums
51
        }
52
        $validForums   = array_keys($access_forums);
53
        $forumCriteria = ' AND t.forum_id IN (' . implode(',', $validForums) . ')';
54
        unset($access_forums);
55
        $approveCriteria = ' AND t.approved = 1 AND p.approved = 1';
56
        $db               = XoopsDatabaseFactory::getDatabaseConnection();
57
        $query            = 'SELECT t.*, f.forum_name, f.allow_subject_prefix, p.post_id, p.icon, p.uid, p.poster_name, u.uname, u.name FROM ' . $db->prefix('bb_topics') . ' t, ' . $db->prefix('bb_forums') . ' f, ' . $db->prefix('bb_posts') . ' p LEFT JOIN ' . $db->prefix('users') . ' u ON u.uid = p.uid WHERE f.forum_id=t.forum_id ' . $forumCriteria . $approveCriteria . ' AND t.topic_last_post_id=p.post_id ORDER BY ' . $order . ' DESC';
58
        $result           = $db->query($query, $limit, 0);
59
        if (!$result) {
60
            return '';
61
        }
62
        $rows = array();
63
        while ($row = $db->fetchArray($result)) {
64
            $rows[] = $row;
65
        }
66
        if (count($rows) < 1) {
67
            return false;
68
        }
69
        $myts = MyTextSanitizer::getInstance();
70
71
        foreach ($rows as $arr) {
72
            $title = $myts->htmlSpecialChars($arr['topic_title']);
73
            if ($itemsSize > 0) {
74
                $title = xoops_substr($title, 0, $itemsSize + 3);
75
            }
76
77
            $block[] = array(
78
                'date'     => formatTimestamp($arr['topic_time'], $dateFormat),
79
                'category' => $arr['forum_name'],
80
                'author'   => $arr['uid'],
81
                'title'    => $title,
82
                'link'     => "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $arr['topic_id'] . '&amp;post_id=' . $arr['post_id'] . '#forumpost' . $arr['post_id'] . "'>" . $title . '</a>'
83
            );
84
        }
85
    } else { // Newbb 1
86
        $db    = XoopsDatabaseFactory::getDatabaseConnection();
87
        $myts  = MyTextSanitizer::getInstance();
88
        $order = 't.topic_time';
89
        $time  = $tmpuser = '';
90
        $query = 'SELECT t.topic_id, t.topic_title, t.topic_last_post_id, t.topic_time, t.topic_views, t.topic_replies, t.forum_id, f.forum_name FROM ' . $db->prefix('bb_topics') . ' t, ' . $db->prefix('bb_forums') . ' f WHERE f.forum_id=t.forum_id AND f.forum_type <> 1 ORDER BY ' . $order . ' DESC';
91
        if (!$result = $db->query($query, $limit, 0)) {
92
            return '';
93
        }
94
        while ($arr = $db->fetchArray($result)) {
95
            $lastpostername = $db->query('SELECT post_id, uid FROM ' . $db->prefix('bb_posts') . ' WHERE post_id = ' . $arr['topic_last_post_id']);
96
            while ($tmpdb = $db->fetchArray($lastpostername)) {
97
                $tmpuser = XoopsUser::getUnameFromId($tmpdb['uid']);
98
                $time    = formatTimestamp($arr['topic_time'], $dateFormat);
99
            }
100
            $title = $myts->htmlSpecialChars($arr['topic_title']);
101
            if ($itemsSize > 0) {
102
                $title = xoops_substr($title, 0, $itemsSize + 3);
103
            }
104
105
            $block[] = array(
106
                'date'     => $time,
107
                'category' => $arr['forum_name'],
108
                'author'   => $tmpuser,
109
                'title'    => $title,
110
                'link'     => "<a href='" . XOOPS_URL . '/modules/newbb/viewtopic.php?topic_id=' . $arr['topic_id'] . '&amp;forum=' . $arr['forum_id'] . '&amp;post_id=' . $arr['topic_last_post_id'] . '#forumpost' . $arr['topic_last_post_id'] . "'>" . $title . '</a>'
111
            );
112
        }
113
    }
114
115
    return $block;
116
}
117