b_news_archives_onthefly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @author         XOOPS Development Team
16
 */
17
18
use XoopsModules\News;
19
use XoopsModules\News\Helper;
20
use XoopsModules\News\NewsStory;
21
22
// require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
23
24
/**
25
 * Display archives
26
 *
27
 * @param array $options :
28
 *                       0 = sort order (0=older first, 1=newer first)
29
 *                       1 = Starting date, year
30
 *                       2 = Starting date, month
31
 *                       3 = Ending date, year
32
 *                       4 = Ending date, month
33
 *                       5 = until today ?
34
 *
35
 * @return array|string
36
 */
37
function b_news_archives_show($options)
38
{
39
    global $xoopsDB, $xoopsConfig;
40
    // require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
41
    require_once XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/calendar.php';
42
43
    /** @var Helper $helper */
44
    if (!class_exists(Helper::class)) {
45
        //            throw new \RuntimeException('Unable to create the $helper directory');
46
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array|string.
Loading history...
47
    }
48
49
    $helper = Helper::getInstance();
50
    $helper->loadLanguage('main');
51
52
    $months_arr    = [
53
        1  => _CAL_JANUARY,
54
        2  => _CAL_FEBRUARY,
55
        3  => _CAL_MARCH,
56
        4  => _CAL_APRIL,
57
        5  => _CAL_MAY,
58
        6  => _CAL_JUNE,
59
        7  => _CAL_JULY,
60
        8  => _CAL_AUGUST,
61
        9  => _CAL_SEPTEMBER,
62
        10 => _CAL_OCTOBER,
63
        11 => _CAL_NOVEMBER,
64
        12 => _CAL_DECEMBER,
65
    ];
66
    $block         = [];
67
    $sort_order    = 0 == $options[0] ? 'ASC' : 'DESC';
68
    $starting_date = mktime(0, 0, 0, (int)$options[2], 1, (int)$options[1]);
69
    if (1 != (int)$options[5]) {
70
        $ending_date = mktime(23, 59, 59, (int)$options[4], 28, (int)$options[3]);
71
    } else {
72
        $ending_date = time();
73
    }
74
    $sql    = "SELECT DISTINCT(FROM_UNIXTIME(published,'%Y-%m')) AS published FROM " . $xoopsDB->prefix('news_stories') . ' WHERE published>=' . $starting_date . ' AND published<=' . $ending_date . ' ORDER BY published ' . $sort_order;
75
    $result = $xoopsDB->query($sql);
76
    if (!$result) {
77
        return '';
78
    }
79
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
80
        $year                = (int)mb_substr($myrow['published'], 0, 4);
81
        $month               = (int)mb_substr($myrow['published'], 5, 2);
82
        $formated_month      = $months_arr[$month];
83
        $block['archives'][] = ['month' => $month, 'year' => $year, 'formated_month' => $formated_month];
84
    }
85
86
    return $block;
87
}
88
89
/**
90
 * @param $options
91
 *
92
 * @return string
93
 */
94
function b_news_archives_edit($options)
95
{
96
    global $xoopsDB;
97
    $syear    = $smonth = $eyear = $emonth = $older = $recent = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $eyear is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $syear is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $smonth is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $emonth is dead and can be removed.
Loading history...
98
    $selsyear = $selsmonth = $seleyear = $selemonth = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $seleyear is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $selsyear is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $selemonth is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $selsmonth is dead and can be removed.
Loading history...
99
    $form     = '';
100
101
    $selsyear  = $options[1];
102
    $selsmonth = $options[2];
103
    $seleyear  = $options[3];
104
    $selemonth = $options[4];
105
106
    $tmpstory = new NewsStory();
107
    $tmpstory->getOlderRecentNews($older, $recent); // We are searching for the module's older and more recent article's date
108
109
    // Min and max value for the two dates selectors
110
    // We are going to use the older news for the starting date
111
    $syear  = date('Y', $older);
112
    $smonth = date('n', $older);
113
    $eyear  = date('Y', $recent);
114
    $emonth = date('n', $recent);
115
    // Verify parameters
116
    if (0 == $selsyear && 0 == $selsmonth) {
117
        $selsyear  = $syear;
118
        $selsmonth = $smonth;
119
    }
120
    if (0 == $seleyear && 0 == $selemonth) {
121
        $seleyear  = $eyear;
122
        $selemonth = $emonth;
123
    }
124
125
    // Sort order *************************************************************
126
    // (0=older first, 1=newer first)
127
    $form .= '<b>' . _MB_NEWS_ORDER . "</b>&nbsp;<select name='options[]'>";
128
    $form .= "<option value='0'";
129
    if (0 == $options[0]) {
130
        $form .= ' selected';
131
    }
132
    $form .= '>' . _MB_NEWS_OLDER_FIRST . "</option>\n";
133
    $form .= "<option value='1'";
134
    if (1 == $options[0]) {
135
        $form .= ' selected';
136
    }
137
    $form .= '>' . _MB_NEWS_RECENT_FIRST . '</option>';
138
    $form .= "</select>\n";
139
140
    // Starting and ending dates **********************************************
141
    $form .= '<br><br><b>' . _MB_NEWS_STARTING_DATE . '</b><br>';
142
    $form .= _MB_NEWS_CAL_YEAR . "&nbsp;<select name='options[]'>";
143
    for ($i = $syear; $i <= $eyear; ++$i) {
144
        $selected = ($i == $selsyear) ? 'selected' : '';
145
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
146
    }
147
    $form .= '</select>&nbsp;' . _MB_NEWS_CAL_MONTH . "&nbsp;<select name='options[]'>";
148
    for ($i = 1; $i <= 12; ++$i) {
149
        $selected = ($i == $selsmonth) ? 'selected' : '';
150
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
151
    }
152
    $form .= '</select>';
153
154
    $form .= '<br><br><b>' . _MB_NEWS_ENDING_DATE . '</b><br>';
155
    $form .= _MB_NEWS_CAL_YEAR . "&nbsp;<select name='options[]'>";
156
    for ($i = $syear; $i <= $eyear; ++$i) {
157
        $selected = ($i == $seleyear) ? 'selected' : '';
158
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
159
    }
160
    $form .= '</select>&nbsp;' . _MB_NEWS_CAL_MONTH . "&nbsp;<select name='options[]'>";
161
    for ($i = 1; $i <= 12; ++$i) {
162
        $selected = ($i == $selemonth) ? 'selected' : '';
163
        $form     .= "<option value='" . $i . "'" . $selected . '>' . $i . '</option>';
164
    }
165
    $form .= '</select>';
166
167
    // Or until today *********************************************************
168
    $form    .= '<br>';
169
    $checked = 1 == $options[5] ? ' checked' : '';
170
    $form    .= "<input type='checkbox' value='1' name='options[]'" . $checked . '>';
171
    $form    .= ' <b>' . _MB_NEWS_UNTIL_TODAY . '</b>';
172
173
    return $form;
174
}
175
176
/**
177
 * @param $options
178
 */
179
function b_news_archives_onthefly($options): void
180
{
181
    $options = explode('|', $options);
182
    $block   = b_news_archives_show($options);
183
184
    $tpl = new \XoopsTpl();
185
    $tpl->assign('block', $block);
186
    $tpl->display('db:news_block_archives.tpl');
187
}
188