view.archive.php ➔ planet_getCalendar()   F
last analyzed

Complexity

Conditions 14
Paths 544

Size

Total Lines 63

Duplication

Lines 4
Ratio 6.35 %

Importance

Changes 0
Metric Value
cc 14
nc 544
nop 3
dl 4
loc 63
rs 2.7333
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
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
// URL: https://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
use Xmf\Request;
28
use XoopsModules\Planet;
29
/** @var Planet\Helper $helper */
30
$helper = Planet\Helper::getInstance();
31
32
include __DIR__ . '/header.php';
33
34
if (PlanetUtility::planetParseArguments($args_num, $args, $args_str)) {
35
    $args['year']  = @$args_num[0];
36
    $args['month'] = @$args_num[1];
37
    $args['day']   = @$args_num[2];
38
}
39
40
$day     = Request::getInt('day', @$args['day'], 'GET'); //(int)(empty($_GET['day']) ? @$args['day'] : $_GET['day']);
41
$year    = Request::getInt('year', @$args['year'], 'GET'); //(int)(empty($_GET['year']) ? @$args['year'] : $_GET['year']);
42
$month   = Request::getInt('month', @$args['month'], 'GET'); //(int)(empty($_GET['month']) ? @$args['month'] : $_GET['month']);
43
$blog_id = Request::getInt('blog', @$args['blog'], 'GET'); //(int)(empty($_GET['blog']) ? @$args['blog'] : $_GET['blog']);
44
$start   = Request::getInt('start', @$args['start'], 'GET'); //(int)(empty($_GET['start']) ? @$args['start'] : $_GET['start']);
45
46
$page['title'] = planet_constant('MD_ACHIVE');
47
48
$articleHandler = xoops_getModuleHandler('article', $GLOBALS['moddirname']);
49
$blogHandler    = xoops_getModuleHandler('blog', $GLOBALS['moddirname']);
50
51
$xoopsOption['xoops_pagetitle'] = $xoopsModule->getVar('name') . ' - ' . planet_constant('MD_ACHIVE');
52
$xoopsOption['template_main']   = PlanetUtility::planetGetTemplate('archive');
53
require_once XOOPS_ROOT_PATH . '/header.php';
54
include XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/include/vars.php';
55
56
$year = empty($year) ? date('Y') : $year;
57
if ($month < 1) {
58
    $month        = $day = 0;
59
    $page['time'] = sprintf(planet_constant('MD_TIME_Y'), $year);
60
} elseif ($day < 1) {
61
    $day          = 0;
62
    $page['time'] = sprintf(planet_constant('MD_TIME_YM'), $year, $month);
63
} else {
64
    $page['time'] = sprintf(planet_constant('MD_TIME_YMD'), $year, $month, $day);
65
}
66
$time = ['year' => $year, 'month' => $month, 'day' => $day];
67
if ($xoopsUser) {
68
    $timeoffset = ($xoopsUser->getVar('timezone_offset') - $xoopsConfig['server_TZ']) * 3600;
69
} else {
70
    $timeoffset = ($xoopsConfig['default_TZ'] - $xoopsConfig['server_TZ']) * 3600;
71
}
72
73
$criteria = new \CriteriaCompo();
74
if ($blog_id) {
75
    $criteria->add(new \Criteria('blog_id', $blog_id));
76
}
77
$criteria->add(new \Criteria("YEAR(FROM_UNIXTIME(art_time_publish - $timeoffset))", $year));
78
if ($month) {
79
    $criteria->add(new \Criteria("MONTH(FROM_UNIXTIME(art_time_publish - $timeoffset))", $month));
80
    if ($day) {
81
        $criteria->add(new \Criteria("DAY(FROM_UNIXTIME(art_time_publish - $timeoffset))", $day));
82
    }
83
}
84
$criteria->setStart($start);
85
$criteria->setLimit($helper->getConfig('articles_perpage'));
86
87
$articles_obj   = $articleHandler->getAll($criteria, ['uid', 'art_title', 'art_time', 'blog_id', 'art_content']);
88
$articles_count = $articleHandler->getCount($criteria);
89
90
$articles = [];
91
$blogs_id = [];
92
foreach ($articles_obj as $id => $article) {
93
    $articles[]                            = [
94
        'id'      => $id,
95
        'blog'    => ['id' => $article->getVar('blog_id'), 'title' => ''],
96
        'title'   => $article->getVar('art_title'),
97
        'time'    => $article->getTime(),
98
        'content' => $article->getVar('art_content')
99
    ];
100
    $articles[]                            = $_article;
101
    $blogs_id[$article->getVar('blog_id')] = 1;
102
    unset($_article);
103
}
104
$criteria_blog = new \Criteria('blog_id', '(' . implode(',', array_keys($blog_array)) . ')', 'IN');
105
$blogs         = $blogHandler->getList($criteria_blog);
106
foreach (array_keys($articles) as $key) {
107
    $articles[$key]['blog']['title'] = $blogs[$articles[$key]['blog']['id']];
108
}
109
if ($blog_id > 0) {
110
    $page['blog'] = $blogs[$blog_id];
111
}
112
113
if ($articles_count > $helper->getConfig('articles_perpage')) {
114
    include XOOPS_ROOT_PATH . '/class/pagenav.php';
115
    $nav     = new \XoopsPageNav($articles_count, $helper->getConfig('articles_perpage'), $start, 'start', 'month=' . $month . '&amp;day=' . $day . '&amp;year=' . $year . '&amp;blog=' . $blog_id);
116
    $pagenav = $nav->renderNav(4);
117
} else {
118
    $pagenav = '';
119
}
120
121
$timenav  = null;
122
$calendar = null;
123
$months   = null;
124
if (empty($start)) {
125
    if ($blog_id) {
126
        $blog_criteria = ' AND blog_id=' . $blog_id;
127
    } else {
128
        $blog_criteria = '';
129
    }
130
    // Get monthly list
131
    if (empty($month)) {
132
        $sql    = "SELECT MONTH(FROM_UNIXTIME(art_time - $timeoffset)) AS mon, COUNT(DISTINCT art_id) AS count
133
            FROM " . planet_DB_prefix('article') . "
134
            WHERE YEAR(FROM_UNIXTIME(art_time - $timeoffset)) = $year
135
            " . $blog_criteria . '
136
            GROUP BY mon
137
            ';
138
        $result = $xoopsDB->query($sql);
139
        $months = [];
140
        while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
141
            $months[] = [
142
                'title' => planet_constant('MD_MONTH_' . (int)$myrow['mon']) . ' (' . (int)$myrow['count'] . ')',
143
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . '' . $year . '/' . $myrow['mon'] . '/b' . $blog_id
144
            ];
145
        }
146
        $timenav['prev'] = [
147
            'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . '' . ($year - 1) . '/b' . $blog_id,
148
            'title' => sprintf(planet_constant('MD_TIME_Y'), $year - 1)
149
        ];
150
        if ($year < date('Y')) {
151
            $timenav['next'] = [
152
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . '' . ($year + 1) . '/b' . $blog_id,
153
                'title' => sprintf(planet_constant('MD_TIME_Y'), $year + 1)
154
            ];
155
        }
156
    } // Get daily list
