CodeIgniter calendar to add multiple events per da

2019-04-16 20:35发布

问题:

I am creating a Calendar view using CodeIgniter Calendar Library. I have multiple events per day but calendar shows only one event per day.

If the day has more than one events it shows only the last event.

Would appreciate if someone can help me to modify my coding below.

Best regards

Controller

function calendar($year=null, $month=null)
{
    $calendar = $this->timesheet_model->get_calendar($year, $month);

    $data = array('title'       => 'Calendar | Onsys',
              'main_content'    => 'view_timesheet_calendar',
              'calendar'    => $calendar
        );
    $this->load->view('template', $data);
}

Model (without calendar template)

    function get_calendar($year, $month)
{
    $calendar_settings = array( 'start_day'     => 'monday',
                                'show_next_prev'    => true,
                                'next_prev_url' => base_url('timesheet/calendar')  );

    $calendar_settings['template'] = '';

$events = $this->get_tasks($year, $month);

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

    return $this->calendar->generate($year, $month, $events);
}




function get_tasks($year, $month)
{
$events = array();

$this->db->select('projecttasksid,taskname,taskstartdate')->from('projecttasks')->like('taskstartdate', "$year-$month");
$sql_stmt = $this->db->get();
$sql_stmt = $sql_stmt->result();

foreach( $sql_stmt as $row )
{
    $day = (int)substr($row->taskstartdate, 8,2);
    $events[$day] = $row->taskname;
}

return $events;
}

回答1:

  1. Create a MY_Calendar.php in libraries that extends the standard calendar library
  2. Add the generate() method from the original library and replace line 273 $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);

with:

if (isset($data[$day]))
{
    // Cells with content
    $temp = ($is_current_month === TRUE && $day == $cur_day) ?
    $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
    if(is_array($data[$day]))
    {
        foreach ($data[$day] as $item_key => $item_value) {
            $addition .= '<a href="'.$item_value.'">'.$item_key.'</a>';
        }
        $out .= str_replace(array('<a href="{content}">{day}</a>'), $addition, $temp);  
    }
    else
        $out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
}

3. Now you can pass data with nested arrays and the calendar will add a link for every item you add;

example:

$data = array(
        3  => 'http://example.com/news/article/2006/03/',
        7  => 'http://example.com/news/article/2006/07/',
        13 => 'http://example.com/news/article/2006/13/',
        26 => array(
            'http://example.com/news/article/1/26/',
            'http://example.com/news/article/2/26/'
            'http://example.com/news/article/3/26/'
            )
);



回答2:

Another way to modify the calendar library is this..

edit this line from system/libraries/calendar.php 'line#301'

// Cells with no content
                    $temp = ($is_current_month === TRUE && $day == $cur_day) ?
                            $this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
                    $out .= str_replace('{day}', $day, $temp);

to

$temp = ($is_current_month === TRUE && $day == $cur_day) ?
                            $this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];

                    if (count($data[$day]) > 1) {//edited open
                        $addition ='';
                        foreach ($data[$day] as $item_key => $item_value) {
                            $addition .= '<a href="'.site_url('events/info/'.$item_value['id']).'">'.$item_value['content'].'</a><br>';                             
                        }
                        $out .= str_replace(array('{content}', '{day}'), array($addition, $day), $temp);

                    }else{
                        $event_url = '<a href="'.site_url('events/info/'.$data[$day][0]['id']).'">'.$data[$day][0]['content'].'</a>';
                        $out .= str_replace(array('{content}', '{day}'), array($event_url, $day), $temp);
                    }

The following changes will wrap the data with anchor. I did this for specific reason.

Then create your data like this

    $cal_data = array();

    $dateData = array(
            'year' => $year,
            'month' => $month
        );

    $result = $this->model_events->getDate($dateData);

    if ($result !== NULL) {
        foreach ($result as $key => $value) {
            $name = $value->cus_fn.' '.$value->cus_mn.' '.$value->cus_ln;
            $day = substr($value->sched_date, strlen($value->sched_date) - 2, 2);

            if (substr($day, 0, 1) == '0') $day = substr($day, 1, 1);

            if(isset($cal_data[$day])){
                array_push($cal_data[$day], array('id'=>$value->reservation_id, 'content'=>$name));
            }else{

                $cal_data[$day] = array(array(
                        'id' => $value->reservation_id,
                        'content' => $name
                    ));
            }

        }
    }

$data['cal'] = $this->calendar->generate($year, $month, $cal_data);

and thats it.