18. april 2007 - 22:48
#1
Jeg begyndte engang på et kalendersystem, men fik aldrig gjort det færdigt. JEg poster det herunder - så kan du jo se, om det er noget du har lyst til at arbejde videre på. Nederst er der et eksempel på brug, men der ydes ikke support (da det er mere end 2 år siden jeg sidst så på det). Desuden er det PHP4-OO ;(
<?php
/*
24 nov 04: Began working on JgCalendar
25 nov 04: Working version - user must write own content-handlers and make_link (this is just a framework)
30 nov 04: Added ContentHandlerYear and made id's in year-, month- and day-modes on current day/month/year
Added IsTodayMatch() and made GetToday() private (IsTodayMatch is enough for ContentHandler-functions)!
*/
class JgCalendar
{
var $mDateYear;
var $mDateMonth;
var $mDateDay;
var $mNamesMonths;
var $mNamesDays;
var $mShowFacts;
function JgCalendar($lang = false)
{
switch ($lang) {
case 'da':
$this->mNamesMonths = array(false, 'Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December');
$this->mNamesDays = array(false, 'Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lørdag', 'Søndag');
$this->mNamesTrans = array('today' => 'I dag', 'facts' => 'Fakta om dagen', 'the' => 'd.', 'suffix' => '.', 'weekno' => 'Uge-nr.', 'day_of_year' => 'Årsdag', 'is_dst' => 'Sommertid', 'is_leap' => 'Skudår', 'yes' => 'Ja', 'no' => 'Nej');
break;
default:
$this->mNamesMonths = array(false, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); // Keep real month-number association
$this->mNamesDays = array(false, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
$this->mNamesTrans = array('today' => 'Today', 'facts' => 'Facts of the Day', 'the' => 'the', 'suffix' => 1, 'weekno' => 'Week no.', 'day_of_year' => 'Day of year', 'is_dst' => 'Daylight savings time', 'is_leap' => 'Leap year', 'yes' => 'Yes', 'no' => 'No');
}
}
function SetDate($year = 0, $month = 0, $day = 0)
{
if ($this->_NumBetween($year, 1972, 2037) == true) {
$this->mDateYear = $year;
$this->mDateMonth = ($month == 0 ? 1 : $month); // Make sure to get correct year even though month is 0 (will be set to 0 again)
$this->mDateDay = ($day == 0 ? 1 : $day); // Same as above
list($this->mDateYear, $this->mDateMonth, $this->mDateDay) = split('-', date('Y-n-j', mktime(0, 0, 0, $this->mDateMonth, $this->mDateDay, $this->mDateYear)));
if ($day == 0) {
$this->mDateDay = 0;
if ($month == 0) {
$this->mDateMonth = 0;
}
}
} else {
$this->mDateYear = date('Y');
$this->mDateMonth = date('n');
$this->mDateDay = 0;
}
return array('year' => $this->mDateYear, 'month' => $this->mDateMonth, 'day' => $this->mDateDay);
}
function IsTodayMatch($year, $month = false, $day = false)
{
if ($month === false) { // $day and $month can also be negative or 0 (from _ShowCalendarMonth) - hence; default false and type-test!
$length = 4;
} elseif ($day === false) {
$length = 7;
} else {
$length = 10;
}
return !strncmp($this->_GetToday('stamp'), ($year . '-' . $month . '-' . $day), $length); // Compare stamp at length (strncmp() returns 0 on equal)!
}
function ShowCalendar($show_quick_nav = false, $show_day_facts = false)
{
$this->mShowFacts = $show_day_facts;
$retval = "<table border=\"1\">\n";
$retval .= "\t<tr>\n";
$retval .= "\t\t<th>" . $this->_Navigate('<<', '-y', 'y', $this->mDateYear) . "</th>\n";
$retval .= "\t\t<th" . ($this->IsTodayMatch($this->mDateYear) ? ' id="JgCalendarThisYear"' : '') . ">";
$retval .= $this->_Navigate($this->mDateYear, '', 'y', $this->mDateYear) . "</th>\n";
$retval .= "\t\t<th>" . $this->_Navigate('>>', '+y', 'y', $this->mDateYear) . "</th>\n";
$retval .= "\t</tr>\n";
if ($this->mDateMonth > 0) {
$retval .= $this->_ShowCalendarMonth();
} else {
$retval .= $this->_ShowCalendarYear();
}
if ($show_quick_nav == true) {
$today = $this->_GetToday();
$retval .= "\t<tr>\n";
$retval .= "\t\t<td colspan=\"3\">" . $this->_Translate('today') . ": ";
$retval .= $this->_Navigate($today['year'], '', 'y', $today['year']) . " / ";
$retval .= $this->_Navigate($today['month'], '', 'ym', $today['year'], $today['month']) . " / ";
$retval .= $this->_Navigate($today['day'], '', 'ymd', $today['year'], $today['month'], $today['day']) . "</td>\n";
$retval .= "\t</tr>\n";
}
$retval .= "</table>\n";
print $retval;
}
function ContentHandlerYear($year, $month, $month_name, $link)
{
return $link; // Dummy
}
function ContentHandlerMonth($year, $month, $day, $link)
{
return $link; // Dummy
}
function ContentHandlerDay($year, $month, $day)
{
return sprintf('JgCalendar showing: %s/%s/%s', $year, $month, $day); // Dummy
}
function MakeLink($text, $date)
{
return '<a href="?JgCalendarDate=' . urlencode($date) . '">' . htmlentities($text) . '</a>'; // Dummy
}
function _ShowCalendarYear()
{
$retval = "\t<tr>\n";
$retval .= "\t\t<td colspan=\"3\">\n";
$retval .= "\t\t\t<ul>\n";
for ($i = 1; $i <= 12; $i++) {
$month_name = $this->mNamesMonths[$i];
$retval .= "\t\t\t\t<li" . ($this->IsTodayMatch($this->mDateYear, $i) ? ' id="JgCalendarThisMonth"' : '') . ">";
$retval .= $this->ContentHandlerYear($this->mDateYear, $i, htmlentities($month_name), $this->_Navigate($month_name, '', 'ym', $this->mDateYear, $i)) . "</li>\n";
}
$retval .= "\t\t\t</ul>\n";
$retval .= "\t\t</td>\n";
$retval .= "\t</tr>\n";
return $retval;
}
function _ShowCalendarMonth()
{
$retval = "\t<tr>\n";
$retval .= "\t\t<th>" . $this->_Navigate('<<', '-m', 'ym', $this->mDateYear, $this->mDateMonth) . "</th>\n";
$retval .= "\t\t<th" . ($this->IsTodayMatch($this->mDateYear, $this->mDateMonth) ? ' id="JgCalendarThisMonth"' : '') . ">";
$retval .= $this->_Navigate($this->mNamesMonths[$this->mDateMonth], '', 'ym', $this->mDateYear, $this->mDateMonth) . "</th>\n";
$retval .= "\t\t<th>" . $this->_Navigate('>>', '+m', 'ym', $this->mDateYear, $this->mDateMonth) . "</th>\n";
$retval .= "\t</tr>\n";
if ($this->mDateDay > 0) {
$retval .= $this->_ShowCalendarDay();
} else {
$d_info = $this->_GetDateInfo($this->mDateYear, $this->mDateMonth, 1);
$retval .= "\t<tr>\n";
$retval .= "\t\t<td colspan=\"3\">\n";
$retval .= "\t\t\t<table border=\"1\">\n";
$retval .= "\t\t\t\t<tr>\n";
for ($i = 0; $i <= 7; $i++) {
$retval .= "\t\t\t\t\t<th>" . ($i > 0 ? substr($this->mNamesDays[$i], 0, 1) : '') . "</th>\n";
}
$retval .= "\t\t\t\t</tr>\n";
for ($i = 1; $i <= 7 * ceil(($d_info['first_day'] + $d_info['days_in_month'] - 1) / 7); $i++) { // Calculate number of day-cells i table
$day = $i - $d_info['first_day'] + 1;
if (($i - 1) % 7 == 0) {
$retval .= "\t\t\t\t<tr>\n";
$retval .= "\t\t\t\t\t<td>" . date('W', mktime(0, 0, 0, $this->mDateMonth, $day, $this->mDateYear)) . "</td>\n";
}
$retval .= "\t\t\t\t\t<td" . ($this->IsTodayMatch($this->mDateYear, $this->mDateMonth, $day) ? ' id="JgCalendarThisDay"' : '') . ">";
if ($day > 0 && $day <= $d_info['days_in_month']) {
$retval .= $this->ContentHandlerMonth($this->mDateYear, $this->mDateMonth, $day, $this->_Navigate($day, '', 'ymd', $this->mDateYear, $this->mDateMonth, $day));
}
$retval .= "</td>\n";
$retval .= ($i % 7 ? '' : "\t\t\t\t</tr>\n");
}
$retval .= "\t\t\t</table>\n";
$retval .= "\t\t</td>\n";
$retval .= "\t</tr>\n";
}
return $retval;
}
function _ShowCalendarDay()
{
$d_info = $this->_GetDateInfo($this->mDateYear, $this->mDateMonth, $this->mDateDay);
$retval = "\t<tr>\n";
$retval .= "\t\t<th>" . $this->_Navigate('<<', '-d', 'ymd', $this->mDateYear, $this->mDateMonth, $this->mDateDay) . "</th>\n";
$retval .= "\t\t<th" . ($this->IsTodayMatch($this->mDateYear, $this->mDateMonth, $this->mDateDay) ? ' id="JgCalendarThisDay"' : '') . ">";
$retval .= htmlentities($this->mNamesDays[$d_info['day_of_week']]) . ' ' . $this->_Translate('the') . ' ' . $this->mDateDay . ($this->_Translate('suffix') == 1 ? $d_info['suffix'] : $this->_Translate('suffix')) . "</th>\n";
$retval .= "\t\t<th>" . $this->_Navigate('>>', '+d', 'ymd', $this->mDateYear, $this->mDateMonth, $this->mDateDay) . "</th>\n";
$retval .= "\t</tr>\n";
$retval .= "\t<tr>\n";
$retval .= "\t\t<td colspan=\"3\">\n";
$retval .= "\t\t\t<p>" . $this->ContentHandlerDay($this->mDateYear, $this->mDateMonth, $this->mDateDay) . "</p>\n"; // Get content from content-handler (made by user)
if ($this->mShowFacts == true) {
$retval .= "\t\t\t<hr />\n";
$retval .= "\t\t\t<p>\n";
$retval .= "\t\t\t\t<em>" . $this->_Translate('facts') . ":</em>\n";
$retval .= "\t\t\t\t<ul>\n";
foreach (array('weekno', 'day_of_year') as $data) {
$retval .= "\t\t\t\t\t<li>" . $this->_Translate($data) . ": " . $d_info[$data] . "</li>\n";
}
foreach (array('is_dst', 'is_leap') as $data) {
$retval .= "\t\t\t\t\t<li>" . $this->_Translate($data) . ": " . ($d_info[$data] == 0 ? $this->_Translate('no') : $this->_Translate('yes')) . "</li>\n";
}
$retval .= "\t\t\t\t</ul>\n";
$retval .= "\t\t\t</p>\n";
}
$retval .= "\t\t</td>\n";
$retval .= "\t</tr>\n";
return $retval;
}
function _GetToday($part = 'all')
{
$stamp = date('Y-n-j');
list($year, $month, $day) = split('-', $stamp);
$parts = array('year' => $year, 'month' => $month, 'day' => $day, 'stamp' => $stamp);
if ($part != 'all' && isset($parts[$part]) == true) { // Valid part
$parts = $parts[$part];
}
return $parts;
}
function _GetDateInfo($year, $month, $day)
{
$keys = array('suffix', 'weekno', 'day_of_year', 'day_of_week', 'days_in_month', 'is_dst', 'is_leap');
$values = split(':', date('S:W:z:w:t:I:L', mktime(0, 0, 0, $month, $day, $year)));
$retval = array();
for ($i = 0; $i < count($keys); $i++) {
$retval[$keys[$i]] = $values[$i];
}
$retval['day_of_year'] += 1;
$retval['day_of_week'] += ($retval['day_of_week'] == 0 ? 7 : 0); // Like above!
$retval['first_day'] = date('w', mktime(0, 0, 0, $month, 1, $year)); // Day of week for 1st in month
$retval['first_day'] += ($retval['first_day'] == 0 ? 7 : 0); // Sunday is the last day in the week!
return $retval;
}
function _Navigate($text, $cmd, $fmt, $y, $m = 1, $d = 1)
{
if (preg_match('/^[+-][ymd]$/', $cmd) == 1) {
$var = substr($cmd, 1); // y, m or d
$cmd = substr($cmd, 0, 1); // Action (+ or -)
$$var += ($cmd == '+' ? 1 : -1);
}
list($y, $m, $d) = split('-', date('Y-n-j', mktime(0, 0, 0, $m, $d, $y))); // Calculate correct date
$date = $y . (strpos($fmt, 'm') == false ? '' : '/' . $m . (strpos($fmt, 'd') == false ? '' : '/' . $d)); // format: 'y', 'y/m' or 'y/m/d' (ugly one-liner, but hey - it works!)
return $this->MakeLink($text, $date);
}
function _NumBetween($num, $lower = 0, $higher = 1)
{
return (is_numeric($num) == true && $num >= $lower && $num <= $higher);
}
function _Translate($word)
{
return htmlentities($this->mNamesTrans[$word]);
}
}
if (isset($_GET['d'])) {
$parts = split('/', $_GET['d']);
$y = isset($parts[0]) ? $parts[0] : 0;
$m = isset($parts[1]) ? $parts[1] : 0;
$d = isset($parts[2]) ? $parts[2] : 0;
} else {
$y = 0;
$m = 0;
$d = 0;
}
class MyJgCalendar extends JgCalendar
{
function ContentHandlerYear($y, $m, $m_name, $link)
{
if ($this->IsTodayMatch($y, $m)) {
$link = '<strong>' . $link . '</strong>';
}
return $link;
}
function ContentHandlerMonth($y, $m, $d, $link)
{
if ($this->IsTodayMatch($y, $m, $d)) {
$link = '<strong>' . $link . '</strong>';
}
return $link;
}
function ContentHandlerDay($y, $m, $d)
{
return "Min kalender: " . $d . '/' . $m . '-'. $y;
}
function MakeLink($text, $date)
{
return '<a href="?d=' . urlencode($date) . '">' . htmlentities($text) . '</a>';
}
}
?>
<style>
#JgCalendarThisDay, #JgCalendarThisMonth, #JgCalendarThisYear {background-color: #eeeeee;}
</style>
<?php
$t = new MyJgCalendar('da');
$date = $t->SetDate($y, $m, $d);
$t->ShowCalendar(true, true);
//print_r($date);
?>