157
    elseif (empty($day)) {
158
        $sql    = "SELECT DAY(FROM_UNIXTIME(art_time - $timeoffset)) AS day, COUNT(DISTINCT a.art_id) AS count
159
            FROM " . planet_DB_prefix('article') . "
160
            WHERE YEAR(FROM_UNIXTIME(art_time - $timeoffset)) = $year
161
            AND MONTH(FROM_UNIXTIME(art_time - $timeoffset)) = $month
162
            " . $blog_criteria . '
163
            GROUP BY day
164
            ';
165
        $result = $xoopsDB->query($sql);
166
        $days   = [];
167
        while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
168
            $days[$myrow['day']]['count'] = $myrow['count'];
169
        }
170
        for ($i = 1; $i <= 31; ++$i) {
171
            if (!isset($days[$i])) {
172
                continue;
173
            }
174
            $days[$i] = [
175
                'title' => $days[$i]['count'],
176
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . '' . $year . '/' . $month . '/' . $i . '/b' . $blog_id
177
            ];
178
        }
179
        $calendar   = planet_getCalendar($year, $month, $days);
180
        $month_next = $month + 1;
181
        $month_prev = $month - 1;
182
        $_year      = $year;
183
        if (12 == $month) {
184
            $month_next = 1;
185
            $_year      = $year + 1;
186
        }
187
        if (1 == $month) {
188
            $month_pre = 12;
189
            $_year     = $year - 1;
190
        }
191
        $timenav['prev'] = [
192
            'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . '' . $_year . '/' . $month_prev . '/b' . $blog_id,
193
            'title' => planet_constant('MD_MONTH_' . $month_prev)
194
        ];
