summaryrefslogtreecommitdiffstats
path: root/system
diff options
context:
space:
mode:
authorAndrew <browner12@gmail.com>2014-02-08 08:24:00 +0100
committerAndrew <browner12@gmail.com>2014-02-08 08:24:00 +0100
commite0333dbd16ad1b173064f6d113aa50105f4726e4 (patch)
tree9cd7985eb07819c80328b8e4145816b9b797237f /system
parent93326a5b1bbaade4d416ad17a768cab27337ec5b (diff)
simplify calendar library
it seemed quite strange that we ask users to pass in a template string the we parse with REGEX, to extract key/value pairs, when we can simply ask users for an array. the class still contains default variables for each part of the template. Users can simply pass in an array (instead of a string) when they initialize, $config['template'], which overwrites default values. this cuts down a little on file size by removing parse_template() function. I think arrays are much more straightforward and easy to read than 'encoded' strings. don't have to keep a running list of all template keys like in previous parse_template() function. gets rid of $this->temp[] property and changes it to $this->template[] which is more descriptive when generating output
Diffstat (limited to 'system')
-rw-r--r--system/libraries/Calendar.php103
1 files changed, 35 insertions, 68 deletions
diff --git a/system/libraries/Calendar.php b/system/libraries/Calendar.php
index 711d23bec..89367dc6d 100644
--- a/system/libraries/Calendar.php
+++ b/system/libraries/Calendar.php
@@ -56,9 +56,9 @@ class CI_Calendar {
/**
* Calendar layout template
*
- * @var string
+ * @var array
*/
- public $template = '';
+ public $template = array();
/**
* Day of the week to start the calendar on
@@ -150,6 +150,9 @@ class CI_Calendar {
$this->$key = $val;
}
}
+
+ // Set template options
+ $this->template();
// Set the next_prev_url to the controller if required but not defined
if ($this->show_next_prev === TRUE && empty($this->next_prev_url))
@@ -223,11 +226,8 @@ class CI_Calendar {
$is_current_month = ($cur_year == $year && $cur_month == $month);
- // Generate the template data array
- $this->parse_template();
-
// Begin building the calendar output
- $out = $this->temp['table_open']."\n\n".$this->temp['heading_row_start']."\n";
+ $out = $this->template['table_open']."\n\n".$this->template['heading_row_start']."\n";
// "previous" month link
if ($this->show_next_prev === TRUE)
@@ -236,97 +236,97 @@ class CI_Calendar {
$this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
$adjusted_date = $this->adjust_date($month - 1, $year);
- $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell'])."\n";
+ $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->template['heading_previous_cell'])."\n";
}
// Heading containing the month/year
$colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
- $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan,
- str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->temp['heading_title_cell']));
+ $this->template['heading_title_cell'] = str_replace('{colspan}', $colspan,
+ str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->template['heading_title_cell']));
- $out .= $this->temp['heading_title_cell']."\n";
+ $out .= $this->template['heading_title_cell']."\n";
// "next" month link
if ($this->show_next_prev === TRUE)
{
$adjusted_date = $this->adjust_date($month + 1, $year);
- $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
+ $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->template['heading_next_cell']);
}
- $out .= "\n".$this->temp['heading_row_end']."\n\n"
+ $out .= "\n".$this->template['heading_row_end']."\n\n"
// Write the cells containing the days of the week
- .$this->temp['week_row_start']."\n";
+ .$this->template['week_row_start']."\n";
$day_names = $this->get_day_names();
for ($i = 0; $i < 7; $i ++)
{
- $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
+ $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->template['week_day_cell']);
}
- $out .= "\n".$this->temp['week_row_end']."\n";
+ $out .= "\n".$this->template['week_row_end']."\n";
// Build the main body of the calendar
while ($day <= $total_days)
{
- $out .= "\n".$this->temp['cal_row_start']."\n";
+ $out .= "\n".$this->template['cal_row_start']."\n";
for ($i = 0; $i < 7; $i++)
{
if ($day > 0 && $day <= $total_days)
{
- $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
+ $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->template['cal_cell_start_today'] : $this->template['cal_cell_start'];
if (isset($data[$day]))
{
// Cells with content
$temp = ($is_current_month === TRUE && $day == $cur_day) ?
- $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
+ $this->template['cal_cell_content_today'] : $this->template['cal_cell_content'];
$out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
}
else
{
// Cells with no content
$temp = ($is_current_month === TRUE && $day == $cur_day) ?
- $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
+ $this->template['cal_cell_no_content_today'] : $this->template['cal_cell_no_content'];
$out .= str_replace('{day}', $day, $temp);
}
- $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
+ $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->template['cal_cell_end_today'] : $this->template['cal_cell_end'];
}
elseif ($this->show_other_days === TRUE)
{
- $out .= $this->temp['cal_cell_start_other'];
+ $out .= $this->template['cal_cell_start_other'];
if ($day <= 0)
{
// Day of previous month
$prev_month = $this->adjust_date($month - 1, $year);
$prev_month_days = $this->get_total_days($prev_month['month'], $prev_month['year']);
- $out .= str_replace('{day}', $prev_month_days + $day, $this->temp['cal_cell_other']);
+ $out .= str_replace('{day}', $prev_month_days + $day, $this->template['cal_cell_other']);
}
else
{
// Day of next month
- $out .= str_replace('{day}', $day - $total_days, $this->temp['cal_cell_other']);
+ $out .= str_replace('{day}', $day - $total_days, $this->template['cal_cell_other']);
}
- $out .= $this->temp['cal_cell_end_other'];
+ $out .= $this->template['cal_cell_end_other'];
}
else
{
// Blank cells
- $out .= $this->temp['cal_cell_start'].$this->temp['cal_cell_blank'].$this->temp['cal_cell_end'];
+ $out .= $this->template['cal_cell_start'].$this->template['cal_cell_blank'].$this->template['cal_cell_end'];
}
$day++;
}
- $out .= "\n".$this->temp['cal_row_end']."\n";
+ $out .= "\n".$this->template['cal_row_end']."\n";
}
- return $out .= "\n".$this->temp['table_close'];
+ return $out .= "\n".$this->template['table_close'];
}
// --------------------------------------------------------------------
@@ -469,15 +469,15 @@ class CI_Calendar {
// --------------------------------------------------------------------
/**
- * Set Default Template Data
+ * Set Template Data
*
- * This is used in the event that the user has not created their own template
+ * This sets default template data. The user can override default values by passing in a template array
*
- * @return array
+ * @return null
*/
- public function default_template()
+ public function template()
{
- return array(
+ $default = array(
'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
'heading_row_start' => '<tr>',
'heading_previous_cell' => '<th><a href="{previous_url}">&lt;&lt;</a></th>',
@@ -503,43 +503,10 @@ class CI_Calendar {
'cal_row_end' => '</tr>',
'table_close' => '</table>'
);
+
+ $this->template = array_merge($default, $this->template);
}
-
- // --------------------------------------------------------------------
-
- /**
- * Parse Template
- *
- * Harvests the data within the template {pseudo-variables}
- * used to display the calendar
- *
- * @return void
- */
- public function parse_template()
- {
- $this->temp = $this->default_template();
-
- if ($this->template === '')
- {
- return;
- }
-
- $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today');
-
- foreach (array('table_open', 'table_close', 'heading_row_start', 'heading_previous_cell', 'heading_title_cell', 'heading_next_cell', 'heading_row_end', 'week_row_start', 'week_day_cell', 'week_row_end', 'cal_row_start', 'cal_cell_start', 'cal_cell_content', 'cal_cell_no_content', 'cal_cell_blank', 'cal_cell_end', 'cal_row_end', 'cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today', 'cal_cell_start_other', 'cal_cell_other', 'cal_cell_end_other') as $val)
- {
- if (preg_match('/\{'.$val.'\}(.*?)\{\/'.$val.'\}/si', $this->template, $match))
- {
- $this->temp[$val] = $match[1];
- }
- elseif (in_array($val, $today, TRUE))
- {
- $this->temp[$val] = $this->temp[substr($val, 0, -6)];
- }
- }
- }
-
}
/* End of file Calendar.php */
-/* Location: ./system/libraries/Calendar.php */ \ No newline at end of file
+/* Location: ./system/libraries/Calendar.php */