Completed
Branch master (adb512)
by Michael
02:46
created

view.archive.php ➔ planet_getCalendar()   F

Complexity

Conditions 14
Paths 544

Size

Total Lines 63
Code Lines 42

Duplication

Lines 4
Ratio 6.35 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 42
c 4
b 0
f 0
nc 544
nop 3
dl 4
loc 63
rs 3.619

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: http://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
include __DIR__ . '/header.php';
28
29
if (planet_parse_args($args_num, $args, $args_str)) {
30
    $args['year']  = @$args_num[0];
31
    $args['month'] = @$args_num[1];
32
    $args['day']   = @$args_num[2];
33
}
34
35
$day     = (int)(empty($_GET['day']) ? @$args['day'] : $_GET['day']);
36
$year    = (int)(empty($_GET['year']) ? @$args['year'] : $_GET['year']);
37
$month   = (int)(empty($_GET['month']) ? @$args['month'] : $_GET['month']);
38
$blog_id = (int)(empty($_GET['blog']) ? @$args['blog'] : $_GET['blog']);
39
$start   = (int)(empty($_GET['start']) ? @$args['start'] : $_GET['start']);
40
41
$page['title'] = planet_constant('MD_ACHIVE');
42
43
$article_handler = xoops_getModuleHandler('article', $GLOBALS['moddirname']);
44
$blog_handler    = xoops_getModuleHandler('blog', $GLOBALS['moddirname']);
45
46
$xoopsOption['xoops_pagetitle'] = $xoopsModule->getVar('name') . ' - ' . planet_constant('MD_ACHIVE');
47
$xoopsOption['template_main']   = planet_getTemplate('archive');
48
include_once XOOPS_ROOT_PATH . '/header.php';
49
include XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/include/vars.php';
50
51
$year = empty($year) ? date('Y') : $year;
52
if ($month < 1) {
53
    $month        = $day = 0;
54
    $page['time'] = sprintf(planet_constant('MD_TIME_Y'), $year);
55
} elseif ($day < 1) {
56
    $day          = 0;
57
    $page['time'] = sprintf(planet_constant('MD_TIME_YM'), $year, $month);
58
} else {
59
    $page['time'] = sprintf(planet_constant('MD_TIME_YMD'), $year, $month, $day);
60
}
61
$time = array('year' => $year, 'month' => $month, 'day' => $day);
62
if ($xoopsUser) {
63
    $timeoffset = ($xoopsUser->getVar('timezone_offset') - $xoopsConfig['server_TZ']) * 3600;
64
} else {
65
    $timeoffset = ($xoopsConfig['default_TZ'] - $xoopsConfig['server_TZ']) * 3600;
66
}
67
68
$criteria = new CriteriaCompo();
69
if ($blog_id) {
70
    $criteria->add(new Criteria('blog_id', $blog_id));
71
}
72
$criteria->add(new Criteria("YEAR(FROM_UNIXTIME(art_time_publish - $timeoffset))", $year));
73
if ($month) {
74
    $criteria->add(new Criteria("MONTH(FROM_UNIXTIME(art_time_publish - $timeoffset))", $month));
75
    if ($day) {
76
        $criteria->add(new Criteria("DAY(FROM_UNIXTIME(art_time_publish - $timeoffset))", $day));
77
    }
78
}
79
$criteria->setStart($start);
80
$criteria->setLimit($xoopsModuleConfig['articles_perpage']);
81
82
$articles_obj   =& $article_handler->getAll($criteria, array('uid', 'art_title', 'art_time', 'blog_id', 'art_content'));
83
$articles_count = $article_handler->getCount($criteria);
84
85
$articles = array();
86
$blogs_id = array();
87
foreach ($articles_obj as $id => $article) {
88
    $articles[]                            = array(
89
        'id'      => $id,
90
        'blog'    => array('id' => $article->getVar('blog_id'), 'title' => ''),
91
        'title'   => $article->getVar('art_title'),
92
        'time'    => $article->getTime(),
93
        'content' => $article->getVar('art_content')
94
    );
95
    $articles[]                            = $_article;
96
    $blogs_id[$article->getVar('blog_id')] = 1;
97
    unset($_article);
98
}
99
$criteria_blog = new Criteria('blog_id', '(' . implode(',', array_keys($blog_array)) . ')', 'IN');
100
$blogs         = $blog_handler->getList($criteria_blog);
101
foreach (array_keys($articles) as $key) {
102
    $articles[$key]['blog']['title'] = $blogs[$articles[$key]['blog']['id']];
103
}
104
if ($blog_id > 0) {
105
    $page['blog'] = $blogs[$blog_id];;
106
}
107
108
if ($articles_count > $xoopsModuleConfig['articles_perpage']) {
109
    include XOOPS_ROOT_PATH . '/class/pagenav.php';
110
    $nav     = new XoopsPageNav($articles_count, $xoopsModuleConfig['articles_perpage'], $start, 'start',
111
                                'month=' . $month . '&amp;day=' . $day . '&amp;year=' . $year . '&amp;blog='
112
                                . (int)$blog_id);
113
    $pagenav = $nav->renderNav(4);
114
} else {
115
    $pagenav = '';
116
}
117
118
$timenav  = null;
119
$calendar = null;
120
$months   = null;
121
if (empty($start)) {
122
    if ($blog_id) {
123
        $blog_criteria = ' AND blog_id=' . $blog_id;
124
    } else {
125
        $blog_criteria = '';
126
    }
127
    // Get monthly list
128
    if (empty($month)) {
129
        $sql    = "SELECT MONTH(FROM_UNIXTIME(art_time - $timeoffset)) AS mon, COUNT(DISTINCT art_id) AS count
130
            FROM " . planet_DB_prefix('article') . "
131
            WHERE YEAR(FROM_UNIXTIME(art_time - $timeoffset)) = $year
132
            " . $blog_criteria . '
133
            GROUP BY mon
134
            ';
135
        $result = $xoopsDB->query($sql);
136
        $months = array();
137
        while ($myrow = $xoopsDB->fetchArray($result)) {
138
            $months[] = array(
139
                'title' => planet_constant('MD_MONTH_' . (int)$myrow['mon']) . ' (' . (int)$myrow['count'] . ')',
140
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . ''
141
                           . $year . '/' . $myrow['mon'] . '/b' . $blog_id
142
            );
143
        }
144
        $timenav['prev'] = array(
145
            'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . ''
146
                       . ($year - 1) . '/b' . $blog_id,
147
            'title' => sprintf(planet_constant('MD_TIME_Y'), $year - 1)
148
        );
149
        if ($year < date('Y')) {
150
            $timenav['next'] = array(
151
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . ''
152
                           . ($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   = array();
167
        while ($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] = array(
175
                'title' => $days[$i]['count'],
176
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . ''
177
                           . $year . '/' . $month . '/' . $i . '/b' . $blog_id
178
            );
179
        }
180
        $calendar   = planet_getCalendar($year, $month, $days);
181
        $month_next = $month + 1;
182
        $month_prev = $month - 1;
183
        $_year      = $year;
184
        if ($month == 12) {
185
            $month_next = 1;
186
            $_year      = $year + 1;
187
        }
188
        if ($month == 1) {
189
            $month_pre = 12;
190
            $_year     = $year - 1;
191
        }
192
        $timenav['prev'] = array(
193
            'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . ''
194
                       . $_year . '/' . $month_prev . '/b' . $blog_id,
195
            'title' => planet_constant('MD_MONTH_' . $month_prev)
196
        );
197
        if ($year < date('Y') || $month < date('n')) {
198
            $timenav['next'] = array(
199
                'url'   => XOOPS_URL . '/modules/' . $GLOBALS['moddirname'] . '/view.archive.php' . URL_DELIMITER . ''
200
                           . $_year . '/' . $month_next . '/b' . $blog_id,
201
                'title' => planet_constant('MD_MONTH_' . $month_next)
202
            );
203
        }
204
    }
205
}
206
207
$xoopsTpl->assign('dirname', $GLOBALS['moddirname']);
208
$xoopsTpl->assign('modulename', $xoopsModule->getVar('name'));
209
$xoopsTpl->assign('xoops_module_header', $xoopsOption['xoops_module_header']);
210
$xoopsTpl->assign('xoops_pagetitle', $xoopsOption['xoops_pagetitle']);
211
$xoopsTpl->assign('articles', $articles);
212
213
$xoopsTpl->assign('blog', $blog_id);
214
$xoopsTpl->assign('months', $months);
215
$xoopsTpl->assign('calendar', $calendar);
216
$xoopsTpl->assign('time', $time);
217
$xoopsTpl->assign('page', $page);
218
$xoopsTpl->assign('timenav', $timenav);
219
$xoopsTpl->assign('pagenav', $pagenav);
220
221
include_once __DIR__ . '/footer.php';
222
223
/**
224
 * @param null $year
225
 * @param null $month
226
 * @param null $days
227
 * @return string
228
 */
229
function planet_getCalendar($year = null, $month = null, $days = null) {
230
    $year      = empty($year) ? date('Y') : $year;
231
    $month     = empty($month) ? date('n') : $month;
232
    $unixmonth = mktime(0, 0, 0, $month, 1, $year);
233
234
    ob_start();
235
    echo '<table id="calendar">';
236
    echo '<caption>';
237
    printf(planet_constant('MD_TIME_YM'), $year, planet_constant('MD_MONTH_' . $month));
238
    echo '</caption>';
239
240
    for ($i = 1; $i <= 7; ++$i) {
241
        echo "\n\t\t<th abbr=\"" . planet_constant('MD_WEEK_' . $i) . "\" scope=\"col\" title=\""
242
             . planet_constant('MD_WEEK_' . $i) . "\">" . planet_constant('MD_WEEK_' . $i) . '</th>';
243
    }
244
245
    echo '<tr>';
246
247
    // See how much we should pad in the beginning
248
    $week_begins = 1;
249
    $pad         = planet_calendar_week_mod(date('w', $unixmonth) - $week_begins);
250
    if (0 != $pad) {
251
        echo "\n\t\t" . '<td colspan="' . $pad . '">&nbsp;</td>';
252
    }
253
254
    $daysinmonth = (int)date('t', $unixmonth);
255
    for ($day = 1; $day <= $daysinmonth; ++$day) {
256
        if (isset($newrow) && $newrow) {
257
            echo "\n\t</tr>\n\t<tr>\n\t\t";
258
        }
259
        $newrow = false;
260
261
        echo '<td>';
262
263
        if (!empty($days[$day]['url'])) {
264
            echo '<a href="' . $days[$day]['url'] . "\"";
265 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...
266
                echo "title=\"" . $days[$day]['title'] . "\"";
267
            }
268
            echo ">$day</a>";
269 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...
270
            echo "<acronym title=\"" . $days[$day]['title'] . "\">$day</acronym>";
271
        } else {
272
            echo $day;
273
        }
274
        echo '</td>';
275
276
        if (6 == planet_calendar_week_mod(date('w', mktime(0, 0, 0, $month, $day, $year)) - $week_begins)) {
277
            $newrow = true;
278
        }
279
    }
280
281
    $pad = 7 - planet_calendar_week_mod(date('w', mktime(0, 0, 0, $month, $day, $year)) - $week_begins);
282
    if ($pad != 0 && $pad != 7) {
283
        echo "\n\t\t" . '<td class="pad" colspan="' . $pad . '">&nbsp;</td>';
284
    }
285
286
    echo "\n\t</tr>\n\t</tbody>\n\t</table>";
287
    $calendar = ob_get_contents();
288
    ob_end_clean();
289
290
    return $calendar;
291
}
292
293
// Used in get_calendar
294
/**
295
 * @param $num
296
 * @return mixed
297
 */
298
function planet_calendar_week_mod($num) {
299
    $base = 7;
300
301
    return ($num - $base * floor($num / $base));
302
}
303