195
        if ($year < date('Y') || $month < date('n')) {
196
            $timenav['next'] = [
197
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . '' . $_year . '/' . $month_next . '/b' . $blog_id,
198
                'title' => planet_constant('MD_MONTH_' . $month_next)
199
            ];
200
        }
201
    }
202
}
203
204
$xoopsTpl->assign('dirname', $GLOBALS['moddirname']);
205
$xoopsTpl->assign('modulename', $xoopsModule->getVar('name'));
206
$xoopsTpl->assign('xoops_module_header', $xoopsOption['xoops_module_header']);
207
$xoopsTpl->assign('xoops_pagetitle', $xoopsOption['xoops_pagetitle']);
208
$xoopsTpl->assign('articles', $articles);
209
210
$xoopsTpl->assign('blog', $blog_id);
211
$xoopsTpl->assign('months', $months);
212
$xoopsTpl->assign('calendar', $calendar);
213
$xoopsTpl->assign('time', $time);
214
$xoopsTpl->assign('page', $page);
215
$xoopsTpl->assign('timenav', $timenav);
216
$xoopsTpl->assign('pagenav', $pagenav);
217
218
require_once __DIR__ . '/footer.php';
219
220
/**
221
 * @param null $year
222
 * @param null $month
223
 * @param null $days
224
 * @return string
225
 */
226
function planet_getCalendar($year = null, $month = null, $days = null)
227
{
228
    $year      = empty($year) ? date('Y') : $year;
229
    $month     = empty($month) ? date('n') : $month;
230
    $unixmonth = mktime(0, 0, 0, $month, 1, $year);
231
232
    ob_start();
233
    echo '<table id="calendar">';
234
    echo '<caption>';
235
    printf(planet_constant('MD_TIME_YM'), $year, planet_constant('MD_MONTH_' . $month));
236
    echo '</caption>';
237
238
    for ($i = 1; $i <= 7; ++$i) {
239
        echo "\n\t\t<th abbr=\"" . planet_constant('MD_WEEK_' . $i) . '" scope="col" title="' . planet_constant('MD_WEEK_' . $i) . '">' . planet_constant('MD_WEEK_' . $i) . '</th>';
240
    }
241
242
    echo '<tr>';
243
244
    // See how much we should pad in the beginning
245
    $week_begins = 1;
246
    $pad         = planet_calendar_week_mod(date('w', $unixmonth) - $week_begins);
247
    if (0 != $pad) {
248
        echo "\n\t\t" . '<td colspan="' . $pad . '">&nbsp;</td>';
249
    }
250
251
    $daysinmonth = (int)date('t', $unixmonth);
252
    for ($day = 1; $day <= $daysinmonth; ++$day) {
253
        if (isset($newrow) && $newrow) {
254
            echo "\n\t</tr>\n\t<tr>\n\t\t";
255
        }
256
        $newrow = false;
257
258
        echo '<td>';
259
260
        if (!empty($days[$day]['url'])) {
261
            echo '<a href="' . $days[$day]['url'] . '"';
262 View Code Duplication
            if (!empty($days[$day]['title'])) {
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...
263
                echo 'title="' . $days[$day]['title'] . '"';
264
            }
265
            echo ">$day</a>";
266 View Code Duplication
        } elseif (!empty($days[$day]['title'])) {
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...
267
            echo '<acronym title="' . $days[$day]['title'] . "\">$day</acronym>";
268
        } else {
269
            echo $day;
270
        }
271
        echo '</td>';
272
273
        if (6 == planet_calendar_week_mod(date('w', mktime(0, 0, 0, $month, $day, $year)) - $week_begins)) {
274
            $newrow = true;
275
        }
276
    }
277
278
    $pad = 7 - planet_calendar_week_mod(date('w', mktime(0, 0, 0, $month, $day, $year)) - $week_begins);
279
    if (0 != $pad && 7 != $pad) {
280
        echo "\n\t\t" . '<td class="pad" colspan="' . $pad . '">&nbsp;</td>';
281
    }
282
283
    echo "\n\t</tr>\n\t</tbody>\n\t</table>";
284
    $calendar = ob_get_contents();
285
    ob_end_clean();
286
287
    return $calendar;
288
}
289
290
// Used in get_calendar
291
/**
292
 * @param $num
293
 * @return mixed
294
 */
295
function planet_calendar_week_mod($num)
296
{
297
    $base = 7;
298
299
    return ($num - $base * floor($num / $base));
300
}
301