function calendar_build_custom_calendar

6.x calendar.module calendar_build_custom_calendar($month, $year)

Actually renders the HTML for the calendar.

1 call to calendar_build_custom_calendar()

File

modules/calendar/calendar.module, line 3562

Code

function calendar_build_custom_calendar($month, $year) {
  global $user;

  fp_add_css(fp_get_module_path('calendar') . '/css/style.css');

  $extra = "";

  $today = date('Y-m-d', convert_time(time()));

  // Create array containing abbreviations of days of week.
  $daysOfWeek = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

  // What is the first day of the month in question?
  $firstDayOfMonth = mktime(0, 0, 0, $month, 1, $year);

  $check_month = $month - 1;
  $check_year = $year;
  if ($check_month < 1) {
    $check_year--;
    $check_month = 12;
  }

  $last_day_of_prev_month = cal_days_in_month(CAL_GREGORIAN, $check_month, $check_year);


  $original_month = intval($month);


  // How many days does this month contain?
  $numberDays = date('t', $firstDayOfMonth);

  // Retrieve some information about the first day of the
  // month in question.
  $dateComponents = getdate($firstDayOfMonth);

  // What is the name of the month in question?
  $monthName = $dateComponents ['month'];

  $short_month_name = date('M', $dateComponents [0]);

  // What is the index value (0-6) of the first day of the
  // month in question.
  $dayOfWeek = $dateComponents ['wday'];




  $calendar = "";
  $prev_month = $month - 1;
  $prev_year = $year;
  if ($prev_month < 1) {
    $prev_year--;
    $prev_month = 12;
  }

  $prev_url = fp_url("calendar", "year_month=$prev_year-$prev_month");


  $next_month = $month + 1;
  $next_year = $year;
  if ($next_month > 12) {
    $next_year++;
    $next_month = 1;
  }

  $next_url = fp_url("calendar", "year_month=$next_year-$next_month");


  // Create the table tag opener and day headers
  $calendar .= "<div class='calendar-header'><span class='cal-prev-link'><a href='$prev_url'>&laquo; Prev</a></span>$extra$short_month_name $year<span class='cal-next-link'><a href='$next_url'>Next &raquo;</a></span></div>";
  $calendar .= "<table class='custom-calendar calendar-calendar calendar-month-" . strtoupper($monthName) . " calendar-weeks-displayed-zWEEKSDISPLAYEDz calendar-visible-$extra'>";
  $calendar .= "<tr>";

  // Create the calendar headers

  foreach ($daysOfWeek as $day) {
    $calendar .= "<th class='header'>$day</th>";
  }

  // Create the rest of the calendar
  $start_day = 1;
  $start_month = $month;
  $start_year = $year;

  // If our month starts on any day OTHER than Sunday (0), then we need to include days at the end of the LAST month!
  if ($dayOfWeek > 0) {
    for ($t = $dayOfWeek; $t > 1; $t--) {
      $last_day_of_prev_month--;
    }
    $start_day = $last_day_of_prev_month;
    $start_month = intval($month) - 1;
    if ($start_month < 1) {
      $start_year--;
      $start_month = 12;
    }
    $dayOfWeek = 0;
  }


  // Similarly, if the last day of this month falls on any other day than Saturday (6), we need to advance it to the NEXT month.
  $to_day = $numberDays;
  $to_month = $month;
  $to_year = $year;

  $last_w = date('w', strtotime("$to_year-$to_month-$to_day"));

  if ($last_w < 6) {
    $to_day = 1;

    for ($t = $last_w + 1; $t < 6; $t++) {
      $to_day++;
    }

    $to_month++;
    if ($to_month > 12) {
      $to_year++;
      $to_month = 1;
    }


  }


  $all_dates = calendar_get_date_range("$start_year-$start_month-$start_day", "$to_year-$to_month-$to_day");


  // Initiate the day counter, starting with the 1st.
  $currentDay = 1;

  $calendar .= "</tr><tr>";

  // The variable $dayOfWeek is used to
  // ensure that the calendar
  // display consists of exactly 7 columns.

  if ($dayOfWeek > 0) {
    $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
  }

  $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  $next_month = str_pad($next_month, 2, "0", STR_PAD_LEFT);
  $number_of_weeks_displayed = 1;

  $db = get_global_database_handler();

  $all_appointments = array();
  // Get all of our activity dates into an array, so we can then convert them to local timezone, to figure out what dates they belong on.
  $res = db_query("SELECT DISTINCT(a.cid) FROM content__appointment a, content n
                   WHERE a.vid = n.vid
                   AND a.cid = n.cid
                   AND delete_flag = 0
                   AND (field__appointment_datetime LIKE ? OR field__appointment_datetime LIKE ?)
                   AND (field__faculty_id = ? OR field__student_id = ?)                                     
                   ORDER BY field__appointment_datetime ASC", array("$year-$month%", "$next_year-$next_month%", $user->cwid, $user->cwid)); // Because of stupid timezones, I need to grab from the NEXT month as well, just in case.
  while ($cur = db_fetch_object($res)) {
    $cid = $cur->cid;
    $content = content_load($cid);

    $mobile_day_cids [] = $cid;
    $db_date_ts = strtotime($content->field__appointment_datetime ['value']); // This is in UTC     
    $converted_date_ts = convert_time($db_date_ts); // Convert to our local timezone
    $start_date = date('Y-m-d', $converted_date_ts);
    $start_time = date('g:ia', $converted_date_ts);


    $duration_min = $content->field__appointment_duration_minutes ['value'];

    $end_time = format_date(strtotime("$start_date $start_time + $duration_min MINUTES"), 'just_time');

    $all_appointments [$start_date][$start_time][] = array(
      'db_date_ts' => $db_date_ts,
      'converted_date_ts' => $converted_date_ts,
      'start_date' => $start_date,
      'start_time' => $start_time,
      'cid' => $cid,
      'content' => $content,
    );

  } // while

  foreach ($all_dates as $the_date) {

    $extra_class = "";

    $temp = explode("-", $the_date);
    $year = $temp [0];
    $month = intval($temp [1]);
    $month = str_pad($month, 2, "0", STR_PAD_LEFT);

    if (intval($month) != $original_month) {
      $extra_class .= " out-of-range-day";
    }

    $currentDay = intval($temp [2]);

    // Seventh column (Saturday) reached. Start a new row.

    if ($dayOfWeek == 7) {
      $number_of_weeks_displayed++;
      $dayOfWeek = 0;
      $calendar .= "</tr><tr>";

    }

    $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);

    $date = "$year-$month-$currentDayRel";

    $corrected_date = date('Y-m-d', strtotime($date));
    if ($date == $today) {
      $extra_class .= " today";
    }

    $add_link = "";
    if (user_has_permission('add_appointment_content')) {
      $url = fp_url('content/add/appointment', "window_mode=popup&date=$date&faculty_id=$user->cwid");
      $add_link = "<a class='add-date' href='javascript:fpOpenLargeIframeDialog(\"$url\", \"Add New Appointment\", \"\");'><i class='fa fa-plus'></i></a>";
    }

    $calendar .= "<td class='day $extra_class' rel='$date'><span class='date-box-top'>$add_link<span class='the-day-num'>$currentDay</span></span>";

    $mobile_day_count = 0;
    $mobile_day_cids = array();
    $html = "";

    if (isset($all_appointments [$date])) {
      foreach ($all_appointments [$date] as $start_time => $XX) {
        foreach ($all_appointments [$date][$start_time] as $c => $details) {

          $start_date = $date;

          $cid = $details ['cid'];
          $content = $details ['content'];

          $mobile_day_cids [] = $cid;


          $duration_min = $content->field__appointment_duration_minutes ['value'];

          $end_time = format_date(strtotime("$start_date $start_time + $duration_min MINUTES"), 'just_time');


          $student_name = fp_get_student_name($content->field__student_id ['value']);
          if (!$student_name) {
            $student_name = fp_get_faculty_name($content->field__student_id ['value']);
          }

          $faculty_name = fp_get_faculty_name($content->field__faculty_id ['value']);

          $apt_title = $faculty_name . " - " . $student_name;


          if ($student_name == $faculty_name) {
            $apt_title = $student_name;
          }


          // Let's make the apt_title be a link to open the appointment.
          $view_url = fp_url("content/$cid", "window_mode=popup&content_tabs=false");
          $dtitle = t("View Appointment");
          $view_link = "<a href='javascript:fpOpenLargeIframeDialog(\"$view_url\",\"$dtitle\");' title='" . t("View") . "' >$apt_title</a>";


          $time_line = "<div class='time-line'>$start_time - $end_time</div>";

          $appt_extra_class = "";
          if ($content->published == 0) {
            $appt_extra_class = "appt-canceled";
            $view_link = "<span title='This appointment was canceled.'>$apt_title</span>"; // no longer a link, since it was canceled
          }


          $html = "<div class='calendar-entry $appt_extra_class'>
                                <div class='name-shift-line'>$view_link</div>
                                $time_line
                              </div>";

          $_SESSION ['calendar_cache'][$date][$cid] = $html; // save to our SESSION, we may need it later in a mobile context.

          $calendar .= $html;

          $mobile_day_count++;


        }
      }
    }





    if ($mobile_day_count > 0) {
      //$url = fp_url('calendar', "year_month=$year_month&mobile_date=$date&mobile_cids=" . join(",", $mobile_day_cids));
      $url = fp_url('calendar', "mobile_date=$date&mobile_cids=" . join(",", $mobile_day_cids));
      $calendar .= "<div style='display: none;' class='mobile-day-count'>
                            <a href='" . $url . "'>$mobile_day_count</a>
                          </div>";
    }



    $calendar .= '</td>';

    // Increment counters

    //$currentDay++;
    $dayOfWeek++;

  }



  // Complete the row of the last week in month, if necessary

  if ($dayOfWeek != 7) {

    $remainingDays = 7 - $dayOfWeek;
    $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";

  }

  $calendar .= "</tr>";

  $calendar .= "</table>";

  $calendar = str_replace("zWEEKSDISPLAYEDz", $number_of_weeks_displayed, $calendar);

  return $calendar;

}