Skip to content

Commit e8dfc1b

Browse files
committed
Improve the Calendar library
1 parent 320d6db commit e8dfc1b

File tree

1 file changed

+54
-79
lines changed

1 file changed

+54
-79
lines changed

system/libraries/Calendar.php

Lines changed: 54 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22
/**
33
* CodeIgniter
44
*
55
* An open source application development framework for PHP 5.1.6 or newer
66
*
77
* NOTICE OF LICENSE
8-
*
8+
*
99
* Licensed under the Open Software License version 3.0
10-
*
10+
*
1111
* This source file is subject to the Open Software License (OSL 3.0) that is
1212
* bundled with this package in the files license.txt / license.rst. It is
1313
* also available through the world wide web at this URL:
@@ -40,15 +40,15 @@
4040
*/
4141
class CI_Calendar {
4242

43-
var $CI;
44-
var $lang;
45-
var $local_time;
46-
var $template = '';
47-
var $start_day = 'sunday';
48-
var $month_type = 'long';
49-
var $day_type = 'abr';
50-
var $show_next_prev = FALSE;
51-
var $next_prev_url = '';
43+
private $CI;
44+
public $lang;
45+
public $local_time;
46+
public $template = '';
47+
public $start_day = 'sunday';
48+
public $month_type = 'long';
49+
public $day_type = 'abr';
50+
public $show_next_prev = FALSE;
51+
public $next_prev_url = '';
5252

5353
/**
5454
* Constructor
@@ -85,7 +85,7 @@ public function __construct($config = array())
8585
* @param array config preferences
8686
* @return void
8787
*/
88-
function initialize($config = array())
88+
public function initialize($config = array())
8989
{
9090
foreach ($config as $key => $val)
9191
{
@@ -107,22 +107,19 @@ function initialize($config = array())
107107
* @param array the data to be shown in the calendar cells
108108
* @return string
109109
*/
110-
function generate($year = '', $month = '', $data = array())
110+
public function generate($year = '', $month = '', $data = array())
111111
{
112112
// Set and validate the supplied month/year
113113
if ($year == '')
114-
$year = date("Y", $this->local_time);
115-
116-
if ($month == '')
117-
$month = date("m", $this->local_time);
118-
119-
if (strlen($year) == 1)
114+
$year = date('Y', $this->local_time);
115+
elseif (strlen($year) === 1)
120116
$year = '200'.$year;
121-
122-
if (strlen($year) == 2)
117+
elseif (strlen($year) === 2)
123118
$year = '20'.$year;
124119

125-
if (strlen($month) == 1)
120+
if ($month == '')
121+
$month = date('m', $this->local_time);
122+
elseif (strlen($month) === 1)
126123
$month = '0'.$month;
127124

128125
$adjusted_date = $this->adjust_date($month, $year);
@@ -149,22 +146,17 @@ function generate($year = '', $month = '', $data = array())
149146

150147
// Set the current month/year/day
151148
// We use this to determine the "today" date
152-
$cur_year = date("Y", $this->local_time);
153-
$cur_month = date("m", $this->local_time);
154-
$cur_day = date("j", $this->local_time);
149+
$cur_year = date('Y', $this->local_time);
150+
$cur_month = date('m', $this->local_time);
151+
$cur_day = date('j', $this->local_time);
155152

156153
$is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;
157154

158155
// Generate the template data array
159156
$this->parse_template();
160157

161158
// Begin building the calendar output
162-
$out = $this->temp['table_open'];
163-
$out .= "\n";
164-
165-
$out .= "\n";
166-
$out .= $this->temp['heading_row_start'];
167-
$out .= "\n";
159+
$out = $this->temp['table_open'] . "\n\n" . $this->temp['heading_row_start'] . "\n";
168160

169161
// "previous" month link
170162
if ($this->show_next_prev == TRUE)
@@ -173,18 +165,16 @@ function generate($year = '', $month = '', $data = array())
173165
$this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->next_prev_url);
174166

175167
$adjusted_date = $this->adjust_date($month - 1, $year);
176-
$out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
177-
$out .= "\n";
168+
$out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell'])."\n";
178169
}
179170

180171
// Heading containing the month/year
181172
$colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
182173

183-
$this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
184-
$this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)."&nbsp;".$year, $this->temp['heading_title_cell']);
174+
$this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan,
175+
str_replace('{heading}', $this->get_month_name($month).'&nbsp;'.$year, $this->temp['heading_title_cell']));
185176

186-
$out .= $this->temp['heading_title_cell'];
187-
$out .= "\n";
177+
$out .= $this->temp['heading_title_cell'] . "\n";
188178

189179
// "next" month link
190180
if ($this->show_next_prev == TRUE)
@@ -193,14 +183,9 @@ function generate($year = '', $month = '', $data = array())
193183
$out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
194184
}
195185

196-
$out .= "\n";
197-
$out .= $this->temp['heading_row_end'];
198-
$out .= "\n";
199-
200-
// Write the cells containing the days of the week
201-
$out .= "\n";
202-
$out .= $this->temp['week_row_start'];
203-
$out .= "\n";
186+
$out .= "\n" . $this->temp['heading_row_end'] . "\n\n"
187+
// Write the cells containing the days of the week
188+
. $this->temp['week_row_start'] . "\n";
204189

205190
$day_names = $this->get_day_names();
206191

@@ -209,33 +194,29 @@ function generate($year = '', $month = '', $data = array())
209194
$out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
210195
}
211196

