From a2aaba9cab6b7bdf755a023ed2503cf8cf46925a Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 17 Oct 2018 01:30:10 +0200 Subject: [PATCH] User: Bugfixes & code cleanup --- .../2018_10_01_000000_create_users_tables.php | 4 +- db/migrations/ChangesReferences.php | 1 + .../controller/shift_entries_controller.php | 9 +- includes/controller/shifts_controller.php | 3 +- .../controller/user_angeltypes_controller.php | 4 +- .../user_driver_licenses_controller.php | 3 +- .../controller/user_worklog_controller.php | 12 +- includes/controller/users_controller.php | 26 ++-- includes/helper/email_helper.php | 30 +--- includes/mailer/shifts_mailer.php | 4 +- includes/model/Shifts_model.php | 36 ++--- includes/model/User_model.php | 23 +-- includes/pages/admin_active.php | 139 ++++++++++-------- includes/pages/admin_arrive.php | 4 +- includes/pages/admin_free.php | 60 ++++---- includes/pages/admin_user.php | 13 +- includes/pages/user_settings.php | 8 +- includes/view/AngelTypes_view.php | 17 ++- includes/view/PublicDashboard_view.php | 2 +- includes/view/ShiftEntry_view.php | 2 +- includes/view/Shifts_view.php | 7 +- includes/view/UserAngelTypes_view.php | 12 +- includes/view/UserWorkLog_view.php | 8 +- includes/view/User_view.php | 106 ++++++------- src/Models/User/State.php | 5 + 25 files changed, 264 insertions(+), 274 deletions(-) diff --git a/db/migrations/2018_10_01_000000_create_users_tables.php b/db/migrations/2018_10_01_000000_create_users_tables.php index b9429a2d..55ee382e 100644 --- a/db/migrations/2018_10_01_000000_create_users_tables.php +++ b/db/migrations/2018_10_01_000000_create_users_tables.php @@ -102,7 +102,7 @@ class CreateUsersTables extends Migration $user->save(); $contact = new Contact([ - 'dect' => ($data->DECT && $data->DECT != '-') ? $data->DECT : null, + 'dect' => $data->DECT ? $data->DECT : null, 'mobile' => $data->Handy ?: ($data->Telefon ?: null), ]); $contact->user() @@ -170,7 +170,7 @@ class CreateUsersTables extends Migration public function down() { $this->schema->create('User', function (Blueprint $table) { - $table->increments('UID'); + $table->integer('UID', true, false); $table->string('Nick', 23)->unique()->default(''); $table->string('Name', 23)->nullable(); diff --git a/db/migrations/ChangesReferences.php b/db/migrations/ChangesReferences.php index d50f552e..6bdd187d 100644 --- a/db/migrations/ChangesReferences.php +++ b/db/migrations/ChangesReferences.php @@ -3,6 +3,7 @@ namespace Engelsystem\Migrations; use Illuminate\Database\Schema\Blueprint; +use stdClass; trait ChangesReferences { diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index bbf2e5b0..c5c1e181 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -11,7 +11,7 @@ use Engelsystem\ShiftSignupState; function shift_entries_controller() { $user = auth()->user(); - if ($user) { + if (!$user) { redirect(page_link_to('login')); } @@ -75,10 +75,9 @@ function shift_entry_create_controller() */ function shift_entry_create_controller_admin($shift, $angeltype) { - $user = auth()->user(); + $signup_user = auth()->user(); $request = request(); - $signup_user = $user; if ($request->has('user_id')) { $signup_user = User::find($request->input('user_id')); } @@ -113,8 +112,8 @@ function shift_entry_create_controller_admin($shift, $angeltype) $users = User::all(); $users_select = []; - foreach ($users as $u) { - $users_select[$u->id] = $u->name; + foreach ($users as $user) { + $users_select[$user->id] = $user->name; } $angeltypes_select = []; diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 07e81957..ee6714d4 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -332,13 +332,12 @@ function shifts_controller() function shift_next_controller() { global $privileges; - $user = auth()->user(); if (!in_array('user_shifts', $privileges)) { redirect(page_link_to('/')); } - $upcoming_shifts = ShiftEntries_upcoming_for_user($user->id); + $upcoming_shifts = ShiftEntries_upcoming_for_user(auth()->user()->id); if (!empty($upcoming_shifts)) { redirect(shift_link($upcoming_shifts[0])); diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index 6080d08b..4269313d 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -9,9 +9,7 @@ use Engelsystem\Models\User\User; */ function user_angeltypes_unconfirmed_hint() { - $user = auth()->user(); - - $unconfirmed_user_angeltypes = User_unconfirmed_AngelTypes($user->id); + $unconfirmed_user_angeltypes = User_unconfirmed_AngelTypes(auth()->user()->id); if (count($unconfirmed_user_angeltypes) == 0) { return null; } diff --git a/includes/controller/user_driver_licenses_controller.php b/includes/controller/user_driver_licenses_controller.php index f836c74d..9c44adb5 100644 --- a/includes/controller/user_driver_licenses_controller.php +++ b/includes/controller/user_driver_licenses_controller.php @@ -76,9 +76,8 @@ function user_driver_license_edit_link($user = null) */ function user_driver_license_load_user() { - $user = auth()->user(); $request = request(); - $user_source = $user; + $user_source = auth()->user(); if ($request->has('user_id')) { $user_source = User::find($request->input('user_id')); diff --git a/includes/controller/user_worklog_controller.php b/includes/controller/user_worklog_controller.php index 51b70ba2..333fd76e 100644 --- a/includes/controller/user_worklog_controller.php +++ b/includes/controller/user_worklog_controller.php @@ -9,12 +9,10 @@ use Engelsystem\Models\User\User; */ function user_worklog_delete_controller() { - $user = auth()->user(); - $request = request(); $userWorkLog = UserWorkLog($request->input('user_worklog_id')); if (empty($userWorkLog)) { - redirect(user_link($user->id)); + redirect(user_link(auth()->user()->id)); } $user_source = User::find($userWorkLog['user_id']); @@ -38,12 +36,10 @@ function user_worklog_delete_controller() */ function user_worklog_edit_controller() { - $user = auth()->user(); - $request = request(); $userWorkLog = UserWorkLog($request->input('user_worklog_id')); if (empty($userWorkLog)) { - redirect(user_link($user->id)); + redirect(user_link(auth()->user()->id)); } $user_source = User::find($userWorkLog['user_id']); @@ -110,12 +106,10 @@ function user_worklog_from_request($userWorkLog) */ function user_worklog_add_controller() { - $user = auth()->user(); - $request = request(); $user_source = User::find($request->input('user_id')); if (!$user_source) { - redirect(user_link($user->id)); + redirect(user_link(auth()->user()->id)); } $userWorkLog = UserWorkLog_new($user_source->id); diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index c8d8a4a9..b54bc967 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -167,7 +167,7 @@ function user_edit_vouchers_controller() if ($valid) { $user_source->state->got_voucher = $vouchers; - $user->state->save(); + $user_source->state->save(); success(__('Saved the number of vouchers.')); engelsystem_log(User_Nick_render($user_source) . ': ' . sprintf('Got %s vouchers', @@ -268,26 +268,30 @@ function users_list_controller() redirect(page_link_to('')); } - $order_by = 'Nick'; + $order_by = 'name'; if ($request->has('OrderBy') && in_array($request->input('OrderBy'), [ - 'Nick', - 'Name', - 'Vorname', - 'DECT', + 'name', + 'last_name', + 'first_name', + 'dect', 'email', - 'Size', - 'Gekommen', - 'Aktiv', + 'shirt_size', + 'arrived', + 'active', 'force_active', - 'Tshirt', - 'lastLogIn' + 'got_shirt', + 'last_login_at', ])) { $order_by = $request->input('OrderBy'); } /** @var User[] $users */ $users = User::query() + ->leftJoin('users_contact', 'users.id', '=', 'users_contact.user_id') + ->leftJoin('users_personal_data', 'users.id', '=', 'users_personal_data.user_id') + ->leftJoin('users_state', 'users.id', '=', 'users_state.user_id') ->orderBy($order_by) + ->orderBy('name') ->get(); foreach ($users as $user) { $user->setAttribute('freeloads', count(ShiftEntries_freeloaded_by_user($user->id))); diff --git a/includes/helper/email_helper.php b/includes/helper/email_helper.php index 4fc10226..566217ef 100644 --- a/includes/helper/email_helper.php +++ b/includes/helper/email_helper.php @@ -4,29 +4,15 @@ use Engelsystem\Mail\EngelsystemMailer; use Engelsystem\Models\User\User; /** - * @param array|User $recipientUser - * @param string $title - * @param string $message - * @param bool $notIfItsMe + * @param User $recipientUser + * @param string $title + * @param string $message + * @param bool $notIfItsMe * @return bool */ function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe = false) { - $user = Auth()->user(); - - if ($recipientUser instanceof User) { - $id = $user->id; - $lang = $user->settings->language; - $email = $user->contact->email ? $user->contact->email : $user->email; - $username = $user->name; - } else { - $id = $recipientUser['UID']; - $lang = $recipientUser['Sprache']; - $email = $recipientUser['email']; - $username = $recipientUser['Nick']; - } - - if ($notIfItsMe && $user->id == $id) { + if ($notIfItsMe && Auth()->user()->id == $recipientUser->id) { return true; } @@ -36,12 +22,12 @@ function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe /** @var EngelsystemMailer $mailer */ $mailer = app('mailer'); - $translator->setLocale($lang); + $translator->setLocale($recipientUser->settings->language); $status = $mailer->sendView( - $email, + $recipientUser->contact->email ? $recipientUser->contact->email : $recipientUser->email, $title, 'emails/mail', - ['username' => $username, 'message' => $message] + ['username' => $recipientUser->name, 'message' => $message] ); $translator->setLocale($locale); diff --git a/includes/mailer/shifts_mailer.php b/includes/mailer/shifts_mailer.php index 5304a897..37b26838 100644 --- a/includes/mailer/shifts_mailer.php +++ b/includes/mailer/shifts_mailer.php @@ -64,7 +64,7 @@ function mail_shift_change($old_shift, $new_shift) $message .= $new_room['Name'] . "\n"; foreach ($users as $user) { - $user = User::find($user->id); + $user = (new User())->forceFill($user); if ($user->settings->email_shiftinfo) { engelsystem_email_to_user( $user, @@ -92,7 +92,7 @@ function mail_shift_delete($shift) $message .= $room['Name'] . "\n"; foreach ($users as $user) { - $user = User::find($user->id); + $user = (new User())->forceFill($user); if ($user->settings->email_shiftinfo) { engelsystem_email_to_user($user, __('Your Shift was deleted'), $message, true); } diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index d7799a1e..cc909954 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -233,20 +233,23 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) */ function ShiftEntries_by_ShiftsFilter(ShiftsFilter $shiftsFilter) { - $sql = ' - SELECT - users.* - `ShiftEntry`.`UID`, - `ShiftEntry`.`TID`, - `ShiftEntry`.`SID`, - `ShiftEntry`.`Comment`, - `ShiftEntry`.`freeloaded` - FROM `Shifts` - JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID` - JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id` - WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') - AND `start` BETWEEN ? AND ? - ORDER BY `Shifts`.`start`'; + $sql = sprintf(' + SELECT + users.*, + `ShiftEntry`.`UID`, + `ShiftEntry`.`TID`, + `ShiftEntry`.`SID`, + `ShiftEntry`.`Comment`, + `ShiftEntry`.`freeloaded` + FROM `Shifts` + JOIN `ShiftEntry` ON `ShiftEntry`.`SID`=`Shifts`.`SID` + JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id` + WHERE `Shifts`.`RID` IN (%s) + AND `start` BETWEEN ? AND ? + ORDER BY `Shifts`.`start` + ', + implode(',', $shiftsFilter->getRooms()) + ); return DB::select( $sql, [ @@ -298,7 +301,7 @@ function Shift_free_entries($needed_angeltype, $shift_entries) /** * Check if shift signup is allowed from the end users point of view (no admin like privileges) * - * @param User $user + * @param User $user * @param array $shift The shift * @param array $angeltype The angeltype to which the user wants to sign up * @param array|null $user_angeltype @@ -575,7 +578,6 @@ function Shift_update_by_psid($shift) */ function Shift_create($shift) { - $user = auth()->user(); DB::insert(' INSERT INTO `Shifts` ( `shifttype_id`, @@ -599,7 +601,7 @@ function Shift_create($shift) $shift['title'], $shift['URL'], $shift['PSID'], - $user->id, + auth()->user()->id, time(), time(), ] diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 6192fa20..da25a9a2 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -73,16 +73,16 @@ function User_is_freeloader($user) function Users_by_angeltype_inverted($angeltype) { return User::query() - ->leftJoin('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id') + ->select('users.*') ->leftJoin('UserAngelTypes', function ($query) use ($angeltype) { /** @var JoinClause $query */ $query ->on('users.id', '=', 'UserAngelTypes.user_id') - ->on('UserAngelTypes.angeltype_id', '=', $angeltype['id']); + ->where('UserAngelTypes.angeltype_id', '=', $angeltype['id']); }) ->whereNull('UserAngelTypes.id') ->orderBy('users.name') - ->get('users.*'); + ->get(); } /** @@ -94,17 +94,17 @@ function Users_by_angeltype_inverted($angeltype) function Users_by_angeltype($angeltype) { return User::query() + ->select('users.*', + 'UserAngelTypes.id AS user_angeltype_id', + 'UserAngelTypes.confirm_user_id', + 'UserAngelTypes.supporter', + 'UserDriverLicenses.user_id AS wants_to_drive', + 'UserDriverLicenses.*' + ) ->join('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id') ->leftJoin('UserDriverLicenses', 'users.id', '=', 'UserDriverLicenses.user_id') ->where('UserAngelTypes.angeltype_id', '=', $angeltype['id']) - ->get([ - 'users.*', - '`UserAngelTypes`.`id` AS `user_angeltype_id`', - '`UserAngelTypes`.`confirm_user_id`', - '`UserAngelTypes`.`supporter`', - '(`UserDriverLicenses`.`user_id` IS NOT NULL) AS `wants_to_drive`', - '`UserDriverLicenses`.*', - ]); + ->get(); } /** @@ -226,6 +226,7 @@ function User_reset_api_key($user, $log = true) function User_generate_password_recovery_token($user) { $reset = PasswordReset::findOrNew($user->id); + $reset->user_id = $user->id; $reset->token = md5($user->name . time() . rand()); $reset->save(); diff --git a/includes/pages/admin_active.php b/includes/pages/admin_active.php index 8d9f35bd..5b0beae9 100644 --- a/includes/pages/admin_active.php +++ b/includes/pages/admin_active.php @@ -2,6 +2,8 @@ use Engelsystem\Models\User\State; use Engelsystem\Models\User\User; +use Illuminate\Database\Query\Builder; +use Illuminate\Database\Query\JoinClause; /** * @return string @@ -23,8 +25,7 @@ function admin_active() $msg = ''; $search = ''; $forced_count = State::whereForceActive(true)->count(); - $count = $forced_count; - $limit = ''; + $count = null; $set_active = ''; if ($request->has('search')) { @@ -34,8 +35,6 @@ function admin_active() $show_all_shifts = $request->has('show_all_shifts'); if ($request->has('set_active')) { - $valid = true; - if ($request->has('count') && preg_match('/^\d+$/', $request->input('count'))) { $count = strip_request_item('count'); if ($count < $forced_count) { @@ -46,41 +45,43 @@ function admin_active() redirect(page_link_to('admin_active')); } } else { - $valid = false; - $msg .= error(__('Please enter a number of angels to be marked as active.'), true); + $msg .= error(__('Please enter a number of angels to be marked as active.')); + redirect(page_link_to('admin_active')); } - if ($valid) { - $limit = ' LIMIT ' . $count; - } if ($request->has('ack')) { State::query() + ->where('got_shirt', '=', false) ->where('got_shirt', '=', false) ->update(['active' => false]); - /** @var User[] $users */ - $users = User::query()->raw(sprintf(' - SELECT - `users`.*, - COUNT(`ShiftEntry`.`id`) AS `shift_count`, - (%s + ( - SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id` - AND `work_timestamp` < %s - )) AS `shift_length` - FROM `users` - LEFT JOIN `ShiftEntry` ON `users`.`id` = `ShiftEntry`.`UID` - LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID` - LEFT JOIN `users_state` ON `users`.`id` = `users_state`.`user_id` - WHERE `users_state`.`arrived` = 1 - AND `users_state`.`force_active` = 0 - GROUP BY `users`.`id` - ORDER BY `force_active` DESC, `shift_length` DESC - %s - ', - $shift_sum_formula, - time(), - $limit - )); + $query = User::query() + ->selectRaw( + sprintf( + ' + users.*, + COUNT(ShiftEntry.id) AS shift_count, + (%s + ( + SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id` + AND `work_timestamp` < ? + )) AS `shift_length` + ', + $shift_sum_formula + ), + [time()] + ) + ->leftJoin('ShiftEntry', 'users.id', '=', 'ShiftEntry.UID') + ->leftJoin('Shifts', 'ShiftEntry.SID', '=', 'Shifts.SID') + ->leftJoin('users_state', 'users.id', '=', 'users_state.user_id') + ->where('users_state.arrived', '=', true) + ->where('users_state.force_active', '=', false) + ->groupBy('users.id') + ->orderByDesc('force_active') + ->orderByDesc('shift_length') + ->orderByDesc('name') + ->limit($count); + + $users = $query->get(); $user_nicks = []; foreach ($users as $usr) { $usr->state->active = true; @@ -88,10 +89,9 @@ function admin_active() $user_nicks[] = User_Nick_render($usr); } - State::whereForceActive(true)->update(['active' => 'true']); + State::whereForceActive(true)->update(['active' => true]); engelsystem_log('These angels are active now: ' . join(', ', $user_nicks)); - $limit = ''; $msg = success(__('Marked angels.'), true); } else { $set_active = '« ' @@ -120,7 +120,7 @@ function admin_active() } elseif ($request->has('not_active') && preg_match('/^\d+$/', $request->input('not_active'))) { $user_id = $request->input('not_active'); $user_source = User::find($user_id); - if (!$user_source) { + if ($user_source) { $user_source->state->active = false; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' is NOT active now.'); @@ -131,7 +131,7 @@ function admin_active() } elseif ($request->has('tshirt') && preg_match('/^\d+$/', $request->input('tshirt'))) { $user_id = $request->input('tshirt'); $user_source = User::find($user_id); - if (!$user_source) { + if ($user_source) { $user_source->state->got_shirt = true; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' has tshirt now.'); @@ -142,7 +142,7 @@ function admin_active() } elseif ($request->has('not_tshirt') && preg_match('/^\d+$/', $request->input('not_tshirt'))) { $user_id = $request->input('not_tshirt'); $user_source = User::find($user_id); - if (!$user_source) { + if ($user_source) { $user_source->state->got_shirt = false; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' has NO tshirt.'); @@ -152,28 +152,45 @@ function admin_active() } } - $users = User::query()->raw(sprintf(' - SELECT - `users`.*, - COUNT(`ShiftEntry`.`id`) AS `shift_count`, - (%s + ( - SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id` - AND `work_timestamp` < %s - )) AS `shift_length` - FROM `users` - LEFT JOIN `users_state` ON `users`.`id` = `users_state`.`user_id` - LEFT JOIN `ShiftEntry` ON `users`.`id` = `ShiftEntry`.`UID` - LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID` ' - . ($show_all_shifts ? '' : 'AND (`Shifts`.`end` < ' . time() . " OR `Shifts`.`end` IS NULL)") . ' - WHERE `users_state`.`arrived` = 1 - GROUP BY `users`.`id` - ORDER BY `users_state`.`force_active` DESC, `shift_length` DESC - %s - ', - $shift_sum_formula, - time(), - $limit - )); + $query = User::query() + ->selectRaw( + sprintf( + ' + users.*, + COUNT(ShiftEntry.id) AS shift_count, + (%s + ( + SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id` + AND `work_timestamp` < ? + )) AS `shift_length` + ', + $shift_sum_formula + ), + [time()] + ) + ->leftJoin('ShiftEntry', 'users.id', '=', 'ShiftEntry.UID') + ->leftJoin('Shifts', function ($join) use ($show_all_shifts) { + /** @var JoinClause $join */ + $join->on('ShiftEntry.SID', '=', 'Shifts.SID'); + if ($show_all_shifts) { + $join->where(function ($query) { + /** @var Builder $query */ + $query->where('Shifts.end', '<', time()) + ->orWhereNull('Shifts.end'); + }); + } + }) + ->leftJoin('users_state', 'users.id', '=', 'users_state.user_id') + ->where('users_state.arrived', '=', true) + ->groupBy('users.id') + ->orderByDesc('force_active') + ->orderByDesc('shift_length') + ->orderByDesc('name'); + + if (!is_null($count)) { + $query->limit($count); + } + + $users = $query->get(); $matched_users = []; if ($search == '') { $tokens = []; @@ -202,6 +219,7 @@ function admin_active() $userData['active'] = glyph_bool($usr->state->active == 1); $userData['force_active'] = glyph_bool($usr->state->force_active == 1); $userData['tshirt'] = glyph_bool($usr->state->got_shirt == 1); + $userData['shift_count'] = $usr['shift_count']; $actions = []; if (!$usr->state->active) { @@ -262,6 +280,7 @@ function admin_active() foreach (array_keys($tshirt_sizes) as $size) { $gc = State::query() ->leftJoin('users_settings', 'users_state.user_id', '=', 'users_settings.user_id') + ->leftJoin('users_personal_data', 'users_state.user_id', '=', 'users_personal_data.user_id') ->where('users_state.got_shirt', '=', true) ->where('users_personal_data.shirt_size', '=', $size) ->count(); @@ -283,7 +302,7 @@ function admin_active() form_submit('submit', __('Search')) ], page_link_to('admin_active')), $set_active == '' ? form([ - form_text('count', __('How much angels should be active?'), $count), + form_text('count', __('How much angels should be active?'), $count ? $count : $forced_count), form_submit('set_active', __('Preview')) ]) : $set_active, $msg . msg(), diff --git a/includes/pages/admin_arrive.php b/includes/pages/admin_arrive.php index 40b8bf44..870522e2 100644 --- a/includes/pages/admin_arrive.php +++ b/includes/pages/admin_arrive.php @@ -44,7 +44,7 @@ function admin_arrive() if ($user_source) { $user_source->state->arrived = true; $user_source->state->arrival_date = new Carbon\Carbon(); - $user_source->personalData->save(); + $user_source->state->save(); engelsystem_log('User set has arrived: ' . User_Nick_render($user_source)); success(__('Angel has been marked as arrived.')); @@ -170,7 +170,7 @@ function admin_arrive() form_submit('submit', __('Search')) ]), table([ - 'nick' => __('Nickname'), + 'name' => __('Nickname'), 'rendered_planned_arrival_date' => __('Planned arrival'), 'arrived' => __('Arrived?'), 'rendered_arrival_date' => __('Arrival date'), diff --git a/includes/pages/admin_free.php b/includes/pages/admin_free.php index 9b1f581b..d8787f36 100644 --- a/includes/pages/admin_free.php +++ b/includes/pages/admin_free.php @@ -2,6 +2,7 @@ use Engelsystem\Database\DB; use Engelsystem\Models\User\User; +use Illuminate\Database\Query\JoinClause; /** * @return string @@ -24,18 +25,6 @@ function admin_free() $search = strip_request_item('search'); } - $angelTypeSearch = ''; - $angelType = $request->input('angeltype', ''); - if (!empty($angelType)) { - $angelTypeSearch = ' INNER JOIN `UserAngelTypes` ON (`UserAngelTypes`.`angeltype_id` = ' - . DB::getPdo()->quote($angelType) - . ' AND `UserAngelTypes`.`user_id` = `users`.`id`'; - if ($request->has('confirmed_only')) { - $angelTypeSearch .= ' AND `UserAngelTypes`.`confirm_user_id`'; - } - $angelTypeSearch .= ') '; - } - $angel_types_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`'); $angel_types = [ '' => 'alle Typen' @@ -44,27 +33,34 @@ function admin_free() $angel_types[$angel_type['id']] = $angel_type['name']; } - /** @var User[] $users */ - $users = User::query()->raw(sprintf(' - SELECT `users`.* - FROM `users` - %s - LEFT JOIN `ShiftEntry` ON `users`.`id` = `ShiftEntry`.`UID` - LEFT JOIN `users_state` ON `users`.`id` = `users_state`.`user_id` - LEFT JOIN `Shifts` - ON ( - `ShiftEntry`.`SID` = `Shifts`.`SID` - AND `Shifts`.`start` < %u - AND `Shifts`.`end` > %u - ) - WHERE `users_state`.`arrived` = 1 - AND `Shifts`.`SID` IS NULL - GROUP BY `users`.`id` - ORDER BY `users` - ', $angelTypeSearch, time(), time() - ) - ); + $angelType = $request->input('angeltype', ''); + $query = User::query() + ->select('users.*') + ->leftJoin('ShiftEntry', 'users.id', 'ShiftEntry.UID') + ->leftJoin('users_state', 'users.id', 'users_state.user_id') + ->leftJoin('Shifts', function ($join) { + /** @var JoinClause $join */ + $join->on('ShiftEntry.SID', '=', 'Shifts.SID') + ->where('Shifts.start', '<', time()) + ->where('Shifts.end', '>', time()); + }) + ->where('users_state.arrived', '=', 1) + ->whereNull('Shifts.SID') + ->groupBy('users.id'); + + if (!empty($angelType)) { + $query->join('UserAngelTypes', function ($join) use ($angelType, $request, $query) { + /** @var JoinClause $join */ + $join->on('UserAngelTypes.user_id', '=', 'users.id') + ->where('UserAngelTypes.angeltype_id', '=', $angelType); + + if ($request->has('confirmed_only')) { + $join->whereNotNull('UserAngelTypes.confirm_user_id'); + } + }); + } + $users = $query->get(); $free_users_table = []; if ($search == '') { $tokens = []; diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 3a9a484c..a87a76cf 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -60,8 +60,13 @@ function admin_user() $html .= " email" . '' . "\n"; } $html .= ' Size' - . html_select_key('size', 'eSize', $tshirt_sizes, $user_source->personalData->shirt_size, - __('Please select...')) . '' . "\n"; + . html_select_key( + 'size', + 'eSize', + $tshirt_sizes, $user_source->personalData->shirt_size, + __('Please select...') + ) + . '' . "\n"; $options = [ '1' => __('Yes'), @@ -84,7 +89,7 @@ function admin_user() // T-Shirt bekommen? $html .= ' T-Shirt' . "\n"; - $html .= html_options('eTshirt', $options, $user_source->personalData->shirt_size) . '' . "\n"; + $html .= html_options('eTshirt', $options, $user_source->state->got_shirt) . '' . "\n"; $html .= '' . "\n" . ''; @@ -244,7 +249,7 @@ function admin_user() if (in_array('admin_active', $privileges)) { $force_active = $request->input('force_active'); } - if($user_source->settings->email_human){ + if ($user_source->settings->email_human) { $user_source->email = $request->postData('eemail'); } $user_source->name = User_validate_Nick($request->postData('eNick')); diff --git a/includes/pages/user_settings.php b/includes/pages/user_settings.php index bc97099e..f833eec5 100644 --- a/includes/pages/user_settings.php +++ b/includes/pages/user_settings.php @@ -57,7 +57,11 @@ function user_settings_main($user_source, $enable_tshirt_size, $tshirt_sizes) if ($request->has('planned_departure_date')) { $tmp = parse_date('Y-m-d H:i', $request->input('planned_departure_date') . ' 00:00'); - $result = User_validate_planned_departure_date($user_source->state->arrival_date->getTimestamp(), $tmp); + $plannedArrivalDate = $user_source->personalData->planned_arrival_date; + $result = User_validate_planned_departure_date( + $plannedArrivalDate ? $plannedArrivalDate->getTimestamp() : 0, + $tmp + ); $user_source->personalData->planned_departure_date = Carbon::createFromTimestamp($result->getValue()); if (!$result->isValid()) { $valid = false; @@ -66,7 +70,7 @@ function user_settings_main($user_source, $enable_tshirt_size, $tshirt_sizes) } // Trivia - $user_source->name = strip_request_item('lastname', $user_source['Name']); + $user_source->personalData->last_name = strip_request_item('lastname', $user_source['Name']); $user_source->personalData->first_name = strip_request_item('prename', $user_source['Vorname']); if (strlen(strip_request_item('dect')) <= 5) { $user_source->contact->dect = strip_request_item('dect', $user_source['DECT']); diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index 3ce063ab..58c9567b 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -203,6 +203,7 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a $members_unconfirmed = []; foreach ($members as $member) { $member->name = User_Nick_render($member); + $member['dect'] = $member->contact->dect; if ($angeltype['requires_driver_license']) { $member['wants_to_drive'] = glyph_bool($member['wants_to_drive']); $member['has_car'] = glyph_bool($member['has_car']); @@ -294,8 +295,8 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) { if ($angeltype['requires_driver_license'] && ($supporter || $admin_angeltypes)) { return [ - 'Nick' => __('Nick'), - 'DECT' => __('DECT'), + 'name' => __('Nick'), + 'dect' => __('DECT'), 'wants_to_drive' => __('Driver'), 'has_car' => __('Has car'), 'has_license_car' => __('Car'), @@ -307,8 +308,8 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) ]; } return [ - 'Nick' => __('Nick'), - 'DECT' => __('DECT'), + 'name' => __('Nick'), + 'dect' => __('DECT'), 'actions' => '' ]; } @@ -420,16 +421,16 @@ function AngelType_view_info( if (count($members_confirmed) > 0) { $members_confirmed[] = [ - 'Nick' => __('Sum'), - 'DECT' => count($members_confirmed), + 'name' => __('Sum'), + 'dect' => count($members_confirmed), 'actions' => '' ]; } if (count($members_unconfirmed) > 0) { $members_unconfirmed[] = [ - 'Nick' => __('Sum'), - 'DECT' => count($members_unconfirmed), + 'name' => __('Sum'), + 'dect' => count($members_unconfirmed), 'actions' => '' ]; } diff --git a/includes/view/PublicDashboard_view.php b/includes/view/PublicDashboard_view.php index 2bfca3fa..baab2f8a 100644 --- a/includes/view/PublicDashboard_view.php +++ b/includes/view/PublicDashboard_view.php @@ -16,7 +16,7 @@ function public_dashboard_view($stats, $free_shifts) ]; foreach ($free_shifts as $i => $shift) { $shift_panels[] = public_dashboard_shift_render($shift); - if($i % 4 == 3) { + if ($i % 4 == 3) { $shift_panels[] = '
'; } } diff --git a/includes/view/ShiftEntry_view.php b/includes/view/ShiftEntry_view.php index 9b528256..5d4364f5 100644 --- a/includes/view/ShiftEntry_view.php +++ b/includes/view/ShiftEntry_view.php @@ -76,7 +76,7 @@ function ShiftEntry_delete_title() * @param array $room * @param array $angeltype * @param array $angeltypes_select - * @param User $signup_user + * @param User $signup_user * @param array $users_select * @return string */ diff --git a/includes/view/Shifts_view.php b/includes/view/Shifts_view.php index 2485ec67..4c329f1b 100644 --- a/includes/view/Shifts_view.php +++ b/includes/view/Shifts_view.php @@ -76,10 +76,8 @@ function Shift_editor_info_render($shift) */ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null) { - $user = auth()->user(); - if (empty($user_angeltype)) { - $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype); + $user_angeltype = UserAngelType_by_User_and_AngelType(auth()->user()->id, $angeltype); } if ($angeltype['shift_signup_state']->isSignupAllowed()) { @@ -105,7 +103,6 @@ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null) function Shift_view($shift, $shifttype, $room, $angeltypes_source, ShiftSignupState $shift_signup_state) { global $privileges; - $user = auth()->user(); $shift_admin = in_array('admin_shifts', $privileges); $user_shift_admin = in_array('user_shifts_admin', $privileges); @@ -143,7 +140,7 @@ function Shift_view($shift, $shifttype, $room, $angeltypes_source, ShiftSignupSt $admin_rooms ? button(room_link($room), glyph('map-marker') . $room['Name']) : '', ]; } - $buttons[] = button(user_link($user->id), ' ' . __('My shifts')); + $buttons[] = button(user_link(auth()->user()->id), ' ' . __('My shifts')); $content[] = buttons($buttons); $content[] = Shift_view_header($shift, $room); diff --git a/includes/view/UserAngelTypes_view.php b/includes/view/UserAngelTypes_view.php index 78bf6fe9..1c583389 100644 --- a/includes/view/UserAngelTypes_view.php +++ b/includes/view/UserAngelTypes_view.php @@ -4,7 +4,7 @@ use Engelsystem\Models\User\User; /** * @param array $user_angeltype - * @param User $user + * @param User $user * @param array $angeltype * @param bool $supporter * @return string @@ -91,7 +91,7 @@ function UserAngelTypes_confirm_all_view($angeltype) /** * @param array $user_angeltype - * @param User $user + * @param User $user * @param array $angeltype * @return string */ @@ -120,7 +120,7 @@ function UserAngelType_confirm_view($user_angeltype, $user, $angeltype) /** * @param array $user_angeltype - * @param User $user + * @param User $user * @param array $angeltype * @return string */ @@ -146,9 +146,9 @@ function UserAngelType_delete_view($user_angeltype, $user, $angeltype) } /** - * @param array $angeltype + * @param array $angeltype * @param User[] $users_source - * @param int $user_id + * @param int $user_id * @return string */ function UserAngelType_add_view($angeltype, $users_source, $user_id) @@ -176,7 +176,7 @@ function UserAngelType_add_view($angeltype, $users_source, $user_id) } /** - * @param User $user + * @param User $user * @param array $angeltype * @return string */ diff --git a/includes/view/UserWorkLog_view.php b/includes/view/UserWorkLog_view.php index ee35350b..8b4e7ae3 100644 --- a/includes/view/UserWorkLog_view.php +++ b/includes/view/UserWorkLog_view.php @@ -5,7 +5,7 @@ use Engelsystem\Models\User\User; /** * Delete work log entry. * - * @param User $user_source + * @param User $user_source * @param array $userWorkLog * @return string */ @@ -36,7 +36,7 @@ function UserWorkLog_delete_title() /** * Render edit table. * - * @param User $user_source + * @param User $user_source * @param array $userWorkLog * @return string */ @@ -54,7 +54,7 @@ function UserWorkLog_edit_form($user_source, $userWorkLog) /** * Form for edit a user work log entry. * - * @param User $user_source + * @param User $user_source * @param array $userWorkLog * @return string */ @@ -72,7 +72,7 @@ function UserWorkLog_edit_view($user_source, $userWorkLog) /** * Form for adding a user work log entry. * - * @param User $user_source + * @param User $user_source * @param array $userWorkLog * @return string */ diff --git a/includes/view/User_view.php b/includes/view/User_view.php index c055169e..2b4154b4 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -25,7 +25,6 @@ function User_settings_view( $tshirt_sizes ) { $personalData = $user_source->personalData; - $state = $user_source->state; return page_with_title(settings_title(), [ msg(), div('row', [ @@ -34,12 +33,12 @@ function User_settings_view( form_info('', __('Here you can change your user details.')), form_info(entry_required() . ' = ' . __('Entry required!')), form_text('nick', __('Nick'), $user_source->name, true), - form_text('lastname', __('Last name'), $user_source->personalData->last_name), - form_text('prename', __('First name'), $user_source->personalData->first_name), + form_text('lastname', __('Last name'), $personalData->last_name), + form_text('prename', __('First name'), $personalData->first_name), form_date( 'planned_arrival_date', __('Planned date of arrival') . ' ' . entry_required(), - $state->arrival_date ? $state->arrival_date->getTimestamp() : '', + $personalData->planned_arrival_date ? $personalData->planned_arrival_date->getTimestamp() : '', $buildup_start_date, $teardown_end_date ), @@ -52,7 +51,7 @@ function User_settings_view( ), form_text('dect', __('DECT'), $user_source->contact->dect), form_text('mobile', __('Mobile'), $user_source->contact->mobile), - form_text('mail', __('E-Mail') . ' ' . entry_required(), $user_source->contact->email), + form_text('mail', __('E-Mail') . ' ' . entry_required(), $user_source->email), form_checkbox( 'email_shiftinfo', __( @@ -70,7 +69,7 @@ function User_settings_view( 'tshirt_size', __('Shirt size'), $tshirt_sizes, - $user_source->personalData->shirt_size, + $personalData->shirt_size, __('Please select...') ) : '', form_info('', __('Please visit the angeltypes page to manage your angeltypes.')), @@ -215,31 +214,31 @@ function Users_view( $usersList = []; foreach ($users as $user) { $u = []; - $u['Nick'] = User_Nick_render($user); - $u['Vorname'] = $user->personalData->first_name; - $u['Name'] = $user->personalData->last_name; - $u['DECT'] = $user->contact->dect; - $u['Gekommen'] = glyph_bool($user->state->arrived); - $u['got_voucher'] = glyph_bool($user->state->got_voucher); + $u['name'] = User_Nick_render($user); + $u['first_name'] = $user->personalData->first_name; + $u['last_name'] = $user->personalData->last_name; + $u['dect'] = $user->contact->dect; + $u['arrived'] = glyph_bool($user->state->arrived); + $u['got_voucher'] = $user->state->got_voucher; $u['freeloads'] = $user->getAttribute('freeloads'); - $u['Aktiv'] = glyph_bool($user->state->active); + $u['active'] = glyph_bool($user->state->active); $u['force_active'] = glyph_bool($user->state->force_active); - $u['Tshirt'] = glyph_bool($user->state->got_shirt); - $u['Size'] = $user->personalData->shirt_size; - $u['lastLogIn'] = $user->last_login_at ? $user->last_login_at->format(__('m/d/Y h:i a')) : ''; + $u['got_shirt'] = glyph_bool($user->state->got_shirt); + $u['shirt_size'] = $user->personalData->shirt_size; + $u['last_login_at'] = $user->last_login_at ? $user->last_login_at->format(__('m/d/Y h:i a')) : ''; $u['actions'] = table_buttons([ button_glyph(page_link_to('admin_user', ['id' => $user->id]), 'edit', 'btn-xs') ]); $usersList[] = $u; } $usersList[] = [ - 'Nick' => '' . __('Sum') . '', - 'Gekommen' => $arrived_count, + 'name' => '' . __('Sum') . '', + 'arrived' => $arrived_count, 'got_voucher' => $voucher_count, - 'Aktiv' => $active_count, + 'active' => $active_count, 'force_active' => $force_active_count, 'freeloads' => $freeloads_count, - 'Tshirt' => $tshirts_count, + 'got_shirt' => $tshirts_count, 'actions' => '' . count($usersList) . '' ]; @@ -249,19 +248,19 @@ function Users_view( button(page_link_to('register'), glyph('plus') . __('New user')) ]), table([ - 'Nick' => Users_table_header_link('Nick', __('Nick'), $order_by), - 'Vorname' => Users_table_header_link('Vorname', __('Prename'), $order_by), - 'Name' => Users_table_header_link('Name', __('Name'), $order_by), - 'DECT' => Users_table_header_link('DECT', __('DECT'), $order_by), - 'Gekommen' => Users_table_header_link('Gekommen', __('Arrived'), $order_by), - 'got_voucher' => Users_table_header_link('got_voucher', __('Voucher'), $order_by), - 'freeloads' => __('Freeloads'), - 'Aktiv' => Users_table_header_link('Aktiv', __('Active'), $order_by), - 'force_active' => Users_table_header_link('force_active', __('Forced'), $order_by), - 'Tshirt' => Users_table_header_link('Tshirt', __('T-Shirt'), $order_by), - 'Size' => Users_table_header_link('Size', __('Size'), $order_by), - 'lastLogIn' => Users_table_header_link('lastLogIn', __('Last login'), $order_by), - 'actions' => '' + 'name' => Users_table_header_link('name', __('Nick'), $order_by), + 'first_name' => Users_table_header_link('first_name', __('Prename'), $order_by), + 'last_name' => Users_table_header_link('last_name', __('Name'), $order_by), + 'dect' => Users_table_header_link('dect', __('DECT'), $order_by), + 'arrived' => Users_table_header_link('arrived', __('Arrived'), $order_by), + 'got_voucher' => Users_table_header_link('got_voucher', __('Voucher'), $order_by), + 'freeloads' => __('Freeloads'), + 'active' => Users_table_header_link('active', __('Active'), $order_by), + 'force_active' => Users_table_header_link('force_active', __('Forced'), $order_by), + 'got_shirt' => Users_table_header_link('got_shirt', __('T-Shirt'), $order_by), + 'shirt_size' => Users_table_header_link('shirt_size', __('Size'), $order_by), + 'last_login_at' => Users_table_header_link('last_login_at', __('Last login'), $order_by), + 'actions' => '' ], $usersList) ]); } @@ -282,24 +281,16 @@ function Users_table_header_link($column, $label, $order_by) } /** - * @param User|array $user + * @param User $user * @return string|false */ function User_shift_state_render($user) { - if ($user instanceof User) { - $id = $user->id; - $arrived = $user->state->arrived; - } else { - $arrived = $user['Gekommen']; - $id = $user['UID']; - } - - if (!$arrived) { + if (!$user->state->arrived) { return ''; } - $upcoming_shifts = ShiftEntries_upcoming_for_user($id); + $upcoming_shifts = ShiftEntries_upcoming_for_user($user->id); if (empty($upcoming_shifts)) { return '' . __('Free') . ''; } @@ -735,7 +726,7 @@ function User_view_state_admin($freeloader, $user_source) } elseif ($user_source->state->active) { $state[] = '' . __('Active') . ''; } - if ($user_source->personalData->shirt_size) { + if ($user_source->state->got_shirt) { $state[] = '' . __('T-Shirt') . ''; } } else { @@ -844,20 +835,14 @@ function User_groups_render($user_groups) */ function User_Nick_render($user) { - if ($user instanceof User) { - $id = $user->id; - $name = $user->name; - $arrived = $user->state->arrived; - } else { - $id = $user['UID']; - $name = $user['Nick']; - $arrived = $user['Gekommen']; + if (is_array($user)) { + $user = (new User())->forceFill($user); } return render_profile_link( - ' ' . htmlspecialchars($name) . '', - $id, - ($arrived ? '' : 'text-muted') + ' ' . htmlspecialchars($user->name) . '', + $user->id, + ($user->state->arrived ? '' : 'text-muted') ); } @@ -917,9 +902,7 @@ function render_user_freeloader_hint() */ function render_user_arrived_hint() { - $user = auth()->user(); - - if (!$user->state->arrived) { + if (!auth()->user()->state->arrived) { /** @var Carbon $buildup */ $buildup = config('buildup_start'); if (!empty($buildup) && $buildup->lessThan(new Carbon())) { @@ -935,9 +918,7 @@ function render_user_arrived_hint() */ function render_user_tshirt_hint() { - $user = auth()->user(); - - if (config('enable_tshirt_size') && !$user->personalData->shirt_size) { + if (config('enable_tshirt_size') && !auth()->user()->personalData->shirt_size) { $text = __('You need to specify a tshirt size in your settings!'); return render_profile_link($text, null, 'alert-link'); } @@ -951,7 +932,6 @@ function render_user_tshirt_hint() function render_user_dect_hint() { $user = auth()->user(); - if ($user->state->arrived && !$user->contact->dect) { $text = __('You need to specify a DECT phone number in your settings! If you don\'t have a DECT phone, just enter \'-\'.'); return render_profile_link($text, null, 'alert-link'); diff --git a/src/Models/User/State.php b/src/Models/User/State.php index 93bb8590..e12aa5e1 100644 --- a/src/Models/User/State.php +++ b/src/Models/User/State.php @@ -22,6 +22,11 @@ class State extends HasUserModel /** @var string The table associated with the model */ protected $table = 'users_state'; + /** @var array The attributes that should be mutated to dates */ + protected $dates = [ + 'arrival_date', + ]; + /** The attributes that are mass assignable */ protected $fillable = [ 'user_id',