/** * Returns an array of events in a month, keyed by each day of the month. * There will always be a key present for each day of the month, but if * there are no events for a day then it's corresponding value will be an * empty array. * * @param integer Month number (1-12). * @param integer Year (four-digit). * @return array Multi-dimensional result set array. */ public function getEventArray($month, $year) { // FIXME: Rewrite this query to use date ranges in WHERE, so that // indexes can be used. $sql = sprintf("SELECT\n calendar_event.calendar_event_id AS eventID,\n calendar_event.data_item_id AS dataItemID,\n calendar_event.data_item_type AS dataItemType,\n calendar_event.joborder_id AS jobOrderID,\n calendar_event.duration AS duration,\n calendar_event.all_day AS allDay,\n calendar_event.title AS title,\n calendar_event.description AS description,\n calendar_event.reminder_enabled AS reminderEnabled,\n calendar_event.reminder_email AS reminderEmail,\n calendar_event.reminder_time AS reminderTime,\n calendar_event.public AS public,\n DATE_FORMAT(\n calendar_event.date, '%%d'\n ) AS day,\n DATE_FORMAT(\n calendar_event.date, '%%m'\n ) AS month,\n DATE_FORMAT(\n calendar_event.date, '%%y'\n ) AS year,\n DATE_FORMAT(\n calendar_event.date, '%%m-%%d-%%y'\n ) AS date,\n DATE_FORMAT(\n calendar_event.date, '%%h:%%i %%p'\n ) AS time,\n DATE_FORMAT(\n calendar_event.date, '%%H'\n ) AS hour,\n DATE_FORMAT(\n calendar_event.date, '%%i'\n ) AS minute,\n calendar_event.date AS dateSort,\n DATE_FORMAT(\n calendar_event.date_created, '%%m-%%d-%%y (%%h:%%i %%p)'\n ) AS dateCreated,\n calendar_event_type.calendar_event_type_id AS eventType,\n calendar_event_type.short_description AS eventTypeDescription,\n entered_by_user.user_id AS userID,\n entered_by_user.first_name AS enteredByFirstName,\n entered_by_user.last_name AS enteredByLastName\n FROM\n calendar_event\n LEFT JOIN calendar_event_type\n ON calendar_event.type = calendar_event_type.calendar_event_type_id\n LEFT JOIN user AS entered_by_user\n ON calendar_event.entered_by = entered_by_user.user_id\n WHERE\n DATE_FORMAT(calendar_event.date, '%%c') = %s\n AND\n DATE_FORMAT(calendar_event.date, '%%Y') = %s\n AND\n calendar_event.site_id = %s\n ORDER BY\n dateSort ASC", $month, $year, $this->_siteID); $rs = $this->_db->getAllAssoc($sql); /* Build an array of result set arrays for each day of the month. * Days without any events scheduled will have an empty array. */ $daysInMonth = DateUtility::getDaysInMonth($month, $year); for ($i = 1; $i <= $daysInMonth; ++$i) { /* See if we can find a row in the result set that has 'day' set * to $i. */ $firstOffset = ResultSetUtility::findRowByColumnValue($rs, 'day', $i); /* Found? If yes, $firstOffset now contains the offset of the row; * otherwise false. */ if ($firstOffset === false) { /* No events for this date. */ $array[$i] = array(); continue; } /* Store the first row we found that has 'day' set to $i. */ $array[$i] = array($rs[$firstOffset]); /* There could be more than one row that has 'day' set to $i * (multiple events on the same day). We are going to tell * findRowByColumnValue() to skip the first row (we found it * and stored it already), and then keep increasing the number * of rows to skip until we can't find any more rows. */ for ($skip = 1;; ++$skip) { $nextOffset = ResultSetUtility::findRowByColumnValue($rs, 'day', $i, $skip); if ($nextOffset === false) { /* No more rows for this date. */ break; } /* Found another one; store the row. */ $array[$i][] = $rs[$nextOffset]; } } return $array; }
function testGetDaysInMonth() { $this->assertIdentical(DateUtility::getDaysInMonth(CALENDAR_MONTH_MARCH, 2006), 31); $this->assertIdentical(DateUtility::getDaysInMonth(CALENDAR_MONTH_MARCH, 1987), DateUtility::getDaysInMonth(CALENDAR_MONTH_MARCH, 2006)); $this->assertIdentical(DateUtility::getDaysInMonth(CALENDAR_MONTH_APRIL, 1987), 30); /* Leap years... */ $this->assertIdentical(DateUtility::getDaysInMonth(CALENDAR_MONTH_FEBRUARY, 2008), 29); $this->assertIdentical(DateUtility::getDaysInMonth(CALENDAR_MONTH_FEBRUARY, 2006), 28); }
public function showCalendar() { $currentHour = DateUtility::getAdjustedDate('H'); $currentDay = DateUtility::getAdjustedDate('j'); $currentMonth = DateUtility::getAdjustedDate('n'); $currentYear = DateUtility::getAdjustedDate('Y'); $currentUnixTime = DateUtility::getAdjustedDate(); $currentDateMDY = DateUtility::getAdjustedDate('m-d-y'); $currentWeek = DateUtility::getWeekNumber($currentUnixTime) - DateUtility::getWeekNumber( mktime(0, 0, 0, $currentMonth, 1, $currentYear) ); /* Do we have a valid date argument? If a month was specified and * isn't valid, fatal() out. If none was specified, use the current * month. */ if ($this->isRequiredIDValid('month', $_GET) && $this->isRequiredIDValid('year', $_GET)) { $month = $_GET['month']; $year = $_GET['year']; if (!checkdate($month, 1, $year)) { CommonErrors::fatal(COMMONERROR_BADFIELDS, $this, 'Invalid date.'); } if ($month == $currentMonth && $year == $currentYear) { $isCurrentMonth = true; } else { $isCurrentMonth = false; } } else { $month = $currentMonth; $year = $currentYear; $isCurrentMonth = true; } if (isset($_GET['view'])) { $view = $_GET['view']; } else { $view = 'DEFAULT_VIEW'; } if (isset($_GET['week'])) { $week = $_GET['week']; } else { $week = $currentWeek+1; } if (isset($_GET['day'])) { $day = $_GET['day']; } else { $day = $currentDay; } if (isset($_GET['showEvent'])) { $showEvent = $_GET['showEvent']; } else { $showEvent = null; } $userIsSuperUser = ($this->_accessLevel < ACCESS_LEVEL_SA ? 0 : 1); if ($userIsSuperUser && isset($_GET['superuser']) && $_GET['superuser'] == 1) { $superUserActive = true; } else { $superUserActive = false; } $startingWeekday = DateUtility::getStartingWeekday($month, $year); $daysInMonth = DateUtility::getDaysInMonth($month, $year); $calendar = new Calendar($this->_siteID); $monthBefore = $month - 1; $monthAfter = $month + 1; $yearBefore = $year; $yearAfter = $year; if ($monthAfter > 12) { $monthAfter = 1; $yearAfter = $year + 1; } if ($monthBefore < 1) { $monthBefore = 12; $yearBefore = $year - 1; } $eventsStringNow = $calendar->makeEventString( $calendar->getEventArray($month, $year), $month, $year ); $eventsStringBefore = $calendar->makeEventString( $calendar->getEventArray($monthBefore, $yearBefore), $monthBefore, $yearBefore ); $eventsStringAfter = $calendar->makeEventString( $calendar->getEventArray($monthAfter, $yearAfter), $monthAfter, $yearAfter ); $eventsString = implode( '@', array($eventsStringNow, $eventsStringBefore, $eventsStringAfter, $userIsSuperUser) ); /* Textual representation of the month and year. */ $dateString = date( 'F Y', mktime($_SESSION['CATS']->getTimeZoneOffset(), 0, 0, $month, 1, $year) ); /* The offset is the number of days after the first Sunday on a given * calendar page on which the 1st of the month falls. We subtract 1 * because Sunday has a value of 1. */ $startingOffset = $startingWeekday - 1; $userEmail = $_SESSION['CATS']->getEmail(); $calendarEventTypes = $calendar->getAllEventTypes(); $calendarSettings = new CalendarSettings($this->_siteID); $calendarSettingsRS = $calendarSettings->getAll(); if ($view == 'DEFAULT_VIEW') { $view = $calendarSettingsRS['calendarView']; } $summaryHTML = $calendar->getUpcomingEventsHTML(12, UPCOMING_FOR_CALENDAR); if (!eval(Hooks::get('CALENDAR_SHOW'))) return; if (SystemUtility::isSchedulerEnabled() && !$_SESSION['CATS']->isDemo()) { $allowEventReminders = true; } else { $allowEventReminders = false; } /* FIXME: Configurable */ $this->_template->assign('dayHourStart', $calendarSettingsRS['dayStart']); $this->_template->assign('dayHourEnd', $calendarSettingsRS['dayStop']); $this->_template->assign('firstDayMonday', $calendarSettingsRS['firstDayMonday']); $this->_template->assign('allowAjax', ($calendarSettingsRS['noAjax'] == 0 ? true : false)); $this->_template->assign('defaultPublic', ($calendarSettingsRS['defaultPublic'] == 0 ? 'false' : 'true')); $this->_template->assign('militaryTime', false); $this->_template->assign('active', $this); $this->_template->assign('currentDateMDY', $currentDateMDY); $this->_template->assign('startingWeekday', $startingWeekday); $this->_template->assign('daysInMonth', $daysInMonth); $this->_template->assign('currentHour', $currentHour); $this->_template->assign('currentDay', $currentDay); $this->_template->assign('currentMonth', $currentMonth); $this->_template->assign('currentYear', $currentYear); $this->_template->assign('startingOffset', $startingOffset); $this->_template->assign('userEmail', $userEmail); $this->_template->assign('userID', $this->_userID); $this->_template->assign('userEmail', $_SESSION['CATS']->getEmail()); $this->_template->assign('summaryHTML', $summaryHTML); $this->_template->assign('userIsSuperUser', $userIsSuperUser); $this->_template->assign('superUserActive', $superUserActive); $this->_template->assign('calendarEventTypes', $calendarEventTypes); $this->_template->assign('view', $view); $this->_template->assign('day', $day); $this->_template->assign('week', $week); $this->_template->assign('month', $month); $this->_template->assign('year', $year); $this->_template->assign('showEvent', $showEvent); $this->_template->assign('dateString', $dateString); $this->_template->assign('isCurrentMonth', $isCurrentMonth); $this->_template->assign('eventsString', $eventsString); $this->_template->assign('allowEventReminders', $allowEventReminders); $this->_template->display('./modules/calendar/Calendar.php'); }