212-
$out .= "\n";
213-
$out .= $this->temp['week_row_end'];
214-
$out .= "\n";
197+
$out .= "\n" . $this->temp['week_row_end'] . "\n";
215198

216199
// Build the main body of the calendar
217200
while ($day <= $total_days)
218201
{
219-
$out .= "\n";
220-
$out .= $this->temp['cal_row_start'];
221-
$out .= "\n";
202+
$out .= "\n" . $this->temp['cal_row_start'] . "\n";
222203

223204
for ($i = 0; $i < 7; $i++)
224205
{
225-
$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
206+
$out .= ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
226207

227208
if ($day > 0 AND $day <= $total_days)
228209
{
229210
if (isset($data[$day]))
230211
{
231212
// Cells with content
232-
$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
233-
$out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
213+
$temp = ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
214+
$out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
234215
}
235216
else
236217
{
237218
// Cells with no content
238-
$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
219+
$temp = ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
239220
$out .= str_replace('{day}', $day, $temp);
240221
}
241222
}
@@ -245,17 +226,14 @@ function generate($year = '', $month = '', $data = array())
245226
$out .= $this->temp['cal_cell_blank'];
246227
}
247228

248-
$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
229+
$out .= ($is_current_month === TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
249230
$day++;
250231
}
251232

252-
$out .= "\n";
253-
$out .= $this->temp['cal_row_end'];
254-
$out .= "\n";
233+
$out .= "\n" . $this->temp['cal_row_end'] . "\n";
255234
}
256235

257-
$out .= "\n";
258-
$out .= $this->temp['table_close'];
236+
$out .= "\n" . $this->temp['table_close'];
259237

260238
return $out;
261239
}
@@ -272,7 +250,7 @@ function generate($year = '', $month = '', $data = array())
272250
* @param integer the month
273251
* @return string
274252
*/
275-
function get_month_name($month)
253+
public function get_month_name($month)
276254
{
277255
if ($this->month_type == 'short')
278256
{
@@ -287,7 +265,7 @@ function get_month_name($month)
287265

288266
if ($this->CI->lang->line($month) === FALSE)
289267
{
290-
return ucfirst(str_replace('cal_', '', $month));
268+
return ucfirst(substr($month, 4));
291269
}
292270

293271
return $this->CI->lang->line($month);
@@ -305,7 +283,7 @@ function get_month_name($month)
305283
* @param string
306284
* @return array
307285
*/
308-
function get_day_names($day_type = '')
286+
public function get_day_names($day_type = '')
309287
{
310288
if ($day_type != '')
311289
$this->day_type = $day_type;
@@ -324,9 +302,9 @@ function get_day_names($day_type = '')
324302
}
325303

326304
$days = array();
327-
foreach ($day_names as $val)
305+
for ($i = 0, $c = count($day_names); $i < $c; $i++)
328306
{
329-
$days[] = ($this->CI->lang->line('cal_'.$val) === FALSE) ? ucfirst($val) : $this->CI->lang->line('cal_'.$val);
307+
$days[] = ($this->CI->lang->line('cal_'.$day_names[$i]) === FALSE) ? ucfirst($day_names[$i]) : $this->CI->lang->line('cal_'.$day_names[$i]);
330308
}
331309

332310
return $days;
@@ -346,7 +324,7 @@ function get_day_names($day_type = '')
346324
* @param integer the year
347325
* @return array
348326
*/
349-
function adjust_date($month, $year)
327+
public function adjust_date($month, $year)
350328
{
351329
$date = array();
352330

@@ -365,7 +343,7 @@ function adjust_date($month, $year)
365343
$date['year']--;
366344
}
367345

368-
if (strlen($date['month']) == 1)
346+
if (strlen($date['month']) === 1)
369347
{
370348
$date['month'] = '0'.$date['month'];
371349
}
@@ -383,7 +361,7 @@ function adjust_date($month, $year)
383361
* @param integer the year
384362
* @return integer
385363
*/
386-
function get_total_days($month, $year)
364+
public function get_total_days($month, $year)
387365
{
388366
$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
389367

@@ -414,7 +392,7 @@ function get_total_days($month, $year)
414392
* @access public
415393
* @return array
416394
*/
417-
function default_template()
395+
public function default_template()
418396
{
419397
return array (
420398
'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
@@ -452,7 +430,7 @@ function default_template()
452430
* @access public
453431
* @return void
454432
*/
455-
function parse_template()
433+
public function parse_template()
456434
{
457435
$this->temp = $this->default_template();
458436

@@ -467,14 +445,11 @@ function parse_template()
467445
{
468446
if (preg_match("/\{".$val."\}(.*?)\{\/".$val."\}/si", $this->template, $match))
469447
{
470-
$this->temp[$val] = $match['1'];
448+
$this->temp[$val] = $match[1];
471449
}
472-
else
450+
elseif (in_array($val, $today, TRUE))
473451
{
474-
if (in_array($val, $today, TRUE))
475-
{
476-
$this->temp[$val] = $this->temp[str_replace('_today', '', $val)];
477-
}
452+
$this->temp[$val] = $this->temp[substr($val, 0, -6)];
478453
}
479454
}
480455
}
@@ -484,4 +459,4 @@ function parse_template()
484459
// END CI_Calendar class
485460

486461
/* End of file Calendar.php */
487-
/* Location: ./system/libraries/Calendar.php */
462+
/* Location: ./system/libraries/Calendar.php */

0 commit comments

Comments
 (0)