You are here

function theme_availability_calendars_month in Availability Calendars 7.2

Same name and namespace in other branches
  1. 5 availability_calendars.module \theme_availability_calendars_month()
  2. 6.2 availability_calendars.page.inc \theme_availability_calendars_month()
  3. 6 availability_calendars.module \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 90

Code

function theme_availability_calendars_month($variables) {

  // Calendar code based on example at http://evolt.org/node/60673 :
  $node = $variables['node'];
  $year = $variables['year'];
  $month = sprintf('%02d', $variables['month']);
  $settings = $variables['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] = "";
      }
    }
  }
  $month_title = format_date(mktime(12, 0, 0, $month, 1, $year), 'custom', 'F Y');
  if ($settings->showeditlink && availability_calendars_can_edit($node)) {

    // Add edit link to month_title.
    $month_title .= ' ' . l(t('edit'), 'availability-calendars/' . $node->nid . '/' . date('Y/m', mktime(0, 0, 0, $month, 1, $year)) . '/edit', array(
      'query' => array(
        'destination' => 'node/' . $node->nid,
      ),
      'attributes' => array(
        'title' => t('Click to edit week notes and/or statuses per day'),
      ),
    ));
  }
  $headers = array();

  // Container for header row.
  if ($settings->showweeknotes) {
    array_push($headers, array(
      'data' => '&nbsp;',
      'class' => array(
        '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, $day);
  }

  // Find all entries in database for this month ($availability, $notes) and pre-populate.
  $notes = availability_calendars_get_node_notes($settings->calendar_id, $year, $month);
  $states = availability_calendars_get_node_states($settings->calendar_id, $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,
        'header' => TRUE,
      ));
    }
    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);
        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' => '<div></div>',
          'class' => array(
            'calother',
          ),
        ));
      }
    }
    array_push($rows, array(
      'data' => $cells,
      'class' => array(
        '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,
        'header' => TRUE,
      ));
    }
    for ($i = 0; $i < 7; $i++) {
      array_push($cells, array(
        'data' => '<div></div>',
        'class' => array(
          'calother',
        ),
      ));
    }
    array_push($rows, array(
      'data' => $cells,
    ));
  }
  $output = theme('table', array(
    'caption' => $month_title,
    'header' => $headers,
    'rows' => $rows,
    'attributes' => array(
      'id' => "cal-{$year}-{$month}-{$node->nid}",
      'class' => array(
        'cal',
        'calmonth',
      ),
    ),
    'sticky' => FALSE,
  ));

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