Completed
Branch master (caa4fc)
by Michael
06:53 queued 04:04
created

news_archives.php ➔ b_news_archives_edit()   F

Complexity

Conditions 16
Paths 2592

Size

Total Lines 81
Code Lines 56

Duplication

Lines 16
Ratio 19.75 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 56
c 1
b 0
f 0
nc 2592
nop 1
dl 16
loc 81
rs 2.0759

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 43 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
//  ------------------------------------------------------------------------ //
3
//                XOOPS - PHP Content Management System                      //
4
//                  Copyright (c) 2000-2016 XOOPS.org                        //
5
//                       <http://xoops.org/>                             //
6
// ------------------------------------------------------------------------- //
7
//  This program is free software; you can redistribute it and/or modify     //
8
//  it under the terms of the GNU General Public License as published by     //
9
//  the Free Software Foundation; either version 2 of the License, or        //
10
//  (at your option) any later version.                                      //
11
//                                                                           //
12
//  You may not change or alter any portion of this comment or credits       //
13
//  of supporting developers from this source code or any supporting         //
14
//  source code which is considered copyrighted (c) material of the          //
15
//  original comment or credit authors.                                      //
16
//                                                                           //
17
//  This program is distributed in the hope that it will be useful,          //
18
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
//  GNU General Public License for more details.                             //
21
//                                                                           //
22
//  You should have received a copy of the GNU General Public License        //
23
//  along with this program; if not, write to the Free Software              //
24
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
//  ------------------------------------------------------------------------ //
26
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
27
28
include_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
29
30
/**
31
 * Display archives
32
 *
33
 * @param array $options :
34
 *                       0 = sort order (0=older first, 1=newer first)
35
 *                       1 = Starting date, year
36
 *                       2 = Starting date, month
37
 *                       3 = Ending date, year
38
 *                       4 = Ending date, month
39
 *                       5 = until today ?
40
 *
41
 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
 */
43
function b_news_archives_show($options)
44
{
45
    global $xoopsDB, $xoopsConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
46
    include_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
47
    include_once XOOPS_ROOT_PATH . '/modules/news/include/functions.php';
48
    include_once XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/calendar.php';
49 View Code Duplication
    if (file_exists(XOOPS_ROOT_PATH . '/modules/news/language/' . $xoopsConfig['language'] . '/main.php')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
        include_once XOOPS_ROOT_PATH . '/modules/news/language/' . $xoopsConfig['language'] . '/main.php';
51
    } else {
52
        include_once XOOPS_ROOT_PATH . '/modules/news/language/english/main.php';
53
    }
54
55
    $months_arr    = array(
56
        1  => _CAL_JANUARY,
57
        2  => _CAL_FEBRUARY,
58
        3  => _CAL_MARCH,
59
        4  => _CAL_APRIL,
60
        5  => _CAL_MAY,
61
        6  => _CAL_JUNE,
62
        7  => _CAL_JULY,
63
        8  => _CAL_AUGUST,
64
        9  => _CAL_SEPTEMBER,
65
        10 => _CAL_OCTOBER,
66
        11 => _CAL_NOVEMBER,
67
        12 => _CAL_DECEMBER
68
    );
69
    $block         = array();
70
    $sort_order    = $options[0] == 0 ? 'ASC' : 'DESC';
71
    $starting_date = mktime(0, 0, 0, (int)$options[2], 1, (int)$options[1]);
72
    if ((int)$options[5] != 1) {
73
        $ending_date = mktime(23, 59, 59, (int)$options[4], 28, (int)$options[3]);
74
    } else {
75
        $ending_date = time();
76
    }
77
    $sql    = "SELECT distinct(FROM_UNIXTIME(published,'%Y-%m')) as published FROM "
78
              . $xoopsDB->prefix('news_stories')
79
              . ' WHERE published>='
80
              . $starting_date
81
              . ' AND published<='
82
              . $ending_date
83
              . ' ORDER BY published '
84
              . $sort_order;
85
    $result = $xoopsDB->query($sql);
86
    if (!$result) {
87
        return '';
88
    }
89
    while ($myrow = $xoopsDB->fetchArray($result)) {
90
        $year                = (int)substr($myrow['published'], 0, 4);
91
        $month               = (int)substr($myrow['published'], 5, 2);
92
        $formated_month      = $months_arr[$month];
93
        $block['archives'][] = array('month' => $month, 'year' => $year, 'formated_month' => $formated_month);
94
    }
95
96
    return $block;
97
}
98
99
/**
100
 * @param $options
101
 *
102
 * @return string
103
 */
