I'm working on a calendar/planner application and I need some advice.
I'm working on the following part of my application:
It shows the days of the month from 1 till the end of the month, 28/29, 30 or 31. I achieved this.. (here) but my code is extremely ugly and I'm sure there must be another way to do this.
I'm working in CodeIgniter. My controller contains the following function to populate the list with the days of the month:
public function init_days()
{
// post values? in case of previous/next months (ajax)
if($this->input->post('post_month') && $this->input->post('post_year'))
{
$month = $this->input->post('post_month');
$year = $this->input->post('post_year');
$data = $this->planner_model->calendar_data($month, $year);
}
else
{
$data = $this->planner_model->calendar_data();
}
// init empty calendar
$data['calendar'] = '';
// easy var names
$current_month = $data['current_month'];
$current_year = $data['current_year'];
// echo list into $data['calendar']
for($i = 1; $i <= $data['days_in_month']; $i++)
{
if($current_month == date('n') && $current_year == date('Y'))
{
if($i < $data['current_day_of_month'])
{
$data['calendar'] .= "<li class='prev_month' value='$i'>$i</li>";
}
if($i == $data['current_day_of_month'])
{
$data['calendar'] .= "<li class='today' value='$i'>$i</li>";
}
if($i > $data['current_day_of_month'])
{
$data['calendar'] .= "<li class='next_month' value='$i'>$i</li>";
}
}
if( ($current_month > date('n') && $current_year == date('Y')) || ($current_year > date('Y')) )
{
$data['calendar'] .= "<li class='next_month' value='$i'>$i</li>";
}
if( ($current_month < date('n') && $current_year == date('Y')) || ($current_year < date('Y')) )
{
$data['calendar'] .= "<li class='prev_month' value='$i'>$i</li>";
}
}
$data['month_name'] = ucfirst($this->get_month_name($current_month));
header('Content-type: application/json');
echo json_encode($data);
}
My model returns the $data array that gets called by the controller (in the else clause, first part):
public function calendar_data($month = '', $year = '')
{
if( ! empty($month) && ! empty($year))
{
$data['current_year'] = $year;
$data['current_month'] = $month;
$data['current_day_of_month'] = date('j');
$data['current_day_of_week'] = date('w');
$data['days_in_month'] = cal_days_in_month(CAL_GREGORIAN, $month, $year);
}
else
{
$data['current_year'] = date('Y');
$data['current_month'] = date('n');
$data['current_day_of_month'] = date('j');
$data['current_day_of_week'] = date('w');
$data['days_in_month'] = cal_days_in_month(CAL_GREGORIAN, $data['current_month'], $data['current_year']);
}
return $data;
}
I then output this in my view with an AJAX call on $(document).ready.
$("#day_list").html(data['calendar']).fadeIn();
I'm not happy with the code. It's a mess, and I'm fairly sure I'm breaking MVC here; aren't I? Could someone perhaps give some advice or insights on how to do this in a 'better' way? Thanks a lot.
The full source is here in case anyone would be kind enough to look through it and tell me if there's other stuff I'm doing wrong.