Documentation » API Reference

Application:

Class: Calendar extends HtmlLayout

  • Package: saf.Date
  • Author: John Luxford <lux@simian.ca>
  • Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
  • License: http://www.sitellite.org/index/license Simian Open Software License
  • Version: 1.2, 2003-01-20, $Id: Calendar.php,v 1.3 2007/10/06 00:06:30 lux Exp $
  • Access: public

Calendar implements a graphical calendar generator. This class
derives most of its functionality from the saf.GUI.Layout package,
so for more information and additional functionality, please read up
on that one as well.

New in 1.2:
- Stopped using saf.Template in favour of saf.Template.Simple. Templates
now use the {tagname} syntax instead of ##tagname##.
- Modified the example to include saf/init.php instead of the more
verbose stuff that was there before.


Usage Example


<?php

// let's create a browseable daily, weekly, and monthly calendar with next|previous links

// load the necessary packages
include_once ('saf/init.php');

$loader->inc ('saf.Date.Calendar');
$loader->inc ('saf.Database');
$loader->inc ('saf.Template.Simple');
$loader->inc ('saf.CGI');

// create a few supporting objects
// adjust database parameters as necessary (for more info see saf.Database)
$db = new Database ('MySQL:www.sitellite3.lo:DBNAME''USER''PASS');
$simple = new SimpleTemplate;
$cgi = new CGI;

// set a few defaults for $cgi params
if (empty ($cgi->date)) {
    
$cgi->date date ('Y-m-d');
}

if (empty (
$cgi->show)) {
    
$cgi->show 'week';
}

if (empty (
$cgi->startOn)) {
    
$cgi->startOn 'Sun';
}

// create our calendar
$c = new Calendar ($cgi->date$cgi->show$cgi->startOn);

// set some visual properties for the cell and table elements in our calendar
$c->cell['width'] = '14%';
$c->cell['height'] = '75';
$c->cell['style'] = "border: #000 1px solid; font: 12px Arial";
$c->table['style'] = "border: #000 1px solid";
$c->table['border'] = "1";
$c->table['width'] = "100%";

