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(-) (limited to 'system') 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 From 227a5d7ed9ef65fb1e0c32dc4aaa379c1a96d5b7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 9 Feb 2014 15:32:44 -0600 Subject: template as array instead of string library accepts `$config['template']` as array and string, to perform template replacements. updated `$this->temp` var to `$this->replacements` to be less ambiguous about what it does --- system/libraries/Calendar.php | 130 +++++++++++++++++++++++++++++------------- 1 file changed, 91 insertions(+), 39 deletions(-) (limited to 'system') diff --git a/system/libraries/Calendar.php b/system/libraries/Calendar.php index 89367dc6d..977342249 100644 --- a/system/libraries/Calendar.php +++ b/system/libraries/Calendar.php @@ -56,44 +56,51 @@ class CI_Calendar { /** * Calendar layout template * + * @var string|array + */ + public $template = ''; + + /** + * Replacements array for template + * * @var array */ - public $template = array(); + public $replacements = array(); /** * Day of the week to start the calendar on * * @var string */ - public $start_day = 'sunday'; + public $start_day = 'sunday'; /** * How to display months * * @var string */ - public $month_type = 'long'; + public $month_type = 'long'; /** * How to display names of days * * @var string */ - public $day_type = 'abr'; + public $day_type = 'abr'; /** * Whether to show next/prev month links * * @var bool */ - public $show_next_prev = FALSE; + public $show_next_prev = FALSE; /** * Url base to use for next/prev month links * * @var bool */ - public $next_prev_url = ''; + public $next_prev_url = ''; /** * Show days of other months @@ -139,7 +146,7 @@ class CI_Calendar { * Accepts an associative array as input, containing display preferences * * @param array config preferences - * @return void + * @return CI_Calendar */ public function initialize($config = array()) { @@ -150,15 +157,14 @@ 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)) { $this->next_prev_url = $this->CI->config->site_url($this->CI->router->class.'/'.$this->CI->router->method); } + + return $this; } // -------------------------------------------------------------------- @@ -226,8 +232,11 @@ 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->template['table_open']."\n\n".$this->template['heading_row_start']."\n"; + $out = $this->replacements['table_open']."\n\n".$this->replacements['heading_row_start']."\n"; // "previous" month link if ($this->show_next_prev === TRUE) @@ -236,97 +245,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->template['heading_previous_cell'])."\n"; + $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->replacements['heading_previous_cell'])."\n"; } // Heading containing the month/year $colspan = ($this->show_next_prev === TRUE) ? 5 : 7; - $this->template['heading_title_cell'] = str_replace('{colspan}', $colspan, - str_replace('{heading}', $this->get_month_name($month).' '.$year, $this->template['heading_title_cell'])); + $this->replacements['heading_title_cell'] = str_replace('{colspan}', $colspan, + str_replace('{heading}', $this->get_month_name($month).' '.$year, $this->replacements['heading_title_cell'])); - $out .= $this->template['heading_title_cell']."\n"; + $out .= $this->replacements['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->template['heading_next_cell']); + $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->replacements['heading_next_cell']); } - $out .= "\n".$this->template['heading_row_end']."\n\n" + $out .= "\n".$this->replacements['heading_row_end']."\n\n" // Write the cells containing the days of the week - .$this->template['week_row_start']."\n"; + .$this->replacements['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->template['week_day_cell']); + $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->replacements['week_day_cell']); } - $out .= "\n".$this->template['week_row_end']."\n"; + $out .= "\n".$this->replacements['week_row_end']."\n"; // Build the main body of the calendar while ($day <= $total_days) { - $out .= "\n".$this->template['cal_row_start']."\n"; + $out .= "\n".$this->replacements['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->template['cal_cell_start_today'] : $this->template['cal_cell_start']; + $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_start_today'] : $this->replacements['cal_cell_start']; if (isset($data[$day])) { // Cells with content $temp = ($is_current_month === TRUE && $day == $cur_day) ? - $this->template['cal_cell_content_today'] : $this->template['cal_cell_content']; + $this->replacements['cal_cell_content_today'] : $this->replacements['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->template['cal_cell_no_content_today'] : $this->template['cal_cell_no_content']; + $this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content']; $out .= str_replace('{day}', $day, $temp); } - $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->template['cal_cell_end_today'] : $this->template['cal_cell_end']; + $out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_end_today'] : $this->replacements['cal_cell_end']; } elseif ($this->show_other_days === TRUE) { - $out .= $this->template['cal_cell_start_other']; + $out .= $this->replacements['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->template['cal_cell_other']); + $out .= str_replace('{day}', $prev_month_days + $day, $this->replacements['cal_cell_other']); } else { // Day of next month - $out .= str_replace('{day}', $day - $total_days, $this->template['cal_cell_other']); + $out .= str_replace('{day}', $day - $total_days, $this->replacements['cal_cell_other']); } - $out .= $this->template['cal_cell_end_other']; + $out .= $this->replacements['cal_cell_end_other']; } else { // Blank cells - $out .= $this->template['cal_cell_start'].$this->template['cal_cell_blank'].$this->template['cal_cell_end']; + $out .= $this->replacements['cal_cell_start'].$this->replacements['cal_cell_blank'].$this->replacements['cal_cell_end']; } $day++; } - $out .= "\n".$this->template['cal_row_end']."\n"; + $out .= "\n".$this->replacements['cal_row_end']."\n"; } - return $out .= "\n".$this->template['table_close']; + return $out .= "\n".$this->replacements['table_close']; } // -------------------------------------------------------------------- @@ -362,7 +371,7 @@ class CI_Calendar { * Get Day Names * * Returns an array of day names (Sunday, Monday, etc.) based - * on the type. Options: long, short, abrev + * on the type. Options: long, short, abr * * @param string * @return array @@ -469,15 +478,15 @@ class CI_Calendar { // -------------------------------------------------------------------- /** - * Set Template Data + * Set Default Template Data * - * This sets default template data. The user can override default values by passing in a template array + * This is used in the event that the user has not created their own template * - * @return null + * @return array */ - public function template() + public function default_template() { - $default = array( + return array( 'table_open' => '', 'heading_row_start' => '', 'heading_previous_cell' => '', @@ -503,9 +512,52 @@ class CI_Calendar { 'cal_row_end' => '', 'table_close' => '
<<
' ); + } + + // -------------------------------------------------------------------- + + /** + * Parse Template + * + * Harvests the data within the template {pseudo-variables} + * used to display the calendar + * + * @return CI_Calendar + */ + public function parse_template() + { + $this->replacements = $this->default_template(); + + if (!$this->template) + { + return $this; + } + + if(is_string($this->template)) + { + $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->replacements[$val] = $match[1]; + } + elseif (in_array($val, $today, TRUE)) + { + $this->replacements[$val] = $this->replacements[substr($val, 0, -6)]; + } + } + + return $this; + } - $this->template = array_merge($default, $this->template); + elseif(is_array($this->template)) + { + $this->replacements = array_merge($this->replacements, $this->template); + } } + } /* End of file Calendar.php */ -- cgit v1.2.3-24-g4f1b From 021f8edfee76a1a3a58fe34a1367e876f8f89003 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 9 Feb 2014 16:31:22 -0600 Subject: updates per narfbg's comments --- system/libraries/Calendar.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'system') diff --git a/system/libraries/Calendar.php b/system/libraries/Calendar.php index 977342249..3cb30dba2 100644 --- a/system/libraries/Calendar.php +++ b/system/libraries/Calendar.php @@ -56,7 +56,7 @@ class CI_Calendar { /** * Calendar layout template * - * @var string|array + * @var mixed */ public $template = ''; @@ -528,12 +528,12 @@ class CI_Calendar { { $this->replacements = $this->default_template(); - if (!$this->template) + if (empty($this->template)) { return $this; } - if(is_string($this->template)) + if (is_string($this->template)) { $today = array('cal_cell_start_today', 'cal_cell_content_today', 'cal_cell_no_content_today', 'cal_cell_end_today'); @@ -551,11 +551,12 @@ class CI_Calendar { return $this; } - - elseif(is_array($this->template)) + elseif (is_array($this->template)) { $this->replacements = array_merge($this->replacements, $this->template); } + + return $this; } } -- cgit v1.2.3-24-g4f1b From 41713aaa665189dd0a191c333c73c4a7b9f37c78 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 11 Feb 2014 02:07:50 -0600 Subject: updates per narfbg request I did a bunch of Git tutorials, and (i think) understand a lot more now. this update is to address narfbg comments, and add to pull request. --- system/libraries/Calendar.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'system') diff --git a/system/libraries/Calendar.php b/system/libraries/Calendar.php index 3cb30dba2..688b495a6 100644 --- a/system/libraries/Calendar.php +++ b/system/libraries/Calendar.php @@ -548,8 +548,6 @@ class CI_Calendar { $this->replacements[$val] = $this->replacements[substr($val, 0, -6)]; } } - - return $this; } elseif (is_array($this->template)) { @@ -562,4 +560,4 @@ class CI_Calendar { } /* End of file Calendar.php */ -/* Location: ./system/libraries/Calendar.php */ +/* Location: ./system/libraries/Calendar.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b