web/wp-content/plugins/ajax-calendar/models/calendar.php
branchwordpress
changeset 109 03b0d1493584
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 
       
     3 class Calendar
       
     4 {
       
     5 	var $categories;
       
     6 	var $url;
       
     7 	var $showit = true;
       
     8 	var $split_open  = "&laquo;-&raquo;";
       
     9 	var $split_close = "&raquo;-&laquo;";
       
    10 
       
    11 	function Calendar ($url, $categories = '')
       
    12 	{
       
    13 		$this->url = $url;
       
    14 		
       
    15 		if ($categories)
       
    16 			$this->categories = explode (',', $categories);
       
    17 	}
       
    18 
       
    19 	function show ($month_num = '', $year_num = '', $full = '0', $categories = '')
       
    20   {
       
    21     global $wpdb, $m, $monthnum, $year, $timedifference, $month, $month_abbrev, $weekday, $weekday_initial, $weekday_abbrev, $posts, $wp_rewrite;
       
    22 
       
    23     $month_num = intval ($month_num);
       
    24     $year_num  = intval ($year_num);
       
    25 
       
    26     if ($year_num <= 2003)
       
    27       $year_num = date ('Y');
       
    28 
       
    29     if ($month_num <= 0 || $month_num > 12)
       
    30       $month_num = date ('n');
       
    31 
       
    32     $monthnum = $month_num;
       
    33     $year     = $year_num;
       
    34 
       
    35     // Is this cached?
       
    36 		$cache = wp_cache_get ($year."_".$monthnum."_".$full, 'calendar');
       
    37     if ($cache)
       
    38 			$text = $cache;
       
    39     else
       
    40     {
       
    41       $next_month  = $monthnum + 1;
       
    42       $next_year   = $year;
       
    43       $prev_month  = $monthnum - 1;
       
    44       $prev_year   = $year;
       
    45       $notfull     = $full == 0 ? 1 : 0;
       
    46 
       
    47       if ($next_month == 13)
       
    48       {
       
    49         $next_month = 1;
       
    50         $next_year++;
       
    51       }
       
    52 
       
    53       if ($prev_month == 0)
       
    54       {
       
    55         $prev_month = 12;
       
    56         $prev_year--;
       
    57       }
       
    58 
       
    59       ob_start ();
       
    60       $this->wp_calendar ($month_num, $year_num, $full);
       
    61       $text = ob_get_contents ();
       
    62       ob_end_clean ();
       
    63 
       
    64       // Cache this
       
    65 			wp_cache_add ($year."_".$monthnum."_".$full, $text, 'calendar');
       
    66     }
       
    67 
       
    68     return $text;
       
    69   }
       
    70 
       
    71 
       
    72   // This is a duplicate of the WP function, but modified slightly to provide the enhanced functionality
       
    73   function wp_calendar ($thismonth, $thisyear, $full)
       
    74   {
       
    75   	global $wpdb, $m, $monthnum, $year, $timedifference, $month, $month_abbrev, $weekday, $weekday_initial, $weekday_abbrev, $posts;
       
    76 		global $giraffe_ajax_always;
       
    77 
       
    78 		if ($giraffe_ajax_always && $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest')
       
    79 			$full = 1;
       
    80 
       
    81   	// week_begins = 0 stands for Sunday
       
    82   	$week_begins = intval(get_settings('start_of_week'));
       
    83   	$add_hours = intval(get_settings('gmt_offset'));
       
    84   	$add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
       
    85 
       
    86   	// Let's figure out when we are
       
    87   	$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
       
    88 
       
    89 	// Get the current post
       
    90     $current = $wpdb->get_row ("SELECT count(post_date) as count FROM $wpdb->posts WHERE post_date='".date('Y')."-".date ('n')."-01' AND post_status='publish'  AND post_type='post'");
       
    91 
       
    92   	// Get the next and previous month and year with at least one post
       
    93   	$previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
       
    94   		FROM $wpdb->posts
       
    95   		WHERE post_date < '$thisyear-$thismonth-01'
       
    96   		AND post_status = 'publish' AND post_type='post'
       
    97   			ORDER BY post_date DESC
       
    98   			LIMIT 1");
       
    99   	$next = $wpdb->get_row("SELECT	DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
       
   100   		FROM $wpdb->posts
       
   101   		WHERE post_date >	'$thisyear-$thismonth-01'
       
   102   		AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
       
   103   		AND post_status = 'publish' AND post_type='post'
       
   104   			ORDER	BY post_date ASC
       
   105   			LIMIT 1");
       
   106 
       
   107     if ($current->count == 0)
       
   108       $initiallink = $month[zeroise($thismonth, 2)].' '.$thisyear;
       
   109     else
       
   110       $initiallink = '<a href="'.get_month_link (date ('Y'), date ('n')).'" onclick="xajax_giraffe_ajax_calendar ('.date ('n').','.date ('Y').',\''.$full.'\');return false;">' . $month[zeroise($thismonth, 2)] . ' ' . date('Y', $unixmonth) . '</a>';
       
   111 
       
   112   	echo '<table id="wp-calendar">
       
   113   	<caption id="wp-calendar-caption">'.$initiallink.'</caption>
       
   114   	<thead>
       
   115   	<tr>';
       
   116 
       
   117   	$day_abbrev = $weekday_initial;
       
   118   	if ( $daylength > 1 )
       
   119   		$day_abbrev = $weekday_abbrev;
       
   120 
       
   121   	$myweek = array();
       
   122 
       
   123   	for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
       
   124   		$myweek[]=$weekday[($wdcount+$week_begins)%7];
       
   125   	}
       
   126 
       
   127   	foreach ( $myweek as $wd ) {
       
   128   		echo "\n\t\t<th abbr=\"$wd\" scope=\"col\" title=\"$wd\">" . $day_abbrev[$wd] . '</th>';
       
   129   	}
       
   130 
       
   131   	echo '</tr></thead><tfoot><tr>';
       
   132 
       
   133   	if ($previous)
       
   134   	{
       
   135   	  $link = get_month_link($previous->year, $previous->month);
       
   136   	  $linktext = sprintf(__('View posts for %1$s %2$s'), $month[zeroise($previous->month, 2)], date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)));
       
   137    	  ?>
       
   138    		<td abbr="<?php $month[zeroise($previous->month, 2)]?>" colspan="3" id="prev">
       
   139    		  <a href="<?php echo $link ?>" title="<?php echo $linktext ?>" onclick="return calendar(<?php echo $previous->year ?>,<?php echo $previous->month ?>,<?php echo $full ?>)">
       
   140    		  &laquo; <?php echo $month_abbrev[$month[zeroise($previous->month, 2)]] ?>
       
   141    		  </a></td>
       
   142    		<?php
       
   143    	}
       
   144    	else
       
   145 
       
   146   		echo "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
       
   147 
       
   148 	if ($this->showit)
       
   149 	{
       
   150      echo '<td id="showit"><a href="'.get_month_link ($thisyear, $thismonth).'" onclick="return calendar ('.$thisyear.','.$thismonth.','.($full == 0 ? 1 : 0).')">'.$this->split_open.'</a></td>';
       
   151 
       
   152   	if ( $next ) {
       
   153   		echo "\n\t\t".'<td abbr="' . $month[zeroise($next->month, 2)] . '" colspan="3" id="next"><a href="' .
       
   154   		get_month_link($next->year, $next->month) . '" title="View posts for ' . $month[zeroise($next->month, 2)] . ' ' .
       
   155   		date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year)) .
       
   156   		'" onclick="return calendar ('.$next->year.','.$next->month.','.$full.')"'.
       
   157   		'>' . $month_abbrev[$month[zeroise($next->month, 2)]] . ' &raquo;</a></td>';
       
   158   	} else {
       
   159   		echo "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
       
   160   	}
       
   161 	}
       
   162 	
       
   163   	echo '
       
   164   	</tr>
       
   165   	</tfoot>
       
   166 
       
   167   	<tbody>
       
   168   	<tr>';
       
   169 
       
   170   	// Get days with posts
       
   171 	$sql = "SELECT DISTINCT DAYOFMONTH(post_date)
       
   172   		FROM $wpdb->posts 
       
   173 		LEFT JOIN {$wpdb->prefix}term_relationships ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id)
       
   174 		LEFT JOIN {$wpdb->prefix}term_taxonomy ON ({$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id)
       
   175   		WHERE MONTH(post_date) = $thismonth
       
   176   		AND YEAR(post_date) = $thisyear
       
   177   		AND post_status = 'publish' AND post_type='post'";
       
   178 		
       
   179 	if (count ($this->categories) > 0)
       
   180 		$sql .= " AND {$wpdb->prefix}term_taxonomy.term_id IN (".implode (',', $this->categories).')';
       
   181 
       
   182   	$dayswithposts = $wpdb->get_results($sql, ARRAY_N);
       
   183 		
       
   184   	if ( $dayswithposts ) {
       
   185   		foreach ( $dayswithposts as $daywith ) {
       
   186   			$daywithpost[] = $daywith[0];
       
   187   		}
       
   188   	} else {
       
   189   		$daywithpost = array();
       
   190   	}
       
   191 
       
   192 
       
   193 
       
   194   	if ( strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') )
       
   195   		$ak_title_separator = "\n";
       
   196   	else
       
   197   		$ak_title_separator = ', ';
       
   198 
       
   199   	$ak_titles_for_day = array();
       
   200   	$ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
       
   201   		."FROM $wpdb->posts "
       
   202   		."WHERE YEAR(post_date) = '$thisyear' "
       
   203   		."AND MONTH(post_date) = '$thismonth' "
       
   204   		."AND post_status = 'publish' AND post_type='post'"
       
   205   	);
       
   206   	if ( $ak_post_titles ) {
       
   207   		foreach ( $ak_post_titles as $ak_post_title ) {
       
   208   				if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
       
   209   					$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
       
   210   				if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
       
   211   					$ak_titles_for_day["$ak_post_title->dom"] = str_replace('"', '&quot;', wptexturize($ak_post_title->post_title));
       
   212   				else
       
   213   					$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . str_replace('"', '&quot;', wptexturize($ak_post_title->post_title));
       
   214   		}
       
   215   	}
       
   216 
       
   217 
       
   218   	// See how much we should pad in the beginning
       
   219   	$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
       
   220   	if ( 0 != $pad )
       
   221   		echo "\n\t\t".'<td colspan="'.$pad.'" class="pad">&nbsp;</td>';
       
   222 
       
   223   	$daysinmonth = intval(date('t', $unixmonth));
       
   224   	for ( $day = 1; $day <= $daysinmonth; ++$day ) {
       
   225   		if ( isset($newrow) && $newrow )
       
   226   			echo "\n\t</tr>\n\t<tr>\n\t\t";
       
   227   		$newrow = false;
       
   228 
       
   229   		if ( $day == gmdate('j', (time() + (get_settings('gmt_offset') * 3600))) && $thismonth == gmdate('m', time()+(get_settings('gmt_offset') * 3600)) && $thisyear == gmdate('Y', time()+(get_settings('gmt_offset') * 3600)) )
       
   230   			echo '<td id="today">';
       
   231   		else
       
   232   			echo '<td>';
       
   233 
       
   234   		if ( in_array($day, $daywithpost) ) // any posts today?
       
   235   				echo '<a href="' . get_day_link($thisyear, $thismonth, $day) . "\" title=\"$ak_titles_for_day[$day]\">$day</a>";
       
   236   		else
       
   237   			echo $day;
       
   238   		echo '</td>';
       
   239 
       
   240   		if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
       
   241   			$newrow = true;
       
   242   	}
       
   243 
       
   244   	$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
       
   245   	if ( $pad != 0 && $pad != 7 )
       
   246   		echo "\n\t\t".'<td class="pad" colspan="'.$pad.'">&nbsp;</td>';
       
   247 
       
   248   	echo "\n\t</tr>";
       
   249   	echo "\n\t</tbody>\n\t</table>";
       
   250 
       
   251     if ($full == 1)
       
   252     {
       
   253       global $wpdb;
       
   254 
       
   255       $res = $wpdb->get_results ("SELECT ID,post_title FROM $wpdb->posts WHERE month(post_date) = '$monthnum' AND year(post_date) = '$year' AND post_status = 'publish'  AND post_type='post' ORDER BY post_date");
       
   256       if ($res)
       
   257       {
       
   258         $text .= "<div id=\"wp-calendar-split\"><ul>";
       
   259         foreach ($res AS $pres)
       
   260           $text .= "<li><a href=\"".get_permalink ($pres->ID)."\">".apply_filters ('the_title', $pres->post_title)."</a></li>";
       
   261 
       
   262         $text .= "</ul></div>";
       
   263       }
       
   264 
       
   265       echo $text;
       
   266     }
       
   267   }
       
   268 }
       
   269 ?>