dried code by introducing selectOne for select queries with only one result line expected

This commit is contained in:
msquare 2017-07-28 20:11:09 +02:00
parent 69a1ee2bfe
commit f82e5456d2
25 changed files with 83 additions and 173 deletions

View File

@ -43,7 +43,7 @@ function shift_entry_add_controller()
$type = AngelType($type_id); $type = AngelType($type_id);
} else { } else {
// TODO: Move queries to model // TODO: Move queries to model
$type = DB::select(' $type = DB::selectOne('
SELECT * SELECT *
FROM `UserAngelTypes` FROM `UserAngelTypes`
JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`) JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`)
@ -56,7 +56,6 @@ function shift_entry_add_controller()
) )
) )
', [$type_id, $user['UID']]); ', [$type_id, $user['UID']]);
$type = array_shift($type);
} }
if (empty($type)) { if (empty($type)) {
@ -241,7 +240,7 @@ function shift_entry_delete_controller()
} }
$entry_id = $request->input('entry_id'); $entry_id = $request->input('entry_id');
$shift_entry_source = DB::select(' $shift_entry_source = DB::selectOne('
SELECT SELECT
`User`.`Nick`, `User`.`Nick`,
`ShiftEntry`.`Comment`, `ShiftEntry`.`Comment`,
@ -260,9 +259,7 @@ function shift_entry_delete_controller()
WHERE `ShiftEntry`.`id`=?', WHERE `ShiftEntry`.`id`=?',
[$entry_id] [$entry_id]
); );
if (count($shift_entry_source) > 0) { if (!empty($shift_entry_source)) {
$shift_entry_source = array_shift($shift_entry_source);
if (!in_array('user_shifts_admin', $privileges) && (!in_array('shiftentry_edit_angeltype_supporter', 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']))) $privileges) || !User_is_AngelType_supporter($user, AngelType($shift_entry_source['angeltype_id'])))
) { ) {

View File

@ -256,14 +256,8 @@ function AngelType_ids()
*/ */
function AngelType($angeltype_id) function AngelType($angeltype_id)
{ {
$angelType_source = DB::select( return DB::selectOne(
'SELECT * FROM `AngelTypes` WHERE `id`=?', 'SELECT * FROM `AngelTypes` WHERE `id`=?',
[$angeltype_id] [$angeltype_id]
); );
if (empty($angelType_source)) {
return null;
}
return array_shift($angelType_source);
} }

View File

@ -9,13 +9,7 @@ use Engelsystem\Database\DB;
*/ */
function EventConfig() function EventConfig()
{ {
$event_config = DB::select('SELECT * FROM `EventConfig` LIMIT 1'); return DB::selectOne('SELECT * FROM `EventConfig` LIMIT 1');
if (empty($event_config)) {
return null;
}
return array_shift($event_config);
} }
/** /**

View File

@ -20,11 +20,7 @@ function Message_ids()
*/ */
function Message($message_id) function Message($message_id)
{ {
$message_source = DB::select('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); return DB::selectOne('SELECT * FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]);
if (empty($message_source)) {
return null;
}
return array_shift($message_source);
} }
/** /**

View File

@ -58,17 +58,11 @@ function Room_create($name, $from_frab, $public, $number = null)
*/ */
function Room($room_id, $onlyVisible = true) function Room($room_id, $onlyVisible = true)
{ {
$room_source = DB::select(' return DB::selectOne('
SELECT * SELECT *
FROM `Room` FROM `Room`
WHERE `RID` = ? WHERE `RID` = ?
' . ($onlyVisible ? 'AND `show` = \'Y\'' : ''), ' . ($onlyVisible ? 'AND `show` = \'Y\'' : ''),
[$room_id] [$room_id]
); );
if (empty($room_source)) {
return null;
}
return array_shift($room_source);
} }

View File

@ -28,10 +28,9 @@ function ShiftEntry_new()
*/ */
function ShiftEntries_freeleaded_count() function ShiftEntries_freeleaded_count()
{ {
$result = DB::select('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1'); $result = DB::selectOne('SELECT COUNT(*) FROM `ShiftEntry` WHERE `freeloaded` = 1');
$result = array_shift($result);
if (!is_array($result)) { if (empty($result)) {
return 0; return 0;
} }
@ -129,13 +128,7 @@ function ShiftEntry_update($shift_entry)
*/ */
function ShiftEntry($shift_entry_id) function ShiftEntry($shift_entry_id)
{ {
$shift_entry = DB::select('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); return DB::selectOne('SELECT * FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]);
if (empty($shift_entry)) {
return null;
}
return $shift_entry[0];
} }
/** /**

View File

@ -70,13 +70,7 @@ function ShiftType_create($name, $angeltype_id, $description)
*/ */
function ShiftType($shifttype_id) function ShiftType($shifttype_id)
{ {
$shifttype = DB::select('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]); return DB::selectOne('SELECT * FROM `ShiftTypes` WHERE `id`=?', [$shifttype_id]);
if (empty($shifttype)) {
return null;
}
return array_shift($shifttype);
} }
/** /**

View File

@ -112,7 +112,7 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
*/ */
function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
{ {
$result = DB::select(' return DB::selectOne('
SELECT SELECT
`NeededAngelTypes`.*, `NeededAngelTypes`.*,
`Shifts`.`SID`, `Shifts`.`SID`,
@ -150,12 +150,6 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
$angeltype['id'] $angeltype['id']
] ]
); );
if (empty($result)) {
return null;
}
return $result[0];
} }
/** /**
@ -453,13 +447,13 @@ function Shift_update($shift)
*/ */
function Shift_update_by_psid($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)) { if (empty($shift_source)) {
throw new Exception('Shift not found.'); throw new Exception('Shift not found.');
} }
$shift['SID'] = $shift_source[0]['SID']; $shift['SID'] = $shift_source['SID'];
return Shift_update($shift); return Shift_update($shift);
} }
@ -537,18 +531,16 @@ function Shifts_by_user($user, $include_freeload_comments = false)
*/ */
function Shift($shift_id) function Shift($shift_id)
{ {
$shifts_source = DB::select(' $result = DB::selectOne('
SELECT `Shifts`.*, `ShiftTypes`.`name` SELECT `Shifts`.*, `ShiftTypes`.`name`
FROM `Shifts` FROM `Shifts`
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
WHERE `SID`=?', [$shift_id]); WHERE `SID`=?', [$shift_id]);
if (empty($shifts_source)) { if (empty($result)) {
return null; return null;
} }
$result = $shifts_source[0];
$shiftsEntry_source = DB::select(' $shiftsEntry_source = DB::select('
SELECT `id`, `TID` , `UID` , `freeloaded` SELECT `id`, `TID` , `UID` , `freeloaded`
FROM `ShiftEntry` FROM `ShiftEntry`

View File

@ -197,17 +197,11 @@ function UserAngelType_create($user, $angeltype)
*/ */
function UserAngelType($user_angeltype_id) function UserAngelType($user_angeltype_id)
{ {
$angeltype = DB::select(' return DB::selectOne('
SELECT * SELECT *
FROM `UserAngelTypes` FROM `UserAngelTypes`
WHERE `id`=? WHERE `id`=?
LIMIT 1', [$user_angeltype_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) function UserAngelType_by_User_and_AngelType($user, $angeltype)
{ {
$angeltype = DB::select(' return DB::selectOne('
SELECT * SELECT *
FROM `UserAngelTypes` FROM `UserAngelTypes`
WHERE `user_id`=? WHERE `user_id`=?
@ -231,10 +225,4 @@ function UserAngelType_by_User_and_AngelType($user, $angeltype)
$angeltype['id'] $angeltype['id']
] ]
); );
if (empty($angeltype)) {
return null;
}
return array_shift($angeltype);
} }

View File

@ -45,16 +45,10 @@ function UserDriverLicense_valid($user_driver_license)
*/ */
function UserDriverLicense($user_id) function UserDriverLicense($user_id)
{ {
$user_driver_license = DB::select(' return DB::selectOne('
SELECT * SELECT *
FROM `UserDriverLicenses` FROM `UserDriverLicenses`
WHERE `user_id`=?', [$user_id]); WHERE `user_id`=?', [$user_id]);
if (empty($user_driver_license)) {
return null;
}
return array_shift($user_driver_license);
} }
/** /**

View File

@ -87,8 +87,7 @@ function User_update($user)
*/ */
function User_force_active_count() function User_force_active_count()
{ {
$result = DB::select('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1'); $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `force_active` = 1');
$result = array_shift($result);
if (empty($result)) { if (empty($result)) {
return 0; return 0;
@ -102,8 +101,7 @@ function User_force_active_count()
*/ */
function User_active_count() function User_active_count()
{ {
$result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1'); $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Aktiv` = 1');
$result = array_shift($result);
if (empty($result)) { if (empty($result)) {
return 0; return 0;
@ -117,8 +115,7 @@ function User_active_count()
*/ */
function User_got_voucher_count() function User_got_voucher_count()
{ {
$result = DB::select('SELECT SUM(`got_voucher`) FROM `User`'); $result = DB::selectOne('SELECT SUM(`got_voucher`) FROM `User`');
$result = array_shift($result);
if (empty($result)) { if (empty($result)) {
return 0; return 0;
@ -132,8 +129,7 @@ function User_got_voucher_count()
*/ */
function User_arrived_count() function User_arrived_count()
{ {
$result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1'); $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Gekommen` = 1');
$result = array_shift($result);
if (empty($result)) { if (empty($result)) {
return 0; return 0;
@ -147,8 +143,7 @@ function User_arrived_count()
*/ */
function User_tshirts_count() function User_tshirts_count()
{ {
$result = DB::select('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1'); $result = DB::selectOne('SELECT COUNT(*) FROM `User` WHERE `Tshirt` = 1');
$result = array_shift($result);
if (empty($result)) { if (empty($result)) {
return 0; return 0;
@ -382,13 +377,7 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de
*/ */
function User($user_id) function User($user_id)
{ {
$user_source = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); return DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]);
if (empty($user_source)) {
return null;
}
return array_shift($user_source);
} }
/** /**
@ -400,13 +389,7 @@ function User($user_id)
*/ */
function User_by_api_key($api_key) function User_by_api_key($api_key)
{ {
$user = DB::select('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]); return DB::selectOne('SELECT * FROM `User` WHERE `api_key`=? LIMIT 1', [$api_key]);
if (empty($user)) {
return null;
}
return $user[0];
} }
/** /**
@ -417,30 +400,18 @@ function User_by_api_key($api_key)
*/ */
function User_by_email($email) function User_by_email($email)
{ {
$user = DB::select('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]); return DB::selectOne('SELECT * FROM `User` WHERE `email`=? LIMIT 1', [$email]);
if (empty($user)) {
return null;
}
return array_shift($user);
} }
/** /**
* Returns User by password token. * Returns User by password token.
* *
* @param string $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) function User_by_password_recovery_token($token)
{ {
$user = DB::select('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]); return DB::selectOne('SELECT * FROM `User` WHERE `password_recovery_token`=? LIMIT 1', [$token]);
if (empty($user)) {
return null;
}
return array_shift($user);
} }
/** /**

View File

@ -210,19 +210,17 @@ function admin_active()
$shirt_statistics = []; $shirt_statistics = [];
foreach (array_keys($tshirt_sizes) as $size) { foreach (array_keys($tshirt_sizes) as $size) {
if (!empty($size)) { if (!empty($size)) {
$sc = DB::select( $sc = DB::selectOne(
'SELECT count(*) FROM `User` WHERE `Size`=? AND `Gekommen`=1', 'SELECT count(*) FROM `User` WHERE `Size`=? AND `Gekommen`=1',
[$size] [$size]
); );
$sc = array_shift($sc); $sc = array_shift($sc);
$sc = array_shift($sc);
$gc = DB::select( $gc = DB::selectOne(
'SELECT count(*) FROM `User` WHERE `Size`=? AND `Tshirt`=1', 'SELECT count(*) FROM `User` WHERE `Size`=? AND `Tshirt`=1',
[$size] [$size]
); );
$gc = array_shift($gc); $gc = array_shift($gc);
$gc = array_shift($gc);
$shirt_statistics[] = [ $shirt_statistics[] = [
'size' => $size, 'size' => $size,
@ -232,8 +230,7 @@ function admin_active()
} }
} }
$uc = DB::select('SELECT count(*) FROM `User` WHERE `Tshirt`=1'); $uc = DB::selectOne('SELECT count(*) FROM `User` WHERE `Tshirt`=1');
$uc = array_shift($uc);
$uc = array_shift($uc); $uc = array_shift($uc);
$shirt_statistics[] = [ $shirt_statistics[] = [

View File

@ -107,23 +107,21 @@ function admin_groups()
return error('Incomplete call, missing Groups ID.', true); 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'); $privileges = $request->get('privileges');
if (!is_array($privileges)) { if (!is_array($privileges)) {
$privileges = []; $privileges = [];
} }
if (!empty($group)) { if (!empty($group)) {
$group = array_shift($group);
DB::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]); DB::delete('DELETE FROM `GroupPrivileges` WHERE `group_id`=?', [$group_id]);
$privilege_names = []; $privilege_names = [];
foreach ($privileges as $privilege) { foreach ($privileges as $privilege) {
if (preg_match('/^\d{1,}$/', $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', 'SELECT `name` FROM `Privileges` WHERE `id`=? LIMIT 1',
[$privilege] [$privilege]
); );
if (!empty($group_privileges_source)) { if (!empty($group_privileges_source)) {
$group_privileges_source = array_shift($group_privileges_source);
DB::insert( DB::insert(
'INSERT INTO `GroupPrivileges` (`group_id`, `privilege_id`) VALUES (?, ?)', 'INSERT INTO `GroupPrivileges` (`group_id`, `privilege_id`) VALUES (?, ?)',
[$group_id, $privilege] [$group_id, $privilege]

View File

@ -21,14 +21,13 @@ function admin_news()
return error('Incomplete call, missing News ID.', true); 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)) { if (empty($news)) {
return error('No News found.', true); return error('No News found.', true);
} }
switch ($request->input('action')) { switch ($request->input('action')) {
case 'edit': case 'edit':
$news = array_shift($news);
$user_source = User($news['UID']); $user_source = User($news['UID']);
$html .= form([ $html .= form([
@ -70,7 +69,6 @@ function admin_news()
break; break;
case 'delete': case 'delete':
$news = array_shift($news);
DB::delete('DELETE FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]); DB::delete('DELETE FROM `News` WHERE `ID`=? LIMIT 1', [$news_id]);
engelsystem_log('News deleted: ' . $news['Betreff']); engelsystem_log('News deleted: ' . $news['Betreff']);
success(_('News entry deleted.')); success(_('News entry deleted.'));

View File

@ -105,11 +105,11 @@ function admin_questions()
return error('Incomplete call, missing Question ID.', true); return error('Incomplete call, missing Question ID.', true);
} }
$question = DB::select( $question = DB::selectOne(
'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1', 'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id] [$question_id]
); );
if (count($question) > 0 && $question[0]['AID'] == null) { if (!empty($question) && $question['AID'] == null) {
$answer = trim( $answer = trim(
preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui",
'', '',
@ -129,7 +129,7 @@ function admin_questions()
$question_id, $question_id,
] ]
); );
engelsystem_log('Question ' . $question[0]['Question'] . ' answered: ' . $answer); engelsystem_log('Question ' . $question['Question'] . ' answered: ' . $answer);
redirect(page_link_to('admin_questions')); redirect(page_link_to('admin_questions'));
} else { } else {
return error('Enter an answer!', true); return error('Enter an answer!', true);
@ -145,13 +145,13 @@ function admin_questions()
return error('Incomplete call, missing Question ID.', true); return error('Incomplete call, missing Question ID.', true);
} }
$question = DB::select( $question = DB::selectOne(
'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1', 'SELECT * FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id] [$question_id]
); );
if (count($question) > 0) { if (!empty($question)) {
DB::delete('DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', [$question_id]); 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')); redirect(page_link_to('admin_questions'));
} else { } else {
return error('No question found.', true); return error('No question found.', true);

View File

@ -325,7 +325,7 @@ function admin_shifts()
); );
foreach ($_SESSION['admin_shifts_types'] as $type_id => $count) { foreach ($_SESSION['admin_shifts_types'] as $type_id => $count) {
$angel_type_source = DB::select(' $angel_type_source = DB::selectOne('
SELECT * SELECT *
FROM `AngelTypes` FROM `AngelTypes`
WHERE `id` = ? WHERE `id` = ?
@ -341,7 +341,7 @@ function admin_shifts()
$count $count
] ]
); );
$needed_angel_types_info[] = $angel_type_source[0]['name'] . ': ' . $count; $needed_angel_types_info[] = $angel_type_source['name'] . ': ' . $count;
} }
} }
} }

View File

@ -116,20 +116,20 @@ function admin_user()
$html .= '<hr />'; $html .= '<hr />';
$my_highest_group = DB::select( $my_highest_group = DB::selectOne(
'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1', 'SELECT group_id FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1',
[$user['UID']] [$user['UID']]
); );
if (count($my_highest_group) > 0) { if (!empty($my_highest_group)) {
$my_highest_group = $my_highest_group[0]['group_id']; $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', 'SELECT `group_id` FROM `UserGroups` WHERE `uid`=? ORDER BY `group_id` LIMIT 1',
[$user_id] [$user_id]
); );
if (count($his_highest_group) > 0) { if (!empty($his_highest_group)) {
$his_highest_group = $his_highest_group[0]['group_id']; $his_highest_group = $his_highest_group['group_id'];
} }
if ($user_id != $user['UID'] && $my_highest_group <= $his_highest_group) { if ($user_id != $user['UID'] && $my_highest_group <= $his_highest_group) {
@ -188,7 +188,7 @@ function admin_user()
count($my_highest_group) > 0 count($my_highest_group) > 0
&& ( && (
count($his_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(' $groups_source = DB::select('
@ -203,7 +203,7 @@ function admin_user()
', ',
[ [
$user_id, $user_id,
$my_highest_group[0]['group_id'], $my_highest_group['group_id'],
] ]
); );
$groups = []; $groups = [];

View File

@ -399,9 +399,8 @@ function guest_login()
if ($request->has('submit')) { if ($request->has('submit')) {
if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 0) { if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 0) {
$nick = User_validate_Nick($request->input('nick')); $nick = User_validate_Nick($request->input('nick'));
$login_user = DB::select('SELECT * FROM `User` WHERE `Nick`=?', [$nick]); $login_user = DB::selectOne('SELECT * FROM `User` WHERE `Nick`=?', [$nick]);
if (count($login_user) > 0) { if (!empty($login_user)) {
$login_user = $login_user[0];
if ($request->has('password')) { if ($request->has('password')) {
if (!verify_password($request->post('password'), $login_user['Passwort'], $login_user['UID'])) { if (!verify_password($request->post('password'), $login_user['Passwort'], $login_user['UID'])) {
$valid = false; $valid = false;

View File

@ -17,13 +17,12 @@ function guest_stats()
list($arrived_user_count) = DB::select('SELECT count(*) AS `user_count` FROM `User` WHERE `Gekommen`=1'); 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']; $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`) SELECT SUM(`Shifts`.`end` - `Shifts`.`start`)
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `Shifts` USING (`SID`) JOIN `Shifts` USING (`SID`)
WHERE `Shifts`.`end` < UNIX_TIMESTAMP() WHERE `Shifts`.`end` < UNIX_TIMESTAMP()
'); ');
$done_shifts_seconds = array_shift($done_shifts_seconds);
$done_shifts_seconds = (int)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); $stats['done_work_hours'] = round($done_shifts_seconds / (60 * 60), 0);

View File

@ -130,11 +130,11 @@ function user_messages()
return error(_('Incomplete call, missing Message ID.'), true); return error(_('Incomplete call, missing Message ID.'), true);
} }
$message = DB::select( $message = DB::selectOne(
'SELECT `RUID` FROM `Messages` WHERE `id`=? LIMIT 1', 'SELECT `RUID` FROM `Messages` WHERE `id`=? LIMIT 1',
[$message_id] [$message_id]
); );
if (count($message) > 0 && $message[0]['RUID'] == $user['UID']) { if (!empty($message) && $message['RUID'] == $user['UID']) {
DB::update( DB::update(
'UPDATE `Messages` SET `isRead`=\'Y\' WHERE `id`=? LIMIT 1', 'UPDATE `Messages` SET `isRead`=\'Y\' WHERE `id`=? LIMIT 1',
[$message_id] [$message_id]
@ -152,11 +152,11 @@ function user_messages()
return error(_('Incomplete call, missing Message ID.'), true); return error(_('Incomplete call, missing Message ID.'), true);
} }
$message = DB::select( $message = DB::selectOne(
'SELECT `SUID` FROM `Messages` WHERE `id`=? LIMIT 1', 'SELECT `SUID` FROM `Messages` WHERE `id`=? LIMIT 1',
[$message_id] [$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]); DB::delete('DELETE FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]);
redirect(page_link_to('user_messages')); redirect(page_link_to('user_messages'));
} else { } else {

View File

@ -31,8 +31,7 @@ function user_myshifts()
$user_id = $user['UID']; $user_id = $user['UID'];
} }
$shifts_user = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); $shifts_user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]);
$shifts_user = array_shift($shifts_user);
if ($request->has('reset')) { if ($request->has('reset')) {
if ($request->input('reset') == 'ack') { if ($request->input('reset') == 'ack') {
@ -49,7 +48,7 @@ function user_myshifts()
]); ]);
} elseif ($request->has('edit') && preg_match('/^\d*$/', $request->input('edit'))) { } elseif ($request->has('edit') && preg_match('/^\d*$/', $request->input('edit'))) {
$user_id = $request->input('edit'); $user_id = $request->input('edit');
$shift = DB::select(' $shift = DB::selectOne('
SELECT SELECT
`ShiftEntry`.`freeloaded`, `ShiftEntry`.`freeloaded`,
`ShiftEntry`.`freeload_comment`, `ShiftEntry`.`freeload_comment`,
@ -74,7 +73,6 @@ function user_myshifts()
] ]
); );
if (count($shift) > 0) { if (count($shift) > 0) {
$shift = array_shift($shift);
$freeloaded = $shift['freeloaded']; $freeloaded = $shift['freeloaded'];
$freeload_comment = $shift['freeload_comment']; $freeload_comment = $shift['freeload_comment'];
@ -128,7 +126,7 @@ function user_myshifts()
} }
} elseif ($request->has('cancel') && preg_match('/^\d*$/', $request->input('cancel'))) { } elseif ($request->has('cancel') && preg_match('/^\d*$/', $request->input('cancel'))) {
$user_id = $request->input('cancel'); $user_id = $request->input('cancel');
$shift = DB::select(' $shift = DB::selectOne('
SELECT * SELECT *
FROM `Shifts` FROM `Shifts`
INNER JOIN `ShiftEntry` USING (`SID`) INNER JOIN `ShiftEntry` USING (`SID`)
@ -140,7 +138,6 @@ function user_myshifts()
] ]
); );
if (count($shift) > 0) { if (count($shift) > 0) {
$shift = array_shift($shift);
if ( if (
($shift['start'] > time() + config('last_unsubscribe') * 3600) ($shift['start'] > time() + config('last_unsubscribe') * 3600)
|| in_array('user_shifts_admin', $privileges) || in_array('user_shifts_admin', $privileges)

View File

@ -126,8 +126,7 @@ function user_news_comments()
&& count(DB::select('SELECT `ID` FROM `News` WHERE `ID`=? LIMIT 1', [$request->input('nid')])) > 0 && count(DB::select('SELECT `ID` FROM `News` WHERE `ID`=? LIMIT 1', [$request->input('nid')])) > 0
) { ) {
$nid = $request->input('nid'); $nid = $request->input('nid');
$news = DB::select('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$nid]); $news = DB::selectOne('SELECT * FROM `News` WHERE `ID`=? LIMIT 1', [$nid]);
$news = array_shift($news);
if ($request->has('text')) { if ($request->has('text')) {
$text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($request->input('text'))); $text = preg_replace("/([^\p{L}\p{P}\p{Z}\p{N}\n]{1,})/ui", '', strip_tags($request->input('text')));
DB::insert(' DB::insert('

View File

@ -61,11 +61,11 @@ function user_questions()
return error(_('Incomplete call, missing Question ID.'), true); return error(_('Incomplete call, missing Question ID.'), true);
} }
$question = DB::select( $question = DB::selectOne(
'SELECT `UID` FROM `Questions` WHERE `QID`=? LIMIT 1', 'SELECT `UID` FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id] [$question_id]
); );
if (count($question) > 0 && $question[0]['UID'] == $user['UID']) { if (!empty($question) && $question['UID'] == $user['UID']) {
DB::delete( DB::delete(
'DELETE FROM `Questions` WHERE `QID`=? LIMIT 1', 'DELETE FROM `Questions` WHERE `QID`=? LIMIT 1',
[$question_id] [$question_id]

View File

@ -11,10 +11,9 @@ function load_auth()
$user = null; $user = null;
if (isset($_SESSION['uid'])) { if (isset($_SESSION['uid'])) {
$user = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]); $user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]);
if (count($user) > 0) { if (!empty($user)) {
// User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten // User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
$user = array_shift($user);
DB::update(' DB::update('
UPDATE `User` UPDATE `User`
SET `lastLogIn` = ? SET `lastLogIn` = ?

View File

@ -80,6 +80,23 @@ class Db
return self::$stm->fetchAll(PDO::FETCH_ASSOC); 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 * Run an insert query
* *