restype = 'schedule';
$this->restypename = 'Schedule';
$this->namefield = 'name';
$this->basecdata['obj'] = $this;
$this->deletable = 1;
$this->deletetoggled = 0;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn getData($args)
///
/// \param $args - unused in this class
///
/// \return array of data as returned from getSchedules
///
/// \brief wrapper for calling getSchedules
///
/////////////////////////////////////////////////////////////////////////////
function getData($args) {
return getSchedules();
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn addEditDialogHTML()
///
/// \param $add - unused for this class
///
/// \brief generates HTML for dialog used to edit resource
///
/////////////////////////////////////////////////////////////////////////////
function addEditDialogHTML($add=0) {
global $user, $days;
# dialog for on page editing
$h = '';
$h .= "
\n";
# name
$errmsg = _("Name cannot contain single (') or double (") quotes, less than (<), or greater than (>) and can be from 2 to 30 characters long");
$h .= labeledFormItem('name', _('Name'), 'text', '^([A-Za-z0-9-!@#$%^&\*\(\)_=\+\[\]{}\\\|:;,\./\?~` ]){2,30}$',
1, '', $errmsg, '', '', '200px');
# owner
$extra = array('onKeyPress' => 'setOwnerChecking');
$h .= labeledFormItem('owner', _('Owner'), 'text', '', 1,
"{$user['unityid']}@{$user['affiliation']}", _('Unknown user'),
'checkOwner', $extra, '200px');
#$h .= labeledFormItem('owner', _('Owner'), 'text', '{$user['unityid']}@{$user['affiliation']}',
# 1, '', _('Unknown user'), 'checkOwner', 'onKeyPress', 'setOwnerChecking');
$cont = addContinuationsEntry('AJvalidateUserid');
$h .= "\n";
# table of times
$h .= " ";
$h .= "
Schedule Times
\n";
$h .= "The start and end day/times are based on a week's time period with ";
$h .= "the start/end point being 'Sunday 12:00 am'. i.e. The earliest ";
$h .= "start day/time is 'Sunday 12:00 am' and the latest end day/";
$h .= "time is 'Sunday 12:00 am'
\n"; # btn div
$h .= "\n"; # groupdlg
return $h;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn checkResourceInUse($rscid)
///
/// \return empty string if not being used; string of where resource is
/// being used if being used
///
/// \brief checks to see if a schedule is being used
///
/////////////////////////////////////////////////////////////////////////////
function checkResourceInUse($rscid) {
$msg = '';
$query = "SELECT hostname "
. "FROM computer "
. "WHERE scheduleid = $rscid AND "
. "deleted = 0";
$qh = doQuery($query);
$comps = array();
while($row = mysql_fetch_assoc($qh))
$comps[] = $row['hostname'];
if(count($comps))
$msg = "This schedule cannot be deleted because the following computers have it selected as their schedule:
\n" . implode(" \n", $comps);
return $msg;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn AJsaveResource()
///
/// \brief saves changes to resource
///
/////////////////////////////////////////////////////////////////////////////
function AJsaveResource() {
$add = getContinuationVar('add', 0);
$data = $this->validateResourceData();
if($data['error']) {
$ret = array('status' => 'error', 'msg' => $data['errormsg']);
sendJSON($ret);
return;
}
if($add) {
if(! $data['rscid'] = $this->addResource($data)) {
sendJSON(array('status' => 'adderror',
'errormsg' => 'Error encountered while trying to create new schedule. Please contact an admin for assistance.'));
return;
}
}
else {
$ownerid = getUserlistID($data['owner']);
$query = "UPDATE schedule "
. "SET name = '{$data['name']}', "
. "ownerid = $ownerid "
. "WHERE id = {$data['rscid']}";
doQuery($query);
}
if(! $add) {
$query = "DELETE FROM scheduletimes WHERE scheduleid = {$data['rscid']}";
doQuery($query, 101);
}
$qvals = array();
foreach($data['times'] as $time)
$qvals[] = "({$data['rscid']}, {$time['start']}, {$time['end']})";
$allvals = implode(',', $qvals);
$query = "INSERT INTO scheduletimes "
. "(scheduleid, start, end) "
. "VALUES $allvals";
doQuery($query, 101);
# clear user resource cache for this type
$key = getKey(array(array($this->restype . "Admin"), array("administer"), 0, 1, 0, 0));
unset($_SESSION['userresources'][$key]);
$key = getKey(array(array($this->restype . "Admin"), array("administer"), 0, 0, 0, 0));
unset($_SESSION['userresources'][$key]);
$key = getKey(array(array($this->restype . "Admin"), array("manageGroup"), 0, 1, 0, 0));
unset($_SESSION['userresources'][$key]);
$key = getKey(array(array($this->restype . "Admin"), array("manageGroup"), 0, 0, 0, 0));
unset($_SESSION['userresources'][$key]);
$tmp = $this->getData(array('includedeleted' => 0, 'rscid' => $data['rscid']));
$data = $tmp[$data['rscid']];
$arr = array('status' => 'success');
if($add) {
$arr['action'] = 'add';
$arr['nogroups'] = 0;
$groups = getUserResources(array($this->restype . 'Admin'), array('manageGroup'), 1);
if(count($groups[$this->restype]))
$arr['groupingHTML'] = $this->groupByResourceHTML();
else
$arr['nogroups'] = 1;
}
else
$arr['action'] = 'edit';
$arr['data'] = $data;
sendJSON($arr);
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn addResource($data)
///
/// \param $data - array of needed data for adding a new resource
///
/// \return id of new resource
///
/// \brief handles adding a new schedule and other associated data to the
/// database
///
/////////////////////////////////////////////////////////////////////////////
function addResource($data) {
global $user;
$ownerid = getUserlistID($data['owner']);
$query = "INSERT INTO schedule "
. "(name, "
. "ownerid) "
. "VALUES ('{$data['name']}', "
. "$ownerid)";
doQuery($query);
$rscid = dbLastInsertID();
if($rscid == 0) {
return 0;
}
$query = "INSERT INTO resource "
. "(resourcetypeid, "
. "subid) "
. "VALUES (15, "
. "$rscid)";
doQuery($query, 223);
return $rscid;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn validateResourceData()
///
/// \return array with these fields:\n
/// \b rscid - id of resource (from schedule table)\n
/// \b name\n
/// \b owner\n
/// \b times - array of arrays, each having 2 keys: start and end, each in
/// unix timestamp format\n
/// \b error - 0 if submitted data validates; 1 if anything is invalid\n
/// \b errormsg - if error = 1; string of error messages separated by html
/// break tags
///
/// \brief validates form input from editing or adding a schedule
///
/////////////////////////////////////////////////////////////////////////////
function validateResourceData() {
global $user;
$return = array('error' => 0);
$errormsg = array();
$return['rscid'] = getContinuationVar('rscid', 0);
$return["name"] = processInputVar("name", ARG_STRING);
$return["owner"] = processInputVar("owner", ARG_STRING, "{$user["unityid"]}@{$user['affiliation']}");
$times = processInputVar('times', ARG_STRING);
if(! preg_match("/^([A-Za-z0-9-!@#$%^&\*\(\)_=\+\[\]{}\\\|:;,\.\/\?~` ]){2,30}$/", $return['name'])) {
$return['error'] = 1;
$errormsg[] = "Name cannot contain single (') or double (") quotes, "
. "less than (<), or greater than (>) and can be from 2 to 30 "
. "characters long";
}
elseif($this->checkForScheduleName($return['name'], $return['rscid'])) {
$return['error'] = 1;
$errormsg[] = "A schedule already exists with this name.";
}
if(! validateUserid($return['owner'])) {
$return['error'] = 1;
$errormsg[] = "Submitted owner is not valid";
}
if(! preg_match('/^([0-9]+:[0-9]+,)*([0-9]+:[0-9]+){1}$/', $times)) {
$return['error'] = 1;
$errormsg[] = "Invalid time data submitted";
}
if(! $return['error']) {
$times = explode(',', $times);
$return['times'] = array();
foreach($times as $pair) {
list($start, $end) = explode(':', $pair);
foreach($return['times'] as $check) {
if($start < $check['end'] && $end > $check['start']) {
$return['error'] = 1;
$errormsg[] = "Two sets of times are overlapping - please correct and save again";
break(2);
}
}
$return['times'][] = array('start' => $start, 'end' => $end);
}
}
if($return['error'])
$return['errormsg'] = implode(' ', $errormsg);
return $return;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn toggleDeleteResourceExtra()
///
/// \return html
///
/// \brief generates additional HTML to be included at the end of the confirm
/// toggle delete resource page
///
/////////////////////////////////////////////////////////////////////////////
function toggleDeleteResourceExtra() {
$rscid = processInputVar('rscid', ARG_NUMERIC);
$data = $this->getData('');
$h = '';
if(count($data[$rscid]["times"])) {
$h .= "
\n";
$h .= "
\n";
$h .= "
Start
\n";
$h .= "
\n";
$h .= "
End
\n";
$h .= "
\n";
foreach($data[$rscid]["times"] as $time) {
$h .= "
\n";
$h .= "
" . $this->minToDaytime($time["start"]) . "
\n";
$h .= "
\n";
$h .= "
" . $this->minToDaytime($time["end"]) . "
\n";
$h .= "
\n";
}
$h .= "
\n";
}
return $h;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn submitToggleDeleteResourceExtra($rscid, $deleted)
///
/// \param $rscid - id of a resource (from schedule table)
/// \param $deleted - (optional, default=0) 1 if resource was previously
/// deleted; 0 if not
///
/// \brief handles deleting associated entries from scheduletimes table
///
/////////////////////////////////////////////////////////////////////////////
function submitToggleDeleteResourceExtra($rscid, $deleted=0) {
$query = "DELETE FROM scheduletimes "
. "WHERE scheduleid = $rscid";
doQuery($query);
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn checkForScheduleName($name, $id)
///
/// \param $name - the name of a schedule
/// \param $id - id of a schedule to ignore
///
/// \return 1 if $name is already in the schedule table, 0 if not
///
/// \brief checks for $name being in the schedule table except for $id
///
/////////////////////////////////////////////////////////////////////////////
function checkForScheduleName($name, $id) {
$query = "SELECT id FROM schedule "
. "WHERE name = '$name'";
if(! empty($id))
$query .= " AND id != $id";
$qh = doQuery($query, 101);
if(mysql_num_rows($qh))
return 1;
return 0;
}
/////////////////////////////////////////////////////////////////////////////
///
/// \fn minToDaytime($min)
///
/// \param $min - minute of the week
///
/// \return day of week and time of day
///
/// \brief calculates day of week and time of day from $min and returns a
/// nicely formatted string of "Weekday HH:MM am/pm"
///
/////////////////////////////////////////////////////////////////////////////
function minToDaytime($min) {
$days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday");
$time = minuteToTime($min % 1440);
if((int)($min / 1440) == 0) {
$day = 0;
}
elseif((int)($min / 1440) == 1) {
$day = 1;
}
elseif((int)($min / 1440) == 2) {
$day = 2;
}
elseif((int)($min / 1440) == 3) {
$day = 3;
}
elseif((int)($min / 1440) == 4) {
$day = 4;
}
elseif((int)($min / 1440) == 5) {
$day = 5;
}
elseif((int)($min / 1440) == 6) {
$day = 6;
}
elseif((int)($min / 1440) > 6) {
$day = 0;
}
return $days[$day] . " $time";
}
}
?>