Revision: 1855
http://mrbs.svn.sourceforge.net/mrbs/?rev=1855&view=rev
Author: cimorrison
Date: 2011-07-28 09:33:00 +0000 (Thu, 28 Jul 2011)
Log Message:
-----------
Made the week numbers and months in the mini calendars into links, following a
suggestion by Marco Tedaldi. At the same time simplified the code used
elsewhere in MRBS to make sure that a date is valid.
Modified Paths:
--------------
mrbs/trunk/web/mincals.inc
mrbs/trunk/web/month.php
mrbs/trunk/web/mrbs_sql.inc
mrbs/trunk/web/trailer.inc
Modified: mrbs/trunk/web/mincals.inc
===================================================================
--- mrbs/trunk/web/mincals.inc 2011-07-27 13:14:25 UTC (rev 1854)
+++ mrbs/trunk/web/mincals.inc 2011-07-28 09:33:00 UTC (rev 1855)
@@ -20,282 +20,303 @@
class Calendar
{
- var $month;
- var $year;
- var $day;
- var $h;
- var $area;
- var $room;
- var $dmy;
+ var $month;
+ var $year;
+ var $day;
+ var $h;
+ var $area;
+ var $room;
+ var $dmy;
- function Calendar($day, $month, $year, $h, $area, $room, $dmy)
- {
- $this->day = $day;
- $this->month = $month;
- $this->year = $year;
- $this->h = $h;
- $this->area = $area;
- $this->room = $room;
- $this->dmy = $dmy;
- }
+ function Calendar($day, $month, $year, $h, $area, $room, $dmy)
+ {
+ $this->day = $day;
+ $this->month = $month;
+ $this->year = $year;
+ $this->h = $h;
+ $this->area = $area;
+ $this->room = $room;
+ $this->dmy = $dmy;
+ }
- function getCalendarLink($month, $year)
+ function getCalendarLink($month, $year)
+ {
+ return "";
+ }
+
+ // Takes an optional fourth argument $page (which can be 'day', 'week' or
'month')
+ function getDateLink($day, $month, $year)
+ {
+ $page = (func_num_args() > 3) ? func_get_arg(3) : $this->dmy;
+ if (empty($this->room))
{
- return "";
+ return
$page.".php?year=$year&month=$month&day=$day&area=".$this->area;
}
+ else
+ {
+ return
$page.".php?year=$year&month=$month&day=$day&area=".$this->area."&room=".$this->room;
+ }
+ }
- function getDateLink($day, $month, $year)
- {
- if( empty($this->room) )
- return
$this->dmy.".php?year=$year&month=$month&day=$day&area=".$this->area;
- else
- return
$this->dmy.".php?year=$year&month=$month&day=$day&area=".$this->area."&room=".$this->room;
- }
-
- function getDaysInMonth($month, $year)
- {
- if ($month < 1 || $month > 12)
- {
- return 0;
- }
+ function getDaysInMonth($month, $year)
+ {
+ if ($month < 1 || $month > 12)
+ {
+ return 0;
+ }
- $days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
+ $days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
- $d = $days[$month - 1];
+ $d = $days[$month - 1];
- if ($month == 2)
- {
- // Check for leap year
- // Forget the 4000 rule, I doubt I'll be around then...
+ if ($month == 2)
+ {
+ // Check for leap year
+ // Forget the 4000 rule, I doubt I'll be around then...
- if ($year%4 == 0)
- {
- if ($year%100 == 0)
- {
- if ($year%400 == 0)
- {
- $d = 29;
- }
- }
- else
- {
- $d = 29;
- }
- }
- }
+ if ($year%4 == 0)
+ {
+ if ($year%100 == 0)
+ {
+ if ($year%400 == 0)
+ {
+ $d = 29;
+ }
+ }
+ else
+ {
+ $d = 29;
+ }
+ }
+ }
- return $d;
- }
+ return $d;
+ }
- function getFirstDays()
+ function getFirstDays()
+ {
+ global $weekstarts, $strftime_format;
+
+ $basetime = mktime(12,0,0,6,11+$weekstarts,2000);
+ for ($i = 0, $s = ""; $i < 7; $i++)
{
- global $weekstarts, $strftime_format;
-
- $basetime = mktime(12,0,0,6,11+$weekstarts,2000);
- for ($i = 0, $s = ""; $i < 7; $i++)
+ $show = $basetime + ($i * 24 * 60 * 60);
+ $fl = utf8_strftime($strftime_format['dayname_cal'], $show);
+ // add a class if it's a hidden day so that we can apply special styling
+ if (is_hidden_day(($i + $weekstarts) % 7))
{
- $show = $basetime + ($i * 24 * 60 * 60);
- $fl = utf8_strftime($strftime_format['dayname_cal'], $show);
- // add a class if it's a hidden day so that we can apply special
styling
- if (is_hidden_day(($i + $weekstarts) % 7))
- {
- $s .= "<th class=\"hidden\">$fl</th>\n";
- }
- else
- {
- $s .= "<th>$fl</th>\n";
- }
+ $s .= "<th class=\"hidden\">$fl</th>\n";
}
- return $s;
+ else
+ {
+ $s .= "<th>$fl</th>\n";
+ }
}
+ return $s;
+ }
- function getHTML()
- {
- global $weekstarts;
- global $PHP_SELF;
- global $day, $month, $year;
- global $mincals_week_numbers;
- global $strftime_format;
+ function getHTML()
+ {
+ global $weekstarts;
+ global $PHP_SELF;
+ global $day, $month, $year;
+ global $mincals_week_numbers;
+ global $strftime_format;
- if (!isset($weekstarts)) $weekstarts = 0;
- $s = "";
+ if (!isset($weekstarts)) $weekstarts = 0;
+ $s = "";
- // For the week view we will need to know the start of the week that
should be highlighted
- if (preg_match("/week/i", basename($PHP_SELF)))
- {
+ // For the week view we will need to know the start of the week that
should be highlighted
+ if (preg_match("/week/i", basename($PHP_SELF)))
+ {
- // First of all work out how many days we have to skip back to the
- // start of the week
- $sticky_dow = date("w", mktime(12, 0, 0, $month, $day, $year));
- $skipback = ($sticky_dow - $weekstarts + 7) % 7; // How
many days to skip back to first day of week:
- // Then work out the time of the "sticky day"
- // We use gmmktime() here rather than mktime() as gmmktime() is
independent of DST. This avoids
- // problems when we come to look at the difference ($diff) further
down if the two ends of the
- // time period straddle a DST boundary.
- $sticky_time = gmmktime(12, 0, 0, $month, $day, $year); // Use
gm to avoid DST problems
- $start_highlight_gmtime = $sticky_time - ($skipback * 24 * 60 * 60);
- }
+ // First of all work out how many days we have to skip back to the
+ // start of the week
+ $sticky_dow = date("w", mktime(12, 0, 0, $month, $day, $year));
+ $skipback = ($sticky_dow - $weekstarts + 7) % 7; // How
many days to skip back to first day of week:
+ // Then work out the time of the "sticky day"
+ // We use gmmktime() here rather than mktime() as gmmktime() is
independent of DST. This avoids
+ // problems when we come to look at the difference ($diff) further down
if the two ends of the
+ // time period straddle a DST boundary.
+ $sticky_time = gmmktime(12, 0, 0, $month, $day, $year); // Use
gm to avoid DST problems
+ $start_highlight_gmtime = $sticky_time - ($skipback * 24 * 60 * 60);
+ }
- $daysInMonth = $this->getDaysInMonth($this->month, $this->year);
- // $prevYear is the current year unless the previous month is
- // December then you need to decrement the year
- if( $this->month - 1 > 0 )
- {
- $prevMonth = $this->month - 1;
- $prevYear = $this->year;
- }
- else
- {
- $prevMonth = 12;
- $prevYear = $this->year -1;
- }
- $daysInPrevMonth = $this->getDaysInMonth($prevMonth, $prevYear);
- $date = mktime(12, 0, 0, $this->month, 1, $this->year);
+ $daysInMonth = $this->getDaysInMonth($this->month, $this->year);
+ // $prevYear is the current year unless the previous month is
+ // December then you need to decrement the year
+ if( $this->month - 1 > 0 )
+ {
+ $prevMonth = $this->month - 1;
+ $prevYear = $this->year;
+ }
+ else
+ {
+ $prevMonth = 12;
+ $prevYear = $this->year -1;
+ }
+ $daysInPrevMonth = $this->getDaysInMonth($prevMonth, $prevYear);
+ $date = mktime(12, 0, 0, $this->month, 1, $this->year);
- $first = (strftime("%w",$date) + 7 - $weekstarts) % 7; // day number of
the first day of the month
- $monthName = utf8_strftime($strftime_format['month_cal'], $date);
+ $first = (strftime("%w",$date) + 7 - $weekstarts) % 7; // day number of
the first day of the month
+ $monthName = utf8_strftime($strftime_format['month_cal'], $date);
- //$prevMonth = $this->getCalendarLink($this->month - 1 > 0 ?
$this->month - 1 : 12, $this->month - 1 > 0 ? $this->year : $this->year - 1);
- //$nextMonth = $this->getCalendarLink($this->month + 1 <= 12 ?
$this->month + 1 : 1, $this->month + 1 <= 12 ? $this->year : $this->year + 1);
+ //$prevMonth = $this->getCalendarLink($this->month - 1 > 0 ?
$this->month - 1 : 12, $this->month - 1 > 0 ? $this->year : $this->year - 1);
+ //$nextMonth = $this->getCalendarLink($this->month + 1 <= 12 ?
$this->month + 1 : 1, $this->month + 1 <= 12 ? $this->year : $this->year + 1);
- $s .= "<table class=\"calendar\">\n";
- $s .= "<thead>\n";
- // prints month name and year
+ $s .= "<table class=\"calendar\">\n";
+ $s .= "<thead>\n";
+ // prints month name and year
- $cols = 7;
- if ($mincals_week_numbers)
- {
- $cols++;
- }
- $s .= "<tr>\n";
- $s .= "<th colspan=\"$cols\">$monthName $this->year</th>\n";
- $s .= "</tr>\n";
+ $cols = 7;
+ if ($mincals_week_numbers)
+ {
+ $cols++;
+ }
+ $s .= "<tr>\n";
+ $s .= "<th colspan=\"$cols\">";
+ $link_day = $day;
+ // decrement day until it's a valid one for the month, in case you're
moving to a month with fewer
+ // days than the current one
+ while (!checkdate($this->month, $link_day, $this->year) && ($link_day > 1))
+ {
+ $link_day--;
+ }
+ $link = $this->getDateLink($link_day, $this->month, $this->year, 'month');
+ $s .= "<a href=\"$link\">$monthName $this->year</a>";
+ $s .= "</th>\n";
+ $s .= "</tr>\n";
+ $s .= "<tr>\n";
+ if ($mincals_week_numbers)
+ {
+ $s .= "<th></th>\n";
+ }
+ // gets days of week
+ $s .= $this->getFirstDays();
+ $s .= "</tr>\n";
+ $s .= "</thead>\n";
+
+ $s .= "<tbody>\n";
+ $d = 1 - $first; // this is the date of the top left hand cell in the
calendar (which may of course
+ // be negative and therefore not displayed)
+
+ while ($d <= $daysInMonth)
+ {
$s .= "<tr>\n";
+
if ($mincals_week_numbers)
{
- $s .= "<th></th>\n";
+ $s .= "<td valign=\"middle\" class=\"mincals_week_number\">";
+ $link = $this->getDateLink($d, $this->month, $this->year, 'week');
+ $s .= "<a href=\"$link\">";
+ $s .= date("W",gmmktime (12, 0, 0, $this->month, $d, $this->year));
+ $s .= "</a>";
+ $s .= "</td>\n";
}
- // gets days of week
- $s .= $this->getFirstDays();
- $s .= "</tr>\n";
- $s .= "</thead>\n";
-
- $s .= "<tbody>\n";
- $d = 1 - $first; // this is the date of the top left hand cell in the
calendar (which may of course
- // be negative and therefore not displayed)
-
- while ($d <= $daysInMonth)
+ for ($i = 0; $i < 7; $i++)
{
- $s .= "<tr>\n";
+ $hide_this_day = is_hidden_day(($i + $weekstarts) % 7);
+ $s .= "<td";
- if ($mincals_week_numbers)
+ if ($hide_this_day)
{
- $s .= "<td valign=\"middle\"
class=\"mincals_week_number\">".date("W",gmmktime (12, 0, 0, $this->month, $d,
$this->year))."</td>\n";
+ $s .= " class=\"hidden\"";
}
- for ($i = 0; $i < 7; $i++)
+
+ // If this cell is the "sticky day" (ie the day passed through in GET
parameters)
+ // then assign an id so that we can apply some special styling
+ if (($d == $day) && ($this->month == $month) && ($this->year == $year))
{
- $hide_this_day = is_hidden_day(($i + $weekstarts) % 7);
- $s .= "<td";
+ $s .= " id=\"sticky_day\"";
+ }
+ $s .= ">";
+
+ if ($d > 0 && $d <= $daysInMonth) // valid days of the month
+ {
+ $link = $this->getDateLink($d, $this->month, $this->year);
- if ($hide_this_day)
+ if ($link == "")
{
- $s .= " class=\"hidden\"";
- }
-
- // If this cell is the "sticky day" (ie the day passed through in
GET parameters)
- // then assign an id so that we can apply some special styling
- if (($d == $day) && ($this->month == $month) && ($this->year ==
$year))
+ $s .= $d;
+ }
+
+ else // we have a valid link
{
- $s .= " id=\"sticky_day\"";
- }
- $s .= ">";
-
- if ($d > 0 && $d <= $daysInMonth) // valid days of the month
- {
- $link = $this->getDateLink($d, $this->month, $this->year);
-
- if ($link == "")
+ // start the anchor or span, depending on whether it's a hidden day
+ if ($hide_this_day)
{
- $s .= $d;
- }
-
- else // we have a valid link
+ $s .= "<span";
+ }
+ else
{
- // start the anchor or span, depending on whether it's a hidden
day
- if ($hide_this_day)
- {
- $s .= "<span";
- }
- else
- {
- $s .= "<a";
- }
+ $s .= "<a";
+ }
- // then work out whether to mark it as the current day/week/month
+ // then work out whether to mark it as the current day/week/month
- if (preg_match("/day/i", basename($PHP_SELF))) //
DAY VIEW
+ if (preg_match("/day/i", basename($PHP_SELF))) // DAY
VIEW
+ {
+ if (($d == $this->day) and ($this->h))
{
- if (($d == $this->day) and ($this->h))
- {
- $s .= " class=\"current\"";
- }
- }
+ $s .= " class=\"current\"";
+ }
+ }
- elseif (preg_match("/week/i", basename($PHP_SELF))) //
WEEK VIEW
+ elseif (preg_match("/week/i", basename($PHP_SELF))) //
WEEK VIEW
+ {
+ // work out current time and if we're up to 7 days after the
+ // start of the week to be highlighted then mark it as the
+ // current week. We are using gmmktime() rather than mktime()
because
+ // gmmktime() is independent of DST and we do not want the
difference
+ // to be affected if the time period straddles a DST boundary.
See
+ // also the comment further up.
+ $this_time = gmmktime (12, 0, 0, $this->month, $d, $this->year);
// Use gm to avoid DST problems
+ $diff = $this_time - $start_highlight_gmtime; // seconds
+ $diff = $diff/86400; // days
+ if (($diff >= 0) && ($diff < 7))
{
- // work out current time and if we're up to 7 days after the
- // start of the week to be highlighted then mark it as the
- // current week. We are using gmmktime() rather than
mktime() because
- // gmmktime() is independent of DST and we do not want the
difference
- // to be affected if the time period straddles a DST boundary.
See
- // also the comment further up.
- $this_time = gmmktime (12, 0, 0, $this->month, $d,
$this->year); // Use gm to avoid DST problems
- $diff = $this_time - $start_highlight_gmtime; // seconds
- $diff = $diff/86400; // days
- if (($diff >= 0) && ($diff < 7))
- {
- $s .= " class=\"current\"";
- }
- }
+ $s .= " class=\"current\"";
+ }
+ }
- elseif (preg_match("/month/i", basename($PHP_SELF))) //
MONTH VIEW
+ elseif (preg_match("/month/i", basename($PHP_SELF))) //
MONTH VIEW
+ {
+ if ($this->h)
{
- if ($this->h)
- {
- $s .= " class=\"current\"";
- }
+ $s .= " class=\"current\"";
}
+ }
- // Now finish the anchor or span, depending on whether it's a
hidden day
- if ($hide_this_day)
- {
- $s .= ">$d</span>";
- }
- else
- {
- $s .= " href=\"$link\">$d</a>";
- }
+ // Now finish the anchor or span, depending on whether it's a
hidden day
+ if ($hide_this_day)
+ {
+ $s .= ">$d</span>";
}
+ else
+ {
+ $s .= " href=\"$link\">$d</a>";
+ }
}
+ }
- else // invalid days of the month
- {
- $s .= " ";
- }
- $s .= "</td>\n";
- $d++;
- } // end for days in the week
- $s .= "</tr>\n";
- } // end while days in the month
+ else // invalid days of the month
+ {
+ $s .= " ";
+ }
+ $s .= "</td>\n";
+ $d++;
+ } // end for days in the week
+ $s .= "</tr>\n";
+ } // end while days in the month
- $s .= "</tbody>\n";
- $s .= "</table>\n";
+ $s .= "</tbody>\n";
+ $s .= "</table>\n";
- return $s;
- }
+ return $s;
+ }
}
$lastmonth = mktime(12, 0, 0, $month-1, 1, $year);
Modified: mrbs/trunk/web/month.php
===================================================================
--- mrbs/trunk/web/month.php 2011-07-27 13:14:25 UTC (rev 1854)
+++ mrbs/trunk/web/month.php 2011-07-28 09:33:00 UTC (rev 1855)
@@ -133,41 +133,26 @@
$yy = date("Y",$i);
$ym = date("n",$i);
$yd = $day;
-while (!checkdate($ym, $yd, $yy))
+while (!checkdate($ym, $yd, $yy) && ($yd > 1))
{
$yd--;
- if ($yd == 0)
- {
- $yd = 1;
- break;
- }
}
$i= mktime(12,0,0,$month+1,1,$year);
$ty = date("Y",$i);
$tm = date("n",$i);
$td = $day;
-while (!checkdate($tm, $td, $ty))
+while (!checkdate($tm, $td, $ty) && ($td > 1))
{
$td--;
- if ($td == 0)
- {
- $td = 1;
- break;
- }
}
$cy = date("Y");
$cm = date("m");
$cd = $day; // preserve the day information
-while (!checkdate($cm, $cd, $cy))
+while (!checkdate($cm, $cd, $cy) && ($cd > 1))
{
$cd--;
- if ($cd == 0)
- {
- $cd = 1;
- break;
- }
}
Modified: mrbs/trunk/web/mrbs_sql.inc
===================================================================
--- mrbs/trunk/web/mrbs_sql.inc 2011-07-27 13:14:25 UTC (rev 1854)
+++ mrbs/trunk/web/mrbs_sql.inc 2011-07-28 09:33:00 UTC (rev 1855)
@@ -586,15 +586,9 @@
// decremented it to make it a valid date last time round)
$day = $start_dom;
// Make the date valid if day is more than number of days in month:
- while (!checkdate($month, $day, $year))
+ while (!checkdate($month, $day, $year) && ($day > 1))
{
$day--;
- if ($day == 0)
- {
- // should never happen (so not internationalised), but
- // just in case it does, stop an infinite loop
- fatal_error(TRUE, "Error generating series");
- }
}
break;
Modified: mrbs/trunk/web/trailer.inc
===================================================================
--- mrbs/trunk/web/trailer.inc 2011-07-27 13:14:25 UTC (rev 1854)
+++ mrbs/trunk/web/trailer.inc 2011-07-28 09:33:00 UTC (rev 1855)
@@ -176,14 +176,9 @@
$cday = $day;
// decrement day until it's a valid one for the month, in case you're
moving to a month with fewer
// days than the current one
- while (!checkdate($cmonth, $cday, $cyear))
+ while (!checkdate($cmonth, $cday, $cyear) && ($cday > 1))
{
$cday--;
- if ($cday == 0)
- {
- $cday = 1;
- break;
- }
}
if ($i != -2)
{
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Got Input? Slashdot Needs You.
Take our quick survey online. Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits