From e0333dbd16ad1b173064f6d113aa50105f4726e4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 8 Feb 2014 01:24:00 -0600 Subject: 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 --- system/libraries/Calendar.php | 103 ++++++++++++++---------------------------- 1 file 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).' '.$year, $this->temp['heading_title_cell'])); + $this->template['heading_title_cell'] = str_replace('{colspan}', $colspan, + str_replace('{heading}', $this->get_month_name($month).' '.$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' => '', 'heading_row_start' => '', 'heading_previous_cell' => '', @@ -503,43 +503,10 @@ class CI_Calendar { 'cal_row_end' => '', 'table_close' => '
<<
' ); + + $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 */ -- cgit v1.2.3-24-g4f1b