You are here

function availability_calendar_month_cells in Availability Calendars 7.5

Same name and namespace in other branches
  1. 7.3 availability_calendar.theme.inc \availability_calendar_month_cells()
  2. 7.4 availability_calendar.theme.inc \availability_calendar_month_cells()

Helper function that returns the cells for a month table.

The result will be a 1-dimensional array with

  • A cell for each day of the month.
  • Prepended by a cell for days of the previous month to start at the configured first day of the week.
  • Appended with days of the next month to arrive at a whole number of weeks.
  • Appended with empty weeks to let all calendars have 6 weeks.

Parameters

int $year:

int $month:

array $availability:

array $settings:

Return value

array

1 call to availability_calendar_month_cells()
theme_availability_calendar_month in ./availability_calendar.theme.inc
Themes the calendar for a given month.

File

./availability_calendar.theme.inc, line 299

Code

function availability_calendar_month_cells($year, $month, $availability, $settings) {

  // Date arithmetic.
  // Number of days in the month.
  $day = new DateTime();
  $day
    ->setDate($year, $month, 1);
  $days_in_month = (int) $day
    ->format('t');

  // Number of days (from the previous month) to add to first row.
  $days_to_prepend = (int) $day
    ->format('N') - $settings['first_day_of_week'];
  if ($days_to_prepend < 0) {
    $days_to_prepend += 7;
  }

  // Number of days (of the next month) to append to the last row (with dates).
  $days_to_append = ($days_to_prepend + $days_in_month) % 7;
  if ($days_to_append > 0) {
    $days_to_append = 7 - $days_to_append;
  }

  // Show split states?
  $showSplitDays = $settings['allocation_type'] === AC_ALLOCATION_TYPE_OVERNIGHT && $settings['show_split_day'];

  // We are going to create an array with all days we want to show.
  $states = availability_calendar_get_states();
  $total_days = $days_to_prepend + $days_in_month + $days_to_append;
  $today_iso = date(AC_ISODATE);
  $day
    ->modify('-' . ($days_to_prepend + 1) . ' days');
  $previous_day_iso = $day
    ->format(AC_ISODATE);
  $day
    ->modify('+1 day');
  $cells = array();
  for ($i = 0; $i < $total_days; $i++) {
    $day_iso = $day
      ->format(AC_ISODATE);
    $classes = array();
    if (empty($availability[$day_iso]) || (int) $day
      ->format('n') != $month) {
      $classes[] = 'cal-other';
    }
    else {
      if ($day_iso < $today_iso) {
        $classes[] = 'cal-pastdate';
      }
      else {

        // Special state for today and don't show yesterday's state as that is a past date.
        if ($day_iso == $today_iso) {
          $classes[] = 'cal-today';
          if ($showSplitDays) {
            $classes[] = 'cal-pastdate-am';
          }
        }
        else {
          if ($showSplitDays && isset($availability[$previous_day_iso])) {
            $classes[] = $states[$availability[$previous_day_iso]]['css_class'] . '-am';
          }
        }
        $state = $states[$availability[$day_iso]]['css_class'];
        if ($showSplitDays) {
          $state .= '-pm';
        }
        $classes[] = $state;
      }
    }
    $cell_contents = availability_calendar_theme_day((int) $day
      ->format('j'), $settings);
    $cells[$day_iso] = array(
      'data' => $cell_contents,
      'class' => $classes,
    );
    $day
      ->modify('+1 day');
    $previous_day_iso = $day_iso;
  }
  return $cells;
}