From e8dfc1b34ce71e5c47c6da3f553ecae52ad935d8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 24 Dec 2011 01:22:19 +0200 Subject: Improve the Calendar library --- system/libraries/Calendar.php | 133 +++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 79 deletions(-) (limited to 'system/libraries/Calendar.php') diff --git a/system/libraries/Calendar.php b/system/libraries/Calendar.php index f5ef4d576..299cb5047 100644 --- a/system/libraries/Calendar.php +++ b/system/libraries/Calendar.php @@ -1,13 +1,13 @@ - $val) { @@ -107,22 +107,19 @@ class CI_Calendar { * @param array the data to be shown in the calendar cells * @return string */ - function generate($year = '', $month = '', $data = array()) + public function generate($year = '', $month = '', $data = array()) { // Set and validate the supplied month/year if ($year == '') - $year = date("Y", $this->local_time); - - if ($month == '') - $month = date("m", $this->local_time); - - if (strlen($year) == 1) + $year = date('Y', $this->local_time); + elseif (strlen($year) === 1) $year = '200'.$year; - - if (strlen($year) == 2) + elseif (strlen($year) === 2) $year = '20'.$year; - if (strlen($month) == 1) + if ($month == '') + $month = date('m', $this->local_time); + elseif (strlen($month) === 1) $month = '0'.$month; $adjusted_date = $this->adjust_date($month, $year); @@ -149,9 +146,9 @@ class CI_Calendar { // Set the current month/year/day // We use this to determine the "today" date - $cur_year = date("Y", $this->local_time); - $cur_month = date("m", $this->local_time); - $cur_day = date("j", $this->local_time); + $cur_year = date('Y', $this->local_time); + $cur_month = date('m', $this->local_time); + $cur_day = date('j', $this->local_time); $is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE; @@ -159,12 +156,7 @@ class CI_Calendar { $this->parse_template(); // Begin building the calendar output - $out = $this->temp['table_open']; - $out .= "\n"; - - $out .= "\n"; - $out .= $this->temp['heading_row_start']; - $out .= "\n"; + $out = $this->temp['table_open'] . "\n\n" . $this->temp['heading_row_start'] . "\n"; // "previous" month link if ($this->show_next_prev == TRUE) @@ -173,18 +165,16 @@ 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']); - $out .= "\n"; + $out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['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, $this->temp['heading_title_cell']); - $this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)." ".$year, $this->temp['heading_title_cell']); + $this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, + str_replace('{heading}', $this->get_month_name($month).' '.$year, $this->temp['heading_title_cell'])); - $out .= $this->temp['heading_title_cell']; - $out .= "\n"; + $out .= $this->temp['heading_title_cell'] . "\n"; // "next" month link if ($this->show_next_prev == TRUE) @@ -193,14 +183,9 @@ class CI_Calendar { $out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']); } - $out .= "\n"; - $out .= $this->temp['heading_row_end']; - $out .= "\n"; - - // Write the cells containing the days of the week - $out .= "\n"; - $out .= $this->temp['week_row_start']; - $out .= "\n"; + $out .= "\n" . $this->temp['heading_row_end'] . "\n\n" + // Write the cells containing the days of the week + . $this->temp['week_row_start'] . "\n"; $day_names = $this->get_day_names(); @@ -209,33 +194,29 @@ class CI_Calendar { $out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']); } - $out .= "\n"; - $out .= $this->temp['week_row_end']; - $out .= "\n"; + $out .= "\n" . $this->temp['week_row_end'] . "\n"; // Build the main body of the calendar while ($day <= $total_days) { - $out .= "\n"; - $out .= $this->temp['cal_row_start']; - $out .= "\n"; + $out .= "\n" . $this->temp['cal_row_start'] . "\n"; for ($i = 0; $i < 7; $i++) { - $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start']; + $out .= ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start']; if ($day > 0 AND $day <= $total_days) { if (isset($data[$day])) { // Cells with content - $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content']; - $out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp)); + $temp = ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content']; + $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp); } else { // Cells with no content - $temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content']; + $temp = ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content']; $out .= str_replace('{day}', $day, $temp); } } @@ -245,17 +226,14 @@ class CI_Calendar { $out .= $this->temp['cal_cell_blank']; } - $out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end']; + $out .= ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end']; $day++; } - $out .= "\n"; - $out .= $this->temp['cal_row_end']; - $out .= "\n"; + $out .= "\n" . $this->temp['cal_row_end'] . "\n"; } - $out .= "\n"; - $out .= $this->temp['table_close']; + $out .= "\n" . $this->temp['table_close']; return $out; } @@ -272,7 +250,7 @@ class CI_Calendar { * @param integer the month * @return string */ - function get_month_name($month) + public function get_month_name($month) { if ($this->month_type == 'short') { @@ -287,7 +265,7 @@ class CI_Calendar { if ($this->CI->lang->line($month) === FALSE) { - return ucfirst(str_replace('cal_', '', $month)); + return ucfirst(substr($month, 4)); } return $this->CI->lang->line($month); @@ -305,7 +283,7 @@ class CI_Calendar { * @param string * @return array */ - function get_day_names($day_type = '') + public function get_day_names($day_type = '') { if ($day_type != '') $this->day_type = $day_type; @@ -324,9 +302,9 @@ class CI_Calendar { } $days = array(); - foreach ($day_names as $val) + for ($i = 0, $c = count($day_names); $i < $c; $i++) { - $days[] = ($this->CI->lang->line('cal_'.$val) === FALSE) ? ucfirst($val) : $this->CI->lang->line('cal_'.$val); + $days[] = ($this->CI->lang->line('cal_'.$day_names[$i]) === FALSE) ? ucfirst($day_names[$i]) : $this->CI->lang->line('cal_'.$day_names[$i]); } return $days; @@ -346,7 +324,7 @@ class CI_Calendar { * @param integer the year * @return array */ - function adjust_date($month, $year) + public function adjust_date($month, $year) { $date = array(); @@ -365,7 +343,7 @@ class CI_Calendar { $date['year']--; } - if (strlen($date['month']) == 1) + if (strlen($date['month']) === 1) { $date['month'] = '0'.$date['month']; } @@ -383,7 +361,7 @@ class CI_Calendar { * @param integer the year * @return integer */ - function get_total_days($month, $year) + public function get_total_days($month, $year) { $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); @@ -414,7 +392,7 @@ class CI_Calendar { * @access public * @return array */ - function default_template() + public function default_template() { return array ( 'table_open' => '', @@ -452,7 +430,7 @@ class CI_Calendar { * @access public * @return void */ - function parse_template() + public function parse_template() { $this->temp = $this->default_template(); @@ -467,14 +445,11 @@ class CI_Calendar { { if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match)) { - $this->temp[$val] = $match['1']; + $this->temp[$val] = $match[1]; } - else + elseif (in_array($val, $today, TRUE)) { - if (in_array($val, $today, TRUE)) - { - $this->temp[$val] = $this->temp[str_replace('_today', '', $val)]; - } + $this->temp[$val] = $this->temp[substr($val, 0, -6)]; } } } @@ -484,4 +459,4 @@ class CI_Calendar { // END CI_Calendar class /* 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