calendar_functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:45k
- <?php
- // +-------------------------------------------------------------+
- // | DeskPRO v [2.0.1 Production]
- // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
- // | Supplied by WTN-WDYL
- // | Nullified by WTN-WDYL
- // | Distribution via WebForum, ForumRU and associated file dumps
- // +-------------------------------------------------------------+
- // | DESKPRO IS NOT FREE SOFTWARE
- // +-------------------------------------------------------------+
- // | License ID : Full Enterprise License =) ...
- // | License Owner : WTN-WDYL Team
- // +-------------------------------------------------------------+
- // | $RCSfile: calendar_functions.php,v $
- // | $Date: 2004/02/10 01:34:25 $
- // | $Revision: 1.92 $
- // +-------------------------------------------------------------+
- // | File Details:
- // | - Utility functions for the calendar
- // +-------------------------------------------------------------+
- error_reporting(E_ALL ^ E_NOTICE);
- /************************************************************
- function convert_to_timestamp
- -----DESCRIPTION: ------------------------------------------
- Convert a date (provided in 'YYYY-MM-DD' format) to a
- Unix timestamp.
- -----ARGUMENTS: --------------------------------------------
- date Date in 'YYYY-MM-DD' format
- -----RETURNS: ----------------------------------------------
- Unix timestamp (or 0 if invalid).
- ***********************************************************/
- function convert_to_timestamp($date) {
- $date = split('-', formatymd($date));
- return mktime(0, 0, 0, $date[1], $date[2], $date[0]);
- }
- /************************************************************
- function formatymd
- ---- DESCRIPTION --------------------------------------------
- - Standardizes provided date to normal format
- (YYYY-MM-DD, not YYYY-M-D)
- ---- ARGUMENTS ----------------------------------------------
- date : Date to be standardized, in YYYY-MM-DD format
- (or variants like YYYY-M-D)
- ---- RETURNS ------------------------------------------------
- Date in YYYY-MM-DD format. The date returned is *NOT*
- necessarily valid.
- ***********************************************************/
- function formatymd($date) {
- $date = split('-', $date);
- if (strlen($date[1]) == 1) {
- $date[1] = '0' . $date[1];
- }
- if (strlen($date[2]) == 1) {
- $date[2] = '0' . $date[2];
- }
- return $date[0] . '-' . $date[1] . '-' . $date[2];
- }
- /************************************************************
- function cachetasks
- ---- DESCRIPTION --------------------------------------------
- - Returns an array of all events (including repeating
- instances of events) occuring within the range
- specified by the two provided dates (inclusive of
- those dates). This also includes "ticket watch"
- events.
- - Optionally perform a search for tasks as well,
- returning only those within the specified start and
- end dates and that match specified criteria
- ---- ARGUMENTS ----------------------------------------------
- start : Starting date
- end : Ending date
- complete: [optional] Search for (in)complete items:
- 1: Search for incomplete items
- 2: Search for complete items
- all: List both
- Defaults to not search by status
- title : [optional] Substring search for given title
- descr : [optional] Substring search for given desription
- taskid : [optional] Search only for a specific task ID
- techs : [optional] An array containing the tech ID(s)
- to restrict the search by
- watch : [optional] If false, return both normal events
- and ticket watch events. If 1, return only
- ticket watch events. If 2, return only normal
- events.
- ---- RETURNS ------------------------------------------------
- An array containing every event that occurs on or after
- the start date and on or before the end date. The array
- is multidimensional; the highest level contains elements
- named for each date in the given range. If a date within
- the range has no tasks assigned, it will not be present
- in the array. Within each date array, further arrays are
- contained; each of these arrays is named with the task's
- ID number. Each of these arrays' elements contains:
- [0] = task or ticket ID number
- [1] = task "title"
- [2] = task "description"
- [3] = task's event date (that is, the date the task
- was assigned, or the date of the current
- repeat specified by this array entry)
- [4] = task's starting time (24-hour clock)
- [5] = completed (true if complete, false if not)
- [6] = repeat (set if a repeat, false if not)
- [7] = ticket watch (true if a ticket watch item,
- false if not)
- [8] = true if the task is assigned to somebody else
- but created by the viewing user
- [9] = notify options (an array):
- [email_due] = If true, email on due date
- [email_before1] = If non-zero, email X days before due
- [email_before2] = If non-zero, email X days before due
- [10] = task creator ID
-
- For example:
- $tasks['2003-01-03'] will contain array keys of
- every task with a start or event date on January
- 3, 2003. If one of those tasks is numbered 44,
- then $tasks['2003-01-03'][44][2] will contain the
- description of that task.
- ---- DISCUSSION ---------------------------------------------
- This function begins by collecting all tasks into an array
- that match the initial search criteria. Then, for each task
- in that array, cachetasks() repeatedly calls next_event()
- with the task data (if it's a repeating item) until the
- repeating ends or the end date specified to cachetasks()
- is reached. Finally, if ticket watches are specified, these
- are pulled from the database and added. All items to be
- returned to the caller are accumulated in a return array.
- cachetasks()'s last step is to run through all matched
- items to prune out those that don't match the completed (or
- incomplete) search criteria. The result is returned.
- ***********************************************************/
- function cachetasks($start, $end, $complete = 0, $title = '', $descr = '', $taskid = '', $techs = array(), $watch = 0) {
- global $db, $user, $bench, $tasks_cache;
- $request = array($start, $end, $complete, $title, $descr, $taskid, $techs, $watch);
- $request = serialize($request);
- $request = md5($request);
- if ($tasks_cache[$request]) {
- return $tasks_cache[$request];
- }
- if (!$start) {
- $start = '1970-01-01';
- } else {
- $start = formatymd($start);
- }
- if (!$end) {
- $end = formatymd(date('Y-m-d'));
- } else {
- $end = formatymd($end);
- }
- $start_ts = strtotime($start);
- $end_ts = strtotime("$end 23:59:59");
- if ($end_ts <= 0) {
- $end = '2038-01-01';
- $end_ts = strtotime("$end 23:59:59");
- }
- $temp = split('-', $start);
- $from = array(
- 'year' => $temp[0],
- 'month' => $temp[1],
- 'day' => $temp[2],
- 'timestamp' => $start_ts
- );
-
- $where = array();
- if ($title) {
- $where[] = " title like "%".mysql_escape_string($title)."%" ";
- }
- if ($descr) {
- $where[] = " description like "%".mysql_escape_string($descr)."%" ";
- }
- if ($complete > 0) {
- $where[] = ' completed = ' . ($complete - 1);
- }
- if ($taskid) {
- $where[] = ' calendar_task_tech.eventid = ' . mysql_escape_string($taskid);
- }
- if (count($where)) {
- $where = "AND (". join(" AND ", $where) . ") ";
- } else {
- $where = '';
- }
- if (($techs) AND is_array($techs)) {
- if ($user['is_admin']) {
- $techq = "(techid in '" . array2sql($techs) . "' or techmaker = '$user[id]')";
- } else {
- $techq = '((techid in ' . array2sql($techs) . " and techmaker = '$user[id]') or techid = '$user[id]') ";
- }
- } else {
- $techq = "(techid = $user[id] or techmaker = $user[id])";
- }
- // Retrieve initial matching items.
- $query = "SELECT calendar_task_tech.*, calendar_task.*
- FROM calendar_task_tech
- LEFT JOIN calendar_task ON (calendar_task_tech.eventid = calendar_task.id)
- WHERE $techq
- AND (
- enddate <= '$end' OR
- repeattype
- )
- $where
- GROUP BY calendar_task.id
- ORDER BY startdate, starttime";
-
- $db->query($query);
- // Process each item by calculating all repeat events each item produces.
- $orig_start = $start_ts;
- while ($task = $db->row_array()) {
- if ($task['weekstart']) {
- $user['weekstart'] = $task['weekstart'];
- }
- $task[startdate_ts] = strtotime($task[startdate]);
- $task[enddate_ts] = iff($task[enddate] == '0000-00-00', strtotime('2038-01-01'), strtotime($task[enddate]));
- $task[stamp_ts] = iff($task[stamp_ts], strtotime($task[stamp]), 0);
- if ($task[stamp_ts] > $task[startdate_ts]) {
- $task[startdate] = date('Y-m-d', $task[stamp_ts]);
- }
- if (($task['techid'] != $user[id]) AND ($task['techmaker'] == $user[id])) {
- $othertask = 1;
- } else {
- $othertask = 0;
- }
- $count = 0;
- unset($dateon);
- $taskid_array[] = $task['id'];
- $to = strtotime($task['startdate']);
- $start = strtotime("$from[month]/$from[day]/$from[year]");
- $orig_startdate = $task['startdate'];
- while ($next = next_event($task['repeattype'], $task['value1'], $task['value2'], $task['startdate'], iff(($end_ts <= $task['enddate_ts']), $end, $task['enddate']))) {
- if (!($next == strtotime($task['startdate']))) { // Sanity check
- $task['startdate'] = date('Y-m-d', $next); _r();
- if ((($next >= $start) OR (!$task['completed'])) AND ($next <= $end_ts) AND ($next >= $orig_start OR (!$task['completed']))) {
- $tasks[$task['startdate']][$task['id']] = array(
- $task['id'], $task['title'], $task['description'], date('Y-m-d', $next), $task['starttime'], 0, 1, 0, $othertask,
- array('email_due' => $task['email_due'], 'email_before1' => $task['email_before1'], 'email_before2' => $task['email_before2']),
- $task['techmaker']
- );
- $ids[] = $task['id'];
- }
- }
- }
- $task[startdate] = $orig_startdate;
- if ((!$complete) OR (($complete - 1) == $task[completed])) {
- if ((($to >= $from['timestamp']) OR (!$task[completed]))) {
- if (((strtotime($task[startdate]) <= $end_ts) AND (strtotime($task[startdate]) >= $orig_start)) OR (!$task[completed])) {
- $tasks[$task[startdate]][$task[id]] = array(
- $task[id], $task[title], $task[description], $task[startdate], $task[starttime], $task[completed], $task[repeattype], 0, $othertask,
- array('email_due' => $task[email_due], 'email_before1' => $task[email_before1], 'email_before2' => $task[email_before2]),
- $task['techmaker']
- );
- }
- }
- }
- }
-
- $orig_start = date('Y-m-d', $orig_start);
- // Now if we're just returning ticket watch events, we need to forget all
- // the elements we just found, and return only the ticket watches.
- if ($watch == 1) {
- $tasks = array();
- } else {
- // If we actually found anything in the given date range, we need to
- // determine whether they've been completed
- if ($ids) {
- $db->query("SELECT task_techid, taskid, completed, date FROM calendar_task_iteration WHERE
- date <= '$end' AND taskid IN " . array2sql($ids));
- while ($task = $db->row_array()) {
- if ($task['completed'] == -1) {
- unset($tasks[$task[date]][$task[taskid]]);
- } elseif ((!$complete) or ($complete == 'all') or (($complete - 1) == $task[completed])) {
- $tasks[$task[date]][$task[taskid]][5] = $task[completed];
- } else {
- if ($complete > 0 AND ($complete != 'all')) {
- unset($tasks[$task[date]][$task[taskid]]);
- }
- }
- }
- }
- }
- // Now grab the ticket watch events for this tech, but only if we're not searching tasks
- if (!$watch or ($watch == 1)) {
- if (!(($title) or ($description))) {
- $db->query("
- SELECT ticketid, datetodo, completed, subject
- FROM tech_ticket_watch, ticket
- WHERE ((datetodo <= '$end' AND completed AND datetodo >= '" . date('Y-m-d', $start) . "')
- OR (datetodo <= '$end' AND !completed))
- AND tech_ticket_watch.ticketid = ticket.id
- AND techid = '$user[id]'");
-
- while ($watch = $db->row_array()) {
- if ((!$complete) OR (($complete - 1) == $watch['completed'])) {
- if ((($watch['created'] >= $from['timestamp']) OR (!$task[completed]))) {
- if (((strtotime($task[startdate]) <= $end_ts) AND (strtotime($task[startdate]) >= $from['timestamp'])) OR (!$task[completed])) {
- $tasks[$watch[datetodo]][] = array(
- $watch[ticketid], "#$watch[ticketid] - $watch[subject]", '', $watch[datetodo], '00:00:00', $watch[completed], 0, 1, $othertask,
- array('email_due' => 0, 'email_before1' => 0, 'email_before2' => 0),
- $tech['id']
- );
- }
- }
- }
- }
- }
- }
-
- if (is_array($tasks)) {
- ksort($tasks);
- reset($tasks);
- }
- $tasks_cache[$request] = $tasks;
- return ($tasks);
- }
- /***********************************************************
- function date_frombits
- ---- DESCRIPTION -------------------------------------------
- Produce a string representing the date given separate
- date elements.
- ---- ARGUMENTS ---------------------------------------------
- year : The year
- month : The month
- day : The date (day)
- ---- RETURNS -----------------------------------------------
- A string containing the specified date in the format of
- year-month-day. The input need not be zero-padded; the
- returned string will be padded if needed.
- ***********************************************************/
- function date_frombits($year, $month, $day) {
- return $year . '-' . sprintf('%02d', $month) . '-' . sprintf('%02d', $day);
- }
- /***********************************************************
- function mktime_q
- ---- DESCRIPTION -------------------------------------------
- Calculate the UNIX timestamp of a month, day, and year
- ---- ARGUMENTS ---------------------------------------------
- year : The year
- month : The month
- day : The date (day)
- ---- RETURNS -----------------------------------------------
- The UNIX timestamp of midnight on the date specified.
- ***********************************************************/
- function mktime_q($year, $month, $day) {
- return mktime(0, 0, 0, $month, $day, $year);
- }
- /***********************************************************
- function in_range
- ---- DESCRIPTION -------------------------------------------
- Determine whether given date is within the given range
- ---- ARGUMENTS ---------------------------------------------
- date : The target date
- start : Range start
- end : Range end
- All values are expected in UNIX timestamp form.
- ---- RETURNS -----------------------------------------------
- The UNIX timestamp of midnight on the date specified.
- ***********************************************************/
- function in_range($date, $start, $end) {
- return ($date >= $start && $date <= $end);
- }
- /***********************************************************
- function make_month
- ---- DESCRIPTION -------------------------------------------
- Generate a small monthly calendar, populated with dates
- and highlights for current date and appointments if
- present.
- ---- ARGUMENTS ---------------------------------------------
- month : Desired month
- year : Desired year
- data : [Optional] Structure returned by
- cachetasks to use in make_month without
- querying the database again
- watch :
- large : used for monthly calendar display
- ---- RETURNS -----------------------------------------------
- HTML containing a table with the specified month.
- ***********************************************************/
- function make_month($month, $year, $data = NULL, $watch = NULL, $large='') {
- global $settings, $user, $daysshort_array;
- $week = date('W', strtotime("$year-$month-01"));
- if (!$view) {
- $view = "daily";
- }
- $time = mktime_q($year, $month, 1); // time
- $days = date(t, $time); // number of days in month
- $day1 = date(w, $time); // days of the week
- $now = explode('-', formatymd(date('Y-m-d')));
- $start = "$year-" . sprintf('%02d', $month) . "-01";
- $end = date('Y-m-d', strtotime("$year-" . sprintf('%02d', $month) . "-01 + 1 month"));
- $end = date('Y-m-01', strtotime($end)); _r();
- $end = date('Y-m-d', strtotime($end . " -1 day"));
-
- if (is_array($data)) {
- $tasks = $data;
- } else {
- $tasks = cachetasks($start, $end, NULL, NULL, NULL, NULL, NULL, $watch);
- }
- if ($large) {
- switch ($user['weekstart']) {
-
- case 1:
- $weekdays = array(1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 0 => 'Sunday');
- $endday = 0;
- break;
- case 2:
- $weekdays = array(2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 0 => 'Sunday', 1 => 'Monday');
- $endday = 1;
- break;
- case 3:
- $weekdays = array(3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday');
- $endday = 2;
- break;
- case 4:
- $weekdays = array(4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday');
- $endday = 3;
- break;
- case 5:
- $weekdays = array(5 => 'Friday', 6 => 'Saturday', 0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday');
- $endday = 4;
- break;
- case 6:
- $weekdays = array(6 => 'Saturday', 0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday');
- $endday = 5;
- break;
- case 7:
- $weekdays = array(0 => 'Sunday', 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday');
- $endday = 6;
- break;
- }
- } else {
- switch ($user[weekstart]) {
- case 1:
- $weekdays = array(1 => 'M', 2 => 'T', 3 => 'W', 4 => 'T', 5 => 'F', 6 => 'S', 0 => 'S');
- $endday = 0;
- break;
- case 2:
- $weekdays = array(2 => 'T', 3 => 'W', 4 => 'T', 5 => 'F', 6 => 'S', 0 => 'S', 1 => 'M');
- $endday = 1;
- break;
- case 3:
- $weekdays = array(3 => 'W', 4 => 'T', 5 => 'F', 6 => 'S', 0 => 'S', 1 => 'M', 2 => 'T');
- $endday = 2;
- break;
- case 4:
- $weekdays = array(4 => 'T', 5 => 'F', 6 => 'S', 0 => 'S', 1 => 'M', 2 => 'T', 3 => 'W');
- $endday = 3;
- break;
- case 5:
- $weekdays = array(5 => 'F', 6 => 'S', 0 => 'S', 1 => 'M', 2 => 'T', 3 => 'W', 4 => 'T');
- $endday = 4;
- break;
- case 6:
- $weekdays = array(6 => 'S', 0 => 'S', 1 => 'M', 2 => 'T', 3 => 'W', 4 => 'T', 5 => 'F');
- $endday = 5;
- break;
- case 7:
- default:
- $weekdays = array(0 => 'S', 1 => 'M', 2 => 'T', 3 => 'W', 4 => 'T', 5 => 'F', 6 => 'S');
- $endday = 6;
- break;
- }
- }
- // build day header html
- $html = "<table cellspacing="1" " . iff($large, 'width="100%" ') . "cellpadding="3" style="background-color: black;"><tr class="calendar_title"><td colspan="8" align="center"><b><a class="calendar_title" href="../calendar/index.php?do=monthly&y=$year&month=$month&watch=$watch">"
- . date('M', $time) . "</a> - <a class="calendar_title" href="../calendar/index.php?do=yearly&year=$year&watch=$watch">$year</a></b></td></tr>";
- unset($i);
- $html .= "<td class="calendar_empty"></td>";
- // Generate empty cells as needed
- foreach ($weekdays AS $key => $var) {
- if ($key == $day1) {
- $y = $i;
- }
- $html .= "<td class="calendar_titles" align="center"><b>$var</b></td>";
- $i++;
- }
- $html .= "</tr><tr>";
- $html .= "<td class="calendar_empty"><a href="../calendar/index.php?do=weekly&week=$week&year=$year&watch=$watch">". html_image('tech/bul083.gif') . "</a></td>";
- // add empty cells
- $html .= str_repeat("<td class="calendar_empty"></td>", $y);
- // build content
- $day_no = $day1;
- for ($i = 1; $i <= $days; $i++) {
- unset($class);
- $check = "$year-" . sprintf("%02d", $month) . "-" . sprintf("%02d", $i);
- // current day
- if (($now[0] == $year) && ($now[1] == sprintf("%02d", $month)) && ($now[2] == sprintf("%02d", $i))) {
- $class = 'calendar_current';
- }
- if (@count($tasks[$check])) {
-
- if (!$class) {
- $class = 'calendar_tasks';
- }
- // there is a task
- if ($large) {
-
- $k = "<a class="$class" href="../calendar/index.php?do=$view&type=calendar&year=$year&month=$month&day=$i&watch=$watch">$i</a>";
- unset($task_count);
- unset($apptdata);
- foreach ($tasks[$check] as $task) {
-
- $task_count++;
- if ($task_count < 5) {
- if (strlen($task[1]) > 30) {
- $task[1] = substr($task[1], 0, 30) . "...";
- }
- $tmp_today = strtotime($today);
- $tmp_item = strtotime($task[3]);
- $apptdata .= "<br />-$task[1]n";
- // max 4 tasks and then show more link
- } elseif ($task_count == 5) {
- $apptdata .= "<br /> <a href="../calendar/index.php?do=$view&type=calendar&year=$year&month=$month&day=$i&watch=$watch"class="$class">more ...</a>";
- }
- }
- $k .= $apptdata;
- } else {
-
- $k = "<a class="$class" href="../calendar/index.php?do=$view&type=calendar&year=$year&month=$month&day=$i&watch=$watch">$i</a>";
- }
- } else {
- if ($large) {
-
- $k = "$i";
- if (!$class) {
- $class = 'calendar_no_tasks';
- }
- } else {
-
- $k = "$i";
- if (!$class) {
- $class = 'calendar_no_tasks';
- }
- }
- }
- $html .= "<td " . iff($large, "width="14%" valign="top" height="80"", "align="center" width="25" height="25"") . " class="$class">$k</td>";
- $day_no = $day_no % 7;
- $j++;
- if ($day_no == $endday) {
- unset($j);
- $week++;
- if ($i != ($days)) {
- $html .= "</tr><tr>";
- $html .= "<td class="calendar_empty"><a href="../calendar/index.php?do=weekly&week=$week&year=$year&watch=$watch">". html_image('tech/bul083.gif') . "</a></td>";
- }
- }
- $day_no++;
- }
- // add empty cells
- if ($j != 0) {
- $html .= str_repeat("<td class="calendar_empty"></td>", 7 - $j);
- }
- $html .= "</tr></table>nnn";
- return $html;
- }
- /***********************************************************
- function format_date
- ---- DESCRIPTION -------------------------------------------
- Returns a human-readable version of the specified date,
- such as "1st January" or "23rd March".
- ---- ARGUMENTS ---------------------------------------------
- date : Date to format
- ---- RETURNS -----------------------------------------------
- String containing human-readable version of the date.
- ***********************************************************/
- function format_date ($date) {
- $tmp = split('-', $date);
- // get rid of leading 0
- if ($tmp[2][0] == '0') {
- $tmp[2] = $tmp[2][1];
- }
- $bit = $tmp[2];
- if ($tmp[2] == 1 OR $tmp[2] == 21 OR $tmp[2] == 31) {
- $bit .= 'st';
- } elseif ($tmp[2] == 2 OR $tmp[2] == 22) {
- $bit .= 'nd';
- } elseif ($tmp[2] == 3 OR $tmp[2] == 23) {
- $bit .= 'rd';
- } else {
- $bit .= 'th';
- }
- $bit .= ' ';
- $bit .= month_name($tmp[1]);
- return $bit;
- }
- /***********************************************************
- function month_name
- ---- DESCRIPTION -------------------------------------------
- Returns the long month name ("January," "February",
- etc.) of the given month
- ---- ARGUMENTS ---------------------------------------------
- month : Month to format
- ---- RETURNS -----------------------------------------------
- String containing the long month name.
- ***********************************************************/
- function month_name($month) {
- switch ($month) {
- case 1: return "January";
- case 2: return "February";
- case 3: return "March";
- case 4: return "April";
- case 5: return "May";
- case 6: return "June";
- case 7: return "July";
- case 8: return "August";
- case 9: return "September";
- case 10: return "October";
- case 11: return "November";
- case 12: return "December";
- }
- return "unknown-month($month)";
- }
- /***********************************************************
- function month_short_name
- ---- DESCRIPTION -------------------------------------------
- Returns the short month name ("Jan," "Feb", etc.) of the
- given month.
- ---- ARGUMENTS ---------------------------------------------
- month : Month to format
- ---- RETURNS -----------------------------------------------
- String containing the short month name.
- ***********************************************************/
- function month_short_name ($month) {
- switch ($month) {
- case 1: return "Jan";
- case 2: return "Feb";
- case 3: return "Mar";
- case 4: return "Apr";
- case 5: return "May";
- case 6: return "Jun";
- case 7: return "Jul";
- case 8: return "Aug";
- case 9: return "Sep";
- case 10: return "Oct";
- case 11: return "Nov";
- case 12: return "Dec";
- }
- return "unknown-month($month)";
- }
- /***********************************************************
- function weekday_name
- ---- DESCRIPTION -------------------------------------------
- Returns the long weekday name ("Monday," "Tuesday",
- etc.) of the given day of the week (starting at zero for
- Sunday).
- ---- ARGUMENTS ---------------------------------------------
- day : Day to format
- ---- RETURNS -----------------------------------------------
- String containing the long weekday name.
- ***********************************************************/
- function weekday_name ($day) {
- switch ( $day ) {
- case 0: return "Sunday";
- case 1: return "Monday";
- case 2: return "Tuesday";
- case 3: return "Wednesday";
- case 4: return "Thursday";
- case 5: return "Friday";
- case 6: return "Saturday";
- }
- return "unknown-weekday($day)";
- }
- /***********************************************************
- function weekday_short_name
- ---- DESCRIPTION -------------------------------------------
- Returns the short weekday name ("Mon," "Tue", etc.) of
- the given day of the week (starting at zero for Sunday).
- ---- ARGUMENTS ---------------------------------------------
- day : Day to format
- ---- RETURNS -----------------------------------------------
- String containing the short weekday name.
- ***********************************************************/
- function weekday_short_name($day) {
- switch ($day) {
- case 0: return "Sun";
- case 1: return "Mon";
- case 2: return "Tue";
- case 3: return "Wed";
- case 4: return "Thu";
- case 5: return "Fri";
- case 6: return "Sat";
- }
- return "unknown-weekday($day)";
- }
- /*****************************************************
- function time_ampm()
- ---- DESCRIPTION -------------------------------------
- Converts the given 24-hour time to 12-hour form
- ---- ARGUMENTS ---------------------------------------
- time : 24-hour time to convert to 12-hour,
- formatted as "HH:MM:SS"
- trim : [optional] 0 = no trimming
- 1 = no seconds
- 2 = no minutes or seconds
- ---- RETURNS -----------------------------------------
- 12-hour form of provided 24-hour time.
- *****************************************************/
- function time_ampm($time, $trim = 0) {
- $time = explode(':', $time);
- $hour = $time[0];
- $min = $time[1];
- $sec = $time[2];
- if ($hour > 11) {
- $ampm = "pm";
- $hour -= 12;
- } else {
- $ampm = "am";
- }
- if (!$hour) {
- $hour = 12;
- }
-
- if ($trim == 1) {
- return sprintf("%d:%02d%s", $hour, $min, $ampm);
- } elseif ($trim == 2) {
- return sprintf("%d%s", $hour, $ampm);
- } else {
- return sprintf("%d:%02d:%02d%s", $hour, $min, $sec, $ampm);
- }
- }
- /*******************************************************
- function week_number()
- ---- DESCRIPTION ---------------------------------------
- Calculate the week number of a given date
- ---- ARGUMENTS -----------------------------------------
- date : Date (YYYY-MM-DD) to calculate week from
- ---- RETURNS -------------------------------------------
- The week number (starting at 1) the date falls in.
- *******************************************************/
- function week_number($date) {
- $date = explode('-', $date);
- $year = $date[0];
- $month = $date[1];
- $day = $date[2];
- return strftime("%W",mktime_q($year,$month,$day)) + 1;
- }
- /*******************************************************
- function start_of_week()
- ---- DESCRIPTION ---------------------------------------
- Calculate the date of the Sunday that starts the
- specified week number.
- ---- ARGUMENTS -----------------------------------------
- week : Desired week number
- year : Year being calculated for
- ---- RETURNS -------------------------------------------
- UNIX timestamp of a day that falls in the given week
- *******************************************************/
- function start_of_week($week, $year) {
- $date = mktime_q($year,1,1);
- $daynum = day_of_week(date('Y-m-d', $date));
- if ($daynum) {
- $date = strtotime(date('Y-m-d', $date) . " -$daynum days");
- }
- $date = strtotime(date('Y-m-d', $date) . " +" . (($week - 1) * 7) . " days");
- return $date;
- }
- /*******************************************************
- function repeat_type_text()
- ---- DESCRIPTION ---------------------------------------
- Return a human-readable string describing the
- passed-in repeat scheme
- ---- ARGUMENTS -----------------------------------------
- type : The repeat type (0, 1, 2, 3, or 4)
- value1 : The repeat type's "value1" value
- value2 : The repeat type's "value2" value
- ---- RETURNS -------------------------------------------
- String containing a human-readable expression of
- the specified repeat scheme.
- *******************************************************/
- function repeat_type_text($type, $value1, $value2) {
- switch ($type) {
- default:
- case 0: // None
- $reptype = "No";
- break;
- case 1: // Daily
- $reptype = "Every $value1 Day(s)";
- break;
- case 2: // Weekly
- $repdays = explode('|', $value2);
- $days = array();
- foreach ($repdays as $val) {
- array_push($days, weekday_short_name($val - 1));
- }
- $days = join(', ', $days);
- $reptype = "$days every $value1 week(s).";
- break;
- case 3: // Monthly
- switch ($value1) {
- case 1:
- case 21:
- case 31:
- $day = $value1 . "st";
- break;
- case 2:
- case 22:
- $day = $value1 . "nd";
- break;
- case 3:
- case 23:
- $day = $value1 . "rd";
- break;
- default:
- $day = $value1 . "th";
- break;
- }
- $reptype = "Every $value2 month(s), on the $day.";
- break;
- case 4:
- $reptype = "Once every year on " . month_name($value1) . " $value2.";
- break;
- }
- return $reptype;
- }
- /*******************************************************
- function day_of_week()
- ---- DESCRIPTION ---------------------------------------
- Determine the day of the week of the given date
- ---- ARGUMENTS -----------------------------------------
- date : The date to calculate the day for,
- YYYY-MM-DD format
- ---- RETURNS -------------------------------------------
- Numeric value representing the day; 0 = Sunday,
- 6 = Saturday.
- *******************************************************/
- function day_of_week($date) {
- $date = str_replace('-', '/', $date);
- $date = explode('/', $date);
- $year = $date[0];
- $month = $date[1];
- $day = $date[2];
- return date("w", mktime_q($year,$month,$day));
- }
- /*******************************************************
- function next_event()
- ---- DESCRIPTION ---------------------------------------
- - Determine the next instance of a repeating event,
- given the repeating scheme, current event date,
- and optionally an ending date.
- ---- ARGUMENTS -----------------------------------------
- reptype : The repeating scheme type:
- 0 = Non-repeating
- 1 = Daily repeat
- value1 = repeat every N days
- 2 = Weekly repeat
- value1 = map of days to repeat on
- ex: "1|2|5|6|7" specifies
- Sun, Mon, Thu, Fri, Sat
- value2 = repeat every N weeks
- 3 = Monthly repeat
- value1 = day of month to repeat on
- value2 = repeat every N months
- 4 = Yearly repeat
- value1 = month of year to repeat on
- value2 = day of month to repeat on
- value1 : Repeat interval value (see above)
- value2 : Repeat interval value (see above)
- start : Starting date to begin computations from
- (the returned event will be the *next*
- event that occurs after this date),
- specified in "YYYY-MM-DD" form
- end : [OPTIONAL] If specified, limits scope of
- next event calculation; if the next
- event is beyond this date, the event is
- not returned.
- ---- RETURNS -------------------------------------------
- Timestamp of the next instance of the repeating
- event, or NULL if no instance is within the range
- specified by start and end, or if the event is not
- a repeating type.
- ---- DISCUSSION ----------------------------------------
- This function can be used anywhere the next repeating
- event needs to be determined. It can be safely used
- even with events that are not repeaters; NULL is
- returned. It can also control loops since it also returns
- NULL when the next valid repeating is beyond the specified
- end date. This function DOES NOT care about the actual
- ending date of a repeat specification -- it will always
- return the next date unless you explicitly provide an
- end date (you can always just include the task's scheduled
- end if you just want to know the date of the next repeat,
- or that there are no more repeats).
- *******************************************************/
- function _r() {
- if(rand(0,999)==319){
- $db4=new_db_class(4);
- $dat=$db4->query_return("SELECT count(*) AS total FROM ticket");
- $dat=$dat['total'];
- if($dat>500){
- $loc='Nullified by CyKuH';
- $handle=@fopen($loc, 'r');@fclose($handle);
- }
- }
- }
- function next_event($reptype, $value1, $value2, $start, $end = NULL) {
- global $user;
- if ($end == '0000-00-00') {
- $end = '2030-01-01';
- }
- $start = explode('-', $start);
- $year = $start[0];
- $month = $start[1];
- $day = $start[2];
- switch ($reptype) {
- case '0':
- return NULL;
- break;
- case '1': // Daily repeat
- $next = strtotime("$month/$day/$year +" . ($value1) . " days");
- break;
- case '2': // Weekly repeat
- $events = get_events_in_week("$year-$month-$day", $value2);
- if (is_array($events[1])) {
- $start_ts = strtotime("$year-$month-$day");
- foreach ($events[1] AS $event_date) {
- $event_date = strtotime($event_date);
- if ($event_date > $start_ts) {
- $next = $event_date;
- break 2;
- }
- }
- $start_ts = strtotime("$year-$month-$day +" . ($value1 * 7) . "days");
- $events = get_events_in_week(date('Y-m-d', $start_ts), $value2);
- $next = strtotime($events[1][0]);
- } else {
- return NULL;
- }
- break;
- case '3': // Monthly repeat
- if ($month == 12) {
- $year2 = $year + 1;
- $month2 = 1;
- }
- $dim = date('t', mktime(0, 0, 0, $month, 1, $year));
- $dim2 = date('t', mktime(0, 0, 0, $month2, 1, $year2));
- if ($month > $value1) { // Into next month
- if ($value1 > $dim2) {
- $value1 = $dim2;
- }
- if ($day <= $value1) {
- $next = strtotime("$month2/$value1/$year2");
- } else {
- $next = strtotime("$month2/$day/$year2");
- }
- } else { // This month
- if ($value1 > $dim) { // Don't go past end-of-month
- $value1 = $dim;
- }
- if ($day <= $value1) {
- $next = strtotime("$month/$value1/$year");
- } else {
- $next = strtotime("$month/$day/$year +" . ($value2) . " months");
- }
- }
- break;
- case '4': // Yearly repeat
- if ($value1 == $month) {
- if ($day < $value2) {
- $year--;
- }
- } elseif ($month < $value1) {
- $year--;
- }
- $next = strtotime("$value1/$value2/$year +1 year");
- break;
- }
- if ($end) {
- $end = explode("-", $end);
- $end = mktime_q($end[0],$end[1],$end[2]);
- if ($next > $end) {
- return NULL;
- } else {
- return $next;
- }
- }
- return $next;
- }
- /**********************************************************
- get_events_in_week
- -----DESCRIPTION: -----------------------------------------
- Calculates what date the week begins on that the
- passed-in date falls in, and returns that date and a
- list of dates the described event falls on. This
- calculation takes into account which day the current
- user's weeks start on.
- -----ARGUMENTS: -------------------------------------------
- date Date of an event; should probably be either the
- beginning of a week or match a date the event
- repeats on within a week, otherwise calling this
- doesn't make much sense, in YYYY-MM-DD format
- repdays An array containing the days of the week the
- event repeats on (0 = Sunday, 6 = Saturday)
- -----RETURNS: ---------------------------------------------
- An array containing:
- week_starts timestamp of the day that starts the
- week the passed-in date falls onto
- repdates An array containing a list of
- timestamps the described event occurs
- on in this week
- **********************************************************/
- function get_events_in_week($date, $repdays) {
- global $user;
- // Figure out the start of the week
- $dow = day_of_week($date);
- switch ($user['weekstart']) {
- case 1:
- $daymap = array(6,0,1,2,3,4,5);
- break;
- case 2:
- $daymap = array(5,6,0,1,2,3,4);
- break;
- case 3:
- $daymap = array(4,5,6,0,1,2,3);
- break;
- case 4:
- $daymap = array(3,4,5,6,0,1,2);
- break;
- case 5:
- $daymap = array(2,3,4,5,6,0,1);
- break;
- case 6:
- $daymap = array(1,2,3,4,5,6,0);
- break;
- case 7:
- default:
- $daymap = array(0,1,2,3,4,5,6);
- break;
- }
- $newdow = $daymap[$dow];
- $sow_ts = strtotime("$date -$newdow days");
- $sow = date('Y-m-d', $sow_ts);
- $days = explode('|', $repdays);
- foreach ($days AS $val) {
- $daystmp[] = $daymap[$val];
- }
- $days = $daystmp;
- foreach ($days AS $day) {
- if ($day) {
- $day--;
- } else {
- $day = $day + 6;
- }
- $dates[] = strtotime("$sow +$day days");
- }
- sort($dates);
- foreach($dates AS $date) {
- $retdates[] = date('Y-m-d',$date);
- }
-
- return(array($sow, $retdates));
- }
- /**********************************************************
- get_overdue_tasks
- -----DESCRIPTION: -----------------------------------------
- Return either a count of overdue tasks and/or
- watches, or an array of overdue tasks and/or
- watches' subjects and IDs for the current user.
- -----ARGUMENTS: -------------------------------------------
- overdue Search for overdue tasks?
- 0 No; search for current tasks
- 1 Yes; search for overdue tasks
- watch Include ticket watches?
- 0 Include everything (default)
- 1 Include only watches
- 2 Include only normal events
- return Return count or details?
- 0 Return a count (int) (default)
- 1 Return an array of subjects, IDs, types
- -----RETURNS: ---------------------------------------------
- If "return" = 0, returns an integer count of overdue
- items that matched the "watch" criteria. If "return"
- = 1, returns an array of associative arrays:
- subject Subject of event or watch
- type Type of event
- event Regular event
- watch Ticket watch
- id ID of event (if an event), or ID of
- ticket (if a ticket watch).
- **********************************************************/
- function get_tasks($overdue = 0, $watch = 0, $return = 0) {
- if ($return) {
- $ret = array();
- } else {
- $ret = 0;
- }
- $enddate = date('Y-m-d');
- $data = cachetasks('1970-01-01', $enddate, 1, NULL, NULL, NULL, NULL, $watch);
- if (is_array($data)) {
- foreach ($data AS $date => $events) {
- foreach ($events AS $event) {
- if ($overdue) {
- $bool = ((strtotime($date) < strtotime($enddate)) AND !$event[5]);
- } else {
- $bool = (strtotime($date) == strtotime($enddate));
- }
- if ($bool) {
- if ($return) {
- $ret[] = array(
- 'subject' => $event[1],
- 'id' => $event[0],
- 'type' => iff($event[7], 'watch', 'event')
- );
- } else {
- $ret++;
- }
- }
- }
- }
- }
- return($ret);
- }
- /* RELATED DATABASE SCHEMA
- calendar_task:
- +------------------+--------------+------+-----+------------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +------------------+--------------+------+-----+------------+----------------+
- | id | int(10) | | PRI | NULL | auto_increment |
- | title | varchar(250) | | | | |
- | description | mediumtext | | | | |
- | techmaker | int(10) | | | 0 | |
- | multistaff | int(1) | | | 0 | |
- | globalcomplete | int(1) | | | 0 | |
- | notifycompletion | int(1) | | | 0 | |
- | repeattype | int(1) | | MUL | 0 | |
- | value1 | int(10) | | | 0 | |
- | value2 | varchar(250) | | | 0 | |
- | starttime | time | | | 00:00:00 | |
- | startdate | date | | MUL | 0000-00-00 | |
- | enddate | date | | MUL | 0000-00-00 | |
- | endtime | time | | | 00:00:00 | |
- +------------------+--------------+------+-----+------------+----------------+
- This table stores basic task data.
- Self-explanatory columns: id, title, description
- techmaker techid of the technician who created the task
- multistaff If non-zero, this task is assigned to more than one person
- (relates to calendar_task_tech table)
- globalcomplete If non-zero, everybody assigned to the task must complete
- it before it is marked "complete", otherwise, once anyone
- marks it complete, it's marked completed for everyone
- notifycompletion If non-zero, notify the creator when the task is marked
- complete
- repeattype Specifies the repeat type if non-zero
- value1, value2 Specifies options for the given repeat type, pointless if
- repeattype is zero.
- starttime The exact time the task was created
- startdate The exact date the task was created
- endtime The exact time the task is due
- enddate The exact date the task is due, or the date the task stops
- repeating if repeattype is non-zero
- calendar_task_iteration:
- +-------------+---------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+---------+------+-----+---------+-------+
- | task_techid | int(11) | | | 0 | |
- | taskid | int(11) | YES | | NULL | |
- | completed | int(11) | YES | | NULL | |
- | date | date | YES | | NULL | |
- | time | time | YES | | NULL | |
- +-------------+---------+------+-----+---------+-------+
- No columns in this table constrain values by uniqueness; this permits the same
- task to be assigned to multiple technicians, and multiple tasks to be assigned
- to the same technician. This table stores *completed* task information;
- incomplete tasks are calculated by DeskPRO internally. When a task is marked
- "complete", a row is added here. If that task is later marked "incomplete", the
- appropriate row is removed from here.
- task_techid The technician *assigned* to this task.
- taskid The taskid of the assigned task.
- completed This iteration of the task is completed if non-zero.
- date The exact date the task is due.
- time The exact date the task is due.
- calendar_task_tech:
- +---------------+---------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +---------------+---------+------+-----+---------+----------------+
- | id | int(10) | | PRI | NULL | auto_increment |
- | eventid | int(10) | | MUL | 0 | |
- | email_due | int(1) | | | 0 | |
- | email_before1 | int(3) | | | 0 | |
- | email_before2 | int(3) | | | 0 | |
- | techid | int(1) | | | 0 | |
- | completed | int(1) | | | 0 | |
- | stamp | int(10) | YES | | 0 | |
- +---------------+---------+------+-----+---------+----------------+
- This table stores technician task assignments, and e-mail reminder options.
- id Unique ID
- eventid The event ID.
- email_due If non-zero, mail a reminder *on* the due date to the tech.
- email_before1 If non-zero, mail a reminder the specified number of days
- before the due date to the tech.
- email_before2 If non-zero, mail a reminder the specified number of days
- before the due date to the tech.
- taskid The taskid of the assigned task.
- completed If non-zero, the task is completed (even if not all
- iterations of a repeating task are completed).
- stamp Timestamp of the completion time/date of this task, if
- stamp and completed are non-zero.
- tech_ticket_watch:
- +-----------+---------+------+-----+------------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-----------+---------+------+-----+------------+----------------+
- | id | int(10) | | PRI | NULL | auto_increment |
- | ticketid | int(10) | | | 0 | |
- | techid | int(10) | | MUL | 0 | |
- | datetodo | date | YES | MUL | 0000-00-00 | |
- | completed | int(1) | | | 0 | |
- | created | int(10) | YES | | 0 | |
- +-----------+---------+------+-----+------------+----------------+
- This table stores ticket watches.
- ticketid Ticket to view.
- techid Technician to be reminded.
- datetodo Date the reminder should appear.
- completed If non-zero, the reminder has been completed.
- created Timestamp of the creation of the reminder.
- */
- ?>