You are here

function calendar_get_paths in Calendar 5

Same name and namespace in other branches
  1. 5.2 calendar.inc \calendar_get_paths()
1 call to calendar_get_paths()
calendar_nav in ./calendar.module
Function to construct back and next navigation from views arguments

File

./calendar.module, line 373
Adds calendar filtering and displays to Views.

Code

function calendar_get_paths($cur_stamp, $view) {
  calendar_load_date_api();
  $path = array();

  // build an array of the current path and its parts
  $i = 0;
  $path[$i] = array(
    'path' => $view->real_url,
    'type' => 'url',
  );
  foreach ($view->argument as $delta => $arg) {
    if ($view->args[$delta]) {
      $i++;
      $pathtype = str_replace('calendar_', '', $arg['type']);
      $path[$i] = array(
        'path' => $view->args[$delta] != CALENDAR_EMPTY_ARG ? $view->{$pathtype} : CALENDAR_EMPTY_ARG,
        'type' => $pathtype,
      );
    }
  }

  // if there are other arguments after the view arguments, add them to the navigation links
  while ($i < sizeof($view->args)) {
    $i++;
    $path[$i] = array(
      'path' => $view->args[intval($i - 1)],
      'type' => '',
    );
  }

  // reverse through the path, creating a $nextpath and $prevpath arrays
  for ($x = $i; $x >= 0; $x--) {
    if ($path[$x]['path'] != CALENDAR_EMPTY_ARG) {
      switch ($path[$x]['type']) {
        case 'day':
          $day = $path[$x]['path'];
          $next_stamp = $cur_stamp + 86400;
          $prev_stamp = $cur_stamp - 86400;
          $nextpath[$x] = date_format_date('j', $next_stamp);
          $prevpath[$x] = date_format_date('j', $prev_stamp);
          break;
        case 'week':
          $week = $path[$x]['path'];
          $year = $view->year;
          if (!$next_stamp) {
            $cal_range = calendar_week_range($year, $week);
            $next_stamp = $cal_range[1] + 1;
            $prev_stamp = $cal_range[0] - 1;
          }
          $week_year = calendar_week_year($next_stamp);
          $next_year = $week_year[1];
          $nextpath[$x] = 'W' . sprintf("%02d", $week_year[0]);
          $week_year = calendar_week_year($prev_stamp);
          $prev_year = $week_year[1];
          $prevpath[$x] = 'W' . sprintf("%02d", $week_year[0]);
          break;
        case 'month':
          $month = $path[$x]['path'];
          $year = $view->year;
          $next_year = $month < 12 ? $year : $year + 1;
          $prev_year = $month > 1 ? $year : $year - 1;
          if (!$next_stamp) {
            $next_stamp = date_gmmktime(array(
              'mon' => $month < 12 ? $month + 1 : 1,
              'mday' => 1,
              'year' => $next_year,
            ));
            $prev_stamp = date_gmmktime(array(
              'mon' => $month > 1 ? $month - 1 : 12,
              'mday' => 1,
              'year' => $prev_year,
            ));
          }
          $nextpath[$x] = date_format_date('n', $next_stamp);
          $prevpath[$x] = date_format_date('n', $prev_stamp);
          break;
        case 'year':
          $year = $view->year;
          if (!$next_stamp) {
            $next_stamp = date_gmmktime(array(
              'mon' => 1,
              'mday' => 1,
              'year' => $next_year ? $next_year : $year + 1,
            ));
            $prev_stamp = date_gmmktime(array(
              'mon' => 12,
              'mday' => 1,
              'year' => $prev_year ? $prev_year : $year - 1,
            ));
          }
          $nextpath[$x] = date_format_date('Y', $next_stamp);
          $prevpath[$x] = date_format_date('Y', $prev_stamp);
          break;
        default:

          // all other arguments are just passed through
          if ($path[$x]['path']) {
            $nextpath[$x] = $path[$x]['path'];
            $prevpath[$x] = $path[$x]['path'];
          }
          break;
      }
    }
    else {
      $nextpath[$x] = $path[$x]['path'];
      $prevpath[$x] = $path[$x]['path'];
    }
  }
  return array(
    $prevpath,
    $nextpath,
  );
}