You are here

function theme_availability_calendars_month in Availability Calendars 6

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. 7.2 availability_calendars.page.inc \theme_availability_calendars_month()

Implementation of hook_theme(). produces our calendars on the viewing of an availability_calendars enabled node.

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.module
Themed output to display a list of node dates.

File

./availability_calendars.module, line 557
Availability Calendars Module.

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'),
  );
  for ($j = 0; $j < $month_meta['weeksinmonth']; $j++) {
    for ($i = 0; $i < 7; $i++) {
      $counter++;
      $week[$j][$i] = $counter;

      // offset the days
      $week[$j][$i] -= $month_meta['firstday'];
      if ($week[$j][$i] < 1 || $week[$j][$i] > $month_meta['daysinmonth']) {
        $week[$j][$i] = "";
      }
    }
  }
  if (availability_can_edit($node->nid)) {

    //month_title has the edit link if permissable
    $month_title = t("@date", array(
      '@date' => format_date(mktime(12, 0, 0, $month, 1, $year), 'custom', 'F Y'),
    )) . ' ' . l(t('edit'), 'availability-calendars/' . $node->nid . '/' . date('Y/m', mktime(0, 0, 0, $month, 1, $year)) . '/edit', array(
      'query' => 'destination=node/' . $node->nid,
    ));
  }
  else {

    // no edit link
    $month_title = t("@date", array(
      '@date' => format_date(mktime(12, 0, 0, $month, 1, $year), 'custom', 'F Y'),
    ));
  }
  $month_title = '<div class="month_title">' . $month_title . '</div>';

  // add a class to our month_title
  $headers = array();

  // container for header row
  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] : 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_result = db_query('SELECT week, note FROM {availability_calendars_week} WHERE nid = %d AND year = %d AND month = %d', $node->nid, $year, $month);
  while ($note = db_fetch_array($notes_result)) {
    $notes[$note['week']] = $note['note'];
  }
  $status_result = db_query('SELECT day, status FROM {availability_calendars_day} WHERE nid = %d AND year = %d AND month = %d', $node->nid, $year, $month);
  while ($status = db_fetch_array($status_result)) {
    $day_status[$status['day']] = $status['status'];
  }
  $availability_calendars_options = availability_calendars_options();
  $today = mktime(0, 0, 0, date('m'), date('j'), date('Y'));
  $rows = array();

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

  // our container for cells
  $options = availability_calendars_options();
  foreach ($week as $key => $val) {
    $weeknumber = $key + 1;
    array_push($cells, array(
      'data' => $notes[$weeknumber],
      'class' => 'calnote',
    ));

    // add the note cell to the cells array
    for ($i = 0; $i < 7; $i++) {
      $day = $week[$key][$i];
      $daystamp = mktime(0, 0, 0, (int) $month, (int) $day, (int) $year);

      // if there's a date, it's part of this month
      if ($day) {
        $classes = array();
        if ($today == $daystamp) {
          $classes[] = 'caltoday';
        }

        // today
        if ($daystamp < $today) {
          $classes[] = 'calpastdate';
        }

        // past date
        if ($settings->hideold === 1 && $daystamp < $today) {
          $classes[] = 'calnotavailable';
        }
        else {
          if ($day_status[$day] === NULL) {
            $classes[] = $settings->defaultstatus ? $settings->defaultstatus : $options[0];
          }
          else {
            $classes[] = $day_status[$day];
          }
        }
        $classes = implode(' ', $classes);
        $day = strpos($day_status[$day], 'calsplit ') === 0 ? '&nbsp;<div class="cal-split-date">' . $day . '</div><div class="cal-split-bg"></div>' : $day;
        array_push($cells, array(
          'data' => $day,
          'class' => $classes,
        ));
      }
      else {

        // empty, typically row 1 or 5 in a month
        array_push($cells, array(
          'data' => '&nbsp;',
          '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) {
    for ($i = 0; $i < 7; $i++) {
      array_push($cells, array(
        'data' => '&nbsp;',
      ));
    }
    array_push($rows, array(
      'data' => $cells,
    ));
  }
  $output = theme_table($headers, $rows, array(
    'class' => 'cal',
  ));

  // our final table
  // now output the table with the month title above a wrapper to allow for better styling
  return '<div class="calmonth-wrapper">' . $month_title . $output . '</div>';
}