104
function b_news_archives_edit($options)
105
{
106
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
107
    $syear    = $smonth = $eyear = $emonth = $older = $recent = 0;
0 ignored issues
show
Unused Code introduced by
$emonth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$eyear is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$smonth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$syear is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
    $selsyear = $selsmonth = $seleyear = $selemonth = 0;
0 ignored issues
show
Unused Code introduced by
$selemonth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$seleyear is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$selsmonth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
$selsyear is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
    $form     = '';
110
111
    $selsyear  = $options[1];
112
    $selsmonth = $options[2];
113
    $seleyear  = $options[3];
114
    $selemonth = $options[4];
115
116
    $tmpstory = new NewsStory;
117
    $tmpstory->GetOlderRecentNews($older, $recent); // We are searching for the module's older and more recent article's date
118
119
    // Min and max value for the two dates selectors
120
    // We are going to use the older news for the starting date
121
    $syear  = date('Y', $older);
122
    $smonth = date('n', $older);
123
    $eyear  = date('Y', $recent);
124
    $emonth = date('n', $recent);
125
    // Verify parameters
126
    if ($selsyear == 0 && $selsmonth == 0) {
127
        $selsyear  = $syear;
128
        $selsmonth = $smonth;
129
    }
130
    if ($seleyear == 0 && $selemonth == 0) {
131
        $seleyear  = $eyear;
132
        $selemonth = $emonth;
133
    }
134
135
    // Sort order *************************************************************
136
    // (0=older first, 1=newer first)
137
    $form .= '<b>' . _MB_NEWS_ORDER . "</b>&nbsp;<select name='options[]'>";
138
    $form .= "<option value='0'";
139
    if ($options[0] == 0) {
140
        $form .= " selected='selected'";
141
    }
142
    $form .= '>' . _MB_NEWS_OLDER_FIRST . "</option>\n";
143
    $form .= "<option value='1'";
144
    if ($options[0] == 1) {
145
        $form .= " selected='selected'";
146
    }
147
    $form .= '>' . _MB_NEWS_RECENT_FIRST . '</option>';
148
    $form .= "</select>\n";
149
150
    // Starting and ending dates **********************************************
151
    $form .= '<br><br><b>' . _MB_NEWS_STARTING_DATE . '</b><br>';
152
    $form .= _MB_NEWS_CAL_YEAR . "&nbsp;<select name='options[]'>";
153 View Code Duplication
    for ($i = $syear; $i <= $eyear; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
        $selected = ($i == $selsyear) ? "selected='selected'" : '';
155
        $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
156
    }
157
    $form .= '</select>&nbsp;' . _MB_NEWS_CAL_MONTH . "&nbsp;<select name='options[]'>";
158 View Code Duplication
    for ($i = 1; $i <= 12; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
        $selected = ($i == $selsmonth) ? "selected='selected'" : '';
160
        $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
161
    }
162
    $form .= '</select>';
163
164
    $form .= '<br><br><b>' . _MB_NEWS_ENDING_DATE . '</b><br>';
165
    $form .= _MB_NEWS_CAL_YEAR . "&nbsp;<select name='options[]'>";
166 View Code Duplication
    for ($i = $syear; $i <= $eyear; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
        $selected = ($i == $seleyear) ? "selected='selected'" : '';
168
        $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
169
    }
170
    $form .= '</select>&nbsp;' . _MB_NEWS_CAL_MONTH . "&nbsp;<select name='options[]'>";
171 View Code Duplication
    for ($i = 1; $i <= 12; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
        $selected = ($i == $selemonth) ? "selected='selected'" : '';
173
        $form .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
174
    }
175
    $form .= '</select>';
176
177
    // Or until today *********************************************************
178
    $form .= '<br>';
179
    $checked = $options[5] == 1 ? " checked='checked'" : '';
180
    $form .= "<input type='checkbox' value='1' name='options[]'" . $checked . '>';
181
    $form .= ' <b>' . _MB_NEWS_UNTIL_TODAY . '</b>';
182
183
    return $form;
184
}
185
186
/**
187
 * @param $options
188
 */
189 View Code Duplication
function b_news_archives_onthefly($options)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
{
191
    $options = explode('|', $options);
192
    $block   = &b_news_archives_show($options);
193
194
    $tpl = new XoopsTpl();
195
    $tpl->assign('block', $block);
196
    $tpl->display('db:news_block_archives.tpl');
197
}
198