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

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

insert the description of the image here

Code:

<?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 ($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 haven't found a way to highlight just the current month using a template in the table CSS. When I change the style of the line {heading_row_start}<tr>{/heading_row_start} (ref), it modifies all labels of months:

insert the description of the image here

I am using the methods and patterns of basic tutorial (same code). Any suggestions?

Author: ShutUpMagda, 2016-11-26

2 answers

I would do 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 within the increment, it will insert the active tag, which in turn will put a background (according to the css you make)

CSS

.ativo {
     background-color:red !important;
}
 2
Author: Sr. André Baill, 2016-11-26 17:49:48

You can also expand the Class CI_Calendar to make the method generate() pass its value $month to the method parse_template(). Thus, when generating the declared template, parse_template() will be able to use the passed value 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];
}
 0
Author: ShutUpMagda, 2016-11-26 20:13:54