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/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 ++-- 12 files changed, 35 insertions(+), 48 deletions(-) (limited to 'includes/pages') 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] -- cgit v1.2.3-54-g00ecf