Calendar view for newspapers
- Ariadne >
- Community >
- Code Gallery >
- Data >
A small calendar display, with navigation, that shows current date as well as highlights days with articles.
Template 1: pnewspaper::view.calendar.html
<pinp>
setlocale(LC_TIME, 'nl_NL.ISO-8859-1');
if (!$date=getvar("date")) {
if (!sessionid() || !$date=getsessionvar("date")) {
$date=time();
}
}
$date_arr=getdate($date);
$selected=mktime(0,0,0,$date_arr["mon"],$date_arr["mday"], $date_arr["year"]);
if (!cached('calendar.'.$nls.'.'.$selected)) {
$starttime=mktime(0,0,0,$date_arr["mon"],1,$date_arr["year"]);
$monthstart=getdate($starttime);
$firstweekday=$monthstart["wday"];
$endtime=mktime(0,0,0,$date_arr["mon"]+1,1,$date_arr["year"])-1;
$monthend=getdate($endtime);
$monthsize=$monthend["mday"];
// is startdate in the current month -> highlight today.
$now=time();
$today=getdate($now);
if ($today["year"]==$date_arr["year"] && $today["mon"]==$date_arr["mon"]) {
$current=$today["mday"];
}
$highlight=$date_arr["mday"];
// calculate prevmonth start
$mday = $date_arr["mday"];
$newtime = mktime(0,0,0, $date_arr["mon"]-1, $date_arr["mday"], $date_arr["year"]);
$newtime_arr = getdate($newtime);
if ($newtime_arr["mday"] < $mday) {
$mday -= $newtime_arr["mday"];
}
$prevmonth=mktime(0,0,0, $date_arr["mon"]-1, $mday, $date_arr["year"]);
// calculate nextmonth start
$mday = $date_arr["mday"];
$newtime = mktime( 0,0,0, $date_arr["mon"]+1, $date_arr["mday"], $date_arr["year"]);
$newtime_arr = getdate($newtime);
if ($newtime_arr["mday"] < $mday) {
$mday = $mday - $newtime_arr["mday"];
}
$nextmonth=mktime( 0,0,0, $date_arr["mon"]+1, $mday, $date_arr["year"]);
// now populate the month with all entries.
for ($i=1; $i<=$monthend["mday"]; $i++) {
$daylist[$i]="0";
}
$articles=get_articles("default", 0, $starttime, $endtime);
if (is_array($articles)) {
reset($articles);
while (list($key, $article)=each($articles)) {
if (($article->data->startdate<$endtime) &&
($article->data->startdate>$starttime)) {
$from_arr=getdate($article->data->startdate);
$daylist[$from_arr["mday"]]="1";
}
}
}
unset($articles);
</pinp>
<script>
function SetDate(date) {
window.location.search='date='+escape(date);
}
</script>
<link rel="stylesheet" type="text/css" href="calendar.css">
<table class='Calendar' border="0" cellspacing="1" cellpadding="0">
<thead>
<tr>
<td colspan="7" class="CalendarNow">
<a href="javascript:SetDate(<pinp> echo $now; </pinp>);"><pinp>
echo strftime("%e %B",$now); </pinp></a>
</td>
<tr>
<td class="CalendarNav">
<a href="javascript:SetDate(<pinp> echo $prevmonth; </pinp>);"
onFocus="blur()"><</a>
</td>
<td colspan="5" class="CalendarHeader"><pinp>
echo strftime("%B %Y", $date);
</pinp></td>
<td class="CalendarNav"><pinp>
if ($nextmonth<=$now) {
</pinp>
<a href="javascript:SetDate(<pinp> echo $nextmonth; </pinp>);"
onFocus="blur()">></a>
<pinp>
}
</pinp></td>
</tr><tr class="CalendarDay">
<td>z</td>
<td>m</td>
<td>d</td>
<td>w</td>
<td>d</td>
<td>v</td>
<td>z</td>
</tr>
</thead>
<tbody>
<tr>
<pinp>
for ($i=$firstweekday; $i; $i--) {
echo "<td> </td>\n";
}
for ($i=1; $i<=$monthsize; $i++) {
if ($i==$highlight) {
echo "<td class=\"CalendarDateSelected\">";
} else {
echo "<td>";
}
$idate=mktime(23,59,59,$date_arr["mon"],$i,$date_arr["year"]);
$idate=mktime(23,59,59,$date_arr["mon"],$i,$date_arr["year"]);
if ($daylist[$i]) {
echo "<span class='HasData'>";
} else {
echo "<span class='HasNoData'>";
}
if ($daylist[$i]) {
if ($i==$current) {
echo "<a class=\"CalendarDateCurrent\"".
" href=\"javascript:SetDate($idate);\">$i</a>";
} else {
if ($idate<=$now) {
echo "<a class=\"CalendarDate\"".
" href=\"javascript:SetDate($idate);\">$i</a>";
} else {
echo "<span class=\"CalendarDate\">$i</span>";
}
}
} else {
echo "<span class=\"CalendarDate\">$i</span>";
}
echo "</span></td>\n";
if ((($i+$firstweekday) % 7)==0) { // end of the week, next row
echo "</tr>";
if ($i < $monthsize) { // only start a new row if there are more days
echo "<tr>\n";
}
}
}
$date_arr=getdate(mktime(23,59,59,$date_arr["mon"]+1,1,$date_arr["year"]));
$firstweekday=$date_arr["wday"]; // first week day of next month
if ($firstweekday) { // skip if first weekday of next month is sunday
for ($i=1; $i<(8-$firstweekday); $i++) {
echo "<td> </td>\n";
}
}
</pinp>
</tr>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
<pinp>
savecache(999);
}
</pinp>