if (
$c->showPeriod == 'day') {
    
// initialize day view

    // height limit of 75 not wanted
    
unset ($c->cell['height']);

    
// only show from 9 to 5 (4 & 4:30, but not 5 itself)
    
$c->showFromHour 9;
    
$c->showToHour 16;

    
// fill the calendar with pretty things from the database
    
$res $c->fillCalendar (
        
'select * from sitellite_event order by date asc',
        
'date',
        
'<a href="/events/id.{id}">{title}</a><br />'
    
);

    
// create the header of the calendar
    
$yesterday Date::subtract ($cgi->date'1 day');
    
$tomorrow Date::add ($cgi->date'1 day');

    
$c->makeHeader (
        
'<h1>' Date::format ($cgi->date'F jS, Y') . '</h1>' .
        
'<p><a href="caltest.php' $cgi->makeQuery ('date') . '&date=' $yesterday '">' .
            
Date::format ($yesterday'F jS, Y') . '</a> | <a href="caltest.php' .
            
$cgi->makeQuery ('date') . '&date=' $tomorrow '">' .
            
Date::format ($tomorrow'F jS, Y') . '</a></p>',
        array (
            
'style' => 'background-color: #69c; font: 14px Arial',
            
'align' => 'center',
            
'valign' => 'middle',
            
'width' => '100%',
            
'height' => '25',
        ),
        array (
            
'style' => 'background-color: #9cf; font: 12px Arial; font-weight: bold',
            
'align' => 'center',
            
'valign' => 'middle',
            
'height' => '20',
        )
    );

    
// this is another way we could add content to a specific cell
    //$c->append ('_9am', 'Foo bar');

} elseif ($c->showPeriod == 'week') {
    
// initialize week view

    // fill the calendar with pretty things from the database
    
$res $c->fillCalendar (
        
'select * from sitellite_event order by date asc',
        
'date',
        
'<a href="/events/id.{id}">{title}</a><br />',
        
'{day}<br />'
    
);

    
// shade today's box
    
if (is_object ($c->{$c->activeCells[date ('Y-m-d')]})) {
        
$c->set ($c->activeCells[date ('Y-m-d')], 'bgcolor''#ffffdd');
    }

    
// create the header of the calendar
    
$lastWeek Date::subtract ($cgi->date'1 week');
    
$nextWeek Date::add ($cgi->date'1 week');

    
$c->makeHeader (
        
'<h1>Week of ' Date::format ($c->firstDay'F jS, Y') . '</h1>' .
        
'<p><a href="caltest.php' $cgi->makeQuery ('date') . '&date=' $lastWeek .
            
'">Week of ' Date::format ($lastWeek'F jS, Y') . '</a> | <a href="caltest.php' .
            
$cgi->makeQuery ('date') . '&date=' $nextWeek '">Week of ' .
            
Date::format ($nextWeek'F jS, Y') . '</a></p>',
        array (
            
'style' => 'background-color: #69c; font: 14px Arial',
            
'align' => 'center',
            
'valign' => 'middle',
            
'width' => '100%',
            
'height' => '25',
        ),
        array (
            
'style' => 'background-color: #9cf; font: 12px Arial; font-weight: bold',
            
'align' => 'center',
            
'valign' => 'middle',
            
'height' => '20',
        ),
        array (
            
'style' => 'background-color: #ccc',
        ),
        array (
            
'style' => 'background-color: #ccc',
        )
    );

    
// this is another way we could add content to a specific cell
    //$c->append ('_5th', 'Foo bar');

} elseif ($c->showPeriod == 'month') {
    
// initialize month view

    // fill the calendar with pretty things from the database
    
$res $c->fillCalendar (
        
'select * from sitellite_event order by date asc',
        
'date',
        
'<a href="/events/id.{id}">{title}</a><br />',
        
'{day}<br />'
    
);

    
// shade today's box
    
if (is_object ($c->{$c->activeCells[date ('Y-m-d')]})) {
        
$c->set ($c->activeCells[date ('Y-m-d')], 'bgcolor''#ffffdd');
    }

    
// create the header of the calendar
    
$lastMonth Date::subtract ($cgi->date'1 month');
    
$nextMonth Date::add ($cgi->date'1 month');

    
$c->makeHeader (
        
'<h1>' Date::format ($cgi->date'F, Y') . '</h1>' .
        
'<p><a href="caltest.php' $cgi->makeQuery ('date') . '&date=' $lastMonth '">' .
            
Date::format ($lastMonth'F, Y') . '</a> | <a href="caltest.php' .
            
$cgi->makeQuery ('date') . '&date=' $nextMonth '">' .
            
Date::format ($nextMonth'F, Y') . '</a></p>',
        array (
            
'style' => 'background-color: #69c; font: 14px Arial',
            
'align' => 'center',
            
'valign' => 'middle',
            
'width' => '100%',
            
'height' => '25',
        ),
        array (
            
'style' => 'background-color: #9cf; font: 12px Arial; font-weight: bold',
            
'align' => 'center',
            
'valign' => 'middle',
            
'height' => '20',
        ),
        array (
            
'style' => 'background-color: #ccc',
        ),
        array (
            
'style' => 'background-color: #ccc',
        )
    );
}

// the following is a bunch of form junk to make the calendar view user-definable
if ($cgi->show == 'day') {
    
$showDay ' selected="selected"';
} elseif (
$cgi->show == 'week') {
    
$showWeek ' selected="selected"';
} elseif (
$cgi->show == 'month') {
    
$showMonth ' selected="selected"';
}

if (
$cgi->startOn == 'Sun') {
    
$startSun ' selected="selected"';
} elseif (
$cgi->startOn == 'Mon') {
    
$startMon ' selected="selected"';
} elseif (
$cgi->startOn == 'Tue') {
    
$startTue ' selected="selected"';
} elseif (
$cgi->startOn == 'Wed') {
    
$startWed ' selected="selected"';
} elseif (
$cgi->startOn == 'Thu') {
    
$startThu ' selected="selected"';
} elseif (
$cgi->startOn == 'Fri') {
    
$startFri ' selected="selected"';
} elseif (
$cgi->startOn == 'Sat') {
    
$startSat ' selected="selected"';
}

