XPressME Integration Kit

Trac

source: trunk/wp-content/themes/xpress_default/blocks/calender_block_theme.php @ 109

Last change on this file since 109 was 109, checked in by toemon, 16 years ago

カレンダーブロック、人気記事リストの実装、インデックスページテンプレート実装

File size: 7.8 KB
Line 
1<?php
2function calender_block($options)
3{
4        $mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
5        $sun_color = empty( $options[1] ) ? '#DB0000' : $options[1] ;
6        $sat_color = empty( $options[2] ) ? '#004D99' : $options[2] ;
7        $this_template = empty( $options[3] ) ? 'db:'.$mydirname.'_calender_block.html' : trim( $options[3] );
8//      $mydirpath = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
9        $mydirpath = get_xpress_dir_path();
10
11        $block['content'] = get_xpress_calendar($sun_color,$sat_color);
12        return $block ;
13}
14
15function get_xpress_calendar($sun_color = '#DB0000' ,$sat_color = '#004D99' ,$initial = true) {
16        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
17
18
19
20        ob_start();
21        // Quick check. If we have no posts at all, abort!
22        if ( !$posts ) {
23                $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");
24                if ( !$gotsome )
25                        return;
26        }
27
28        if ( isset($_GET['w']) )
29                $w = ''.intval($_GET['w']);
30
31        // week_begins = 0 stands for Sunday
32        $week_begins = intval(get_option('start_of_week'));
33
34        // Let's figure out when we are
35        if ( !empty($monthnum) && !empty($year) ) {
36                $thismonth = ''.zeroise(intval($monthnum), 2);
37                $thisyear = ''.intval($year);
38        } elseif ( !empty($w) ) {
39                // We need to get the month from MySQL
40                $thisyear = ''.intval(substr($m, 0, 4));
41                $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
42                $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')");
43        } elseif ( !empty($m) ) {
44                $thisyear = ''.intval(substr($m, 0, 4));
45                if ( strlen($m) < 6 )
46                                $thismonth = '01';
47                else
48                                $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
49        } else {
50                $thisyear = gmdate('Y', current_time('timestamp'));
51                $thismonth = gmdate('m', current_time('timestamp'));
52        }
53
54        $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
55
56        // Get the next and previous month and year with at least one post
57        $previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
58                FROM $wpdb->posts
59                WHERE post_date < '$thisyear-$thismonth-01'
60                AND post_type = 'post' AND post_status = 'publish'
61                        ORDER BY post_date DESC
62                        LIMIT 1");
63        $next = $wpdb->get_row("SELECT  DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
64                FROM $wpdb->posts
65                WHERE post_date >       '$thisyear-$thismonth-01'
66                AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
67                AND post_type = 'post' AND post_status = 'publish'
68                        ORDER   BY post_date ASC
69                        LIMIT 1");
70
71        echo '<table id="wp-calendar" summary="' . __('Calendar') . '">
72        <caption>' . sprintf(_c('%1$s %2$s|Used as a calendar caption'), $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
73        <thead>
74        <tr>';
75
76        $myweek = array();
77
78        for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
79                $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
80        }
81
82        foreach ( $myweek as $wd ) {
83
84                for($week_num=0;$week_num<=6;$week_num++){
85                        $week_name = $wp_locale->get_weekday($week_num);
86                        if ($week_name === $wd) break;
87                }
88               
89                $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
90                if ($week_num ==0) $day_name = '<span style="color: ' . $sun_color . '">' . $day_name . '</span>';
91                if ($week_num ==6) $day_name = '<span style="color: ' . $sat_color . '">' . $day_name . '</span>';
92                echo "\n\t\t<th align=\"center\" abbr=\"$wd\" scope=\"col\" title=\"$wd\">$day_name</th>";
93        }
94
95        echo '
96        </tr>
97        </thead>
98
99        <tfoot>
100        <tr>';
101
102        if ( $previous ) {
103                echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($previous->month) . '" colspan="3" id="prev"><a href="' .
104                get_month_link($previous->year, $previous->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month),
105                        date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
106        } else {
107                echo "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
108        }
109
110        echo "\n\t\t".'<td class="pad">&nbsp;</td>';
111
112        if ( $next ) {
113                echo "\n\t\t".'<td abbr="' . $wp_locale->get_month($next->month) . '" colspan="3" id="next"><a href="' .
114                get_month_link($next->year, $next->month) . '" title="' . sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month),
115                        date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
116        } else {
117                echo "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
118        }
119
120        echo '
121        </tr>
122        </tfoot>
123
124        <tbody>
125        <tr>';
126
127        // Get days with posts
128        $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
129                FROM $wpdb->posts WHERE MONTH(post_date) = '$thismonth'
130                AND YEAR(post_date) = '$thisyear'
131                AND post_type = 'post' AND post_status = 'publish'
132                AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);
133        if ( $dayswithposts ) {
134                foreach ( (array) $dayswithposts as $daywith ) {
135                        $daywithpost[] = $daywith[0];
136                }
137        } else {
138                $daywithpost = array();
139        }
140
141        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') !== false)
142                $ak_title_separator = "\n";
143        else
144                $ak_title_separator = ', ';
145
146        $ak_titles_for_day = array();
147        $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
148                ."FROM $wpdb->posts "
149                ."WHERE YEAR(post_date) = '$thisyear' "
150                ."AND MONTH(post_date) = '$thismonth' "
151                ."AND post_date < '".current_time('mysql')."' "
152                ."AND post_type = 'post' AND post_status = 'publish'"
153        );
154        if ( $ak_post_titles ) {
155                foreach ( (array) $ak_post_titles as $ak_post_title ) {
156
157                                $post_title = apply_filters( "the_title", $ak_post_title->post_title );
158                                $post_title = str_replace('"', '&quot;', wptexturize( $post_title ));
159
160                                if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
161                                        $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
162                                if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
163                                        $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
164                                else
165                                        $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
166                }
167        }
168
169
170        // See how much we should pad in the beginning
171        $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
172        if ( 0 != $pad )
173                echo "\n\t\t".'<td colspan="'.$pad.'" class="pad">&nbsp;</td>';
174
175        $daysinmonth = intval(date('t', $unixmonth));
176        for ( $day = 1; $day <= $daysinmonth; ++$day ) {
177                if ( isset($newrow) && $newrow )
178                        echo "\n\t</tr>\n\t<tr>\n\t\t";
179                $newrow = false;
180
181                if ( $day == gmdate('j', (time() + (get_option('gmt_offset') * 3600))) && $thismonth == gmdate('m', time()+(get_option('gmt_offset') * 3600)) && $thisyear == gmdate('Y', time()+(get_option('gmt_offset') * 3600)) )
182                        echo '<td id="today" align="center">';
183                else
184                        echo '<td align="center">';
185
186                if ( in_array($day, $daywithpost) ) // any posts today?
187                                echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>";
188                else
189                        echo $day;
190                echo '</td>';
191
192                if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
193                        $newrow = true;
194        }
195
196        $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
197        if ( $pad != 0 && $pad != 7 )
198                echo "\n\t\t".'<td class="pad" colspan="'.$pad.'">&nbsp;</td>';
199
200        echo "\n\t</tr>\n\t</tbody>\n\t</table>";
201
202        $output = ob_get_contents();
203        ob_end_clean();
204//      echo $output;
205//      $cache[ $key ] = $output;
206//      wp_cache_set( 'get_calendar', $cache, 'calendar' );
207        return $output;
208}
209
210?>
Note: See TracBrowser for help on using the repository browser.