From a157004f4aefaa5e2265f852f9432becedee1d66 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 23 Jul 2017 11:46:54 +0200 Subject: handle failed db queries in Db class --- includes/controller/event_config_controller.php | 6 +----- includes/controller/shift_entries_controller.php | 5 +---- includes/controller/shifts_controller.php | 5 +---- includes/controller/shifttypes_controller.php | 10 +++------- includes/controller/users_controller.php | 5 +---- 5 files changed, 7 insertions(+), 24 deletions(-) (limited to 'includes/controller') diff --git a/includes/controller/event_config_controller.php b/includes/controller/event_config_controller.php index dcdcf54a..7d11ecb4 100644 --- a/includes/controller/event_config_controller.php +++ b/includes/controller/event_config_controller.php @@ -91,7 +91,7 @@ function event_config_edit_controller() } if ($valid) { - $result = EventConfig_update( + EventConfig_update( $event_name, $buildup_start_date, $event_start_date, @@ -100,10 +100,6 @@ function event_config_edit_controller() $event_welcome_msg ); - if ($result === false) { - engelsystem_error('Unable to update event config.'); - } - engelsystem_log( 'Changed event config: $event_name, $event_welcome_msg, ' . date('Y-m-d', $buildup_start_date) . ', ' . date('Y-m-d', $event_start_date) . ', ' diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 38aad5bb..54c57332 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -130,7 +130,7 @@ function shift_entry_add_controller() } $comment = strip_request_item_nl('comment'); - $result = ShiftEntry_create([ + ShiftEntry_create([ 'SID' => $shift_id, 'TID' => $selected_type_id, 'UID' => $user_id, @@ -138,9 +138,6 @@ function shift_entry_add_controller() 'freeloaded' => $freeloaded, 'freeload_comment' => $freeload_comment ]); - if ($result === false) { - engelsystem_error('Unable to create shift entry.'); - } if ( $type['restricted'] == 0 diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 21c6e160..b3ebd18b 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -135,10 +135,7 @@ function shift_edit_controller() $shift['start'] = $start; $shift['end'] = $end; - $result = Shift_update($shift); - if ($result === false) { - engelsystem_error('Unable to update shift.'); - } + Shift_update($shift); NeededAngelTypes_delete_by_shift($shift_id); $needed_angel_types_info = []; foreach ($needed_angel_types as $type_id => $count) { diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index acdeb982..790bbb56 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -93,17 +93,13 @@ function shifttype_edit_controller() if ($valid) { if ($shifttype_id) { - $result = ShiftType_update($shifttype_id, $name, $angeltype_id, $description); - if ($result === false) { - engelsystem_error('Unable to update shifttype.'); - } + ShiftType_update($shifttype_id, $name, $angeltype_id, $description); + engelsystem_log('Updated shifttype ' . $name); success(_('Updated shifttype.')); } else { $shifttype_id = ShiftType_create($name, $angeltype_id, $description); - if ($shifttype_id === false) { - engelsystem_error('Unable to create shifttype.'); - } + engelsystem_log('Created shifttype ' . $name); success(_('Created shifttype.')); } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index b747cc83..f441c8af 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -166,10 +166,7 @@ function user_edit_vouchers_controller() if ($valid) { $user_source['got_voucher'] = $vouchers; - $result = User_update($user_source); - if ($result === false) { - engelsystem_error('Unable to update user.'); - } + User_update($user_source); success(_('Saved the number of vouchers.')); engelsystem_log(User_Nick_render($user_source) . ': ' . sprintf('Got %s vouchers', -- cgit v1.2.3-54-g00ecf From 5794c4cab8f6ef0529dfc51343b5ec78b134fb2a Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 28 Jul 2017 18:50:00 +0200 Subject: clear delete queries from false resuls --- includes/controller/shift_entries_controller.php | 5 +---- includes/controller/shifttypes_controller.php | 5 +---- includes/controller/user_angeltypes_controller.php | 5 +---- includes/controller/users_controller.php | 5 +---- includes/model/AngelType_model.php | 7 +------ includes/model/NeededAngelTypes_model.php | 6 ++---- includes/model/Room_model.php | 3 +-- includes/model/ShiftEntry_model.php | 3 +-- includes/model/ShiftTypes_model.php | 3 +-- includes/model/Shifts_model.php | 14 +------------- includes/model/UserAngelTypes_model.php | 10 +--------- includes/model/UserDriverLicenses_model.php | 7 +------ includes/model/User_model.php | 3 --- includes/pages/admin_import.php | 5 +---- includes/pages/admin_rooms.php | 4 +--- includes/pages/user_myshifts.php | 6 ++---- src/Database/Db.php | 4 ++-- 17 files changed, 19 insertions(+), 76 deletions(-) (limited to 'includes/controller') diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 54c57332..03d7a27f 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -269,10 +269,7 @@ function shift_entry_delete_controller() redirect(page_link_to('user_shifts')); } - $result = ShiftEntry_delete($entry_id); - if ($result === false) { - engelsystem_error('Unable to delete shift entry.'); - } + ShiftEntry_delete($entry_id); engelsystem_log( 'Deleted ' . User_Nick_render($shift_entry_source) . '\'s shift: ' . $shift_entry_source['name'] diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index 790bbb56..f4791511 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -28,10 +28,7 @@ function shifttype_delete_controller() } if ($request->has('confirmed')) { - $result = ShiftType_delete($shifttype['id']); - if (empty($result)) { - engelsystem_error('Unable to delete shifttype.'); - } + ShiftType_delete($shifttype['id']); engelsystem_log('Deleted shifttype ' . $shifttype['name']); success(sprintf(_('Shifttype %s deleted.'), $shifttype['name'])); diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index 41185552..ab16139a 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -215,10 +215,7 @@ function user_angeltype_delete_controller() } if ($request->has('confirmed')) { - $result = UserAngelType_delete($user_angeltype); - if ($result === false) { - engelsystem_error('Unable to delete user angeltype.'); - } + UserAngelType_delete($user_angeltype); $success_message = sprintf(_('User %s removed from %s.'), User_Nick_render($user_source), $angeltype['name']); engelsystem_log($success_message); diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index f441c8af..05b97716 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -76,10 +76,7 @@ function user_delete_controller() } if ($valid) { - $result = User_delete($user_source['UID']); - if ($result === false) { - engelsystem_error('Unable to delete user.'); - } + User_delete($user_source['UID']); mail_user_delete($user_source); success(_('User deleted.')); diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index af213432..b9238467 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -77,20 +77,15 @@ function AngelType_contact_info($angeltype) * Delete an Angeltype. * * @param array $angeltype - * @return bool */ function AngelType_delete($angeltype) { - $result = DB::delete(' + DB::delete(' DELETE FROM `AngelTypes` WHERE `id`=? LIMIT 1 ', [$angeltype['id']]); - if (is_null($result)) { - engelsystem_error('Unable to delete angeltype.'); - } engelsystem_log('Deleted angeltype: ' . AngelType_name_render($angeltype)); - return true; } /** diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index e77c715f..981c100a 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -35,22 +35,20 @@ function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) * Deletes all needed angel types from given shift. * * @param int $shift_id id of the shift - * @return int count of affected rows */ function NeededAngelTypes_delete_by_shift($shift_id) { - return (int)DB::delete('DELETE FROM `NeededAngelTypes` WHERE `shift_id` = ?', [$shift_id]); + DB::delete('DELETE FROM `NeededAngelTypes` WHERE `shift_id` = ?', [$shift_id]); } /** * Deletes all needed angel types from given room. * * @param int $room_id id of the room - * @return int count of affected rows */ function NeededAngelTypes_delete_by_room($room_id) { - return (int)DB::delete( + DB::delete( 'DELETE FROM `NeededAngelTypes` WHERE `room_id` = ?', [$room_id] ); diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index fdd9dddc..d76e6036 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -17,11 +17,10 @@ function Rooms($show_all = false) * Delete a room * * @param int $room_id - * @return bool */ function Room_delete($room_id) { - return DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [$room_id]); + DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [$room_id]); } /** diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index 3a282efc..bca63774 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -143,13 +143,12 @@ function ShiftEntry($shift_entry_id) * Delete a shift entry. * * @param int $shift_entry_id - * @return bool */ function ShiftEntry_delete($shift_entry_id) { $shift_entry = ShiftEntry($shift_entry_id); mail_shift_removed(User($shift_entry['UID']), Shift($shift_entry['SID'])); - return DB::delete('DELETE FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); + DB::delete('DELETE FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index 12fe38be..d6efe117 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -6,11 +6,10 @@ use Engelsystem\Database\DB; * Delete a shift type. * * @param int $shifttype_id - * @return bool */ function ShiftType_delete($shifttype_id) { - return DB::delete('DELETE FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); + DB::delete('DELETE FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); } /** diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 6e69fe35..2288e3cc 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -402,34 +402,22 @@ function Shift_signup_allowed( * Delete a shift by its external id. * * @param int $shift_psid - * @return bool */ function Shift_delete_by_psid($shift_psid) { DB::delete('DELETE FROM `Shifts` WHERE `PSID`=?', [$shift_psid]); - - if (DB::getStm()->errorCode() != '00000') { - return false; - } - - return true; } /** * Delete a shift. * * @param int $shift_id - * @return bool */ function Shift_delete($shift_id) { mail_shift_delete(Shift($shift_id)); - $result = DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to delete shift.'); - } - return $result; + DB::delete('DELETE FROM `Shifts` WHERE `SID`=?', [$shift_id]); } /** diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index f8277fd9..43bd23bc 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -125,7 +125,6 @@ function UserAngelType_update($user_angeltype_id, $supporter) * Delete all unconfirmed UserAngelTypes for given Angeltype. * * @param int $angeltype_id - * @return bool */ function UserAngelTypes_delete_all($angeltype_id) { @@ -134,12 +133,6 @@ function UserAngelTypes_delete_all($angeltype_id) WHERE `angeltype_id`=? AND `confirm_user_id` IS NULL ', [$angeltype_id]); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to delete all unconfirmed users.'); - } - - return true; } /** @@ -178,11 +171,10 @@ function UserAngelType_confirm($user_angeltype_id, $confirm_user) * Delete an UserAngelType. * * @param array $user_angeltype - * @return bool */ function UserAngelType_delete($user_angeltype) { - return (bool)DB::delete(' + DB::delete(' DELETE FROM `UserAngelTypes` WHERE `id`=? LIMIT 1', [$user_angeltype['id']]); diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index ee93ac95..bc9b6516 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -132,13 +132,8 @@ function UserDriverLicenses_update($user_driver_license) * Delete a user's driver license entry * * @param int $user_id - * @return bool */ function UserDriverLicenses_delete($user_id) { - $result = DB::delete('DELETE FROM `UserDriverLicenses` WHERE `user_id`=?', [$user_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to remove user driver license information'); - } - return $result; + DB::delete('DELETE FROM `UserDriverLicenses` WHERE `user_id`=?', [$user_id]); } diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 9f767b74..adf81393 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -11,13 +11,10 @@ use Engelsystem\ValidationResult; * Delete a user * * @param int $user_id - * @return bool */ function User_delete($user_id) { DB::delete('DELETE FROM `User` WHERE `UID`=?', [$user_id]); - - return DB::getStm()->errorCode() == '00000'; } /** diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index 959a9d2e..506a9dbe 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -279,10 +279,7 @@ function admin_import() } foreach ($events_deleted as $event) { - $result = Shift_delete_by_psid($event['PSID']); - if ($result === false) { - engelsystem_error('Unable to delete shift.'); - } + Shift_delete_by_psid($event['PSID']); } engelsystem_log('Frab import done'); diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index ad8eab83..d49db707 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -209,9 +209,7 @@ function admin_rooms() ]); } elseif ($request->input('show') == 'delete') { if ($request->has('ack')) { - if (!Room_delete($room_id)) { - engelsystem_error('Unable to delete room.'); - } + Room_delete($room_id); engelsystem_log('Room deleted: ' . $name); success(sprintf(_('Room %s deleted.'), $name)); diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index 94116fc9..a10e6f82 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -145,10 +145,8 @@ function user_myshifts() ($shift['start'] > time() + config('last_unsubscribe') * 3600) || in_array('user_shifts_admin', $privileges) ) { - $result = ShiftEntry_delete($user_id); - if ($result === false) { - engelsystem_error('Unable to delete shift entry.'); - } + ShiftEntry_delete($user_id); + $room = Room($shift['RID']); $angeltype = AngelType($shift['TID']); $shifttype = ShiftType($shift['shifttype_id']); diff --git a/src/Database/Db.php b/src/Database/Db.php index 46edc96b..b433f40d 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -113,13 +113,13 @@ class Db * * @param string $query * @param array $bindings - * @return int|bool + * @return int */ public static function delete($query, array $bindings = []) { self::query($query, $bindings); - return (self::$lastStatus ? self::$stm->rowCount() : false); + return self::$stm->rowCount(); } /** -- cgit v1.2.3-54-g00ecf From a0af8d4624be9ca8579817d291036a6727aeb574 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 28 Jul 2017 19:15:52 +0200 Subject: further switching to db exceptions instead of return false --- includes/controller/shifts_controller.php | 3 - includes/controller/shifttypes_controller.php | 3 - includes/controller/users_controller.php | 4 -- includes/model/AngelType_model.php | 22 +------ includes/model/EventConfig_model.php | 4 -- includes/model/NeededAngelTypes_model.php | 6 -- includes/model/Room_model.php | 4 -- includes/model/ShiftEntry_model.php | 13 ++--- includes/model/ShiftTypes_model.php | 15 ++--- includes/model/Shifts_model.php | 58 +++++-------------- includes/model/UserAngelTypes_model.php | 27 +-------- includes/model/UserDriverLicenses_model.php | 7 +-- includes/model/User_model.php | 40 ++----------- includes/pages/admin_import.php | 13 +---- includes/pages/admin_rooms.php | 3 - includes/pages/admin_shifts.php | 6 -- includes/view/ShiftCalendarLane.php | 5 +- includes/view/ShiftCalendarRenderer.php | 82 ++++++++++++++------------- 18 files changed, 79 insertions(+), 236 deletions(-) (limited to 'includes/controller') diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index b3ebd18b..92823b11 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -350,9 +350,6 @@ function shifts_json_export_all_controller() } $shifts_source = Shifts(); - if ($shifts_source === false) { - engelsystem_error('Unable to load shifts.'); - } header('Content-Type: application/json; charset=utf-8'); raw_output(json_encode($shifts_source)); diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index f4791511..9cf292ee 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -143,9 +143,6 @@ function shifttype_controller() function shifttypes_list_controller() { $shifttypes = ShiftTypes(); - if ($shifttypes === false) { - engelsystem_error('Unable to load shifttypes.'); - } return [ shifttypes_title(), diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index 05b97716..832d93f0 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -259,10 +259,6 @@ function users_list_controller() } $users = Users($order_by); - if ($users === false) { - engelsystem_error('Unable to load users.'); - } - foreach ($users as &$user) { $user['freeloads'] = count(ShiftEntries_freeloaded_by_user($user)); } diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index b9238467..f08733d5 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -213,7 +213,7 @@ function AngelType_validate_name($name, $angeltype) */ function AngelTypes_with_user($user) { - $result = DB::select(' + return DB::select(' SELECT `AngelTypes`.*, `UserAngelTypes`.`id` AS `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, @@ -222,11 +222,6 @@ function AngelTypes_with_user($user) LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` AND `UserAngelTypes`.`user_id` = ? ORDER BY `name`', [$user['UID']]); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load angeltypes.'); - } - return $result; } /** @@ -236,15 +231,10 @@ function AngelTypes_with_user($user) */ function AngelTypes() { - $result = DB::select(' + return DB::select(' SELECT * FROM `AngelTypes` ORDER BY `name`'); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load angeltypes.'); - } - return $result; } /** @@ -255,10 +245,6 @@ function AngelTypes() function AngelType_ids() { $result = DB::select('SELECT `id` FROM `AngelTypes`'); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load angeltypes.'); - } return select_array($result, 'id', 'id'); } @@ -275,10 +261,6 @@ function AngelType($angeltype_id) [$angeltype_id] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load angeltype.'); - } - if (empty($angelType_source)) { return null; } diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index b5d3cc73..c5caf4d5 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -10,10 +10,6 @@ use Engelsystem\Database\DB; function EventConfig() { $event_config = DB::select('SELECT * FROM `EventConfig` LIMIT 1'); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load event config.'); - return null; - } if (empty($event_config)) { return null; diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index 981c100a..d8de5e69 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -71,9 +71,6 @@ function NeededAngelTypes_by_shift($shiftId) ORDER BY `room_id` DESC', [$shiftId] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load needed angeltypes.'); - } // Use settings from room if (count($needed_angeltypes_source) == 0) { @@ -86,9 +83,6 @@ function NeededAngelTypes_by_shift($shiftId) AND `count` > 0 ORDER BY `room_id` DESC ', [$shiftId]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load needed angeltypes.'); - } } $shift_entries = ShiftEntries_by_shift($shiftId); diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index d76e6036..08e0f7bf 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -66,10 +66,6 @@ function Room($room_id, $onlyVisible = true) [$room_id] ); - if (DB::getStm()->errorCode() != '00000') { - return false; - } - if (empty($room_source)) { return null; } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index bca63774..563a611f 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -125,17 +125,16 @@ function ShiftEntry_update($shift_entry) * Get a shift entry. * * @param int $shift_entry_id - * @return array|false|null + * @return array|null */ function ShiftEntry($shift_entry_id) { $shift_entry = DB::select('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); - if (DB::getStm()->errorCode() != '00000') { - return false; - } + if (empty($shift_entry)) { return null; } + return $shift_entry[0]; } @@ -209,7 +208,7 @@ function ShiftEntries_finished_by_user($user) */ function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) { - $result = DB::select(' + return DB::select(' SELECT * FROM `ShiftEntry` WHERE `SID` = ? @@ -220,10 +219,6 @@ function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) $angeltype_id, ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load shift entries.'); - } - return $result; } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index d6efe117..05c1a949 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -71,27 +71,20 @@ function ShiftType_create($name, $angeltype_id, $description) function ShiftType($shifttype_id) { $shifttype = DB::select('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load shift type.'); - } + if (empty($shifttype)) { return null; } + return array_shift($shifttype); } /** * Get all shift types. * - * @return array|false + * @return array */ function ShiftTypes() { - $result = DB::select('SELECT * FROM `ShiftTypes` ORDER BY `name`'); - - if (DB::getStm()->errorCode() != '00000') { - return false; - } - - return $result; + return DB::select('SELECT * FROM `ShiftTypes` ORDER BY `name`'); } diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 2288e3cc..939a4f4e 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -10,11 +10,7 @@ use Engelsystem\ShiftSignupState; */ function Shifts_by_room($room) { - $result = DB::select('SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', [$room['RID']]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load shifts.'); - } - return $result; + return DB::select('SELECT * FROM `Shifts` WHERE `RID`=? ORDER BY `start`', [$room['RID']]); } /** @@ -49,7 +45,8 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter) AND NOT `Shifts`.`PSID` IS NULL) AS tmp_shifts ORDER BY `start`'; - $result = DB::select( + + return DB::select( $sql, [ $shiftsFilter->getStartTime(), @@ -58,10 +55,6 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter) $shiftsFilter->getEndTime(), ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load shifts by filter.'); - } - return $result; } /** @@ -100,7 +93,8 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') AND `start` BETWEEN ? AND ? AND NOT `Shifts`.`PSID` IS NULL'; - $result = DB::select( + + return DB::select( $sql, [ $shiftsFilter->getStartTime(), @@ -109,10 +103,6 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) $shiftsFilter->getEndTime(), ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load needed angeltypes by filter.'); - } - return $result; } /** @@ -160,12 +150,11 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) $angeltype['id'] ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load needed angeltypes by filter.'); - } + if (empty($result)) { return null; } + return $result[0]; } @@ -193,17 +182,13 @@ function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter) WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') AND `start` BETWEEN ? AND ? ORDER BY `Shifts`.`start`'; - $result = DB::select( + return DB::select( $sql, [ $shiftsFilter->getStartTime(), $shiftsFilter->getEndTime(), ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load shift entries by filter.'); - } - return $result; } /** @@ -469,12 +454,9 @@ function Shift_update($shift) function Shift_update_by_psid($shift) { $shift_source = DB::select('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]); - if (DB::getStm()->errorCode() != '00000') { - return false; - } if (empty($shift_source)) { - return null; + throw new Exception('Shift not found.'); } $shift['SID'] = $shift_source[0]['SID']; @@ -485,7 +467,7 @@ function Shift_update_by_psid($shift) * Create a new shift. * * @param array $shift - * @return int|false shift id or false + * @return int ID of the new created shift */ function Shift_create($shift) { @@ -516,9 +498,7 @@ function Shift_create($shift) time(), ] ); - if (DB::getStm()->errorCode() != '00000') { - return false; - } + return DB::getPdo()->lastInsertId(); } @@ -531,7 +511,7 @@ function Shift_create($shift) */ function Shifts_by_user($user, $include_freeload_comments = false) { - $result = DB::select(' + return DB::select(' SELECT `ShiftTypes`.`id` AS `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.`id`, `ShiftEntry`.`SID`, `ShiftEntry`.`TID`, `ShiftEntry`.`UID`, `ShiftEntry`.`freeloaded`, `ShiftEntry`.`Comment`, ' . ($include_freeload_comments ? '`ShiftEntry`.`freeload_comment`, ' : '') . ' @@ -547,10 +527,6 @@ function Shifts_by_user($user, $include_freeload_comments = false) $user['UID'] ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load users shifts.'); - } - return $result; } /** @@ -567,10 +543,6 @@ function Shift($shift_id) JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) WHERE `SID`=?', [$shift_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load shift.'); - } - if (empty($shifts_source)) { return null; } @@ -601,7 +573,7 @@ function Shift($shift_id) /** * Returns all shifts with needed angeltypes and count of subscribed jobs. * - * @return array|false + * @return array */ function Shifts() { @@ -612,10 +584,6 @@ function Shifts() JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID` '); - if (DB::getStm()->errorCode() != '00000') { - return false; - } - foreach ($shifts_source as &$shift) { $needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']); $shift['angeltypes'] = $needed_angeltypes; diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 43bd23bc..3ec151fb 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -27,23 +27,16 @@ function UserAngelType_exists($user, $angeltype) * List users angeltypes. * * @param array $user - * @return array|false + * @return array */ function User_angeltypes($user) { - $result = DB::select(' + return DB::select(' SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` FROM `UserAngelTypes` JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id` WHERE `UserAngelTypes`.`user_id`=? ', [$user['UID']]); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user angeltypes.'); - return false; - } - - return $result; } /** @@ -54,7 +47,7 @@ function User_angeltypes($user) */ function User_unconfirmed_AngelTypes($user) { - $result = DB::select(' + return DB::select(' SELECT `UserAngelTypes`.*, `AngelTypes`.`name`, @@ -69,12 +62,6 @@ function User_unconfirmed_AngelTypes($user) GROUP BY `UserAngelTypes`.`angeltype_id` ORDER BY `AngelTypes`.`name` ', [$user['UID']]); - - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user angeltypes.'); - } - - return $result; } /** @@ -216,10 +203,6 @@ function UserAngelType($user_angeltype_id) WHERE `id`=? LIMIT 1', [$user_angeltype_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user angeltype.'); - } - if (empty($angeltype)) { return null; } @@ -249,10 +232,6 @@ function UserAngelType_by_User_and_AngelType($user, $angeltype) ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user angeltype.'); - } - if (empty($angeltype)) { return null; } diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index bc9b6516..515a2701 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -41,7 +41,7 @@ function UserDriverLicense_valid($user_driver_license) * Get a users driver license information * * @param int $user_id The users id - * @return array|false|null + * @return array|null */ function UserDriverLicense($user_id) { @@ -50,11 +50,6 @@ function UserDriverLicense($user_id) FROM `UserDriverLicenses` WHERE `user_id`=?', [$user_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user driver license.'); - return false; - } - if (empty($user_driver_license)) { return null; } diff --git a/includes/model/User_model.php b/includes/model/User_model.php index adf81393..b08569d0 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -184,23 +184,17 @@ function User_sortable_columns() * Get all users, ordered by Nick by default or by given param. * * @param string $order_by - * @return array|false + * @return array */ function Users($order_by = 'Nick') { - $result = DB::select(sprintf(' + return DB::select(sprintf(' SELECT * FROM `User` ORDER BY `%s` ASC ', trim(DB::getPdo()->quote($order_by), '\'') )); - - if (DB::getStm()->errorCode() != '00000') { - return false; - } - - return $result; } /** @@ -224,7 +218,7 @@ function User_is_freeloader($user) */ function Users_by_angeltype_inverted($angeltype) { - $result = DB::select(' + return DB::select(' SELECT `User`.* FROM `User` LEFT JOIN `UserAngelTypes` @@ -236,10 +230,6 @@ function Users_by_angeltype_inverted($angeltype) $angeltype['id'] ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load users.'); - } - return $result; } /** @@ -250,7 +240,7 @@ function Users_by_angeltype_inverted($angeltype) */ function Users_by_angeltype($angeltype) { - $result = DB::select(' + return DB::select(' SELECT `User`.*, `UserAngelTypes`.`id` AS `user_angeltype_id`, @@ -267,10 +257,6 @@ function Users_by_angeltype($angeltype) $angeltype['id'] ] ); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load members.'); - } - return $result; } /** @@ -397,10 +383,6 @@ function User($user_id) { $user_source = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user.'); - } - if (empty($user_source)) { return null; } @@ -413,16 +395,12 @@ function User($user_id) * * @param string $api_key * User api key - * @return array|null Matching user, null on error + * @return array|null Matching user, null if not found */ function User_by_api_key($api_key) { $user = DB::select('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to find user by api key.'); - } - if (empty($user)) { return null; } @@ -440,10 +418,6 @@ function User_by_email($email) { $user = DB::select('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user.'); - } - if (empty($user)) { return null; } @@ -461,10 +435,6 @@ function User_by_password_recovery_token($token) { $user = DB::select('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); - if (DB::getStm()->errorCode() != '00000') { - engelsystem_error('Unable to load user.'); - } - if (empty($user)) { return null; } diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index 506a9dbe..bda5ba93 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -45,9 +45,6 @@ function admin_import() $add_minutes_end = 15; $shifttypes_source = ShiftTypes(); - if ($shifttypes_source === false) { - engelsystem_error('Unable to load shifttypes.'); - } $shifttypes = []; foreach ($shifttypes_source as $shifttype) { $shifttypes[$shifttype['id']] = $shifttype['name']; @@ -265,17 +262,11 @@ function admin_import() $add_minutes_end ); foreach ($events_new as $event) { - $result = Shift_create($event); - if ($result === false) { - engelsystem_error('Unable to create shift.'); - } + Shift_create($event); } foreach ($events_updated as $event) { - $result = Shift_update_by_psid($event); - if ($result === false) { - engelsystem_error('Unable to update shift.'); - } + Shift_update_by_psid($event); } foreach ($events_deleted as $event) { diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index d49db707..5d10304b 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -50,9 +50,6 @@ function admin_rooms() if (test_request_int('id')) { $room = Room($request->input('id'), false); - if ($room === false) { - engelsystem_error('Unable to load room.'); - } if ($room == null) { redirect(page_link_to('admin_rooms')); } diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 5b53f9cd..36028792 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -44,9 +44,6 @@ function admin_shifts() // Load shift types $shifttypes_source = ShiftTypes(); - if ($shifttypes_source === false) { - engelsystem_error('Unable to load shift types.'); - } $shifttypes = []; foreach ($shifttypes_source as $shifttype) { $shifttypes[$shifttype['id']] = $shifttype['name']; @@ -319,9 +316,6 @@ function admin_shifts() $shift['URL'] = null; $shift['PSID'] = null; $shift_id = Shift_create($shift); - if ($shift_id === false) { - engelsystem_error('Unable to create shift.'); - } engelsystem_log( 'Shift created: ' . $shifttypes[$shift['shifttype_id']] diff --git a/includes/view/ShiftCalendarLane.php b/includes/view/ShiftCalendarLane.php index 774683bd..251b8013 100644 --- a/includes/view/ShiftCalendarLane.php +++ b/includes/view/ShiftCalendarLane.php @@ -38,15 +38,14 @@ class ShiftCalendarLane * Returns true on success. * * @param array $shift The shift to add - * @return boolean true on success */ public function addShift($shift) { if ($this->shiftFits($shift)) { $this->shifts[] = $shift; - return true; + return; } - return false; + throw new Exception('Unable to add shift to shift calendar lane.'); } /** diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index aad0d643..df24ad3b 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -1,9 +1,9 @@ $room_id, + 'RID' => $room_id, 'Name' => $shift['room_name'] ]); - if (!isset($lanes[$room_id])) { + if (! isset($lanes[$room_id])) { // initialize room with one lane $lanes[$room_id] = [ new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot()) @@ -92,25 +93,25 @@ class ShiftCalendarRenderer $shift_added = false; foreach ($lanes[$room_id] as $lane) { /** @var ShiftCalendarLane $lane */ - $shift_added = $lane->addShift($shift); - if ($shift_added == true) { + try { + $lane->addShift($shift); + } catch (Exception $e) { break; } } // If all lanes for this room are busy, create a new lane and add shift to it if ($shift_added == false) { $newLane = new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot()); - if (!$newLane->addShift($shift)) { - engelsystem_error('Unable to add shift to new lane.'); - } + $newLane->addShift($shift); $lanes[$room_id][] = $newLane; } } - + return $lanes; } /** + * * @return int */ public function getFirstBlockStartTime() @@ -119,6 +120,7 @@ class ShiftCalendarRenderer } /** + * * @return int */ public function getLastBlockEndTime() @@ -127,6 +129,7 @@ class ShiftCalendarRenderer } /** + * * @return float */ public function getBlocksPerSlot() @@ -148,9 +151,9 @@ class ShiftCalendarRenderer return ''; } return div('shift-calendar', [ - $this->renderTimeLane(), - $this->renderShiftLanes() - ]) . $this->renderLegend(); + $this->renderTimeLane(), + $this->renderShiftLanes() + ]) . $this->renderLegend(); } /** @@ -166,45 +169,41 @@ class ShiftCalendarRenderer $html .= $this->renderLane($lane); } } - + return $html; } /** * Renders a single lane * - * @param ShiftCalendarLane $lane The lane to render + * @param ShiftCalendarLane $lane + * The lane to render * @return string */ private function renderLane(ShiftCalendarLane $lane) { global $user; - + $shift_renderer = new ShiftCalendarShiftRenderer(); $html = ''; $rendered_until = $this->getFirstBlockStartTime(); - + foreach ($lane->getShifts() as $shift) { while ($rendered_until + ShiftCalendarRenderer::SECONDS_PER_ROW <= $shift['start']) { $html .= $this->renderTick($rendered_until); $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; } - - list($shift_height, $shift_html) = $shift_renderer->render( - $shift, - $this->needed_angeltypes[$shift['SID']], - $this->shift_entries[$shift['SID']], - $user - ); + + list ($shift_height, $shift_html) = $shift_renderer->render($shift, $this->needed_angeltypes[$shift['SID']], $this->shift_entries[$shift['SID']], $user); $html .= $shift_html; $rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW; } - + while ($rendered_until < $this->getLastBlockEndTime()) { $html .= $this->renderTick($rendered_until); $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; } - + return div('lane', [ div('header', $lane->getHeader()), $html @@ -214,21 +213,23 @@ class ShiftCalendarRenderer /** * Renders a tick/block for given time * - * @param int $time unix timestamp - * @param boolean $label Should time labels be generated? + * @param int $time + * unix timestamp + * @param boolean $label + * Should time labels be generated? * @return string rendered tick html */ private function renderTick($time, $label = false) { if ($time % (24 * 60 * 60) == 23 * 60 * 60) { - if (!$label) { + if (! $label) { return div('tick day'); } return div('tick day', [ date('m-dH:i', $time) ]); } elseif ($time % (60 * 60) == 0) { - if (!$label) { + if (! $label) { return div('tick hour'); } return div('tick hour', [ @@ -250,7 +251,7 @@ class ShiftCalendarRenderer _('Time') ]) ]; - for ($block = 0; $block < $this->getBlocksPerSlot(); $block++) { + for ($block = 0; $block < $this->getBlocksPerSlot(); $block ++) { $thistime = $this->getFirstBlockStartTime() + ($block * ShiftCalendarRenderer::SECONDS_PER_ROW); $time_slot[] = $this->renderTick($thistime, true); } @@ -258,7 +259,8 @@ class ShiftCalendarRenderer } /** - * @param array[] $shifts + * + * @param array[] $shifts * @return int */ private function calcFirstBlockStartTime($shifts) @@ -273,7 +275,8 @@ class ShiftCalendarRenderer } /** - * @param array[] $shifts + * + * @param array[] $shifts * @return int */ private function calcLastBlockEndTime($shifts) @@ -288,6 +291,7 @@ class ShiftCalendarRenderer } /** + * * @return int */ private function calcBlocksPerSlot() -- cgit v1.2.3-54-g00ecf From 5cffcd7fdf33067c2ecff6b4995b2ebf12b9b5e2 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 28 Jul 2017 19:44:54 +0200 Subject: fix angeltype edit and display bugs --- includes/controller/angeltypes_controller.php | 2 +- includes/model/User_model.php | 1 + includes/view/AngelTypes_view.php | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) (limited to 'includes/controller') diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index 346a4d73..58fadd5c 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -127,7 +127,7 @@ function angeltype_edit_controller() if (!$supporter_mode) { if ($request->has('name')) { - $result = AngelType_validate_name($request->get('name'), $angeltype); + $result = AngelType_validate_name($request->input('name'), $angeltype); $angeltype['name'] = $result->getValue(); if (!$result->isValid()) { $valid = false; diff --git a/includes/model/User_model.php b/includes/model/User_model.php index b08569d0..f86d5bf6 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -246,6 +246,7 @@ function Users_by_angeltype($angeltype) `UserAngelTypes`.`id` AS `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter`, + (`UserDriverLicenses`.`user_id` IS NOT NULL) as `wants_to_drive`, `UserDriverLicenses`.* FROM `User` JOIN `UserAngelTypes` ON `User`.`UID`=`UserAngelTypes`.`user_id` diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index f74b0eb1..37b4fb2c 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -180,7 +180,6 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a $members_unconfirmed = []; foreach ($members as $member) { $member['Nick'] = User_Nick_render($member); - if ($angeltype['requires_driver_license']) { $member['wants_to_drive'] = glyph_bool($member['wants_to_drive']); $member['has_car'] = glyph_bool($member['has_car']); -- cgit v1.2.3-54-g00ecf From f82e5456d22af7e39a22a9a64e74072cf01e0a31 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 28 Jul 2017 20:11:09 +0200 Subject: dried code by introducing selectOne for select queries with only one result line expected --- includes/controller/shift_entries_controller.php | 9 ++--- includes/model/AngelType_model.php | 8 +--- includes/model/EventConfig_model.php | 8 +--- includes/model/Message_model.php | 6 +-- includes/model/Room_model.php | 8 +--- includes/model/ShiftEntry_model.php | 13 ++----- includes/model/ShiftTypes_model.php | 8 +--- includes/model/Shifts_model.php | 18 +++------ includes/model/UserAngelTypes_model.php | 16 +------- includes/model/UserDriverLicenses_model.php | 8 +--- includes/model/User_model.php | 49 +++++------------------- includes/pages/admin_active.php | 9 ++--- includes/pages/admin_groups.php | 6 +-- includes/pages/admin_news.php | 4 +- includes/pages/admin_questions.php | 12 +++--- includes/pages/admin_shifts.php | 4 +- includes/pages/admin_user.php | 16 ++++---- includes/pages/guest_login.php | 5 +-- includes/pages/guest_stats.php | 3 +- includes/pages/user_messages.php | 8 ++-- includes/pages/user_myshifts.php | 9 ++--- includes/pages/user_news.php | 3 +- includes/pages/user_questions.php | 4 +- includes/sys_auth.php | 5 +-- src/Database/Db.php | 17 ++++++++ 25 files changed, 83 insertions(+), 173 deletions(-) (limited to 'includes/controller') diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 03d7a27f..72189869 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -43,7 +43,7 @@ function shift_entry_add_controller() $type = AngelType($type_id); } else { // TODO: Move queries to model - $type = DB::select(' + $type = DB::selectOne(' SELECT * FROM `UserAngelTypes` JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`) @@ -56,7 +56,6 @@ function shift_entry_add_controller() ) ) ', [$type_id, $user['UID']]); - $type = array_shift($type); } if (empty($type)) { @@ -241,7 +240,7 @@ function shift_entry_delete_controller() } $entry_id = $request->input('entry_id'); - $shift_entry_source = DB::select(' + $shift_entry_source = DB::selectOne(' SELECT `User`.`Nick`, `ShiftEntry`.`Comment`, @@ -260,9 +259,7 @@ function shift_entry_delete_controller() WHERE `ShiftEntry`.`id`=?', [$entry_id] ); - if (count($shift_entry_source) > 0) { - $shift_entry_source = array_shift($shift_entry_source); - + if (!empty($shift_entry_source)) { if (!in_array('user_shifts_admin', $privileges) && (!in_array('shiftentry_edit_angeltype_supporter', $privileges) || !User_is_AngelType_supporter($user, AngelType($shift_entry_source['angeltype_id']))) ) { diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index f08733d5..bc535667 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -256,14 +256,8 @@ function AngelType_ids() */ function AngelType($angeltype_id) { - $angelType_source = DB::select( + return DB::selectOne( 'SELECT * FROM `AngelTypes` WHERE `id`=?', [$angeltype_id] ); - - if (empty($angelType_source)) { - return null; - } - - return array_shift($angelType_source); } diff --git a/includes/model/EventConfig_model.php b/includes/model/EventConfig_model.php index c5caf4d5..646d19c5 100644 --- a/includes/model/EventConfig_model.php +++ b/includes/model/EventConfig_model.php @@ -9,13 +9,7 @@ use Engelsystem\Database\DB; */ function EventConfig() { - $event_config = DB::select('SELECT * FROM `EventConfig` LIMIT 1'); - - if (empty($event_config)) { - return null; - } - - return array_shift($event_config); + return DB::selectOne('SELECT * FROM `EventConfig` LIMIT 1'); } /** diff --git a/includes/model/Message_model.php b/includes/model/Message_model.php index ebd4b37e..9bb037af 100644 --- a/includes/model/Message_model.php +++ b/includes/model/Message_model.php @@ -20,11 +20,7 @@ function Message_ids() */ function Message($message_id) { - $message_source = DB::select('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); - if (empty($message_source)) { - return null; - } - return array_shift($message_source); + return DB::selectOne('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); } /** diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index 08e0f7bf..8425e5ad 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -58,17 +58,11 @@ function Room_create($name, $from_frab, $public, $number = null) */ function Room($room_id, $onlyVisible = true) { - $room_source = DB::select(' + return DB::selectOne(' SELECT * FROM `Room` WHERE `RID` = ? ' . ($onlyVisible ? 'AND `show` = \'Y\'' : ''), [$room_id] ); - - if (empty($room_source)) { - return null; - } - - return array_shift($room_source); } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index 563a611f..3a7254ad 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -28,10 +28,9 @@ function ShiftEntry_new() */ function ShiftEntries_freeleaded_count() { - $result = DB::select('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1'); - if (!is_array($result)) { + if (empty($result)) { return 0; } @@ -129,13 +128,7 @@ function ShiftEntry_update($shift_entry) */ function ShiftEntry($shift_entry_id) { - $shift_entry = DB::select('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); - - if (empty($shift_entry)) { - return null; - } - - return $shift_entry[0]; + return DB::selectOne('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index 05c1a949..227df367 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -70,13 +70,7 @@ function ShiftType_create($name, $angeltype_id, $description) */ function ShiftType($shifttype_id) { - $shifttype = DB::select('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); - - if (empty($shifttype)) { - return null; - } - - return array_shift($shifttype); + return DB::selectOne('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); } /** diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 939a4f4e..88b28998 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -112,7 +112,7 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) */ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) { - $result = DB::select(' + return DB::selectOne(' SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, @@ -150,12 +150,6 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) $angeltype['id'] ] ); - - if (empty($result)) { - return null; - } - - return $result[0]; } /** @@ -453,13 +447,13 @@ function Shift_update($shift) */ function Shift_update_by_psid($shift) { - $shift_source = DB::select('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]); + $shift_source = DB::selectOne('SELECT `SID` FROM `Shifts` WHERE `PSID`=?', [$shift['PSID']]); if (empty($shift_source)) { throw new Exception('Shift not found.'); } - $shift['SID'] = $shift_source[0]['SID']; + $shift['SID'] = $shift_source['SID']; return Shift_update($shift); } @@ -537,18 +531,16 @@ function Shifts_by_user($user, $include_freeload_comments = false) */ function Shift($shift_id) { - $shifts_source = DB::select(' + $result = DB::selectOne(' SELECT `Shifts`.*, `ShiftTypes`.`name` FROM `Shifts` JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) WHERE `SID`=?', [$shift_id]); - if (empty($shifts_source)) { + if (empty($result)) { return null; } - $result = $shifts_source[0]; - $shiftsEntry_source = DB::select(' SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 3ec151fb..5b0caf98 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -197,17 +197,11 @@ function UserAngelType_create($user, $angeltype) */ function UserAngelType($user_angeltype_id) { - $angeltype = DB::select(' + return DB::selectOne(' SELECT * FROM `UserAngelTypes` WHERE `id`=? LIMIT 1', [$user_angeltype_id]); - - if (empty($angeltype)) { - return null; - } - - return $angeltype[0]; } /** @@ -219,7 +213,7 @@ function UserAngelType($user_angeltype_id) */ function UserAngelType_by_User_and_AngelType($user, $angeltype) { - $angeltype = DB::select(' + return DB::selectOne(' SELECT * FROM `UserAngelTypes` WHERE `user_id`=? @@ -231,10 +225,4 @@ function UserAngelType_by_User_and_AngelType($user, $angeltype) $angeltype['id'] ] ); - - if (empty($angeltype)) { - return null; - } - - return array_shift($angeltype); } diff --git a/includes/model/UserDriverLicenses_model.php b/includes/model/UserDriverLicenses_model.php index 515a2701..798aa6ab 100644 --- a/includes/model/UserDriverLicenses_model.php +++ b/includes/model/UserDriverLicenses_model.php @@ -45,16 +45,10 @@ function UserDriverLicense_valid($user_driver_license) */ function UserDriverLicense($user_id) { - $user_driver_license = DB::select(' + return DB::selectOne(' SELECT * FROM `UserDriverLicenses` WHERE `user_id`=?', [$user_id]); - - if (empty($user_driver_license)) { - return null; - } - - return array_shift($user_driver_license); } /** diff --git a/includes/model/User_model.php b/includes/model/User_model.php index f86d5bf6..4757ed3c 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -87,8 +87,7 @@ function User_update($user) */ function User_force_active_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1'); if (empty($result)) { return 0; @@ -102,8 +101,7 @@ function User_force_active_count() */ function User_active_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1'); if (empty($result)) { return 0; @@ -117,8 +115,7 @@ function User_active_count() */ function User_got_voucher_count() { - $result = DB::select('SELECT SUM(`got_voucher`) FROM `User`'); - $result = array_shift($result); + $result = DB::selectOne('SELECT SUM(`got_voucher`) FROM `User`'); if (empty($result)) { return 0; @@ -132,8 +129,7 @@ function User_got_voucher_count() */ function User_arrived_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1'); if (empty($result)) { return 0; @@ -147,8 +143,7 @@ function User_arrived_count() */ function User_tshirts_count() { - $result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1'); - $result = array_shift($result); + $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1'); if (empty($result)) { return 0; @@ -382,13 +377,7 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de */ function User($user_id) { - $user_source = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); - - if (empty($user_source)) { - return null; - } - - return array_shift($user_source); + return DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); } /** @@ -400,13 +389,7 @@ function User($user_id) */ function User_by_api_key($api_key) { - $user = DB::select('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); - - if (empty($user)) { - return null; - } - - return $user[0]; + return DB::selectOne('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); } /** @@ -417,30 +400,18 @@ function User_by_api_key($api_key) */ function User_by_email($email) { - $user = DB::select('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); - - if (empty($user)) { - return null; - } - - return array_shift($user); + return DB::selectOne('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); } /** * Returns User by password token. * * @param string $token - * @return array|null Matching user, null or false on error + * @return array|null Matching user, null when not found */ function User_by_password_recovery_token($token) { - $user = DB::select('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); - - if (empty($user)) { - return null; - } - - return array_shift($user); + return DB::selectOne('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); } /** diff --git a/includes/pages/admin_active.php b/includes/pages/admin_active.php index 2e06f90d..be1217ff 100644 --- a/includes/pages/admin_active.php +++ b/includes/pages/admin_active.php @@ -210,19 +210,17 @@ function admin_active() $shirt_statistics = []; foreach (array_keys($tshirt_sizes) as $size) { if (!empty($size)) { - $sc = DB::select( + $sc = DB::selectOne( 'SELECT count(*) FROM `User` WHERE `Size`=? AND `Gekommen`=1', [$size] ); $sc = array_shift($sc); - $sc = array_shift($sc); - $gc = DB::select( + $gc = DB::selectOne( 'SELECT count(*) FROM `User` WHERE `Size`=? AND `Tshirt`=1', [$size] ); $gc = array_shift($gc); - $gc = array_shift($gc); $shirt_statistics[] = [ 'size' => $size, @@ -232,8 +230,7 @@ function admin_active() } } - $uc = DB::select('SELECT count(*) FROM `User` WHERE `Tshirt`=1'); - $uc = array_shift($uc); + $uc = DB::selectOne('SELECT count(*) FROM `User` WHERE `Tshirt`=1'); $uc = array_shift($uc); $shirt_statistics[] = [ diff --git a/includes/pages/admin_groups.php b/includes/pages/admin_groups.php index c483a79d..ea0d4dbc 100644 --- a/includes/pages/admin_groups.php +++ b/includes/pages/admin_groups.php @@ -107,23 +107,21 @@ function admin_groups() return error('Incomplete call, missing Groups ID.', true); } - $group = DB::select('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]); + $group = DB::selectOne('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]); $privileges = $request->get('privileges'); if (!is_array($privileges)) { $privileges = []; } if (!empty($group)) { - $group = array_shift($group); DB::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]); $privilege_names = []; foreach ($privileges as $privilege) { if (preg_match('/^\d{1,}$/', $privilege)) { - $group_privileges_source = DB::select( + $group_privileges_source = DB::selectOne( 'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1', [$privilege] ); if (!empty($group_privileges_source)) { - $group_privileges_source = array_shift($group_privileges_source); DB::insert( 'INSERT INTO `GroupPrivileges` (`group_id`, `privilege_id`) VALUES (?, ?)', [$group_id, $privilege] diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php index 7f8ca1ba..4eafd3e2 100644 --- a/includes/pages/admin_news.php +++ b/includes/pages/admin_news.php @@ -21,14 +21,13 @@ function admin_news() return error('Incomplete call, missing News ID.', true); } - $news = DB::select('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); + $news = DB::selectOne('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); if (empty($news)) { return error('No News found.', true); } switch ($request->input('action')) { case 'edit': - $news = array_shift($news); $user_source = User($news['UID']); $html .= form([ @@ -70,7 +69,6 @@ function admin_news() break; case 'delete': - $news = array_shift($news); DB::delete('DELETE FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); engelsystem_log('News deleted: ' . $news['Betreff']); success(_('News entry deleted.')); diff --git a/includes/pages/admin_questions.php b/includes/pages/admin_questions.php index d05bace6..2b61b055 100644 --- a/includes/pages/admin_questions.php +++ b/includes/pages/admin_questions.php @@ -105,11 +105,11 @@ function admin_questions() return error('Incomplete call, missing Question ID.', true); } - $question = DB::select( + $question = DB::selectOne( 'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id] ); - if (count($question) > 0 && $question[0]['AID'] == null) { + if (!empty($question) && $question['AID'] == null) { $answer = trim( preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', @@ -129,7 +129,7 @@ function admin_questions() $question_id, ] ); - engelsystem_log('Question ' . $question[0]['Question'] . ' answered: ' . $answer); + engelsystem_log('Question ' . $question['Question'] . ' answered: ' . $answer); redirect(page_link_to('admin_questions')); } else { return error('Enter an answer!', true); @@ -145,13 +145,13 @@ function admin_questions() return error('Incomplete call, missing Question ID.', true); } - $question = DB::select( + $question = DB::selectOne( 'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id] ); - if (count($question) > 0) { + if (!empty($question)) { DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]); - engelsystem_log('Question deleted: ' . $question[0]['Question']); + engelsystem_log('Question deleted: ' . $question['Question']); redirect(page_link_to('admin_questions')); } else { return error('No question found.', true); diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 36028792..b5079ed1 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -325,7 +325,7 @@ function admin_shifts() ); foreach ($_SESSION['admin_shifts_types'] as $type_id => $count) { - $angel_type_source = DB::select(' + $angel_type_source = DB::selectOne(' SELECT * FROM `AngelTypes` WHERE `id` = ? @@ -341,7 +341,7 @@ function admin_shifts() $count ] ); - $needed_angel_types_info[] = $angel_type_source[0]['name'] . ': ' . $count; + $needed_angel_types_info[] = $angel_type_source['name'] . ': ' . $count; } } } diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 510e2292..6bdc8d71 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -116,20 +116,20 @@ function admin_user() $html .= '
'; - $my_highest_group = DB::select( + $my_highest_group = DB::selectOne( 'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', [$user['UID']] ); - if (count($my_highest_group) > 0) { - $my_highest_group = $my_highest_group[0]['group_id']; + if (!empty($my_highest_group)) { + $my_highest_group = $my_highest_group['group_id']; } - $his_highest_group = DB::select( + $his_highest_group = DB::selectOne( 'SELECT `group_id` FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', [$user_id] ); - if (count($his_highest_group) > 0) { - $his_highest_group = $his_highest_group[0]['group_id']; + if (!empty($his_highest_group)) { + $his_highest_group = $his_highest_group['group_id']; } if ($user_id != $user['UID'] && $my_highest_group <= $his_highest_group) { @@ -188,7 +188,7 @@ function admin_user() count($my_highest_group) > 0 && ( count($his_highest_group) == 0 - || ($my_highest_group[0]['group_id'] <= $his_highest_group[0]['group_id']) + || ($my_highest_group['group_id'] <= $his_highest_group['group_id']) ) ) { $groups_source = DB::select(' @@ -203,7 +203,7 @@ function admin_user() ', [ $user_id, - $my_highest_group[0]['group_id'], + $my_highest_group['group_id'], ] ); $groups = []; diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index 106db33a..f8c52767 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -399,9 +399,8 @@ function guest_login() if ($request->has('submit')) { if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 0) { $nick = User_validate_Nick($request->input('nick')); - $login_user = DB::select('SELECT * FROM `User` WHERE `Nick`=?', [$nick]); - if (count($login_user) > 0) { - $login_user = $login_user[0]; + $login_user = DB::selectOne('SELECT * FROM `User` WHERE `Nick`=?', [$nick]); + if (!empty($login_user)) { if ($request->has('password')) { if (!verify_password($request->post('password'), $login_user['Passwort'], $login_user['UID'])) { $valid = false; diff --git a/includes/pages/guest_stats.php b/includes/pages/guest_stats.php index 8aa6f740..bf1814a3 100644 --- a/includes/pages/guest_stats.php +++ b/includes/pages/guest_stats.php @@ -17,13 +17,12 @@ function guest_stats() list($arrived_user_count) = DB::select('SELECT count(*) AS `user_count` FROM `User` WHERE `Gekommen`=1'); $stats['arrived_user_count'] = $arrived_user_count['user_count']; - $done_shifts_seconds = DB::select(' + $done_shifts_seconds = DB::selectOne(' SELECT SUM(`Shifts`.`end` - `Shifts`.`start`) FROM `ShiftEntry` JOIN `Shifts` USING (`SID`) WHERE `Shifts`.`end` < UNIX_TIMESTAMP() '); - $done_shifts_seconds = array_shift($done_shifts_seconds); $done_shifts_seconds = (int)array_shift($done_shifts_seconds); $stats['done_work_hours'] = round($done_shifts_seconds / (60 * 60), 0); diff --git a/includes/pages/user_messages.php b/includes/pages/user_messages.php index a811970d..2dea6207 100644 --- a/includes/pages/user_messages.php +++ b/includes/pages/user_messages.php @@ -130,11 +130,11 @@ function user_messages() return error(_('Incomplete call, missing Message ID.'), true); } - $message = DB::select( + $message = DB::selectOne( 'SELECT `RUID` FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id] ); - if (count($message) > 0 && $message[0]['RUID'] == $user['UID']) { + if (!empty($message) && $message['RUID'] == $user['UID']) { DB::update( 'UPDATE `Messages` SET `isRead`=\'Y\' WHERE `id`=? LIMIT 1', [$message_id] @@ -152,11 +152,11 @@ function user_messages() return error(_('Incomplete call, missing Message ID.'), true); } - $message = DB::select( + $message = DB::selectOne( 'SELECT `SUID` FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id] ); - if (count($message) > 0 && $message[0]['SUID'] == $user['UID']) { + if (!empty($message) && $message['SUID'] == $user['UID']) { DB::delete('DELETE FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); redirect(page_link_to('user_messages')); } else { diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index a10e6f82..81f8f505 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -31,8 +31,7 @@ function user_myshifts() $user_id = $user['UID']; } - $shifts_user = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); - $shifts_user = array_shift($shifts_user); + $shifts_user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); if ($request->has('reset')) { if ($request->input('reset') == 'ack') { @@ -49,7 +48,7 @@ function user_myshifts() ]); } elseif ($request->has('edit') && preg_match('/^\d*$/', $request->input('edit'))) { $user_id = $request->input('edit'); - $shift = DB::select(' + $shift = DB::selectOne(' SELECT `ShiftEntry`.`freeloaded`, `ShiftEntry`.`freeload_comment`, @@ -74,7 +73,6 @@ function user_myshifts() ] ); if (count($shift) > 0) { - $shift = array_shift($shift); $freeloaded = $shift['freeloaded']; $freeload_comment = $shift['freeload_comment']; @@ -128,7 +126,7 @@ function user_myshifts() } } elseif ($request->has('cancel') && preg_match('/^\d*$/', $request->input('cancel'))) { $user_id = $request->input('cancel'); - $shift = DB::select(' + $shift = DB::selectOne(' SELECT * FROM `Shifts` INNER JOIN `ShiftEntry` USING (`SID`) @@ -140,7 +138,6 @@ function user_myshifts() ] ); if (count($shift) > 0) { - $shift = array_shift($shift); if ( ($shift['start'] > time() + config('last_unsubscribe') * 3600) || in_array('user_shifts_admin', $privileges) diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php index 9bdcb6fb..3cf11a6b 100644 --- a/includes/pages/user_news.php +++ b/includes/pages/user_news.php @@ -126,8 +126,7 @@ function user_news_comments() && count(DB::select('SELECT `ID` FROM `News` WHERE `ID`=? LIMIT 1', [$request->input('nid')])) > 0 ) { $nid = $request->input('nid'); - $news = DB::select('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$nid]); - $news = array_shift($news); + $news = DB::selectOne('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$nid]); if ($request->has('text')) { $text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($request->input('text'))); DB::insert(' diff --git a/includes/pages/user_questions.php b/includes/pages/user_questions.php index e90ea011..e4f35577 100644 --- a/includes/pages/user_questions.php +++ b/includes/pages/user_questions.php @@ -61,11 +61,11 @@ function user_questions() return error(_('Incomplete call, missing Question ID.'), true); } - $question = DB::select( + $question = DB::selectOne( 'SELECT `UID` FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id] ); - if (count($question) > 0 && $question[0]['UID'] == $user['UID']) { + if (!empty($question) && $question['UID'] == $user['UID']) { DB::delete( 'DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id] diff --git a/includes/sys_auth.php b/includes/sys_auth.php index f1ec3192..e0ed67e5 100644 --- a/includes/sys_auth.php +++ b/includes/sys_auth.php @@ -11,10 +11,9 @@ function load_auth() $user = null; if (isset($_SESSION['uid'])) { - $user = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]); - if (count($user) > 0) { + $user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]); + if (!empty($user)) { // User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten - $user = array_shift($user); DB::update(' UPDATE `User` SET `lastLogIn` = ? diff --git a/src/Database/Db.php b/src/Database/Db.php index b433f40d..9422ae77 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -79,6 +79,23 @@ class Db return self::$stm->fetchAll(PDO::FETCH_ASSOC); } + + /** + * Run a select query and return only the first result or null if no result is found. + * @param string $query + * @param array $bindings + * @return array|null + */ + public static function selectOne($query, array $bindings = []) + { + $result = self::select($query); + + if(empty($result)) { + return null; + } + + return array_shift($result); + } /** * Run an insert query -- cgit v1.2.3-54-g00ecf From 7f4aa6daa5a8ce72055f2328cf8c17eb8a691b79 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 28 Jul 2017 20:26:12 +0200 Subject: remove exit commands after redirects (redirect does already die) --- includes/controller/rooms_controller.php | 3 --- includes/controller/shifts_controller.php | 1 - includes/controller/user_angeltypes_controller.php | 1 - 3 files changed, 5 deletions(-) (limited to 'includes/controller') diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php index 2d6f1a77..6d0864ae 100644 --- a/includes/controller/rooms_controller.php +++ b/includes/controller/rooms_controller.php @@ -79,10 +79,7 @@ function rooms_controller() case 'list': default: redirect(page_link_to('admin_rooms')); - break; } - - exit; } /** diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 92823b11..96f12baa 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -326,7 +326,6 @@ function shift_next_controller() } redirect(page_link_to('user_shifts')); - exit; } /** diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index ab16139a..f212716d 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -419,6 +419,5 @@ function user_angeltypes_controller() return user_angeltype_add_controller(); default: redirect(page_link_to('angeltypes')); - exit; } } -- cgit v1.2.3-54-g00ecf