You are here

function pretty_calendar_block_content in Pretty Calendar 7

Rendering calendar block content.

Parameters

int $month: Timestamp of selected month.

Return value

string Rendered block content.

2 calls to pretty_calendar_block_content()
pretty_calendar_block_view in ./pretty_calendar.module
Implements hook_block_view().
pretty_calendar_content_loader in ./pretty_calendar.module
AJAX response for calendar navigation.

File

./pretty_calendar.module, line 281
Simple nice calendar module that displays the materials by date.

Code

function pretty_calendar_block_content($month) {
  global $language;

  // Format month array.
  // Get month timestamp.
  $month = mktime(0, 0, 0, date('m', $month), 1, date('Y', $month));

  // Get number of days in the given month.
  $dayofmonth = date('t', $month);
  $day_count = 1;
  $num = 0;

  // Fill first week.
  // Days of previous month in beginning of the week will be skipped.
  for ($i = 0; $i < 7; $i++) {

    // Get mumeric representation of the day of the week.
    $dayofweek = date('w', mktime(0, 0, 0, date('m', $month), $day_count, date('Y', $month)));
    $dayofweek = $dayofweek - 1;
    if ($dayofweek == -1) {
      $dayofweek = 6;
    }
    if ($dayofweek == $i) {
      $week[$num][$i] = $day_count;
      $day_count++;
    }
    else {

      // Leave blank value of the day in previous month.
      $week[$num][$i] = "";
    }
  }

  // Fill other weeks.
  while (TRUE) {
    $num++;
    for ($i = 0; $i < 7; $i++) {
      $week[$num][$i] = $day_count;
      $day_count++;
      if ($day_count > $dayofmonth) {
        break;
      }
    }
    if ($day_count > $dayofmonth) {
      break;
    }
  }

  // Get names of the days.
  $daynames = array();
  for ($i = 1; $i <= 7; $i++) {
    $daynames[] = format_date(mktime(0, 0, 0, 1, $i, 2001), 'custom', 'D', NULL, $language->language);
  }

  // Get month name. Add context to t() for extended translation.
  $month_name = t(format_date($month, 'custom', 'F', NULL, 'en'), array(), array(
    'context' => 'Nominative',
  )) . ' ' . date('Y', $month);
  $field_name = variable_get('pretty_calendar_field_date', '');
  $node_type = variable_get('pretty_calendar_node_type', '');
  $use_tooltips = variable_get('pretty_calendar_preload_tooltips', '-');
  $use_tooltips = $use_tooltips == '-' ? FALSE : TRUE;
  $output = '';

  // Get all nides for selected month.
  $day_nids = array();
  for ($i = 1; $i < 32; $i++) {
    $day_nids[$i] = 0;
  }
  $nids = pretty_calendar_select_month_nodes($month);
  if (isset($nids['error'])) {
    $output .= '<center><span style="color:red">' . t('database error') . '</span></center>';
  }
  else {
    foreach ($nids as $row) {
      $field_name = variable_get('pretty_calendar_field_date', '');
      if ($field_name != '') {
        $field_name .= '_value';
      }
      if (isset($row->{$field_name})) {
        $day_nids[(int) date('d', strtotime($row->{$field_name}))]++;
      }
      elseif (isset($row->created)) {
        $day_nids[(int) date('d', $row->created)]++;
      }
    }
  }

  // Format theme array.
  $days = array();
  for ($i = 0; $i < count($week); $i++) {
    for ($j = 0; $j < 7; $j++) {
      $day = !empty($week[$i][$j]) ? $week[$i][$j] : '';
      $cls = '';

      // Define cell classes.
      if ($j > 4) {
        $cls = 'pretty-calendar-weekend';
      }
      else {
        $cls = 'pretty-calendar-day';
      }
      if ($j == 6) {
        $cls .= ' pretty-calendar-last';
      }
      if ($day == date('d') && date('m.Y') == date('m.Y', $month)) {
        $cls .= ' pretty-calendar-today';
      }

      // YYYY/MM/DD format for links.
      $days_date = '';
      if (!empty($day)) {
        $days_date = date('Y/m', $month) . '/' . ($day > 9 ? $day : '0' . $day);
      }
      $days[] = theme('pretty_calendar_day', array(
        'number' => $day,
        'date' => $days_date,
        'delta' => $j + 1,
        'class' => $cls,
        'count' => !empty($day) ? $day_nids[$day] : 0,
        'using_tooltip' => $use_tooltips,
        'is_empty' => empty($day),
      ));
    }
    $output .= theme('pretty_calendar_week', array(
      'days' => $days,
      'delta' => $i + 1,
    ));
    $days = array();
  }
  return theme('pretty_calendar', array(
    'daynames' => $daynames,
    'content' => $output,
    'month_prev' => mktime(0, 0, 0, date('m', $month) - 1, 1, date('Y', $month)),
    'month_next' => mktime(0, 0, 0, date('m', $month) + 1, 1, date('Y', $month)),
    'month_name' => $month_name,
  ));
}