You are here

function theme_availability_calendars_month in Availability Calendars 6.2

Same name and namespace in other branches
  1. 5 availability_calendars.module \theme_availability_calendars_month()
  2. 6 availability_calendars.module \theme_availability_calendars_month()
  3. 7.2 availability_calendars.page.inc \theme_availability_calendars_month()

Themes the calendar for a given month.

Parameters

object $node:

int $year:

int $month:

object $settings:

Return value

string

1 theme call to theme_availability_calendars_month()
theme_availability_calendars_node in ./availability_calendars.page.inc
Themes the given number of months of the calendar for the given node.

File

./availability_calendars.page.inc, line 79

Code

function theme_availability_calendars_month($node, $year, $month, $settings) {
  $month_meta = availability_calendars_month_meta($year, $month, $settings);

  /**
   * Here we list all the days of the week, an array of 14 (two full weeks) so
   * that if users select monday as the first day we still get a full week in
   * our following loop.
   */
  $days = array(
    13 => t('Mon'),
    12 => t('Tue'),
    11 => t('Wed'),
    10 => t('Thu'),
    9 => t('Fri'),
    8 => t('Sat'),
    7 => t('Sun'),
    6 => t('Mon'),
    5 => t('Tue'),
    4 => t('Wed'),
    3 => t('Thu'),
    2 => t('Fri'),
    1 => t('Sat'),
    0 => t('Sun'),
  );
  $counter = -$month_meta['firstday'];
  for ($j = 0; $j < $month_meta['weeksinmonth']; $j++) {
    for ($i = 0; $i < 7; $i++) {
      $counter++;
      $week[$j][$i] = $counter;

      // Offset the days.
      if ($week[$j][$i] < 1 || $week[$j][$i] > $month_meta['daysinmonth']) {
        $week[$j][$i] = "";
      }
    }
  }

  // #1093408: Summer/winter time offsets: don't use midnight sharp
  $month_title = format_date(mktime(12, 0, 0, $month, 1, $year), 'custom', 'F Y');
  if (availability_calendars_can_edit($node)) {

    // add edit link to month_title.
    $month_title .= ' ' . l(t('edit'), 'availability-calendars/' . $node->nid . '/' . date('Y/m', mktime(12, 0, 0, $month, 1, $year)) . '/edit', array(
      'query' => array(
        'destination' => 'node/' . $node->nid,
      ),
    ));
  }
  $headers = array();

  // container for header row
  if ($settings->showweeknotes) {
    array_push($headers, array(
      'data' => '&nbsp;',
      'class' => 'calempty',
    ));

    // Add one empty cell for the notes column
  }

  // Add a header row showing the day of the week, we do some odd backwards looping through this...
  // Because the options in the node's form are set in a way that requires it.
  for ($i = $settings->startofweek + 7; $i > $settings->startofweek && $i <= 7 + $settings->startofweek; $i--) {
    $day = $settings->firstletter == 0 ? $days[$i] : drupal_substr($days[$i], 0, 1);
    array_push($headers, array(
      'data' => $day,
      'class' => 'dayofweek',
    ));
  }

  // Find all entries in database for this month ($availability, $notes) and pre-populate.
  $notes = availability_calendars_get_node_notes($node->nid, $year, $month);
  $states = availability_calendars_get_node_states($node->nid, $year, $month, $settings);
  $today = date(AC_ISODATE);
  $rows = array();

  // our container for rows
  $cells = array();

  // our container for cells
  foreach ($week as $key => $val) {
    $weeknumber = $key + 1;

    // Add the week note cell to the cells array.
    if ($settings->showweeknotes) {
      $note = !empty($notes[$weeknumber]) ? '<div>' . filter_xss($notes[$weeknumber]) . '</div>' : '<div class="calweeknote-empty"></div>';
      array_push($cells, array(
        'data' => $note,
        'class' => 'calweeknote',
      ));

      // Add the week note cell to the cells array
    }
    for ($i = 0; $i < 7; $i++) {
      $day = $week[$key][$i];

      // if there's a date, it's part of this month.
      if ($day) {
        $daystamp = date(AC_ISODATE, mktime(0, 0, 0, $month, $day, $year));
        $status = $states[$daystamp];
        $classes = array();
        if ($daystamp < $today) {
          $classes[] = 'calpastdate';

          // show or hide old states?
          if ($settings->hideold != 1) {
            $classes[] = $status;
          }
        }
        else {
          if ($today == $daystamp) {
            $classes[] = 'caltoday';

            // today
          }
          $classes[] = $status;
        }
        $day_cell = availability_calenders_day($day, $settings);
        $classes = implode(' ', $classes);
        array_push($cells, array(
          'data' => $day_cell,
          'class' => $classes,
        ));
      }
      else {

        // Empty day, beginning of row 1 or end of row 5/6 in a month.
        array_push($cells, array(
          'data' => '<span></span>',
          'class' => 'calother',
        ));
      }
    }
    array_push($rows, array(
      'data' => $cells,
      'class' => 'calweek',
    ));
    $cells = array();

    // clear out our $cells array before running the next week
  }
  if ($weeknumber == 5) {
    if ($settings->showweeknotes) {
      $note = '<div class="calweeknote-empty"></div>';
      array_push($cells, array(
        'data' => $note,
        'class' => 'calweeknote',
      ));

      // Add the week note cell to the cells array
    }
    for ($i = 0; $i < 7; $i++) {
      array_push($cells, array(
        'data' => '<span></span>',
        'class' => 'calother',
      ));
    }
    array_push($rows, array(
      'data' => $cells,
    ));
  }
  $output = theme('table', $headers, $rows, array(
    'class' => 'cal',
  ), $month_title);

  // Our final table
  // Wrap the table to allow for better styling
  return '<div class="calmonth-wrapper">' . $output . '</div>';
}