File: block.mtcalendar.php

package info (click to toggle)
movabletype-opensource 4.2.3-1%2Blenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 21,268 kB
  • ctags: 15,862
  • sloc: perl: 178,892; php: 26,178; sh: 161; makefile: 82
file content (120 lines) | stat: -rw-r--r-- 5,112 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id: block.mtcalendar.php 1264 2008-01-16 10:45:32Z takayama $

require_once("MTUtil.php");

function smarty_block_mtcalendar($args, $content, &$ctx, &$repeat) {
    $local_vars = array('cal_entries','cal_day','cal_pad_start','cal_pad_end','cal_days_in_month','cal_prefix','cal_left','CalendarDay','CalendarWeekHeader','CalendarWeekFooter','CalendarIfEntries','CalendarIfNoEntries','CalendarIfToday','CalendarIfBlank','entries','current_timestamp','current_timestamp_end','cal_today','CalendarCellNumber');
    // arguments supported: month, category
    // arguments implemented:
    if (!isset($content)) {
        # first iterations:
        $ctx->localize($local_vars);
        $blog_id = $ctx->stash('blog_id');
        $today = strftime("%Y%m", time());
        $prefix = $args['month'];
        if ($prefix) {
            if ($prefix == 'this') {
                $ts = $ctx->stash('current_timestamp');
                $prefix = substr($ts, 0, 6);
            } elseif ($prefix == 'last') {
                $year = substr($today, 0, 4);
                $month = substr($today, 4, 2);
                if ($month - 1 == 0) {
                    $prefix = $year - 1 . "12";
                } else {
                    $prefix = $year . $month - 1;
                }
            } else {
                // error: Invalid month format: must be YYYYMM
            }
        } else {
            $prefix = $today;
        }
        // gather category name...
        $cat_name = '';
        // caching isn't necessary since we're not building
        // entire site-- just one page
        $today .= strftime("%d", time());
        list($start, $end) = start_end_month($prefix);
        $y = substr($prefix, 0, 4);
        $m = substr($prefix, 4, 2);
        $day = 1;
        $days_in_month = days_in($m, $y);
        $pad_start = wday_from_ts($y, $m, 1);
        $pad_end = 6 - wday_from_ts($y, $m, $days_in_month);
        $this_day = $prefix . sprintf("%02d", $day - $pad_start);
        $args = array('current_timestamp' => $start, 'current_timestamp_end' => $end, 'blog_id' => $blog_id, 'lastn' => -1, 'sort_order' => 'ascend');
        $iter =& $ctx->mt->db->fetch_entries($args);
        $ctx->stash('cal_entries', $iter);
        $ctx->stash('cal_pad_start', $pad_start);
        $ctx->stash('cal_pad_end', $pad_end);
        $ctx->stash('cal_days_in_month', $days_in_month);
        $ctx->stash('cal_prefix', $prefix);
        $ctx->stash('cal_today', $today);
        $ctx->stash('CalendarCellNumber', 0);
        $cell = 0;
    } else {
        # subseqent iterations:
        $prefix = $ctx->stash('cal_prefix');
        $day = $ctx->stash('cal_day');
        $pad_start = $ctx->stash('cal_pad_start');
        $pad_end = $ctx->stash('cal_pad_end');
        $days_in_month = $ctx->stash('cal_days_in_month');
        $iter = $ctx->stash('cal_entries');
        $left = $ctx->stash('cal_left');
        $today = $ctx->stash('cal_today');
        $cell = $ctx->stash('CalendarCellNumber');
    }
    $left or $left = array();
    if ($day <= $pad_start + $days_in_month + $pad_end) {
        $is_padding = $day < $pad_start + 1 || $day > $pad_start + $days_in_month;
        $this_day = '';
        if (!$is_padding) {
            $this_day = $prefix . sprintf("%02d", $day - $pad_start);
            $no_loop = 0;
            if (count($left)) {
                if (substr($left[0]['entry_authored_on'], 0, 8) == $this_day) {
                    $entries = $left;
                    $left = array();
                } else {
                    $no_loop = 1;
                }
            }
            if (!$no_loop && count($iter)) {
                while ($entry = array_shift($iter)) {
                    $e_day = substr($entry['entry_authored_on'], 0, 8);
                    if ($e_day != $this_day) {
                        $left[] = $entry;
                        break;
                    }
                    $entries[] = $entry;
                }
            }
            $ctx->stash('cal_left', $left);
            $ctx->stash('entries', $entries);
            $ctx->stash('current_timestamp', $this_day . '000000');
            $ctx->stash('current_timestamp_end', $this_day . '235959');
            $ctx->stash('CalendarDay', $day - $pad_start);
        }
        $ctx->stash('CalendarWeekHeader', ($day - 1) % 7 == 0);
        $ctx->stash('CalendarWeekFooter', $day % 7 == 0);
        $ctx->stash('CalendarIfEntries', !$is_padding && count($entries));
        $ctx->stash('CalendarIfNoEntries', !$is_padding && !count($entries));
        $ctx->stash('CalendarIfToday', $today == $this_day);
        $ctx->stash('CalendarIfBlank', $is_padding);
        $ctx->stash('cal_day', $day + 1);
        $ctx->stash('CalendarCellNumber', $cell+1);
        $ctx->stash('cal_entries', $iter);
        $repeat = true;
    } else {
        $ctx->restore($local_vars);
        $repeat = false;
    }
    return $content;
}
?>