?><form method="get">
<p align="right">
    <input type="hidden" name="date" value="<?php echo $cgi->date?>" />
    View: <select name="show">
        <option value="day"<?php echo $showDay?>>Day</option>
        <option value="week"<?php echo $showWeek?>>Week</option>
        <option value="month"<?php echo $showMonth?>>Month</option>
    </select>
    , Start Week On: <select name="startOn">
        <option value="Sun"<?php echo $startSun?>>Sun</option>
        <option value="Mon"<?php echo $startMon?>>Mon</option>
        <option value="Tue"<?php echo $startTue?>>Tue</option>
        <option value="Wed"<?php echo $startWed?>>Wed</option>
        <option value="Thu"<?php echo $startThu?>>Thu</option>
        <option value="Fri"<?php echo $startFri?>>Fri</option>
        <option value="Sat"<?php echo $startSat?>>Sat</option>
    </select>
    <input type="submit" value="Go" />
</p>
</form><?php

// display the calendar
if ($res) {
    echo 
$c->render ();
} else {
    echo 
$c->error;
}

?>

Return to Top



Properties


$dayLinks false

  • Access: public

Link pattern for days.


$showPeriod

  • Access: public

Tells the calendar what to display on a single screen. May be
'day', 'week', or 'month'.


$beginWeekOn

  • Access: public

Tells the calendar what day of the week to start the calendar
on. Value may be one of 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
and 'Sat'.


$firstDay

  • Access: public

Contains the date (Y-m-d format) of the first day to include
in the current calendar view.


$currentDay

  • Access: public

Contains the current date (Y-m-d format).


$lastDay

  • Access: public

Contains the date (Y-m-d format) of the last day to include
in the current calendar view.


$showFromHour 0

  • Access: public

Tells Calendar to limit the hours displayed in 'day' view.
Must be an integer from 0 to 23.


$showToHour 23

  • Access: public

Tells Calendar to limit the hours displayed in 'day' view.
Must be an integer from 0 to 23.


$activeCells = array ()

  • Access: public

Contains a list of the active cells in the Calendar
matrix. Active cells are the days that are in use.


$cellTemplate

  • Access: public

Contains a default template for use in each cell. The
cell template is not to be confused with the item template,
which displays an individual item on any given day.


$topBlock

  • Access: public

Reference to the cell that contains the space before
the first day of the month in the 'month' $showPeriod.


$bottomBlock

  • Access: public

Reference to the cell that contains the space after
the last day of the month in the 'month' $showPeriod.


$error

  • Access: public

If an error has occured within this class, you'll
find it in $error.

Return to Top



Methods


Calendar ($date ''$showPeriod 'week'$beginWeekOn 'Sun'

  • Access: public

Constructor Method. $date is a date in the format Y-m-d.
$beginWeekOn may be the three letter abbreviation of any of the
weekdays (ie. Sun, Mon, Tue, Wed, Thu, Fri, or Sat).


getFirstAndLastDay ($date ''

  • Access: public

Sets the $firstDay and $lastDay properties based on
the $showPeriod and the $date specified. If a $date is not
specified, it defaults to the $currentDay property. Also
sets the $activeCells array with a key/value list of dates
(Y-m-d format) as keys and a1, a2 style notation as the
values.


getXCells () 

  • Access: public
  • Return: associative array

Returns an array of weekdays (Mon, Tue, Wed)
corresponding with the X cell value they fall under,
based on the $beginWeekOn property.


fillCalendar ($sql ''$dateColumn 'date'$itemTemplate ''$cellTemplate '{day}<br />'$timeColumn 'time'

  • Access: public
  • Return: boolean

Fills the calendar based on an optional SQL query, some
related database information, and templates for the cells
and items. Also initializes the $topBlock and $bottomBlock
cells if $showPeriod is 'month' (these refer to the empty space
at the top and bottom of the monthly calendar), and sets aliases
to each cell which can be referenced as $calendar_object->_1st,
$calendar_object->_2nd, $calendar_object->_3rd, etc., or
$calendar_object->_1200am, $calendar_object->_1230am,
$calendar_object->_100am, etc. if the $showPeriod is 'day'.


makeTime ($hour$tstring

  • Access: public
  • Return: string

Takes a number between 0 and 23 (the $hour), and
a string from the fillCalendar() method of the format
5:30 or 12:00AM, and returns a string in the format H:i:s.


makeHeader ($contents$properties = array (), $headerProperties = array (), $topBlockProperties = array (), $bottomBlockProperties = array ()) 

  • Access: public

Creates the header of the calendar, using $contents
for the main cell, a list of $properties for the <td>
of the main cell, a list of $headerProperties for each of
the day cells (as returned by getXCells(), and properties
for the $topBlock and $bottomBlock cells as well.

Return to Top