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

Asked

Viewed 476 times

1

I made a method that writes a VIEW with a calendar table that shows every month of a year using the library Calendaring Class. It works well, and returns it:

inserir a descrição da imagem aqui

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 couldn’t find a way to highlight just the current month using a template in the table’s CSS. When I change line style {heading_row_start}<tr>{/heading_row_start} (ref), it modifies all month labels:

inserir a descrição da imagem aqui

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

2 answers

2


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

CSS

.ativo {
     background-color:red !important;
}
  • In fact, I was focused on changing the style of the line {heading_row_start}<tr>{/heading_row_start} I did not notice that it is more practical to change the table where the month is generated. I also came up with a solution extending the Ci_calendar class, but I will mark your answer because it is a simpler solution. Thank you!

0

It is also possible expand the class CI_Calendar to make the method generate() pass your value $month for the method parse_template(). 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];
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.