📄 calendar_functions.php
字号:
<?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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -