Catfish
Catfish

Reputation: 19294

How to extend or modify Codeigniter calendar to handle multi-day events and multiple events per day

I'd hate to reinvent the wheel on this one if it's out there somewhere, but has anyone extended or modified the codigniter calendar to handle multi-day events and multiple events per day or is it there a module available somewhere?

Upvotes: 2

Views: 7852

Answers (2)

imlouisrussell
imlouisrussell

Reputation: 946

This can be easily modified in the file system/libraries/Calendar.php with the following addition of code. I know that editing any system file is considered taboo, but this helped me out in my application a ton. Take note of the foreach() loop in the commented section that says //If more than one event on the same day. This is the code that needs to be added to the Calendar.php library file. This resource can be futher elaborated on at the following link.

http://codeigniter.com/forums/viewthread/196998/

Calendar.php modified code:

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'];

                        // If more than one event on the same day
                        if (is_array($data[$day]))
                        {
                          $several_events = '';
                          foreach ($data[$day] as $key => $value)
                          {
                            $several_events .= '<li id="'.$key.'">'.$value.'</li>';
                          }
                          $out .= str_replace('{day}', $day, str_replace('{content}', $several_events, $temp));
                        }

                        // One event per day
                        else
                        {
                          $out .= str_replace('{day}', $day, str_replace('{content}', $data[$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'];
                        $out .= str_replace('{day}', $day, $temp);
                    }
                }
                else
                {
                    // Blank cells
                    $out .= $this->temp['cal_cell_blank'];
                }

Keep in mind that I've shown more than just the edited code above to help you locate it within Calendar.php. I hope this helps!

Upvotes: 1

Eric
Eric

Reputation: 1197

Multiple events for one day is actually very easy. It just takes changing the calendar template and here is an example from some of my code.

public function print_calendar($year = 0, $month = 0)
{
    $prefs = array (
        'show_next_prev'  => FALSE,
        'next_prev_url'   => site_url('events/print_calendar'),
        'day_type'        => 'long',
        'template'        => '
            {heading_row_start}{/heading_row_start}
            {heading_previous_cell}{/heading_previous_cell}
            {heading_title_cell}<h1>{heading}</h1>{/heading_title_cell}
            {heading_next_cell}{/heading_next_cell}
            {heading_row_end}{/heading_row_end}

            {table_open}<table border="0" cellpadding="0" cellspacing="0">{/table_open}

            {week_row_start}<tr>{/week_row_start}
            {week_day_cell}<th bgcolor="#999999">{week_day}</th>{/week_day_cell}
            {week_row_end}</tr>{/week_row_end}

            {cal_row_start}<tr>{/cal_row_start}
            {cal_cell_start}<td background="'.s3_path('images/print_cal_cell.jpg').'">{/cal_cell_start}

            {cal_cell_content}<span class="cal_day">{day}</span>{content}{/cal_cell_content}
            {cal_cell_content_today}<span class="cal_day">{day}</span>{/cal_cell_content_today}

            {cal_cell_no_content}<span class="cal_day">{day}</span>{/cal_cell_no_content}
            {cal_cell_no_content_today}<span class="cal_day">{day}</span>{/cal_cell_no_content_today}

            {cal_cell_blank}&nbsp;{/cal_cell_blank}

            {cal_cell_end}</td>{/cal_cell_end}
            {cal_row_end}</tr>{/cal_row_end}

            {table_close}</table>{/table_close}
        '
    );

    $this->load->library('calendar', $prefs);

    $year = ($year == 0) ? date('Y') : $year;
    $month = ($month == 0) ? date('n') : $month;
    $start = strtotime('1-'.$month.'-'.$year);

    $events = $this->events_model->get_by_date($start, strtotime('+1 month', $start));
    $this->template->set_layout('');

    if ($events)
    {
        foreach ($events as $key => $event)
        {
            $day = date('j', $event['showing_start_time']);
            $vars[$day] = $this->template->build('events/calendar/cal_item', $event, TRUE);
        }
    }

    $vars['content'] = $this->calendar->generate($year, $month, $vars);
    $this->load->view('calendar/cal_layout', $vars);
}

Hopefully that code is clear and can give you a good starting point. What I normally do if they span multiple days just include the item on each day.

Upvotes: 3

Related Questions