Brain’s Calendar class allows for dynamic creation of calendars. Calendars can be formatted through the use of a calendar template, allowing total control over its design. In addition, data can be passed to the calendar cells.
This class is basically a port of CodeIgniter’s Calendar class. The main exception being the way that the calendar is configured. With Brain’s Calendar class, we manually initialize the class if configuration is necessary. The initialize method returns the instance of the class, making method chaining possible.
Calendar Example
$cal = $this->container['calendar']; $prefs = array( // Whatever config you want ... ); echo $cal->initialize( $prefs )->generate();
The code above generates a calendar using the current month and year, based on the server’s time. To display a calendar for a specific month and year, the generate method requires customizing its parameters:
$cal = $this->container['calendar']; // Generate a calendar for March of 2014 echo $cal->generate( 2014, 3 );
Adding Cell Data
To add data to calendar cells, an associative array is used where the keys correspond to the days to populate, and the values contains the data. The array is passed as the third parameter of the generate method. Consider this example:
$cal = $this->container['calendar']; $data = [ '6' => 'http://example.com/page1.html', '8' => 'http://example.com/page2.html', '9' => 'http://example.com/page3.html' ]; // Notice the data is passed as 3rd parameter echo $cal->generate( 2014, 3, $data );
Note: By default data values are expected to be links. Customizing the calendar template will allow the data to be anything you want it to be.
Setting Preferences
The first calendar example shows how to set preferences. There are 7 preferences.
$prefs = [ // A string containing your calendar template. 'template' => '', // A Unix timestamp corresponding to the current time. 'local_time' => time(), // Sets the day of the week the calendar should start on. 'start_day' => 'sunday', /** * Determines what version of the month name to use in the * header. long = January, short = Jan. */ 'month_type' => 'long', /** * Determines what version of the weekday names to use in the * column headers. long = Sunday, short = Sun, abr = Su. */ 'day_type' => 'abr', /** * Determines whether to display links allowing you to toggle to * next/previous months. See information on this feature below. */ 'show_next_prev' => FALSE, // Sets the basepath used in the next/previous calendar links. 'next_prev_url' => '' ];
Showing Next/Prev Month Links
$cal = $this->container['calendar']; $prefs = array( 'show_next_prev' => TRUE, 'next_prev_url' => 'http://example.com/calendar/show/' ); echo $cal->initialize( $prefs )->generate( URI::segment(3), URI::segment(4) );
Creating a Calendar Template
If you want to customize the appearance of the calendar, you will need to have a calendar template. In most situations this will be necessary. What I will usually do is make my template in a view.
{table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open} {heading_row_start}<tr>{/heading_row_start} {heading_previous_cell}<th><a href="{previous_url}"><<</a></th>{/heading_previous_cell} {heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell} {heading_next_cell}<th><a href="{next_url}">>></a></th>{/heading_next_cell} {heading_row_end}</tr>{/heading_row_end} {week_row_start}<tr>{/week_row_start} {week_day_cell}<td>{week_day}</td>{/week_day_cell} {week_row_end}</tr>{/week_row_end} {cal_row_start}<tr>{/cal_row_start} {cal_cell_start}<td>{/cal_cell_start} {cal_cell_content}<a href="{content}">{day}</a>{/cal_cell_content} {cal_cell_content_today}<div class="highlight"><a href="{content}">{day}</a></div>{/cal_cell_content_today} {cal_cell_no_content}{day}{/cal_cell_no_content} {cal_cell_no_content_today}<div class="highlight">{day}</div>{/cal_cell_no_content_today} {cal_cell_blank} {/cal_cell_blank} {cal_cell_end}</td>{/cal_cell_end} {cal_row_end}</tr>{/cal_row_end} {table_close}</table>{/table_close}
Lang
Lang values were moved inside the class, and if you want to change them, simply pass them as a config option with a key named ‘lang’. I did this because I never make multi-language sites, so why bother with lang?