Ajax calendar class
Here is AJAX Calendar, which can be used to display month calendars browsable using AJAX.
It can generate HTML and JavaScript to display a month calendar with links to browse the months using AJAX to avoid page reloading.
The class can make given days be displayed as links to event pages. The browsing may be restricted to given dates. The months and week day names are customizable. The presentation details may be customized using CSS.
Contents
- Download
- Example codes
- Examples in action
- Method list
- Possible error messages
- Latest changes
- Rate us
- Support
Download
Example codes
<?php /*********************/ //This is an example with all possible calendar customizations and //generating calendar and ajax request in the same file /*********************/ //All methods are divided in 2 groups //One should be used in same file or scope, where calendar will be generated //Other should be used in same file or scope, where ajax request will be made //The ones used in if(isset($_GET['ajax_calendar'])) clause //should be used within request file or scope //The ones in else clause should be used within calendar generator file or scope //you should always sepecify timezone when dealing with date function like used in calendar date_default_timezone_set("Europe/Helsinki"); //declaring class instance include("./calendar.class.php"); $calendar = new calendar(); if(isset($_GET['ajax_calendar'])) { //if $_GET['ajax_calendar'] is set, it means request is beeing made //by ajax to get days of other months or year //make sure that you do not output anything to browser, //so you won't mess json structure of calendar //other parameters that are passed with this request are //$_GET['date'] - for date //$_GET['month'] - for month //$_GET['year'] - for year //$_GET['start'] - which day to start week with //and any other custom parameters added //for this example it is parameter $_GET['param'] = "value" //set start month and year which is an oldest motnh to show $calendar->set_start_date(1,1987); //set end month and year which is a newest motnh to show $calendar->set_end_date(3,1987); //set url pattern, for all active days to be used as links where $1 will be replaced by date, $2 replaces by month and $3 by year, $calendar->set_url_pattern("javascript:alert('Date selected $3-$2-$1');"); //$arr = array(1 => "#beginning_of_a_month", 30 => "#end_of_a_month"); //$calendar->set_events($arr); //if ajax request is made to the same page, //make sure nothing other is outputted to browser //you can specify request url in the file where you generate calendar //if nothing is specified then request will be made to the same page $calendar->process_request(); } else { //defining path to jquery and style css included in package echo '<script type="text/javascript" src="./jquery.js"></script>'; echo '<style type="text/css" media="all">@import "./style.css";</style>'; //array with custom month names $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); //applying custom month names $calendar->set_months($months); //array with custom days of week names $weekdays = array("M", "T", "W", "Th", "F", "S", "Sun"); //applying custom days of week names $calendar->set_weekdays($weekdays); //define with which day to start week 0 - Monday, ..., 6 - Sunday //here in example it is Tuesday $calendar->set_week_start(1); //adding request parameter name and value which will appear in ajax request $calendar->add_request_param("param", "value"); //getting errors $errors = $calendar->get_errors(); if(!empty($errors)) { foreach($errors as $error) { echo "<p>".$error."</p>"; } } //generating calendar, you can specify date like in example //if no date is specified, than todays date will be used $calendar->generate_calendar(23,2,1987); } ?>
Examples in action
Method list
- Get errors
- Set month names
- Set names for days of week
- Set from which day should week start
- Ajax request path
- Add parameters for ajax request
- Set pattern for url generation in calendar days
- Set set links to specific dates
- Generate calendars
- Set beginning of calendar
- Set ending of calendar
- Process ajax request
Some methods can be called only from calendar generator part, some can be called only from ajax part. You can check it by method type:
- calendar part- for calendar generation part
- ajax part - for ajax part
Get errors
| Method name | get_errors() |
| Description | Returns array with errors or empty array if there was no errors |
| Example output |
Array
(
[0] => Incorrect amount of months
)
|
Set month names
| Method name | set_months($arr) |
| Type | calendar part |
| Description | Purpose for changing language. Set month names starting from January to December |
| Input parameters | array $arr - array where each element is month name |
| Example input | set_months(array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) |
Set names for days of week
| Method name | set_weekdays($arr) |
| Type | calendar part |
| Description | Purpose for changing language. Set names for days of week starting from Monday to Sunday |
| Input parameters | array $arr - array where each element is day name |
| Example input | set_weekdays(array("M", "T", "W", "Th", "F", "S", "Sun")) |
Set from which day should week start
| Method name | set_week_start($day) |
| Type | calendar part |
| Description | Sets from which day should week start. Values from 0 to 6, where 0 represents Mondey and 6 - Sunay |
| Input parameters | int $day - index of day to start week with |
| Example input | set_week_start(6) |
Ajax request path
| Method name | set_request_path($req) |
| Type | calendar part |
| Description | Sets path where to send ajax request. Default falue - this same file where you're writing script |
| Input parameters | string $req - request path |
| Example input | set_request_path("./ajax.php") |
Add parameters for ajax request
| Method name | add_request_param($name, $value) |
| Type | calendar part |
| Description | Add parameters to pass along with ajax GET request. For example needed, if you want to generate multiple calendars with different settings from one ajax part file. Can't use class defined parameters:
|
| Input parameters | string $name - parameter name string $value - parameter value |
| Example input | add_request_param("calendar_type", "main_page") |
Set pattern for url generation in calendar days
| Method name | set_url_pattern($pattern) |
| Type | ajax part |
| Description | Set pattern to automatically generate urls for calendar days in your specified format. Juts pass url strings, whith following representation:
For example, for the date 1st Januray 2011, url pattern: '/archive-$1-$2-$3' will be generated as '/archive-1-1-2011' |
| Input parameters | string $pattern - url pattern string $value - parameter value |
| Example input | set_url_pattern("/archive-$1-$2-$3") |
Set set links to specific dates
| Method name | set_events($events) |
| Type | ajax part |
| Description | Sets only specific dates as links. Alternative to set_url_pattern method, when you don't want to generate urls for all days in month. You must provide array, where keys are the day of the month in which to display links and values are URL for link. To determine which month is requested use class GET parameters described in add_request_param method. NOTE: set_events() method works only if pattern isn't set using set_url_pattern method |
| Input parameters | array $events - current month days as keys and URL as values. string $value - parameter value |
| Example input | set_url_pattern("/archive-$1-$2-$3") |
Generate calendars
| Method name | generate_calendar($day = "", $month = "", $year = "") |
| Type | calendar part |
| Description | Generates calendar for provided month and year, marking day as today. If any of paramaters is empty, then current day, month or year will be used, for example method call generate_calendar(); will generate calendar for current day, month and year |
| Input parameters | int $day - day of the month. int $month - what month to display on calendar int $year - what year to display on calendar |
| Example input | generate_calendar(21, 12, 2112) |
Set beginning of calendar
| Method name | set_start_date($month = "", $year = "") |
| Type | ajax part |
| Description | Set first month that should be displayed in calendar. It means calendar won't display any month that is before specified month and year. |
| Input parameters | int $month - start month to display on calendar int $year - start year to display on calendar |
| Example input | set_start_date(1, 2000) |
Set ending of calendar
| Method name | set_end_date($month = "", $year = "") |
| Type | ajax part |
| Description | Set last month that should be displayed in calendar. It means calendar won't display any month that is after specified month and year. |
| Input parameters | int $month - last month to display on calendar int $year - last year to display on calendar |
| Example input | set_end_date(12, 2012) |
Process ajax request
| Method name | process_request() |
| Type | ajax part |
| Description | Process ajax request to generate data for calendar in json format |
Possible error messages
List of all errors and meanings
| Error text | Meaning | Solution |
| Incorrect amount of months | You provided incorrect array for month names | Provide array with 12 elements |
| Incorrect amount of weekdays | You provided incorrect array for weekdays | Provide array with 7 elements |
| Incorrect day of the week | You provided incorrect index of the day of the week | Provide integer with values in interval from 0 to 6 |
| Can not override main class parameters | Name for your parameter is already taken by class. | Do not use "date", "month", "year", "start", "ajax_calendar" as parameter names |
Latest changes
You will find predefined CSS file in package to customize your calendar design
Here is a default example:
/*table for calendar header with month, year etc*/ .ajax_calendar_header { border:0; width:250px; } /*table cell that contains month and year*/ .ajax_calendar_main_date { font-weight: bold; width: 60%; text-align: center; } /*table cell of link to switch to previous month when active (means can be switched to previuos months)*/ .ajax_calendar_prev_month_active { width: 10%; font-weight: bold; } /*customizing link inside of active cell (inactive cells doesn't have links)*/ .ajax_calendar_prev_month_active a { text-decoration: none; } /*table cell of link to switch to previous month when inactive (means can not be switched to previous months because of start_date restriction)*/ .ajax_calendar_prev_month_inactive { width: 10%; color: gray; } /*table cell of link to switch to previous year when active*/ .ajax_calendar_prev_year_active { width: 10%; font-weight: bold; } /*customizing link inside of active cell (inactive cells doesn't have links)*/ .ajax_calendar_prev_year_active a { text-decoration: none; } /*table cell of link to switch to previous year when inactive*/ .ajax_calendar_prev_year_inactive { width: 10%; color: gray; } /*table cell of link to switch to next month when active*/ .ajax_calendar_next_month_active { width: 10%; font-weight: bold; } /*customizing link inside of active cell (inactive cells doesn't have links)*/ .ajax_calendar_next_month_active a { text-decoration: none; } /*table cell of link to switch to next month when inactive*/ .ajax_calendar_next_month_inactive { width: 10%; color: gray; } /*table cell of link to switch to next year when active*/ .ajax_calendar_next_year_active { width: 10%; font-weight: bold; } /*customizing link inside of active cell (inactive cells doesn't have links)*/ .ajax_calendar_next_year_active a { text-decoration: none; } /*table cell of link to switch to next year when inactive*/ .ajax_calendar_next_year_inactive { width: 10%; color: gray; } /*table with calendar itself*/ .ajax_calendar { border:6px outset black; width:250px; } /*cells of calendar*/ .ajax_calendar td { border: 1px solid black; text-align: center; } /*table row with days of the week*/ .ajax_calendar_weekdays { font-weight: bold; } /*table cells with inactive days from previous or next months*/ .ajax_calendar_inactive_days { color:black; background-color: gray; } /*table cells with active days from currently selected month*/ .ajax_calendar_active_days { color: blue; } /*table cell with currently selected date*/ .ajax_calendar_today_days { color: red; background-color: yellow; }
Latest changes
03.09.2010 - Added option to pass variables through ajax to php file
Rate us
Try it out and Rate on PHPclasses.org
Support
PHP classes support forum or comments below
You may also be interested in:
Powered by BlogAlike.com










