2013-12-09 17:10:07 +01:00
|
|
|
<?php
|
2014-12-07 17:07:19 +01:00
|
|
|
|
2014-12-07 17:48:35 +01:00
|
|
|
/**
|
|
|
|
* Delete a shift by its external id.
|
|
|
|
*/
|
|
|
|
function Shift_delete_by_psid($shift_psid) {
|
|
|
|
return sql_query("DELETE FROM `Shifts` WHERE `PSID`=" . sql_escape($shift_psid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a shift.
|
|
|
|
*/
|
|
|
|
function Shift_delete($shift_id) {
|
2014-12-07 19:44:59 +01:00
|
|
|
|
|
|
|
mail_shift_delete(Shift($shift_id));
|
|
|
|
|
2014-12-07 17:48:35 +01:00
|
|
|
return sql_query("DELETE FROM `Shifts` WHERE `SID`=" . sql_escape($shift_id));
|
|
|
|
}
|
|
|
|
|
2014-12-07 17:41:40 +01:00
|
|
|
/**
|
|
|
|
* Update a shift.
|
|
|
|
*/
|
|
|
|
function Shift_update($shift) {
|
2014-12-07 19:44:59 +01:00
|
|
|
|
|
|
|
$old_shift = Shift($shift['SID']);
|
|
|
|
mail_shift_change(Shift($shift['SID']), $shift);
|
|
|
|
|
2014-12-07 17:41:40 +01:00
|
|
|
return sql_query("UPDATE `Shifts` SET
|
|
|
|
`start`=" . sql_escape($shift['start']) . ",
|
|
|
|
`end`=" . sql_escape($shift['end']) . ",
|
|
|
|
`RID`=" . sql_escape($shift['RID']) . ",
|
|
|
|
`name`=" . sql_null($shift['name']) . ",
|
|
|
|
`URL`=" . sql_null($shift['URL']) . ",
|
|
|
|
`PSID`=" . sql_null($shift['PSID']) . "
|
|
|
|
WHERE `SID`=" . sql_escape($shift['SID']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a shift by its external id.
|
|
|
|
*/
|
|
|
|
function Shift_update_by_psid($shift) {
|
|
|
|
return sql_query("UPDATE `Shifts` SET
|
|
|
|
`start`=" . sql_escape($shift['start']) . ",
|
|
|
|
`end`=" . sql_escape($shift['end']) . ",
|
|
|
|
`RID`=" . sql_escape($shift['RID']) . ",
|
|
|
|
`name`=" . sql_null($shift['name']) . ",
|
|
|
|
`URL`=" . sql_null($shift['URL']) . "
|
|
|
|
WHERE `PSID`=" . sql_escape($shift['PSID']));
|
|
|
|
}
|
|
|
|
|
2014-12-07 17:34:29 +01:00
|
|
|
/**
|
|
|
|
* Create a new shift.
|
2014-12-07 17:41:40 +01:00
|
|
|
*
|
2014-12-07 17:34:29 +01:00
|
|
|
* @return new shift id or false
|
|
|
|
*/
|
|
|
|
function Shift_create($shift) {
|
|
|
|
$result = sql_query("INSERT INTO `Shifts` SET
|
|
|
|
`start`=" . sql_escape($shift['start']) . ",
|
|
|
|
`end`=" . sql_escape($shift['end']) . ",
|
|
|
|
`RID`=" . sql_escape($shift['RID']) . ",
|
|
|
|
`name`=" . sql_null($shift['name']) . ",
|
|
|
|
`URL`=" . sql_null($shift['URL']) . ",
|
|
|
|
`PSID`=" . sql_null($shift['PSID']));
|
|
|
|
if ($result === false)
|
|
|
|
return false;
|
|
|
|
return sql_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return users shifts.
|
|
|
|
*/
|
2014-08-23 01:55:18 +02:00
|
|
|
function Shifts_by_user($user) {
|
|
|
|
return sql_select("
|
|
|
|
SELECT *
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
|
|
|
|
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
|
|
|
|
WHERE `UID`=" . sql_escape($user['UID']) . "
|
|
|
|
ORDER BY `start`
|
|
|
|
");
|
|
|
|
}
|
2013-12-09 17:10:07 +01:00
|
|
|
|
2013-12-29 15:08:21 +01:00
|
|
|
/**
|
2014-01-07 15:50:16 +01:00
|
|
|
* TODO: $_REQUEST is not allowed in model!
|
2013-12-29 15:08:21 +01:00
|
|
|
* Returns Shift id array
|
|
|
|
*/
|
2014-01-07 15:50:16 +01:00
|
|
|
function Shifts_filtered() {
|
2014-08-23 01:55:18 +02:00
|
|
|
global $_REQUEST;
|
|
|
|
$filter = "";
|
|
|
|
|
|
|
|
// filterRoom (Array of integer) - Array of Room IDs (optional, for list request)
|
|
|
|
if (isset($_REQUEST['filterRoom']) && is_array($_REQUEST['filterRoom'])) {
|
|
|
|
foreach ($_REQUEST['filterRoom'] as $key => $value) {
|
|
|
|
$filter .= ", `RID`=" . sql_escape($value) . " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterTask (Array of integer) - Array if Task (optional, for list request)
|
|
|
|
if (isset($_REQUEST['filterTask']) && is_array($_REQUEST['filterTask'])) {
|
|
|
|
foreach ($_REQUEST['filterTask'] as $key => $value) {
|
|
|
|
// TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterOccupancy (integer) - Occupancy state: (optional, for list request)
|
|
|
|
// 1 occupied, 2 free, 3 occupied and free
|
|
|
|
if (isset($_REQUEST['filterOccupancy']) && is_array($_REQUEST['filterOccupancy'])) {
|
|
|
|
foreach ($_REQUEST['filterOccupancy'] as $key => $value) {
|
|
|
|
// TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// format filter
|
|
|
|
if ($filter != "") {
|
|
|
|
$filter = ' WHERE ' . substr($filter, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// real request
|
|
|
|
$shifts_source = sql_select("SELECT `SID` FROM `Shifts`" . $filter);
|
|
|
|
if ($shifts_source === false)
|
|
|
|
return false;
|
|
|
|
if (count($shifts_source) > 0) {
|
|
|
|
return $shifts_source;
|
|
|
|
}
|
|
|
|
return null;
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns Shift by id.
|
|
|
|
*
|
2014-08-23 01:55:18 +02:00
|
|
|
* @param $id Shift
|
|
|
|
* ID
|
2013-12-29 15:08:21 +01:00
|
|
|
*/
|
2014-01-07 15:50:16 +01:00
|
|
|
function Shift($id) {
|
2014-08-23 01:55:18 +02:00
|
|
|
$shifts_source = sql_select("SELECT * FROM `Shifts` WHERE `SID`=" . sql_escape($id) . " LIMIT 1");
|
|
|
|
$shiftsEntry_source = sql_select("SELECT `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`=" . sql_escape($id));
|
|
|
|
|
|
|
|
if ($shifts_source === false)
|
|
|
|
return false;
|
|
|
|
if (count($shifts_source) > 0) {
|
|
|
|
$result = $shifts_source[0];
|
|
|
|
|
|
|
|
$result['ShiftEntry'] = $shiftsEntry_source;
|
|
|
|
|
|
|
|
$temp = NeededAngelTypes_by_shift($id);
|
|
|
|
foreach ($temp as $e) {
|
|
|
|
$result['NeedAngels'][] = array(
|
|
|
|
'TID' => $e['angel_type_id'],
|
|
|
|
'count' => $e['count'],
|
|
|
|
'restricted' => $e['restricted'],
|
|
|
|
'taken' => $e['taken']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
return null;
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
2013-12-09 17:10:07 +01:00
|
|
|
/**
|
|
|
|
* Returns all shifts with needed angeltypes and count of subscribed jobs.
|
|
|
|
*/
|
|
|
|
function Shifts() {
|
|
|
|
$shifts_source = sql_select("
|
|
|
|
SELECT `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID`
|
|
|
|
");
|
|
|
|
if ($shifts_source === false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach ($shifts_source as &$shift) {
|
2014-01-05 19:33:52 +01:00
|
|
|
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
|
2013-12-09 17:10:07 +01:00
|
|
|
if ($needed_angeltypes === false)
|
|
|
|
return false;
|
2014-08-23 01:55:18 +02:00
|
|
|
|
2013-12-09 17:10:07 +01:00
|
|
|
$shift['angeltypes'] = $needed_angeltypes;
|
|
|
|
}
|
2013-12-09 17:17:23 +01:00
|
|
|
|
|
|
|
return $shifts_source;
|
2013-12-09 17:10:07 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 19:44:59 +01:00
|
|
|
?>
|