Codeigniter; Calendaring Class: highlight the current month in an annual calendar

0

I made a method that writes a VIEW with a calendar table that shows all the months of a year using the library Calendaring Class . It works fine, and returns this:

Code:

<?phpclassCalendarextendsCI_Controller{publicfunctionthis_year(){$data['title']='Calendar:'.date('Y');$this->load->library('calendar');$prefs=array('local_time'=>'none','start_day'=>'sunday','month_type'=>'long','day_type'=>'short','show_next_prev'=>FALSE,'show_other_days'=>TRUE,'template'=>'{table_open}<tableclass="table table-condensed">{/table_open}
        {heading_row_start}<tr class="info">{/heading_row_start}
        {cal_cell_start_today}<td class="today">{/cal_cell_start_today}
        {cal_cell_start_other}<td class="other-day">{/cal_cell_start_other}
    '
        );
        $this->load->library('calendar', $prefs);
        $data['calendar'] = '<table class="table-calendar"><tr>';
        for ($i = 1; $i <= 12; $i++) {
            if ($i % 3 == 0) {
                $data['calendar'].= "<td>{$this->calendar->generate(date('Y'), $i)}</td>";
                $data['calendar'].= '</tr><tr>';
            }
            else {
                $data['calendar'].= "<td>{$this->calendar->generate(date('Y'), $i)}</td>";
            }
        }
        $data['calendar'].= '</tr></table>';
        $this->template->load('template/index', __CLASS__ . "/" . __FUNCTION__, $data);
    }

}

But I did not find a way to highlight only the current month using a template in the table's CSS. When I change the line style {heading_row_start}<tr>{/heading_row_start} ( ref ), it modifies all the month labels:

I'musingthemethodsandstandardsof basic tutorial code). Any suggestions?

    
asked by anonymous 26.11.2016 / 05:17

2 answers

1

I would do it this way:

<?php

class Calendar extends CI_Controller {

    public function this_year() {


        $data['title'] = 'Calendar: ' . date('Y');
        $this->load->library('calendar');
        $prefs = array(
            'local_time' => 'none',
            'start_day' => 'sunday',
            'month_type' => 'long',
            'day_type' => 'short',
            'show_next_prev' => FALSE,
            'show_other_days' => TRUE,
            'template' => '
        {table_open}<table class="table table-condensed">{/table_open}
        {heading_row_start}<tr class="info">{/heading_row_start}
        {cal_cell_start_today}<td class="today">{/cal_cell_start_today}
        {cal_cell_start_other}<td class="other-day">{/cal_cell_start_other}
    '
        );
        $this->load->library('calendar', $prefs);
        $data['calendar'] = '<table class="table-calendar"><tr>';
        for ($i = 1; $i <= 12; $i++) { 
            if($date("m")==$i) $mes_ativo = 'ativo'; else $mes_ativo = ''; // se o mês correspondente for ativo (mes atual), aplicamos a classe ativo (podes usar um css com background color)
            if ($i % 3 == 0) {
                $data['calendar'].= "<td class='{$mes_ativo}'>{$this->calendar->generate(date('Y'), $i)}</td>";
                $data['calendar'].= '</tr><tr>';
            }
            else {
                $data['calendar'].= "<td>{$this->calendar->generate(date('Y'), $i)}</td>";
            }
        }
        $data['calendar'].= '</tr></table>';
        $this->template->load('template/index', __CLASS__ . "/" . __FUNCTION__, $data);
    }

}

Note that I added:

if($date("m")==$i) $mes_ativo = 'ativo'; else $mes_ativo = ''; 

In this case, if the corresponding month is the month that is inside the increment, it will insert the active tag, which in turn will put a background (according to the css you do)

CSS

.ativo {
     background-color:red !important;
}
    
26.11.2016 / 18:49
0

You can also expand the class CI_Calendar to do so that the generate() method passes its value $month to the parse_template() method. So, when generating the declared template, parse_template() will be able to use the value passed to highlight the line {heading_row_start}<tr>{/heading_row_start} :

CI_Calendar::parse_template($month), 483 :

if($val == 'heading_row_start'){
  if( $month == date('m')){
    $this->replacements[$val] = '<tr class="info">';
  }
}
else{
  $this->replacements[$val] = $match[1];
}
    
26.11.2016 / 21:01