File: block.mtcalendar.php

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (131 lines) | stat: -rw-r--r-- 5,572 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
121
122
123
124
125
126
127
128
129
130
131
<?php
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

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');
                if (!$ts) {
                    $entry = $ctx->stash('entry');
                    if ($entry) {
                        $ts = $entry->entry_authored_on;
                    } else {
                        return $ctx->error($ctx->mt->translate(
                            'You used an [_1] tag without a date context set up.', 
                            '<MTCalendar month="this">') );
                    }
                }
                $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 = isset($args['category']) ? $args['category'] : '';

        // 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', 'category' => $cat_name);
        $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;
}
?>