Calendar::get_calendar_data()   F
last analyzed

Complexity

Conditions 17
Paths 608

Size

Total Lines 96
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
eloc 47
nc 608
nop 0
dl 0
loc 96
rs 2.3769
c 0
b 0
f 0
ccs 0
cts 68
cp 0
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Calendar View Controller
5
 */
6
class Calendar extends MY_Controller {
7
8
	/**
9
	 * Constructor
10
	 */
11
	public function __construct()
12
	{
13
		parent::__construct();
14
		$this->load->library('calendar');
15
		$this->load->model('task_model');
16
17
	}
18
19
	/**
20
	 * Calendar View
21
	 */
22
	public function index()
23
	{
24
		//Output
25
		$this->page->build('task/calendar', $this->get_calendar_data());
26
	}
27
28
	/**
29
	 * Get the data for the calendar display
30
	 *
31
	 * @return mixed
32
	 */
33
	protected function get_calendar_data()
34
	{
35
		//Offset time for custom months
36
		if($this->uri->segment(3) && $this->uri->segment(4))
37
		{
38
			$year = $this->uri->segment(3);
39
			$month = $this->uri->segment(4);
40
		}
41
42
		$_months = array(
43
			1 => 'January', 2 => 'February',
44
			3 => 'March', 4 => 'April',
45
			5 => 'May', 6 => 'June',
46
			7 => 'July', 8 => 'August',
47
			9 => 'September', 10 => 'October',
48
			11 => 'November', 12 => 'December'
49
		);
50
51
		$year = (isset($year)) ? $year : date('Y');
52
		$month = (isset($month)) ? $month : date('m');
53
54
		$local_time = time();
55
56
		$data = array();
57
58
		$data['month'] = $_months[(int)$month].' '.$year;
59
		$data['calendar'] = array();
60
		$data['today'] = getdate();
61
62
		$days_in_month = $this->calendar->get_total_days($month, $year);
63
64
		// Set the starting day number
65
		$local_date = mktime(0, 0, 0, $month, 1, $year);
66
		$month_end  = mktime(0, 0, 0, $month, $days_in_month, $year);
67
		$date = getdate($local_date);
68
		$day  = 0 + 1 - $date["wday"];
69
70
		//Get tasks for each day
71
		$content = $this->task_model->get_day_task_list($local_date, $month_end, $days_in_month);
72
73
		// Set the current month/year/day
74
		// We use this to determine the "today" date
75
		$cur_year	= date("Y", $local_time);
76
		$cur_month	= date("m", $local_time);
77
		$cur_day	= date("j", $local_time);
78
79
		$is_current_month = ($cur_year == $year AND $cur_month == $month);
80
81
		$out = null;
82
83
		while ($day <= $days_in_month)
84
		{
85
			for ($i = 0; $i < 7; $i++)
86
			{
87
				if($i == 0)
88
				{
89
					$out .= '<tr>';
90
				}
91
92
				if ($day > 0 AND $day <= $days_in_month)
93
				{
94
					if (isset($content[$day]))
95
					{
96
						// Cells with content
97
						$out .= ($is_current_month == TRUE AND $day == $cur_day) ? '<td class="today">' : '<td>';
98
						$out .= '<div><span class="date">'.$day.'</span><ul>'.$content[$day].'</ul></div></td>';
99
					}
100
					else
101
					{
102
						// Cells with no content
103
						$out .= ($is_current_month == TRUE AND $day == $cur_day) ? '<td class="today">' : '<td>';
104
						$out .= '<div><span class="date">'.$day.'</span>&nbsp;</div></td>';
105
					}
106
				}
107
				else
108
				{
109
					// Blank cells
110
					$out .= '<td>&nbsp;</td>';
111
				}
112
113
114
				$day++;
115
116
				if($i == 6)
117
				{
118
					$out .= '</tr>';
119
				}
120
			}
121
122
123
		}
124
125
		$data['calendar'] = $out;
126
127
		return $data;
128
	}
129
130
}