reduce complexity of shift view

This commit is contained in:
msquare 2016-10-04 18:36:57 +02:00
parent 676f5e1627
commit aa628208ec
2 changed files with 90 additions and 67 deletions

View File

@ -57,7 +57,11 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) {
$SQL .= " $SQL .= "
ORDER BY `start`"; ORDER BY `start`";
return sql_select($SQL); $result = sql_select($SQL);
if ($result === false) {
engelsystem_error("Unable to load shifts by filter.");
}
return $result;
} }
/** /**

View File

@ -29,23 +29,13 @@ function user_shifts() {
} }
/** /**
* Update given ShiftsFilter with filter params from user input * Helper function that updates the start and end time from request data.
* Use update_ShiftsFilter().
* *
* @return the updated ShiftsFilter * @param ShiftsFilter $shiftsFilter
* The shiftfilter to update.
*/ */
function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $days) { function update_ShiftsFilter_timerange(ShiftsFilter $shiftsFilter) {
$shiftsFilter->setUserShiftsAdmin($user_shifts_admin);
if (isset($_REQUEST['filled'])) {
$shiftsFilter->setFilled(check_request_int_array('filled'));
}
if (isset($_REQUEST['rooms'])) {
$shiftsFilter->setRooms(check_request_int_array('rooms'));
}
if (isset($_REQUEST['types'])) {
$shiftsFilter->setTypes(check_request_int_array('types'));
}
if ((isset($_REQUEST['start_time']) && isset($_REQUEST['start_day']) && isset($_REQUEST['end_time']) && isset($_REQUEST['end_day'])) || $shiftsFilter->getStartTime() == null || $shiftsFilter->getEndTime() == null) {
$day = date('Y-m-d', time()); $day = date('Y-m-d', time());
$start_day = in_array($day, $days) ? $day : min($days); $start_day = in_array($day, $days) ? $day : min($days);
if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) {
@ -77,9 +67,67 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da
$shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $start_day . " " . $start_time)); $shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $start_day . " " . $start_time));
$shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $end_day . " " . $end_time)); $shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $end_day . " " . $end_time));
} }
return $shiftsFilter; /**
* Update given ShiftsFilter with filter params from user input
*
* @param ShiftsFilter $shiftsFilter
* The shifts filter to update from request data
* @param boolean $user_shifts_admin
* Has the user user_shift_admin privilege?
* @param string[] $days
* An array of available filter days
*/
function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $days) {
$shiftsFilter->setUserShiftsAdmin($user_shifts_admin);
if (isset($_REQUEST['filled'])) {
$shiftsFilter->setFilled(check_request_int_array('filled'));
}
if (isset($_REQUEST['rooms'])) {
$shiftsFilter->setRooms(check_request_int_array('rooms'));
}
if (isset($_REQUEST['types'])) {
$shiftsFilter->setTypes(check_request_int_array('types'));
}
if ((isset($_REQUEST['start_time']) && isset($_REQUEST['start_day']) && isset($_REQUEST['end_time']) && isset($_REQUEST['end_day'])) || $shiftsFilter->getStartTime() == null || $shiftsFilter->getEndTime() == null) {
update_ShiftsFilter_timerange($shiftsFilter);
}
}
function load_rooms() {
$rooms = sql_select("SELECT `RID` AS `id`, `Name` AS `name` FROM `Room` WHERE `show`='Y' ORDER BY `Name`");
if (count($rooms) == 0) {
error(_("The administration has not configured any rooms yet."));
redirect('?');
}
return $rooms;
}
function load_days() {
$days = sql_select_single_col("
SELECT DISTINCT DATE(FROM_UNIXTIME(`start`)) AS `id`, DATE(FROM_UNIXTIME(`start`)) AS `name`
FROM `Shifts`
ORDER BY `start`");
if (count($days) == 0) {
error(_("The administration has not configured any shifts yet."));
redirect('?');
}
return $days;
}
function load_types() {
global $user;
if (sql_num_query("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0") == 0) {
error(_("The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype."));
redirect('?');
}
$types = sql_select("SELECT `AngelTypes`.`id`, `AngelTypes`.`name`, (`AngelTypes`.`restricted`=0 OR (NOT `UserAngelTypes`.`confirm_user_id` IS NULL OR `UserAngelTypes`.`id` IS NULL)) as `enabled` FROM `AngelTypes` LEFT JOIN `UserAngelTypes` ON (`UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') ORDER BY `AngelTypes`.`name`");
if (empty($types)) {
return sql_select("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0");
}
return $types;
} }
function view_user_shifts() { function view_user_shifts() {
@ -87,35 +135,9 @@ function view_user_shifts() {
global $ical_shifts; global $ical_shifts;
$ical_shifts = []; $ical_shifts = [];
$days = sql_select_single_col(" $days = load_days();
SELECT DISTINCT DATE(FROM_UNIXTIME(`start`)) AS `id`, DATE(FROM_UNIXTIME(`start`)) AS `name` $rooms = load_rooms();
FROM `Shifts` $types = load_types();
ORDER BY `start`");
if (count($days) == 0) {
error(_("The administration has not configured any shifts yet."));
redirect('?');
}
$rooms = sql_select("SELECT `RID` AS `id`, `Name` AS `name` FROM `Room` WHERE `show`='Y' ORDER BY `Name`");
if (count($rooms) == 0) {
error(_("The administration has not configured any rooms yet."));
redirect('?');
}
if (in_array('user_shifts_admin', $privileges)) {
$types = sql_select("SELECT `id`, `name` FROM `AngelTypes` ORDER BY `AngelTypes`.`name`");
} else {
$types = sql_select("SELECT `AngelTypes`.`id`, `AngelTypes`.`name`, (`AngelTypes`.`restricted`=0 OR (NOT `UserAngelTypes`.`confirm_user_id` IS NULL OR `UserAngelTypes`.`id` IS NULL)) as `enabled` FROM `AngelTypes` LEFT JOIN `UserAngelTypes` ON (`UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') ORDER BY `AngelTypes`.`name`");
}
if (empty($types)) {
$types = sql_select("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0");
}
if (count($types) == 0) {
error(_("The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype."));
redirect('?');
}
if (! isset($_SESSION['ShiftsFilter'])) { if (! isset($_SESSION['ShiftsFilter'])) {
$room_ids = array_map('get_ids_from_array', $rooms); $room_ids = array_map('get_ids_from_array', $rooms);
@ -126,9 +148,6 @@ function view_user_shifts() {
$shiftsFilter = $_SESSION['ShiftsFilter']; $shiftsFilter = $_SESSION['ShiftsFilter'];
$shifts = Shifts_by_ShiftsFilter($shiftsFilter, $user); $shifts = Shifts_by_ShiftsFilter($shiftsFilter, $user);
if ($shifts === false) {
engelsystem_error("Unable to load shifts by filter.");
}
$ownshifts_source = sql_select(" $ownshifts_source = sql_select("
SELECT `ShiftTypes`.`name`, `Shifts`.* SELECT `ShiftTypes`.`name`, `Shifts`.*