From 6686d58c0684ddc6ed985dfa355c8654d3d08f71 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 9 Nov 2022 00:02:30 +0100 Subject: [PATCH] Implemented AngelTypes model --- db/factories/AngelTypeFactory.php | 33 +++ ..._11_08_000000_create_angel_types_table.php | 126 +++++++++ includes/controller/angeltypes_controller.php | 156 +++++++---- .../public_dashboard_controller.php | 7 +- includes/controller/rooms_controller.php | 3 +- .../controller/shift_entries_controller.php | 49 ++-- includes/controller/shifts_controller.php | 24 +- .../controller/user_angeltypes_controller.php | 111 +++----- includes/controller/users_controller.php | 6 +- includes/helper/oauth_helper.php | 9 +- includes/includes.php | 1 - includes/model/AngelType_model.php | 249 ----------------- includes/model/NeededAngelTypes_model.php | 16 +- includes/model/ShiftEntry_model.php | 13 +- includes/model/ShiftsFilter.php | 6 +- includes/model/Shifts_model.php | 117 ++++---- includes/model/Stats.php | 34 +-- includes/model/UserAngelTypes_model.php | 72 ++--- includes/model/User_model.php | 25 +- includes/pages/admin_free.php | 12 +- includes/pages/admin_rooms.php | 19 +- includes/pages/admin_shifts.php | 34 ++- includes/pages/guest_login.php | 13 +- includes/pages/schedule/ImportSchedule.php | 4 +- includes/pages/user_myshifts.php | 4 +- includes/pages/user_shifts.php | 17 +- includes/sys_template.php | 8 +- includes/view/AngelTypes_view.php | 253 +++++++++--------- includes/view/ShiftCalendarShiftRenderer.php | 22 +- includes/view/ShiftEntry_view.php | 47 ++-- includes/view/Shifts_view.php | 42 +-- includes/view/UserAngelTypes_view.php | 85 +++--- includes/view/User_view.php | 3 +- src/Models/AngelType.php | 77 ++++++ tests/Unit/Models/AngelTypeTest.php | 54 ++++ 35 files changed, 910 insertions(+), 841 deletions(-) create mode 100644 db/factories/AngelTypeFactory.php create mode 100644 db/migrations/2022_11_08_000000_create_angel_types_table.php delete mode 100644 includes/model/AngelType_model.php create mode 100644 src/Models/AngelType.php create mode 100644 tests/Unit/Models/AngelTypeTest.php diff --git a/db/factories/AngelTypeFactory.php b/db/factories/AngelTypeFactory.php new file mode 100644 index 00000000..a9f6d822 --- /dev/null +++ b/db/factories/AngelTypeFactory.php @@ -0,0 +1,33 @@ + $this->faker->unique()->firstName(), + 'description' => $this->faker->text(), + + 'contact_name' => $this->faker->firstName(), + 'contact_dect' => $this->faker->randomNumber(4), + 'contact_email' => $this->faker->email(), + + 'restricted' => $this->faker->boolean(), + 'requires_driver_license' => $this->faker->boolean(), + 'no_self_signup' => $this->faker->boolean(), + 'show_on_dashboard' => $this->faker->boolean(), + 'hide_register' => $this->faker->boolean(), + ]; + } +} diff --git a/db/migrations/2022_11_08_000000_create_angel_types_table.php b/db/migrations/2022_11_08_000000_create_angel_types_table.php new file mode 100644 index 00000000..7783e0bc --- /dev/null +++ b/db/migrations/2022_11_08_000000_create_angel_types_table.php @@ -0,0 +1,126 @@ +schema->getConnection(); + $this->schema->create('angel_types', function (Blueprint $table) { + $table->increments('id'); + $table->string('name')->unique(); + $table->text('description')->default(''); + + $table->string('contact_name')->default(''); + $table->string('contact_dect')->default(''); + $table->string('contact_email')->default(''); + + $table->boolean('restricted')->default(false); + $table->boolean('requires_driver_license')->default(false); + $table->boolean('no_self_signup')->default(false); + $table->boolean('show_on_dashboard')->default(true); + $table->boolean('hide_register')->default(false); + }); + + if (!$this->schema->hasTable('AngelTypes')) { + return; + } + + /** @var Collection|stdClass[] $records */ + $records = $connection + ->table('AngelTypes') + ->get(); + foreach ($records as $record) { + $connection->table('angel_types')->insert([ + 'id' => $record->id, + 'name' => $record->name, + 'description' => $record->description, + + 'contact_name' => (string)$record->contact_name, + 'contact_dect' => (string)$record->contact_dect, + 'contact_email' => (string)$record->contact_email, + + 'restricted' => $record->restricted, + 'requires_driver_license' => $record->requires_driver_license, + 'no_self_signup' => $record->no_self_signup, + 'show_on_dashboard' => $record->show_on_dashboard, + 'hide_register' => $record->hide_register, + ]); + } + + $this->changeReferences( + 'AngelTypes', + 'id', + 'angel_types', + 'id' + ); + + $this->schema->drop('AngelTypes'); + } + + /** + * Recreates the previous table, copies the data and drops the new one + */ + public function down(): void + { + $connection = $this->schema->getConnection(); + $this->schema->create('AngelTypes', function (Blueprint $table) { + $table->integer('id', true); + $table->string('name', 50)->default('')->unique(); + $table->boolean('restricted'); + $table->mediumText('description'); + $table->boolean('requires_driver_license'); + $table->boolean('no_self_signup'); + $table->string('contact_name', 250)->nullable(); + $table->string('contact_dect', 40)->nullable(); + $table->string('contact_email', 250)->nullable(); + $table->boolean('show_on_dashboard'); + $table->boolean('hide_register')->default(false); + }); + + /** @var Collection|stdClass[] $records */ + $records = $connection + ->table('angel_types') + ->get(); + foreach ($records as $record) { + $connection->table('AngelTypes')->insert([ + 'id' => $record->id, + 'name' => $record->name, + 'description' => $record->description, + + 'contact_name' => $record->contact_name ?: null, + 'contact_dect' => $record->contact_dect ?: null, + 'contact_email' => $record->contact_email ?: null, + + 'restricted' => $record->restricted, + 'requires_driver_license' => $record->requires_driver_license, + 'no_self_signup' => $record->no_self_signup, + 'show_on_dashboard' => $record->show_on_dashboard, + 'hide_register' => $record->hide_register, + ]); + } + + $this->changeReferences( + 'angel_types', + 'id', + 'AngelTypes', + 'id', + 'integer' + ); + + $this->schema->drop('angel_types'); + } +} diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index dd4a993c..174e2dc5 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -1,7 +1,11 @@ id); } else { - $angeltypes = AngelTypes(); + $angeltypes = AngelType::all(); } return [ @@ -82,16 +86,17 @@ function angeltype_delete_controller() throw_redirect(page_link_to('angeltypes')); } - $angeltype = load_angeltype(); + $angeltype = AngelType::findOrFail(request()->input('angeltype_id')); if (request()->hasPostData('delete')) { - AngelType_delete($angeltype); + $angeltype->delete(); + engelsystem_log('Deleted angeltype: ' . AngelType_name_render($angeltype, true)); success(sprintf(__('Angeltype %s deleted.'), AngelType_name_render($angeltype))); throw_redirect(page_link_to('angeltypes')); } return [ - sprintf(__('Delete angeltype %s'), $angeltype['name']), + sprintf(__('Delete angeltype %s'), $angeltype->name), AngelType_delete_view($angeltype) ]; } @@ -109,7 +114,7 @@ function angeltype_edit_controller() if ($request->has('angeltype_id')) { // Edit existing angeltype - $angeltype = load_angeltype(); + $angeltype = AngelType::findOrFail($request->input('angeltype_id')); if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) { throw_redirect(page_link_to('angeltypes')); @@ -120,7 +125,7 @@ function angeltype_edit_controller() // Supporters aren't allowed to create new angeltypes. throw_redirect(page_link_to('angeltypes')); } - $angeltype = AngelType_new(); + $angeltype = new AngelType(); } if ($request->hasPostData('submit')) { @@ -129,41 +134,47 @@ function angeltype_edit_controller() if (!$supporter_mode) { if ($request->has('name')) { $result = AngelType_validate_name($request->postData('name'), $angeltype); - $angeltype['name'] = $result->getValue(); + $angeltype->name = $result->getValue(); if (!$result->isValid()) { $valid = false; error(__('Please check the name. Maybe it already exists.')); } } - $angeltype['restricted'] = $request->has('restricted'); - $angeltype['no_self_signup'] = $request->has('no_self_signup'); - $angeltype['show_on_dashboard'] = $request->has('show_on_dashboard'); - $angeltype['hide_register'] = $request->has('hide_register'); + $angeltype->restricted = $request->has('restricted'); + $angeltype->no_self_signup = $request->has('no_self_signup'); + $angeltype->show_on_dashboard = $request->has('show_on_dashboard'); + $angeltype->hide_register = $request->has('hide_register'); - $angeltype['requires_driver_license'] = $request->has('requires_driver_license'); + $angeltype->requires_driver_license = $request->has('requires_driver_license'); } - $angeltype['description'] = strip_request_item_nl('description', $angeltype['description']); + $angeltype->description = strip_request_item_nl('description', $angeltype->description); - $angeltype['contact_name'] = strip_request_item('contact_name', $angeltype['contact_name']); - $angeltype['contact_dect'] = strip_request_item('contact_dect', $angeltype['contact_dect']); - $angeltype['contact_email'] = strip_request_item('contact_email', $angeltype['contact_email']); + $angeltype->contact_name = strip_request_item('contact_name', $angeltype->contact_name); + $angeltype->contact_dect = strip_request_item('contact_dect', $angeltype->contact_dect); + $angeltype->contact_email = strip_request_item('contact_email', $angeltype->contact_email); if ($valid) { - if (!empty($angeltype['id'])) { - AngelType_update($angeltype); - } else { - $angeltype = AngelType_create($angeltype); - } + $angeltype->save(); success('Angel type saved.'); - throw_redirect(angeltype_link($angeltype['id'])); + engelsystem_log( + 'Saved angeltype: ' . $angeltype->name . ($angeltype->restricted ? ', restricted' : '') + . ($angeltype->no_self_signup ? ', no_self_signup' : '') + . ($angeltype->requires_driver_license ? ', requires driver license' : '') . ', ' + . $angeltype->contact_name . ', ' + . $angeltype->contact_dect . ', ' + . $angeltype->contact_email . ', ' + . $angeltype->show_on_dashboard . ', ' + . $angeltype->hide_register + ); + throw_redirect(angeltype_link($angeltype->id)); } } return [ - sprintf(__('Edit %s'), $angeltype['name']), + sprintf(__('Edit %s'), $angeltype->name), AngelType_edit_view($angeltype, $supporter_mode) ]; } @@ -181,7 +192,7 @@ function angeltype_controller() throw_redirect(page_link_to('/')); } - $angeltype = load_angeltype(); + $angeltype = AngelType::findOrFail(request()->input('angeltype_id')); $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype); $members = Users_by_angeltype($angeltype); @@ -201,7 +212,7 @@ function angeltype_controller() $isSupporter = !is_null($user_angeltype) && $user_angeltype['supporter']; return [ - sprintf(__('Team %s'), $angeltype['name']), + sprintf(__('Team %s'), $angeltype->name), AngelType_view( $angeltype, $members, @@ -221,10 +232,10 @@ function angeltype_controller() /** * On which days do shifts for this angeltype occur? Needed for shiftCalendar. * - * @param array $angeltype + * @param AngelType $angeltype * @return array */ -function angeltype_controller_shiftsFilterDays($angeltype) +function angeltype_controller_shiftsFilterDays(AngelType $angeltype) { $all_shifts = Shifts_by_angeltype($angeltype); $days = []; @@ -241,17 +252,17 @@ function angeltype_controller_shiftsFilterDays($angeltype) /** * Sets up the shift filter for the angeltype. * - * @param array $angeltype - * @param array $days + * @param AngelType $angeltype + * @param array $days * @return ShiftsFilter */ -function angeltype_controller_shiftsFilter($angeltype, $days) +function angeltype_controller_shiftsFilter(AngelType $angeltype, $days) { $request = request(); $shiftsFilter = new ShiftsFilter( auth()->can('user_shifts_admin'), Room_ids(), - [$angeltype['id']] + [$angeltype->id] ); $selected_day = date('Y-m-d'); if (!empty($days) && !in_array($selected_day, $days)) { @@ -281,10 +292,10 @@ function angeltypes_list_controller() $angeltypes = AngelTypes_with_user($user->id); - foreach ($angeltypes as &$angeltype) { + foreach ($angeltypes as $angeltype) { $actions = [ button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]), __('view'), 'btn-sm' ) @@ -292,45 +303,45 @@ function angeltypes_list_controller() if (auth()->can('admin_angel_types')) { $actions[] = button( - page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype->id]), __('edit'), 'btn-sm' ); $actions[] = button( - page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype->id]), __('delete'), 'btn-sm' ); } - $angeltype['membership'] = AngelType_render_membership($angeltype); - if (!empty($angeltype['user_angeltype_id'])) { + $angeltype->membership = AngelType_render_membership($angeltype); + if (!empty($angeltype->user_angeltype_id)) { $actions[] = button( page_link_to( 'user_angeltypes', - ['action' => 'delete', 'user_angeltype_id' => $angeltype['user_angeltype_id']] + ['action' => 'delete', 'user_angeltype_id' => $angeltype->user_angeltype_id] ), __('leave'), 'btn-sm' ); } else { $actions[] = button( - page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype['id']]), + page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype->id]), __('join'), 'btn-sm' ); } - $angeltype['restricted'] = $angeltype['restricted'] ? icon('book') : ''; - $angeltype['no_self_signup'] = $angeltype['no_self_signup'] ? '' : icon('pencil-square'); + $angeltype->is_restricted = $angeltype->restricted ? icon('book') : ''; + $angeltype->no_self_signup_allowed = $angeltype->no_self_signup ? '' : icon('pencil-square'); - $angeltype['name'] = ' 'view', 'angeltype_id' => $angeltype->id]) . '">' - . $angeltype['name'] + . $angeltype->name . ''; - $angeltype['actions'] = table_buttons($actions); + $angeltype->actions = table_buttons($actions); } return [ @@ -340,22 +351,49 @@ function angeltypes_list_controller() } /** - * Loads an angeltype from given angeltype_id request param. + * Validates a name for angeltypes. + * Returns ValidationResult containing validation success and validated name. * - * @return array + * @param string $name Wanted name for the angeltype + * @param AngelType $angeltype The angeltype the name is for + * + * @return ValidationResult result and validated name */ -function load_angeltype() +function AngelType_validate_name($name, AngelType $angeltype) { - $request = request(); - if (!$request->has('angeltype_id')) { - throw_redirect(page_link_to('angeltypes')); + $name = strip_item($name); + if ($name == '') { + return new ValidationResult(false, ''); + } + if ($angeltype->id) { + $valid = AngelType::whereName($name) + ->where('id', '!=', $angeltype->id) + ->count() == 0; + return new ValidationResult($valid, $name); } - $angeltype = AngelType($request->input('angeltype_id')); - if (empty($angeltype)) { - error(__('Angeltype doesn\'t exist . ')); - throw_redirect(page_link_to('angeltypes')); - } - - return $angeltype; + $valid = AngelType::whereName($name)->count() == 0; + return new ValidationResult($valid, $name); +} + +/** + * Returns all angeltypes and subscription state to each of them for given user. + * + * @param int $userId + * @return Collection|AngelType[] + */ +function AngelTypes_with_user($userId): Collection +{ + return AngelType::query() + ->select([ + 'angel_types.*', + 'UserAngelTypes.id AS user_angeltype_id', + 'UserAngelTypes.confirm_user_id', + 'UserAngelTypes.supporter', + ]) + ->leftJoin('UserAngelTypes', function (JoinClause $join) use ($userId) { + $join->on('angel_types.id', 'UserAngelTypes.angeltype_id'); + $join->where('UserAngelTypes.user_id', $userId); + }) + ->get(); } diff --git a/includes/controller/public_dashboard_controller.php b/includes/controller/public_dashboard_controller.php index 7732a07b..34ec7524 100644 --- a/includes/controller/public_dashboard_controller.php +++ b/includes/controller/public_dashboard_controller.php @@ -1,5 +1,6 @@ 0 && (!$filter || in_array($needed_angel['TID'], $filter->getTypes()))) { - $angeltype = AngelType($needed_angel['TID']); - if ($angeltype['show_on_dashboard']) { + $angeltype = AngelType::find($needed_angel['TID']); + if ($angeltype->show_on_dashboard) { $result[] = [ 'need' => $need, - 'angeltype_name' => $angeltype['name'] + 'angeltype_name' => $angeltype->name ]; } } diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php index b9f299d4..6a57d260 100644 --- a/includes/controller/rooms_controller.php +++ b/includes/controller/rooms_controller.php @@ -1,5 +1,6 @@ id], - AngelType_ids() + AngelType::query()->get('id')->pluck('id')->toArray() ); $selected_day = date('Y-m-d'); if (!empty($days) && !in_array($selected_day, $days)) { diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 8fea8bdb..d064e684 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -1,5 +1,6 @@ id)); } - $angeltype = AngelType($request->input('angeltype_id')); + $angeltype = AngelType::find($request->input('angeltype_id')); if (auth()->can('user_shifts_admin')) { return shift_entry_create_controller_admin($shift, $angeltype); @@ -72,11 +73,11 @@ function shift_entry_create_controller(): array * Sign up for a shift. * Case: Admin * - * @param array $shift - * @param array $angeltype + * @param array $shift + * @param AngelType|null $angeltype * @return array */ -function shift_entry_create_controller_admin($shift, $angeltype): array +function shift_entry_create_controller_admin($shift, ?AngelType $angeltype): array { $signup_user = auth()->user(); $request = request(); @@ -88,9 +89,9 @@ function shift_entry_create_controller_admin($shift, $angeltype): array throw_redirect(shift_link($shift)); } - $angeltypes = AngelTypes(); + $angeltypes = AngelType::all(); if ($request->hasPostData('angeltype_id')) { - $angeltype = AngelType($request->postData('angeltype_id')); + $angeltype = AngelType::find($request->postData('angeltype_id')); } if (empty($angeltype)) { if (count($angeltypes) == 0) { @@ -102,7 +103,7 @@ function shift_entry_create_controller_admin($shift, $angeltype): array if ($request->hasPostData('submit')) { ShiftEntry_create([ 'SID' => $shift['SID'], - 'TID' => $angeltype['id'], + 'TID' => $angeltype->id, 'UID' => $signup_user->id, 'Comment' => '', 'freeloaded' => false, @@ -120,11 +121,7 @@ function shift_entry_create_controller_admin($shift, $angeltype): array $users_select[$user->id] = $user->name; } - $angeltypes_select = []; - foreach ($angeltypes as $a) { - $angeltypes_select[$a['id']] = $a['name']; - } - + $angeltypes_select = $angeltypes->pluck('name', 'id')->toArray(); $room = Room::find($shift['RID']); return [ ShiftEntry_create_title(), @@ -136,11 +133,11 @@ function shift_entry_create_controller_admin($shift, $angeltype): array * Sign up for a shift. * Case: Supporter * - * @param array $shift - * @param array $angeltype + * @param array $shift + * @param AngelType $angeltype * @return array */ -function shift_entry_create_controller_supporter($shift, $angeltype): array +function shift_entry_create_controller_supporter($shift, AngelType $angeltype): array { $request = request(); $signup_user = auth()->user(); @@ -156,7 +153,7 @@ function shift_entry_create_controller_supporter($shift, $angeltype): array if ($request->hasPostData('submit')) { ShiftEntry_create([ 'SID' => $shift['SID'], - 'TID' => $angeltype['id'], + 'TID' => $angeltype->id, 'UID' => $signup_user->id, 'Comment' => '', 'freeloaded' => false, @@ -209,16 +206,16 @@ function shift_entry_error_message(ShiftSignupState $shift_signup_state) * Case: User * * @param array $shift - * @param array $angeltype + * @param AngelType $angeltype * @return array */ -function shift_entry_create_controller_user($shift, $angeltype): array +function shift_entry_create_controller_user($shift, AngelType $angeltype): array { $request = request(); $signup_user = auth()->user(); - $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype); - $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']); + $needed_angeltype = (new AngelType())->forceFill(NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)); + $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype->id); $shift_signup_state = Shift_signup_allowed( $signup_user, $shift, @@ -238,14 +235,14 @@ function shift_entry_create_controller_user($shift, $angeltype): array $comment = strip_request_item_nl('comment'); ShiftEntry_create([ 'SID' => $shift['SID'], - 'TID' => $angeltype['id'], + 'TID' => $angeltype->id, 'UID' => $signup_user->id, 'Comment' => $comment, 'freeloaded' => false, 'freeload_comment' => '' ]); - if ($angeltype['restricted'] == false && !UserAngelType_exists($signup_user->id, $angeltype)) { + if (!$angeltype->restricted && !UserAngelType_exists($signup_user->id, $angeltype)) { UserAngelType_create($signup_user->id, $angeltype); } @@ -264,16 +261,16 @@ function shift_entry_create_controller_user($shift, $angeltype): array * Link to create a shift entry. * * @param array $shift - * @param array $angeltype + * @param AngelType $angeltype * @param array $params * @return string URL */ -function shift_entry_create_link($shift, $angeltype, $params = []) +function shift_entry_create_link($shift, AngelType $angeltype, $params = []) { $params = array_merge([ 'action' => 'create', 'shift_id' => $shift['SID'], - 'angeltype_id' => $angeltype['id'] + 'angeltype_id' => $angeltype->id ], $params); return page_link_to('shift_entries', $params); } @@ -327,7 +324,7 @@ function shift_entry_delete_controller() $shiftEntry = shift_entry_load(); $shift = Shift($shiftEntry['SID']); - $angeltype = AngelType($shiftEntry['TID']); + $angeltype = AngelType::find($shiftEntry['TID']); $signout_user = User::find($shiftEntry['UID']); if (!Shift_signout_allowed($shift, $angeltype, $signout_user->id)) { error(__( diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 6c6b4ffb..14df7b7e 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -2,6 +2,7 @@ use Engelsystem\Helpers\Carbon; use Engelsystem\Http\Exceptions\HttpForbidden; +use Engelsystem\Models\AngelType; use Engelsystem\Models\Room; use Engelsystem\Models\Shifts\ScheduleShift; use Engelsystem\Models\Shifts\ShiftType; @@ -70,14 +71,10 @@ function shift_edit_controller() foreach (Rooms() as $room) { $rooms[$room->id] = $room->name; } - $angeltypes = select_array(AngelTypes(), 'id', 'name'); - $shifttypes = select_array(ShiftType::all(), 'id', 'name'); + $angeltypes = AngelType::all()->pluck('name', 'id')->toArray(); + $shifttypes = ShiftType::all()->pluck('name', 'id')->toArray(); - $needed_angel_types = select_array( - NeededAngelTypes_by_shift($shift_id), - 'angel_type_id', - 'count' - ); + $needed_angel_types = collect(NeededAngelTypes_by_shift($shift_id))->pluck('count', 'angel_type_id')->toArray(); foreach (array_keys($angeltypes) as $angeltype_id) { if (!isset($needed_angel_types[$angeltype_id])) { $needed_angel_types[$angeltype_id] = 0; @@ -242,14 +239,14 @@ function shift_delete_controller() if ($request->hasPostData('delete')) { $room = Room::find($shift['RID']); foreach ($shift['ShiftEntry'] as $entry) { - $type = AngelType($entry['TID']); + $type = AngelType::find($entry['TID']); event('shift.entry.deleting', [ 'user' => User::find($entry['user_id']), 'start' => Carbon::createFromTimestamp($shift['start']), 'end' => Carbon::createFromTimestamp($shift['end']), 'name' => $shift['name'], 'title' => $shift['title'], - 'type' => $type['name'], + 'type' => $type->name, 'room' => $room, 'freeloaded' => (bool)$entry['freeloaded'], ]); @@ -304,17 +301,18 @@ function shift_controller() $shifttype = ShiftType::find($shift['shifttype_id']); $room = Room::find($shift['RID']); - $angeltypes = AngelTypes(); + $angeltypes = AngelType::all(); $user_shifts = Shifts_by_user($user->id); $shift_signup_state = new ShiftSignupState(ShiftSignupState::OCCUPIED, 0); - foreach ($angeltypes as &$angeltype) { + foreach ($angeltypes as $angeltype) { $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype); if (empty($needed_angeltype)) { continue; } - $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']); + $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype->id); + $needed_angeltype = (new AngelType())->forceFill($needed_angeltype); $angeltype_signup_state = Shift_signup_allowed( $user, @@ -326,7 +324,7 @@ function shift_controller() $shift_entries ); $shift_signup_state->combineWith($angeltype_signup_state); - $angeltype['shift_signup_state'] = $angeltype_signup_state; + $angeltype->shift_signup_state = $angeltype_signup_state; } return [ diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index a0d4500b..01471ceb 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -1,6 +1,7 @@ input('angeltype_id')); + $angeltype = AngelType::find($request->input('angeltype_id')); if (empty($angeltype)) { error(__('Angeltype doesn\'t exist.')); throw_redirect(page_link_to('angeltypes')); @@ -63,11 +65,11 @@ function user_angeltypes_delete_all_controller(): array } if ($request->hasPostData('deny_all')) { - UserAngelTypes_delete_all($angeltype['id']); + UserAngelTypes_delete_all_unconfirmed($angeltype->id); engelsystem_log(sprintf('Denied all users for angeltype %s', AngelType_name_render($angeltype, true))); success(sprintf(__('Denied all users for angeltype %s.'), AngelType_name_render($angeltype))); - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } return [ @@ -91,20 +93,15 @@ function user_angeltypes_confirm_all_controller(): array throw_redirect(page_link_to('angeltypes')); } - $angeltype = AngelType($request->input('angeltype_id')); - if (empty($angeltype)) { - error(__('Angeltype doesn\'t exist.')); - throw_redirect(page_link_to('angeltypes')); - } - + $angeltype = AngelType::findOrFail($request->input('angeltype_id')); if (!auth()->can('admin_user_angeltypes') && !User_is_AngelType_supporter($user, $angeltype)) { error(__('You are not allowed to confirm all users for this angeltype.')); throw_redirect(page_link_to('angeltypes')); } if ($request->hasPostData('confirm_all')) { - $users = UserAngelTypes_all_unconfirmed($angeltype['id']); - UserAngelTypes_confirm_all($angeltype['id'], $user->id); + $users = UserAngelTypes_all_unconfirmed($angeltype->id); + UserAngelTypes_confirm_all($angeltype->id, $user->id); engelsystem_log(sprintf('Confirmed all users for angeltype %s', AngelType_name_render($angeltype, true))); success(sprintf(__('Confirmed all users for angeltype %s.'), AngelType_name_render($angeltype))); @@ -114,7 +111,7 @@ function user_angeltypes_confirm_all_controller(): array user_angeltype_confirm_email($user, $angeltype); } - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } return [ @@ -124,7 +121,7 @@ function user_angeltypes_confirm_all_controller(): array } /** - * Confirm an user for an angeltype. + * Confirm a user for an angeltype. * * @return array */ @@ -144,12 +141,7 @@ function user_angeltype_confirm_controller(): array throw_redirect(page_link_to('angeltypes')); } - $angeltype = AngelType($user_angeltype['angeltype_id']); - if (empty($angeltype)) { - error(__('Angeltype doesn\'t exist.')); - throw_redirect(page_link_to('angeltypes')); - } - + $angeltype = AngelType::findOrFail($user_angeltype['angeltype_id']); if (!User_is_AngelType_supporter($user, $angeltype)) { error(__('You are not allowed to confirm this users angeltype.')); throw_redirect(page_link_to('angeltypes')); @@ -177,7 +169,7 @@ function user_angeltype_confirm_controller(): array user_angeltype_confirm_email($user_source, $angeltype); - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } return [ @@ -187,11 +179,11 @@ function user_angeltype_confirm_controller(): array } /** - * @param User $user - * @param array $angeltype + * @param User $user + * @param AngelType $angeltype * @return void */ -function user_angeltype_confirm_email(User $user, array $angeltype): void +function user_angeltype_confirm_email(User $user, AngelType $angeltype): void { if (!$user->settings->email_shiftinfo) { return; @@ -204,7 +196,7 @@ function user_angeltype_confirm_email(User $user, array $angeltype): void $user, 'notification.angeltype.confirmed', 'emails/angeltype-confirmed', - ['name' => $angeltype['name'], 'angeltype' => $angeltype, 'username' => $user->name] + ['name' => $angeltype->name, 'angeltype' => $angeltype, 'username' => $user->name] ); } catch (TransportException $e) { /** @var LoggerInterface $logger */ @@ -217,11 +209,11 @@ function user_angeltype_confirm_email(User $user, array $angeltype): void } /** - * @param User $user - * @param array $angeltype + * @param User $user + * @param AngelType $angeltype * @return void */ -function user_angeltype_add_email(User $user, array $angeltype): void +function user_angeltype_add_email(User $user, AngelType $angeltype): void { if (!$user->settings->email_shiftinfo || $user->id == auth()->user()->id) { return; @@ -234,7 +226,7 @@ function user_angeltype_add_email(User $user, array $angeltype): void $user, 'notification.angeltype.added', 'emails/angeltype-added', - ['name' => $angeltype['name'], 'angeltype' => $angeltype, 'username' => $user->name] + ['name' => $angeltype->name, 'angeltype' => $angeltype, 'username' => $user->name] ); } catch (TransportException $e) { /** @var LoggerInterface $logger */ @@ -267,18 +259,8 @@ function user_angeltype_delete_controller(): array throw_redirect(page_link_to('angeltypes')); } - $angeltype = AngelType($user_angeltype['angeltype_id']); - if (empty($angeltype)) { - error(__('Angeltype doesn\'t exist.')); - throw_redirect(page_link_to('angeltypes')); - } - - $user_source = User::find($user_angeltype['user_id']); - if (!$user_source) { - error(__('User doesn\'t exist.')); - throw_redirect(page_link_to('angeltypes')); - } - + $angeltype = AngelType::findOrFail($user_angeltype['angeltype_id']); + $user_source = User::findOrFail($user_angeltype['user_id']); if ($user->id != $user_angeltype['user_id'] && !User_is_AngelType_supporter($user, $angeltype)) { error(__('You are not allowed to delete this users angeltype.')); throw_redirect(page_link_to('angeltypes')); @@ -287,10 +269,10 @@ function user_angeltype_delete_controller(): array if ($request->hasPostData('delete')) { UserAngelType_delete($user_angeltype); - engelsystem_log(sprintf('User %s removed from %s.', User_Nick_render($user_source, true), $angeltype['name'])); - success(sprintf(__('User %s removed from %s.'), User_Nick_render($user_source), $angeltype['name'])); + engelsystem_log(sprintf('User %s removed from %s.', User_Nick_render($user_source, true), $angeltype->name)); + success(sprintf(__('User %s removed from %s.'), User_Nick_render($user_source), $angeltype->name)); - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } return [ @@ -332,17 +314,8 @@ function user_angeltype_update_controller(): array throw_redirect(page_link_to('angeltypes')); } - $angeltype = AngelType($user_angeltype['angeltype_id']); - if (empty($angeltype)) { - error(__('Angeltype doesn\'t exist.')); - throw_redirect(page_link_to('angeltypes')); - } - - $user_source = User::find($user_angeltype['user_id']); - if (!$user_source) { - error(__('User doesn\'t exist.')); - throw_redirect(page_link_to('angeltypes')); - } + $angeltype = AngelType::findOrFail($user_angeltype['angeltype_id']); + $user_source = User::findOrFail($user_angeltype['user_id']); if ($request->hasPostData('submit')) { UserAngelType_update($user_angeltype['id'], $supporter); @@ -361,7 +334,7 @@ function user_angeltype_update_controller(): array User_Nick_render($user_source) )); - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } return [ @@ -377,7 +350,7 @@ function user_angeltype_update_controller(): array */ function user_angeltype_add_controller(): array { - $angeltype = load_angeltype(); + $angeltype = AngelType::findOrFail(request()->input('angeltype_id')); // User is joining by itself if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) { @@ -421,7 +394,7 @@ function user_angeltype_add_controller(): array user_angeltype_add_email($user_source, $angeltype); - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } } @@ -434,16 +407,16 @@ function user_angeltype_add_controller(): array /** * A user joins an angeltype. * - * @param array $angeltype + * @param AngelType $angeltype * @return array */ -function user_angeltype_join_controller($angeltype) +function user_angeltype_join_controller(AngelType $angeltype) { $user = auth()->user(); $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype); if (!empty($user_angeltype)) { - error(sprintf(__('You are already a %s.'), $angeltype['name'])); + error(sprintf(__('You are already a %s.'), $angeltype->name)); throw_redirect(page_link_to('angeltypes')); } @@ -451,7 +424,7 @@ function user_angeltype_join_controller($angeltype) if ($request->hasPostData('submit')) { $user_angeltype_id = UserAngelType_create($user->id, $angeltype); - $success_message = sprintf(__('You joined %s.'), $angeltype['name']); + $success_message = sprintf(__('You joined %s.'), $angeltype->name); engelsystem_log(sprintf( 'User %s joined %s.', User_Nick_render($user, true), @@ -468,11 +441,11 @@ function user_angeltype_join_controller($angeltype) )); } - throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']])); + throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])); } return [ - sprintf(__('Become a %s'), $angeltype['name']), + sprintf(__('Become a %s'), $angeltype->name), UserAngelType_join_view($user, $angeltype) ]; } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index ba172a9c..48bbcc72 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -212,11 +212,11 @@ function user_controller() // TODO: Move queries to model $shift['needed_angeltypes'] = Db::select( ' - SELECT DISTINCT `AngelTypes`.* + SELECT DISTINCT `angel_types`.* FROM `ShiftEntry` - JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` + JOIN `angel_types` ON `ShiftEntry`.`TID`=`angel_types`.`id` WHERE `ShiftEntry`.`SID` = ? - ORDER BY `AngelTypes`.`name` + ORDER BY `angel_types`.`name` ', [$shift['SID']] ); diff --git a/includes/helper/oauth_helper.php b/includes/helper/oauth_helper.php index aec444f2..ab6d6668 100644 --- a/includes/helper/oauth_helper.php +++ b/includes/helper/oauth_helper.php @@ -4,6 +4,7 @@ namespace Engelsystem\Events\Listener; use Engelsystem\Config\Config; use Engelsystem\Database\Database; +use Engelsystem\Models\AngelType; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Psr\Log\LoggerInterface; @@ -55,8 +56,8 @@ class OAuth2 'SSO {provider}: Added to angeltype {angeltype}, confirmed: {confirmed}, supporter: {supporter}', [ 'provider' => $provider, - 'angeltype' => AngelType($team['id'])['name'], - 'confirmed' => $confirmed ? 'yes' : 'no', + 'angeltype' => AngelType::find($team['id'])->name, + 'confirmed' => $confirmed ? 'yes' : 'no', 'supporter' => $supporter ? 'yes' : 'no', ] ); @@ -81,7 +82,7 @@ class OAuth2 'SSO {provider}: Set supporter state for angeltype {angeltype}', [ 'provider' => $provider, - 'angeltype' => AngelType($userAngeltype->angeltype_id)['name'], + 'angeltype' => AngelType::find($userAngeltype->angeltype_id)->name, ] ); } @@ -95,7 +96,7 @@ class OAuth2 'SSO {provider}: Set confirmed state for angeltype {angeltype}', [ 'provider' => $provider, - 'angeltype' => AngelType($userAngeltype->angeltype_id)['name'], + 'angeltype' => AngelType::find($userAngeltype->angeltype_id)->name, ] ); } diff --git a/includes/includes.php b/includes/includes.php index d7ed25ce..a814d83d 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -11,7 +11,6 @@ $includeFiles = [ __DIR__ . '/../includes/sys_page.php', __DIR__ . '/../includes/sys_template.php', - __DIR__ . '/../includes/model/AngelType_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/Room_model.php', __DIR__ . '/../includes/model/ShiftEntry_model.php', diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php deleted file mode 100644 index 937afecf..00000000 --- a/includes/model/AngelType_model.php +++ /dev/null @@ -1,249 +0,0 @@ - null, - 'name' => '', - 'restricted' => false, - 'no_self_signup' => false, - 'description' => '', - 'requires_driver_license' => false, - 'contact_name' => null, - 'contact_dect' => null, - 'contact_email' => null, - 'show_on_dashboard' => true, - 'hide_register' => false - ]; -} - -/** - * Checks if the angeltype has any contact information. - * - * @param array $angeltype Angeltype - * @return bool - */ -function AngelType_has_contact_info($angeltype) -{ - return !empty($angeltype['contact_name']) - || !empty($angeltype['contact_dect']) - || !empty($angeltype['contact_email']); -} - -/** - * Delete an Angeltype. - * - * @param array $angeltype - */ -function AngelType_delete($angeltype) -{ - Db::delete(' - DELETE FROM `AngelTypes` - WHERE `id`=? - LIMIT 1 - ', [$angeltype['id']]); - engelsystem_log('Deleted angeltype: ' . AngelType_name_render($angeltype, true)); -} - -/** - * Update Angeltype. - * - * @param array $angeltype The angeltype - */ -function AngelType_update($angeltype) -{ - Db::update( - ' - UPDATE `AngelTypes` SET - `name` = ?, - `restricted` = ?, - `description` = ?, - `requires_driver_license` = ?, - `no_self_signup` = ?, - `contact_name` = ?, - `contact_dect` = ?, - `contact_email` = ?, - `show_on_dashboard` = ?, - `hide_register` = ? - WHERE `id` = ? - ', - [ - $angeltype['name'], - (int)$angeltype['restricted'], - $angeltype['description'], - (int)$angeltype['requires_driver_license'], - (int)$angeltype['no_self_signup'], - $angeltype['contact_name'], - $angeltype['contact_dect'], - $angeltype['contact_email'], - (int)$angeltype['show_on_dashboard'], - (int)$angeltype['hide_register'], - $angeltype['id'], - ] - ); - - engelsystem_log( - 'Updated angeltype: ' . $angeltype['name'] . ($angeltype['restricted'] ? ', restricted' : '') - . ($angeltype['no_self_signup'] ? ', no_self_signup' : '') - . ($angeltype['requires_driver_license'] ? ', requires driver license' : '') . ', ' - . $angeltype['contact_name'] . ', ' - . $angeltype['contact_dect'] . ', ' - . $angeltype['contact_email'] . ', ' - . $angeltype['show_on_dashboard'] . ', ' - . $angeltype['hide_register'] - ); -} - -/** - * Create an Angeltype. - * - * @param array $angeltype The angeltype - * @return array the created angeltype - */ -function AngelType_create($angeltype) -{ - Db::insert( - ' - INSERT INTO `AngelTypes` ( - `name`, - `restricted`, - `description`, - `requires_driver_license`, - `no_self_signup`, - `contact_name`, - `contact_dect`, - `contact_email`, - `show_on_dashboard`, - `hide_register` - ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ', - [ - $angeltype['name'], - (int)$angeltype['restricted'], - $angeltype['description'], - (int)$angeltype['requires_driver_license'], - (int)$angeltype['no_self_signup'], - $angeltype['contact_name'], - $angeltype['contact_dect'], - $angeltype['contact_email'], - (int)$angeltype['show_on_dashboard'], - (int)$angeltype['hide_register'], - ] - ); - - $angeltype['id'] = Db::getPdo()->lastInsertId(); - engelsystem_log( - 'Created angeltype: ' . $angeltype['name'] - . ($angeltype['restricted'] ? ', restricted' : '') - . ($angeltype['requires_driver_license'] ? ', requires driver license' : '') . ', ' - . $angeltype['contact_name'] . ', ' - . $angeltype['contact_dect'] . ', ' - . $angeltype['contact_email'] . ', ' - . $angeltype['show_on_dashboard'] . ', ' - . $angeltype['hide_register'] - ); - - return $angeltype; -} - -/** - * Validates a name for angeltypes. - * Returns ValidationResult containing validation success and validated name. - * - * @param string $name Wanted name for the angeltype - * @param array $angeltype The angeltype the name is for - * - * @return ValidationResult result and validated name - */ -function AngelType_validate_name($name, $angeltype) -{ - $name = strip_item($name); - if ($name == '') { - return new ValidationResult(false, ''); - } - if (!empty($angeltype) && isset($angeltype['id'])) { - $valid = (count(Db::select(' - SELECT `id` - FROM `AngelTypes` - WHERE `name`=? - AND NOT `id`=? - LIMIT 1 - ', [$name, $angeltype['id']])) == 0); - return new ValidationResult($valid, $name); - } - $valid = (count(Db::select(' - SELECT `id` - FROM `AngelTypes` - WHERE `name`=? - LIMIT 1', [$name])) == 0); - - return new ValidationResult($valid, $name); -} - -/** - * Returns all angeltypes and subscription state to each of them for given user. - * - * @param int $userId - * @return array - */ -function AngelTypes_with_user($userId) -{ - return Db::select(' - SELECT `AngelTypes`.*, - `UserAngelTypes`.`id` AS `user_angeltype_id`, - `UserAngelTypes`.`confirm_user_id`, - `UserAngelTypes`.`supporter` - FROM `AngelTypes` - LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` - AND `UserAngelTypes`.`user_id` = ? - ORDER BY `name`', [$userId]); -} - -/** - * Returns all angeltypes. - * - * @return array - */ -function AngelTypes() -{ - return Db::select(' - SELECT * - FROM `AngelTypes` - ORDER BY `name` - '); -} - -/** - * Returns AngelType id array - * - * @return array - */ -function AngelType_ids() -{ - $result = Db::select('SELECT `id` FROM `AngelTypes`'); - return select_array($result, 'id', 'id'); -} - -/** - * Returns angelType by id. - * - * @param int $angeltype_id angelType ID - * @return array|null - */ -function AngelType($angeltype_id) -{ - $angelType = Db::selectOne( - 'SELECT * FROM `AngelTypes` WHERE `id`=?', - [$angeltype_id] - ); - - return empty($angelType) ? null : $angelType; -} diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index fc47821a..b07fbc8d 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -9,7 +9,7 @@ use Engelsystem\Database\Db; /** * Insert a new needed angel type. * - * @param int $shift_id The shift. Can be null, but then a room_id must be given. + * @param int|null $shift_id The shift. Can be null, but then a room_id must be given. * @param int $angeltype_id The angeltype * @param int|null $room_id The room. Can be null, but then a shift_id must be given. * @param int $count How many angels are needed? @@ -83,12 +83,12 @@ function NeededAngelTypes_by_shift($shiftId) ' SELECT `NeededAngelTypes`.*, - `AngelTypes`.`id`, - `AngelTypes`.`name`, - `AngelTypes`.`restricted`, - `AngelTypes`.`no_self_signup` + `angel_types`.`id`, + `angel_types`.`name`, + `angel_types`.`restricted`, + `angel_types`.`no_self_signup` FROM `NeededAngelTypes` - JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id` + JOIN `angel_types` ON `angel_types`.`id` = `NeededAngelTypes`.`angel_type_id` WHERE `shift_id` = ? AND `count` > 0 ORDER BY `room_id` DESC', @@ -98,9 +98,9 @@ function NeededAngelTypes_by_shift($shiftId) // Use settings from room if (count($needed_angeltypes_source) == 0) { $needed_angeltypes_source = Db::select(' - SELECT `NeededAngelTypes`.*, `AngelTypes`.`name`, `AngelTypes`.`restricted` + SELECT `NeededAngelTypes`.*, `angel_types`.`name`, `angel_types`.`restricted` FROM `NeededAngelTypes` - JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id` + JOIN `angel_types` ON `angel_types`.`id` = `NeededAngelTypes`.`angel_type_id` JOIN `Shifts` ON `Shifts`.`RID` = `NeededAngelTypes`.`room_id` WHERE `Shifts`.`SID` = ? AND `count` > 0 diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index a829a362..0c6d35f9 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -2,6 +2,7 @@ use Carbon\Carbon; use Engelsystem\Database\Db; +use Engelsystem\Models\AngelType; use Engelsystem\Models\Room; use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\User\User; @@ -37,12 +38,12 @@ function ShiftEntries_by_shift($shift_id) `ShiftEntry`.`UID`, `ShiftEntry`.`TID`, `ShiftEntry`.`SID`, - `AngelTypes`.`name` AS `angel_type_name`, + `angel_types`.`name` AS `angel_type_name`, `ShiftEntry`.`Comment`, `ShiftEntry`.`freeloaded` FROM `ShiftEntry` JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id` - JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` + JOIN `angel_types` ON `ShiftEntry`.`TID`=`angel_types`.`id` WHERE `ShiftEntry`.`SID` = ? ', [$shift_id] @@ -61,7 +62,7 @@ function ShiftEntry_create($shift_entry) $shift = Shift($shift_entry['SID']); $shifttype = ShiftType::find($shift['shifttype_id']); $room = Room::find($shift['RID']); - $angeltype = AngelType($shift_entry['TID']); + $angeltype = AngelType::find($shift_entry['TID']); $result = Db::insert( ' INSERT INTO `ShiftEntry` ( @@ -90,7 +91,7 @@ function ShiftEntry_create($shift_entry) . ' at ' . $room->name . ' from ' . date('Y-m-d H:i', $shift['start']) . ' to ' . date('Y-m-d H:i', $shift['end']) - . ' as ' . $angeltype['name'] + . ' as ' . $angeltype->name ); mail_shift_assign($user, $shift); @@ -148,7 +149,7 @@ function ShiftEntry_delete($shiftEntry) $shift = Shift($shiftEntry['SID']); $shifttype = ShiftType::find($shift['shifttype_id']); $room = Room::find($shift['RID']); - $angeltype = AngelType($shiftEntry['TID']); + $angeltype = AngelType::find($shiftEntry['TID']); engelsystem_log( 'Shift signout: ' . User_Nick_render($signout_user, true) @@ -157,7 +158,7 @@ function ShiftEntry_delete($shiftEntry) . ' at ' . $room->name . ' from ' . date('Y-m-d H:i', $shift['start']) . ' to ' . date('Y-m-d H:i', $shift['end']) - . ' as ' . $angeltype['name'] + . ' as ' . $angeltype->name ); mail_shift_removed(User::find($shiftEntry['UID']), Shift($shiftEntry['SID'])); diff --git a/includes/model/ShiftsFilter.php b/includes/model/ShiftsFilter.php index ff805f08..14ce5e27 100644 --- a/includes/model/ShiftsFilter.php +++ b/includes/model/ShiftsFilter.php @@ -46,12 +46,12 @@ class ShiftsFilter * * @param bool $user_shifts_admin * @param int[] $rooms - * @param int[] $types + * @param int[] $angelTypes */ - public function __construct($user_shifts_admin = false, $rooms = [], $types = []) + public function __construct($user_shifts_admin = false, $rooms = [], $angelTypes = []) { $this->rooms = $rooms; - $this->types = $types; + $this->types = $angelTypes; $this->filled = [ ShiftsFilter::FILLED_FREE diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index fdd57cca..c4703b1e 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -1,6 +1,7 @@ 0 AND NOT s.shift_id IS NULL - ', [$angeltype['id'], $angeltype['id']]); + ', [$angeltype->id, $angeltype->id]); } /** * Returns every shift with needed angels in the given time range. * * @param int $start timestamp - * @param int $end timestamp + * @param int $end timestamp * @param ShiftsFilter|null $filter * * @return array @@ -150,13 +151,13 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, - `AngelTypes`.`id`, - `AngelTypes`.`name`, - `AngelTypes`.`restricted`, - `AngelTypes`.`no_self_signup` + `angel_types`.`id`, + `angel_types`.`name`, + `angel_types`.`restricted`, + `angel_types`.`no_self_signup` FROM `Shifts` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` - JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` + JOIN `angel_types` ON `angel_types`.`id`= `NeededAngelTypes`.`angel_type_id` LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') AND `start` BETWEEN ? AND ? @@ -167,13 +168,13 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, - `AngelTypes`.`id`, - `AngelTypes`.`name`, - `AngelTypes`.`restricted`, - `AngelTypes`.`no_self_signup` + `angel_types`.`id`, + `angel_types`.`name`, + `angel_types`.`restricted`, + `angel_types`.`no_self_signup` FROM `Shifts` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` - JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` + JOIN `angel_types` ON `angel_types`.`id`= `NeededAngelTypes`.`angel_type_id` LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') AND `start` BETWEEN ? AND ? @@ -192,27 +193,27 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter) } /** - * @param array $shift - * @param array $angeltype + * @param array $shift + * @param AngelType $angeltype * @return array|null */ -function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) +function NeededAngeltype_by_Shift_and_Angeltype($shift, AngelType $angeltype) { return Db::selectOne( ' SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, - `AngelTypes`.`id`, - `AngelTypes`.`name`, - `AngelTypes`.`restricted`, - `AngelTypes`.`no_self_signup` + `angel_types`.`id`, + `angel_types`.`name`, + `angel_types`.`restricted`, + `angel_types`.`no_self_signup` FROM `Shifts` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` - JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` + JOIN `angel_types` ON `angel_types`.`id`= `NeededAngelTypes`.`angel_type_id` LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id WHERE `Shifts`.`SID`=? - AND `AngelTypes`.`id`=? + AND `angel_types`.`id`=? AND s.shift_id IS NULL UNION @@ -220,23 +221,23 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) SELECT `NeededAngelTypes`.*, `Shifts`.`SID`, - `AngelTypes`.`id`, - `AngelTypes`.`name`, - `AngelTypes`.`restricted`, - `AngelTypes`.`no_self_signup` + `angel_types`.`id`, + `angel_types`.`name`, + `angel_types`.`restricted`, + `angel_types`.`no_self_signup` FROM `Shifts` JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` - JOIN `AngelTypes` ON `AngelTypes`.`id`= `NeededAngelTypes`.`angel_type_id` + JOIN `angel_types` ON `angel_types`.`id`= `NeededAngelTypes`.`angel_type_id` LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id WHERE `Shifts`.`SID`=? - AND `AngelTypes`.`id`=? + AND `angel_types`.`id`=? AND NOT s.shift_id IS NULL ', [ $shift['SID'], - $angeltype['id'], + $angeltype->id, $shift['SID'], - $angeltype['id'] + $angeltype->id ] ); } @@ -296,11 +297,11 @@ function Shift_collides($shift, $shifts) /** * Returns the number of needed angels/free shift entries for an angeltype. * - * @param array $needed_angeltype - * @param array[] $shift_entries + * @param AngelType $needed_angeltype + * @param array[] $shift_entries * @return int */ -function Shift_free_entries($needed_angeltype, $shift_entries) +function Shift_free_entries(AngelType $needed_angeltype, $shift_entries) { $taken = 0; foreach ($shift_entries as $shift_entry) { @@ -309,7 +310,7 @@ function Shift_free_entries($needed_angeltype, $shift_entries) } } - $neededAngels = !empty($needed_angeltype) ? $needed_angeltype['count'] : 0; + $neededAngels = $needed_angeltype->count ?: 0; return max(0, $neededAngels - $taken); } @@ -317,21 +318,21 @@ 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 array $shift The shift - * @param array $angeltype The angeltype to which the user wants to sign up + * @param array $shift The shift + * @param AngelType $angeltype The angeltype to which the user wants to sign up * @param array|null $user_angeltype * @param array|null $user_shifts List of the users shifts - * @param array $needed_angeltype + * @param AngelType $needed_angeltype * @param array[] $shift_entries * @return ShiftSignupState */ function Shift_signup_allowed_angel( $user, $shift, - $angeltype, + AngelType $angeltype, $user_angeltype, $user_shifts, - $needed_angeltype, + AngelType $needed_angeltype, $shift_entries ) { $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); @@ -361,7 +362,9 @@ function Shift_signup_allowed_angel( return new ShiftSignupState(ShiftSignupState::SIGNED_UP, $free_entries); } - $shift_post_signup_total_allowed_seconds = (config('signup_post_fraction') * ($shift['end'] - $shift['start'])) + (config('signup_post_minutes') * 60); + $shift_post_signup_total_allowed_seconds = + (config('signup_post_fraction') * ($shift['end'] - $shift['start'])) + + (config('signup_post_minutes') * 60); if (time() > $shift['start'] + $shift_post_signup_total_allowed_seconds) { // you can only join if the shift is in future @@ -378,8 +381,8 @@ function Shift_signup_allowed_angel( if ( empty($user_angeltype) - || $angeltype['no_self_signup'] == 1 - || ($angeltype['restricted'] == 1 && !isset($user_angeltype['confirm_user_id'])) + || $angeltype->no_self_signup == 1 + || ($angeltype->restricted == 1 && !isset($user_angeltype['confirm_user_id'])) ) { // you cannot join if user is not of this angel type // you cannot join if you are not confirmed @@ -400,11 +403,11 @@ function Shift_signup_allowed_angel( /** * Check if an angeltype supporter can sign up a user to a shift. * - * @param array $needed_angeltype - * @param array[] $shift_entries + * @param AngelType $needed_angeltype + * @param array[] $shift_entries * @return ShiftSignupState */ -function Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entries) +function Shift_signup_allowed_angeltype_supporter(AngelType $needed_angeltype, $shift_entries) { $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); if ($free_entries == 0) { @@ -417,11 +420,11 @@ function Shift_signup_allowed_angeltype_supporter($needed_angeltype, $shift_entr /** * Check if an admin can sign up a user to a shift. * - * @param array $needed_angeltype + * @param AngelType $needed_angeltype * @param array[] $shift_entries * @return ShiftSignupState */ -function Shift_signup_allowed_admin($needed_angeltype, $shift_entries) +function Shift_signup_allowed_admin(AngelType $needed_angeltype, $shift_entries) { $free_entries = Shift_free_entries($needed_angeltype, $shift_entries); @@ -436,12 +439,12 @@ function Shift_signup_allowed_admin($needed_angeltype, $shift_entries) /** * Check if an angel can signout from a shift. * - * @param array $shift The shift - * @param array $angeltype The angeltype - * @param int $signout_user_id The user that was signed up for the shift + * @param array $shift The shift + * @param AngelType $angeltype The angeltype + * @param int $signout_user_id The user that was signed up for the shift * @return bool */ -function Shift_signout_allowed($shift, $angeltype, $signout_user_id) +function Shift_signout_allowed($shift, AngelType $angeltype, $signout_user_id) { $user = auth()->user(); @@ -469,21 +472,21 @@ function Shift_signout_allowed($shift, $angeltype, $signout_user_id) * Check if an angel can sign up for given shift. * * @param User $signup_user - * @param array $shift The shift - * @param array $angeltype The angeltype to which the user wants to sign up + * @param array $shift The shift + * @param AngelType $angeltype The angeltype to which the user wants to sign up * @param array|null $user_angeltype * @param array|null $user_shifts List of the users shifts - * @param array $needed_angeltype + * @param AngelType $needed_angeltype * @param array[] $shift_entries * @return ShiftSignupState */ function Shift_signup_allowed( $signup_user, $shift, - $angeltype, + AngelType $angeltype, $user_angeltype, $user_shifts, - $needed_angeltype, + AngelType $needed_angeltype, $shift_entries ) { if (auth()->can('user_shifts_admin')) { diff --git a/includes/model/Stats.php b/includes/model/Stats.php index 4968fe3d..8299b340 100644 --- a/includes/model/Stats.php +++ b/includes/model/Stats.php @@ -84,14 +84,14 @@ function stats_angels_needed_three_hours(ShiftsFilter $filter = null) ( SELECT SUM(`count`) FROM `NeededAngelTypes` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` ' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ) - ( SELECT COUNT(*) FROM `ShiftEntry` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0 ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' @@ -111,14 +111,14 @@ function stats_angels_needed_three_hours(ShiftsFilter $filter = null) ( SELECT SUM(`count`) FROM `NeededAngelTypes` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID` ' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ) - ( SELECT COUNT(*) FROM `ShiftEntry` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0 ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' @@ -163,14 +163,14 @@ function stats_angels_needed_for_nightshifts(ShiftsFilter $filter = null) ( SELECT SUM(`count`) FROM `NeededAngelTypes` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` ' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ) - ( SELECT COUNT(*) FROM `ShiftEntry` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0 ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' @@ -190,14 +190,14 @@ function stats_angels_needed_for_nightshifts(ShiftsFilter $filter = null) ( SELECT SUM(`count`) FROM `NeededAngelTypes` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID` - ' . ($filter ? 'AND AngelTypes.id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' + ' . ($filter ? 'AND angel_types.id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ) - ( SELECT COUNT(*) FROM `ShiftEntry` - JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` - WHERE `AngelTypes`.`show_on_dashboard`=TRUE + JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID` + WHERE `angel_types`.`show_on_dashboard`=TRUE AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `freeloaded`=0 ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index 3d668c62..b4c89e57 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -1,6 +1,7 @@ 0; + ', [$userId, $angeltype->id])) > 0; } /** @@ -33,9 +34,9 @@ function UserAngelType_exists($userId, $angeltype) function User_angeltypes($userId) { return Db::select(' - SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` + SELECT `angel_types`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` FROM `UserAngelTypes` - JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id` + JOIN `angel_types` ON `UserAngelTypes`.`angeltype_id` = `angel_types`.`id` WHERE `UserAngelTypes`.`user_id`=? ', [$userId]); } @@ -51,28 +52,28 @@ function User_unconfirmed_AngelTypes($userId) return Db::select(' SELECT `UserAngelTypes`.*, - `AngelTypes`.`name`, + `angel_types`.`name`, count(`UnconfirmedMembers`.`user_id`) AS `count` FROM `UserAngelTypes` - JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` + JOIN `angel_types` ON `UserAngelTypes`.`angeltype_id`=`angel_types`.`id` JOIN `UserAngelTypes` AS `UnconfirmedMembers` ON `UserAngelTypes`.`angeltype_id`=`UnconfirmedMembers`.`angeltype_id` WHERE `UserAngelTypes`.`user_id`=? AND `UserAngelTypes`.`supporter`=TRUE - AND `AngelTypes`.`restricted`=TRUE + AND `angel_types`.`restricted`=TRUE AND `UnconfirmedMembers`.`confirm_user_id` IS NULL - GROUP BY `UserAngelTypes`.`angeltype_id`, `UserAngelTypes`.`id`, AngelTypes.name, UserAngelTypes.user_id, UserAngelTypes.confirm_user_id, UserAngelTypes.supporter - ORDER BY `AngelTypes`.`name` + GROUP BY `UserAngelTypes`.`angeltype_id`, `UserAngelTypes`.`id`, angel_types.name, UserAngelTypes.user_id, UserAngelTypes.confirm_user_id, UserAngelTypes.supporter + ORDER BY `angel_types`.`name` ', [$userId]); } /** * Returns true if user is angeltype supporter or has privilege admin_user_angeltypes. * - * @param User $user - * @param array $angeltype + * @param User $user + * @param AngelType $angeltype * @return bool */ -function User_is_AngelType_supporter($user, $angeltype) +function User_is_AngelType_supporter($user, AngelType $angeltype) { if (!$user) { return false; @@ -80,8 +81,10 @@ function User_is_AngelType_supporter($user, $angeltype) $privileges = $user->privileges->pluck('name')->toArray(); - return (count(Db::select( - ' + return + count( + Db::select( + ' SELECT `id` FROM `UserAngelTypes` WHERE `user_id`=? @@ -89,11 +92,12 @@ function User_is_AngelType_supporter($user, $angeltype) AND `supporter`=TRUE LIMIT 1 ', - [ + [ $user->id, - $angeltype['id'] + $angeltype->id ] - )) > 0) + ) + ) > 0 || in_array('admin_user_angeltypes', $privileges); } @@ -118,7 +122,7 @@ function UserAngelType_update($user_angeltype_id, $supporter) * * @param int $angeltype_id */ -function UserAngelTypes_delete_all($angeltype_id) +function UserAngelTypes_delete_all_unconfirmed(int $angeltype_id) { Db::delete(' DELETE FROM `UserAngelTypes` @@ -189,11 +193,11 @@ function UserAngelType_delete($user_angeltype) /** * Create an UserAngelType. * - * @param int $userId - * @param array $angeltype + * @param int $userId + * @param AngelType $angeltype * @return int */ -function UserAngelType_create($userId, $angeltype) +function UserAngelType_create($userId, AngelType $angeltype) { Db::insert( ' @@ -202,7 +206,7 @@ function UserAngelType_create($userId, $angeltype) ', [ $userId, - $angeltype['id'] + $angeltype->id ] ); @@ -217,25 +221,25 @@ function UserAngelType_create($userId, $angeltype) */ function UserAngelType($user_angeltype_id) { - $angelType = Db::selectOne(' + $userAngelType = Db::selectOne(' SELECT * FROM `UserAngelTypes` WHERE `id`=? LIMIT 1', [$user_angeltype_id]); - return empty($angelType) ? null : $angelType; + return empty($userAngelType) ? null : $userAngelType; } /** * Get an UserAngelType by user and angeltype. * - * @param int $userId - * @param array $angeltype + * @param int $userId + * @param AngelType $angeltype * @return array|null */ -function UserAngelType_by_User_and_AngelType($userId, $angeltype) +function UserAngelType_by_User_and_AngelType($userId, AngelType $angeltype) { - $angelType = Db::selectOne( + return Db::selectOne( ' SELECT * FROM `UserAngelTypes` @@ -245,17 +249,15 @@ function UserAngelType_by_User_and_AngelType($userId, $angeltype) ', [ $userId, - $angeltype['id'] + $angeltype->id ] ); - - return empty($angelType) ? null : $angelType; } /** * Get an UserAngelTypes by user * - * @param int $userId + * @param int $userId * @param bool $onlyConfirmed * @return array[]|null */ @@ -265,7 +267,7 @@ function UserAngelTypes_by_User($userId, $onlyConfirmed = false) ' SELECT * FROM `UserAngelTypes` - ' . ($onlyConfirmed ? 'LEFT JOIN AngelTypes AS a ON a.id=UserAngelTypes.angeltype_id' : '') . ' + ' . ($onlyConfirmed ? 'LEFT JOIN angel_types AS a ON a.id=UserAngelTypes.angeltype_id' : '') . ' WHERE `user_id`=? ' . ( diff --git a/includes/model/User_model.php b/includes/model/User_model.php index d8a2b2b6..712547b6 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -2,6 +2,7 @@ use Carbon\Carbon; use Engelsystem\Database\Db; +use Engelsystem\Models\AngelType; use Engelsystem\Models\User\User; use Engelsystem\Models\Worklog; use Engelsystem\ValidationResult; @@ -81,10 +82,10 @@ function Users_by_angeltype_inverted($angeltype) /** * Returns all members of given angeltype. * - * @param array $angeltype + * @param AngelType $angeltype * @return User[]|Collection */ -function Users_by_angeltype($angeltype) +function Users_by_angeltype(AngelType $angeltype) { return User::query() ->select( @@ -96,7 +97,7 @@ function Users_by_angeltype($angeltype) ) ->join('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id') ->leftJoin('users_licenses', 'users.id', '=', 'users_licenses.user_id') - ->where('UserAngelTypes.angeltype_id', '=', $angeltype['id']) + ->where('UserAngelTypes.angeltype_id', '=', $angeltype->id) ->orderBy('users.name') ->get(); } @@ -122,18 +123,6 @@ function User_validate_Nick($nick) return new ValidationResult(true, $nick); } -/** - * Validate user email address. - * - * @param string $mail The email address to validate - * @return ValidationResult - */ -function User_validate_mail($mail) -{ - $mail = strip_item($mail); - return new ValidationResult(check_email($mail), $mail); -} - /** * Validate the planned arrival date * @@ -169,7 +158,7 @@ function User_validate_planned_arrival_date($planned_arrival_date) /** * Validate the planned departure date * - * @param int $planned_arrival_date Unix timestamp + * @param int $planned_arrival_date Unix timestamp * @param int $planned_departure_date Unix timestamp * @return ValidationResult */ @@ -247,10 +236,10 @@ function User_get_eligable_voucher_count($user) $vouchers = $voucher_settings['initial_vouchers']; if ($voucher_settings['shifts_per_voucher']) { - $vouchers += $shifts_done / $voucher_settings['shifts_per_voucher']; + $vouchers += $shifts_done / $voucher_settings['shifts_per_voucher']; } if ($voucher_settings['hours_per_voucher']) { - $vouchers += $shiftsTime / $voucher_settings['hours_per_voucher']; + $vouchers += $shiftsTime / $voucher_settings['hours_per_voucher']; } $vouchers -= $user->state->got_voucher; diff --git a/includes/pages/admin_free.php b/includes/pages/admin_free.php index 7710ba80..4ec0a7a8 100644 --- a/includes/pages/admin_free.php +++ b/includes/pages/admin_free.php @@ -1,6 +1,6 @@ __('All') ]; foreach ($angel_types_source as $angel_type) { - $angel_types[$angel_type['id']] = $angel_type['name']; + $angel_types[$angel_type->id] = $angel_type->name; } $angelType = $request->input('angeltype', ''); @@ -58,11 +58,11 @@ function admin_free() ->where('UserAngelTypes.angeltype_id', '=', $angelType); }); - $query->join('AngelTypes', function ($join) { + $query->join('angel_types', function ($join) { /** @var JoinClause $join */ - $join->on('UserAngelTypes.angeltype_id', '=', 'AngelTypes.id') + $join->on('UserAngelTypes.angeltype_id', '=', 'angel_types.id') ->whereNotNull('UserAngelTypes.confirm_user_id') - ->orWhere('AngelTypes.restricted', '=', '0'); + ->orWhere('angel_types.restricted', '=', '0'); }); } diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 97bfd521..f5f2ce13 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -1,6 +1,7 @@ id] = $angeltype->name; + $angeltypes_count[$angeltype->id] = 0; } if (test_request_int('id')) { @@ -132,11 +133,11 @@ function admin_rooms() NeededAngelTypes_delete_by_room($room_id); $needed_angeltype_info = []; foreach ($angeltypes_count as $angeltype_id => $angeltype_count) { - $angeltype = AngelType($angeltype_id); + $angeltype = AngelType::find($angeltype_id); if (!empty($angeltype)) { NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count); if ($angeltype_count > 0) { - $needed_angeltype_info[] = $angeltype['name'] . ': ' . $angeltype_count; + $needed_angeltype_info[] = $angeltype->name . ': ' . $angeltype_count; } } } @@ -150,9 +151,9 @@ function admin_rooms() } } $angeltypes_count_form = []; - foreach ($angeltypes as $angeltype_id => $angeltype) { + foreach ($angeltypes as $angeltype_id => $angeltypeName) { $angeltypes_count_form[] = div('col-lg-4 col-md-6 col-xs-6', [ - form_spinner('angeltype_count_' . $angeltype_id, $angeltype, $angeltypes_count[$angeltype_id]) + form_spinner('angeltype_count_' . $angeltype_id, $angeltypeName, $angeltypes_count[$angeltype_id]) ]); } @@ -190,14 +191,14 @@ function admin_rooms() foreach ($shifts as $shift) { $shift = Shift($shift['SID']); foreach ($shift['ShiftEntry'] as $entry) { - $type = AngelType($entry['TID']); + $type = AngelType::find($entry['TID']); event('shift.entry.deleting', [ 'user' => User::find($entry['user_id']), 'start' => Carbon::createFromTimestamp($shift['start']), 'end' => Carbon::createFromTimestamp($shift['end']), 'name' => $shift['name'], 'title' => $shift['title'], - 'type' => $type['name'], + 'type' => $type->name, 'room' => $room, 'freeloaded' => (bool)$entry['freeloaded'], ]); diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index d6e1f891..1020fda3 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -3,6 +3,7 @@ use Engelsystem\Database\Db; use Engelsystem\Helpers\Carbon; use Engelsystem\Http\Exceptions\HttpForbidden; +use Engelsystem\Models\AngelType; use Engelsystem\Models\Room; use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\User\User; @@ -46,10 +47,10 @@ function admin_shifts() } // Engeltypen laden - $types = Db::select('SELECT * FROM `AngelTypes` ORDER BY `name`'); + $types = AngelType::all(); $needed_angel_types = []; foreach ($types as $type) { - $needed_angel_types[$type['id']] = 0; + $needed_angel_types[$type->id] = 0; } // Load shift types @@ -152,11 +153,11 @@ function admin_shifts() $angelmode = 'location'; } elseif ($request->input('angelmode') == 'manually') { foreach ($types as $type) { - if (preg_match('/^\d+$/', trim($request->input('type_' . $type['id'], 0)))) { - $needed_angel_types[$type['id']] = trim($request->input('type_' . $type['id'], 0)); + if (preg_match('/^\d+$/', trim($request->input('type_' . $type->id, 0)))) { + $needed_angel_types[$type->id] = trim($request->input('type_' . $type->id, 0)); } else { $valid = false; - error(sprintf(__('Please check the needed angels for team %s.'), $type['name'])); + error(sprintf(__('Please check the needed angels for team %s.'), $type->name)); } } @@ -318,9 +319,9 @@ function admin_shifts() 'needed_angels' => '' ]; foreach ($types as $type) { - if (isset($needed_angel_types[$type['id']]) && $needed_angel_types[$type['id']] > 0) { + if (isset($needed_angel_types[$type->id]) && $needed_angel_types[$type->id] > 0) { $shifts_table_entry['needed_angels'] .= '' . AngelType_name_render($type) . ': ' - . $needed_angel_types[$type['id']] . '
'; + . $needed_angel_types[$type->id] . '
'; } } $shifts_table[] = $shifts_table_entry; @@ -382,12 +383,7 @@ function admin_shifts() $needed_angel_types_info = []; foreach ($session->get('admin_shifts_types', []) as $type_id => $count) { - $angel_type_source = Db::selectOne(' - SELECT * - FROM `AngelTypes` - WHERE `id` = ? - LIMIT 1', [$type_id]); - + $angel_type_source = AngelType::find($type_id); if (!empty($angel_type_source)) { Db::insert( ' @@ -402,7 +398,7 @@ function admin_shifts() ); if ($count > 0) { - $needed_angel_types_info[] = $angel_type_source['name'] . ': ' . $count; + $needed_angel_types_info[] = $angel_type_source->name . ': ' . $count; } } } @@ -424,9 +420,9 @@ function admin_shifts() foreach ($types as $type) { $angel_types .= '
' . form_spinner( - 'type_' . $type['id'], - $type['name'], - $needed_angel_types[$type['id']] + 'type_' . $type->id, + $type->name, + $needed_angel_types[$type->id] ) . '
'; } @@ -549,14 +545,14 @@ function admin_shifts_history(): string $shift = Shift($shift['SID']); $room = Room::find($shift['RID']); foreach ($shift['ShiftEntry'] as $entry) { - $type = AngelType($entry['TID']); + $type = AngelType::find($entry['TID']); event('shift.entry.deleting', [ 'user' => User::find($entry['user_id']), 'start' => Carbon::createFromTimestamp($shift['start']), 'end' => Carbon::createFromTimestamp($shift['end']), 'name' => $shift['name'], 'title' => $shift['title'], - 'type' => $type['name'], + 'type' => $type->name, 'room' => $room, 'freeloaded' => (bool)$entry['freeloaded'], ]); diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index ec2421f8..0a5474ef 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Engelsystem\Database\Database; use Engelsystem\Database\Db; use Engelsystem\Events\Listener\OAuth2; +use Engelsystem\Models\AngelType; use Engelsystem\Models\Group; use Engelsystem\Models\OAuth; use Engelsystem\Models\User\Contact; @@ -63,7 +64,7 @@ function guest_register() $selected_angel_types = []; $planned_arrival_date = null; - $angel_types_source = AngelTypes(); + $angel_types_source = AngelType::all(); $angel_types = []; if (!empty($session->get('oauth2_groups'))) { /** @var OAuth2 $oauth */ @@ -76,13 +77,13 @@ function guest_register() } } foreach ($angel_types_source as $angel_type) { - if ($angel_type['hide_register']) { + if ($angel_type->hide_register) { continue; } - $angel_types[$angel_type['id']] = $angel_type['name'] - . ($angel_type['restricted'] ? ' (' . __('Requires introduction') . ')' : ''); - if (!$angel_type['restricted']) { - $selected_angel_types[] = $angel_type['id']; + $angel_types[$angel_type->id] = $angel_type->name + . ($angel_type->restricted ? ' (' . __('Requires introduction') . ')' : ''); + if (!$angel_type->restricted) { + $selected_angel_types[] = $angel_type->id; } } diff --git a/includes/pages/schedule/ImportSchedule.php b/includes/pages/schedule/ImportSchedule.php index 033bdef7..f43356c3 100644 --- a/includes/pages/schedule/ImportSchedule.php +++ b/includes/pages/schedule/ImportSchedule.php @@ -300,13 +300,13 @@ class ImportSchedule extends BaseController $shiftEntries = $this->db ->table('ShiftEntry') ->select([ - 'shift_types.name', 'Shifts.title', 'AngelTypes.name AS type', 'rooms.id AS room_id', + 'shift_types.name', 'Shifts.title', 'angel_types.name AS type', 'rooms.id AS room_id', 'Shifts.start', 'Shifts.end', 'ShiftEntry.UID as user_id', 'ShiftEntry.freeloaded' ]) ->join('Shifts', 'Shifts.SID', 'ShiftEntry.SID') ->join('schedule_shift', 'Shifts.SID', 'schedule_shift.shift_id') ->join('rooms', 'rooms.id', 'Shifts.RID') - ->join('AngelTypes', 'AngelTypes.id', 'ShiftEntry.TID') + ->join('angel_types', 'angel_types.id', 'ShiftEntry.TID') ->join('shift_types', 'shift_types.id', 'Shifts.shifttype_id') ->where('schedule_shift.guid', $event->getGuid()) ->get(); diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index db01511a..980e8261 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -58,9 +58,9 @@ function user_myshifts() `shift_types`.`name`, `Shifts`.*, `rooms`.`name` as room_name, - `AngelTypes`.`name` AS `angel_type` + `angel_types`.`name` AS `angel_type` FROM `ShiftEntry` - JOIN `AngelTypes` ON (`ShiftEntry`.`TID` = `AngelTypes`.`id`) + JOIN `angel_types` ON (`ShiftEntry`.`TID` = `angel_types`.`id`) JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) JOIN `shift_types` ON (`shift_types`.`id` = `Shifts`.`shifttype_id`) JOIN `rooms` ON (`Shifts`.`RID` = `rooms`.`id`) diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 77f1630f..64a52b30 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -2,6 +2,7 @@ use Engelsystem\Database\Db; use Engelsystem\Helpers\Carbon; +use Engelsystem\Models\AngelType; use Engelsystem\Models\Room; use Engelsystem\ShiftsFilter; use Illuminate\Support\Collection; @@ -152,29 +153,29 @@ function load_types() { $user = auth()->user(); - if (!count(Db::select('SELECT `id`, `name` FROM `AngelTypes`'))) { + if (!AngelType::count()) { error(__('The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype.')); throw_redirect(page_link_to('/')); } $types = Db::select( ' SELECT - `AngelTypes`.`id`, - `AngelTypes`.`name`, + `angel_types`.`id`, + `angel_types`.`name`, ( - `AngelTypes`.`restricted`=0 + `angel_types`.`restricted`=0 OR ( NOT `UserAngelTypes`.`confirm_user_id` IS NULL OR `UserAngelTypes`.`id` IS NULL ) ) AS `enabled` - FROM `AngelTypes` + FROM `angel_types` LEFT JOIN `UserAngelTypes` ON ( - `UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` + `UserAngelTypes`.`angeltype_id`=`angel_types`.`id` AND `UserAngelTypes`.`user_id`=? ) - ORDER BY `AngelTypes`.`name` + ORDER BY `angel_types`.`name` ', [ $user->id, @@ -191,7 +192,7 @@ function load_types() */ function unrestricted_angeltypes() { - return Db::select('SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0'); + return AngelType::whereRestricted(0)->get(['id', 'name'])->toArray(); } /** diff --git a/includes/sys_template.php b/includes/sys_template.php index 27f68115..412615d6 100644 --- a/includes/sys_template.php +++ b/includes/sys_template.php @@ -87,7 +87,7 @@ function mute($text) * Renders a bootstrap label with given content and class. * * @param string $content The text - * @param string $class default, primary, info, success, warning, danger + * @param string $class default, primary, info, success, warning, danger * @return string */ function badge($content, $class = 'default') @@ -322,9 +322,9 @@ function description($data) /** * Rendert eine Datentabelle * - * @param array|string $columns - * @param array[] $rows_raw - * @param bool $data + * @param array|string $columns + * @param array[]|ArrayAccess $rows_raw + * @param bool $data * @return string */ function table($columns, $rows_raw, $data = true) diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index 151417ee..87d6fed7 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -1,9 +1,12 @@ name, $angeltype->id); } - return '' - . ($angeltype['restricted'] ? icon('book') : '') . $angeltype['name'] + return '' + . ($angeltype->restricted ? icon('book') : '') . $angeltype->name . ''; } /** * Render angeltype membership state * - * @param array $user_angeltype UserAngelType and AngelType + * @param AngelType $user_angeltype UserAngelType and AngelType * @return string */ -function AngelType_render_membership($user_angeltype) +function AngelType_render_membership(AngelType $user_angeltype) { - if (!empty($user_angeltype['user_angeltype_id'])) { - if ($user_angeltype['restricted']) { - if (empty($user_angeltype['confirm_user_id'])) { + if (!empty($user_angeltype->user_angeltype_id)) { + if ($user_angeltype->restricted) { + if (empty($user_angeltype->confirm_user_id)) { return icon('book') . __('Unconfirmed'); - } elseif ($user_angeltype['supporter']) { + } elseif ($user_angeltype->supporter) { return icon_bool(true) . __('Supporter'); } return icon_bool(true) . __('Member'); - } elseif ($user_angeltype['supporter']) { + } elseif ($user_angeltype->supporter) { return icon_bool(true) . __('Supporter'); } return icon_bool(true) . __('Member'); @@ -52,13 +55,13 @@ function AngelType_render_membership($user_angeltype) } /** - * @param array $angeltype + * @param AngelType $angeltype * @return string */ -function AngelType_delete_view($angeltype) +function AngelType_delete_view(AngelType $angeltype) { - return page_with_title(sprintf(__('Delete angeltype %s'), $angeltype['name']), [ - info(sprintf(__('Do you want to delete angeltype %s?'), $angeltype['name']), true), + return page_with_title(sprintf(__('Delete angeltype %s'), $angeltype->name), [ + info(sprintf(__('Do you want to delete angeltype %s?'), $angeltype->name), true), form([ buttons([ button(page_link_to('angeltypes'), icon('x-lg') . __('cancel')), @@ -71,59 +74,59 @@ function AngelType_delete_view($angeltype) /** * Render angeltype edit form. * - * @param array $angeltype The angeltype to edit - * @param boolean $supporter_mode Is the user a supporter of this angeltype? + * @param AngelType $angeltype The angeltype to edit + * @param boolean $supporter_mode Is the user a supporter of this angeltype? * @return string */ -function AngelType_edit_view($angeltype, $supporter_mode) +function AngelType_edit_view(AngelType $angeltype, bool $supporter_mode) { - return page_with_title(sprintf(__('Edit %s'), $angeltype['name']), [ + return page_with_title(sprintf(__('Edit %s'), $angeltype->name), [ buttons([ button(page_link_to('angeltypes'), __('Angeltypes'), 'back') ]), msg(), form([ $supporter_mode - ? form_info(__('Name'), $angeltype['name']) - : form_text('name', __('Name'), $angeltype['name']), + ? form_info(__('Name'), $angeltype->name) + : form_text('name', __('Name'), $angeltype->name), $supporter_mode - ? form_info(__('Requires introduction'), $angeltype['restricted'] ? __('Yes') : __('No')) - : form_checkbox('restricted', __('Requires introduction'), $angeltype['restricted']), + ? form_info(__('Requires introduction'), $angeltype->restricted ? __('Yes') : __('No')) + : form_checkbox('restricted', __('Requires introduction'), $angeltype->restricted), form_info( '', __('Angel types which require introduction can only be used by an angel if enabled by a supporter (double opt-in).') ), $supporter_mode - ? form_info(__('No Self Sign Up allowed'), $angeltype['no_self_signup'] ? __('Yes') : __('No')) - : form_checkbox('no_self_signup', __('No Self Sign Up allowed'), $angeltype['no_self_signup']), - $supporter_mode - ? form_info( + ? form_info(__('No Self Sign Up allowed'), $angeltype->no_self_signup ? __('Yes') : __('No')) + : form_checkbox('no_self_signup', __('No Self Sign Up allowed'), $angeltype->no_self_signup), + $supporter_mode ? + form_info( __('Requires driver license'), - $angeltype['requires_driver_license'] - ? __('Yes') - : __('No') - ) - : form_checkbox( + $angeltype->requires_driver_license + ? __('Yes') + : __('No') + ) : + form_checkbox( 'requires_driver_license', __('Requires driver license'), - $angeltype['requires_driver_license'] + $angeltype->requires_driver_license ), $supporter_mode - ? form_info(__('Show on dashboard'), $angeltype['show_on_dashboard'] ? __('Yes') : __('No')) - : form_checkbox('show_on_dashboard', __('Show on dashboard'), $angeltype['show_on_dashboard']), + ? form_info(__('Show on dashboard'), $angeltype->show_on_dashboard ? __('Yes') : __('No')) + : form_checkbox('show_on_dashboard', __('Show on dashboard'), $angeltype->show_on_dashboard), $supporter_mode - ? form_info(__('Hide at Registration'), $angeltype['hide_register'] ? __('Yes') : __('No')) - : form_checkbox('hide_register', __('Hide at Registration'), $angeltype['hide_register']), - form_textarea('description', __('Description'), $angeltype['description']), + ? form_info(__('Hide at Registration'), $angeltype->hide_register ? __('Yes') : __('No')) + : form_checkbox('hide_register', __('Hide at Registration'), $angeltype->hide_register), + form_textarea('description', __('Description'), $angeltype->description), form_info('', __('Please use markdown for the description.')), heading(__('Contact'), 3), form_info( '', __('Primary contact person/desk for user questions.') ), - form_text('contact_name', __('Name'), $angeltype['contact_name']), - form_text('contact_dect', __('DECT'), $angeltype['contact_dect']), - form_text('contact_email', __('E-Mail'), $angeltype['contact_email']), + form_text('contact_name', __('Name'), $angeltype->contact_name), + form_text('contact_dect', __('DECT'), $angeltype->contact_dect), + form_text('contact_email', __('E-Mail'), $angeltype->contact_email), form_submit('submit', __('Save')) ]) ]); @@ -132,7 +135,7 @@ function AngelType_edit_view($angeltype, $supporter_mode) /** * Renders the buttons for the angeltype view. * - * @param array $angeltype + * @param AngelType $angeltype * @param array|null $user_angeltype * @param bool $admin_angeltypes * @param bool $supporter @@ -140,13 +143,19 @@ function AngelType_edit_view($angeltype, $supporter_mode) * @param User|null $user * @return string */ -function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, $supporter, $user_driver_license, $user) -{ +function AngelType_view_buttons( + AngelType $angeltype, + $user_angeltype, + $admin_angeltypes, + $supporter, + $user_driver_license, + $user +) { $buttons = [ button(page_link_to('angeltypes'), __('Angeltypes'), 'back') ]; - if ($angeltype['requires_driver_license']) { + if ($angeltype->requires_driver_license) { $buttons[] = button( user_driver_license_edit_link($user), icon('wallet2') . __('my driving license') @@ -155,19 +164,19 @@ function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, if (is_null($user_angeltype)) { $buttons[] = button( - page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype['id']]), + page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype->id]), __('join'), 'add' ); } else { - if ($angeltype['requires_driver_license'] && !$user_driver_license->wantsToDrive()) { + if ($angeltype->requires_driver_license && !$user_driver_license->wantsToDrive()) { error(__('This angeltype requires a driver license. Please enter your driver license information!')); } - if ($angeltype['restricted'] && empty($user_angeltype['confirm_user_id'])) { + if ($angeltype->restricted && empty($user_angeltype['confirm_user_id'])) { error(sprintf( __('You are unconfirmed for this angeltype. Please go to the introduction for %s to get confirmed.'), - $angeltype['name'] + $angeltype->name )); } $buttons[] = button( @@ -178,14 +187,14 @@ function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, if ($admin_angeltypes || $supporter) { $buttons[] = button( - page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype->id]), __('edit'), 'edit' ); } if ($admin_angeltypes) { $buttons[] = button( - page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype->id]), __('delete'), 'delete' ); @@ -197,13 +206,13 @@ function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, /** * Renders and sorts the members of an angeltype into supporters, members and unconfirmed members. * - * @param array $angeltype - * @param User[] $members - * @param bool $admin_user_angeltypes - * @param bool $admin_angeltypes + * @param AngelType $angeltype + * @param User[] $members + * @param bool $admin_user_angeltypes + * @param bool $admin_angeltypes * @return array [supporters, members, unconfirmed members] */ -function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $admin_angeltypes) +function AngelType_view_members(AngelType $angeltype, $members, $admin_user_angeltypes, $admin_angeltypes) { $supporters = []; $members_confirmed = []; @@ -211,7 +220,7 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a foreach ($members as $member) { $member->name = User_Nick_render($member) . User_Pronoun_render($member); $member['dect'] = $member->contact->dect; - if ($angeltype['requires_driver_license']) { + if ($angeltype->requires_driver_license) { $member['wants_to_drive'] = icon_bool($member->license->wantsToDrive()); $member['has_car'] = icon_bool($member->license->has_car); $member['has_license_car'] = icon_bool($member->license->drive_car); @@ -221,7 +230,7 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a $member['has_license_forklift'] = icon_bool($member->license->drive_forklift); } - if ($angeltype['restricted'] && empty($member['confirm_user_id'])) { + if ($angeltype->restricted && empty($member['confirm_user_id'])) { $member['actions'] = table_buttons([ button( page_link_to( @@ -261,17 +270,17 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a } else { if ($admin_user_angeltypes) { $member['actions'] = table_buttons([ - $admin_angeltypes - ? button( + $admin_angeltypes ? + button( page_link_to('user_angeltypes', [ - 'action' => 'update', - 'user_angeltype_id' => $member['user_angeltype_id'], - 'supporter' => 1 + 'action' => 'update', + 'user_angeltype_id' => $member['user_angeltype_id'], + 'supporter' => 1 ]), __('Add supporter rights'), 'btn-sm' - ) - : '', + ) : + '', button( page_link_to('user_angeltypes', [ 'action' => 'delete', @@ -296,14 +305,14 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a /** * Creates the needed member table headers according to given rights and settings from the angeltype. * - * @param array $angeltype - * @param bool $supporter - * @param bool $admin_angeltypes + * @param AngelType $angeltype + * @param bool $supporter + * @param bool $admin_angeltypes * @return array */ -function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) +function AngelType_view_table_headers(AngelType $angeltype, $supporter, $admin_angeltypes) { - if ($angeltype['requires_driver_license'] && ($supporter || $admin_angeltypes)) { + if ($angeltype->requires_driver_license && ($supporter || $admin_angeltypes)) { return [ 'name' => __('Nick'), 'dect' => __('DECT'), @@ -327,7 +336,7 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) /** * Render an angeltype page containing the member lists. * - * @param array $angeltype + * @param AngelType $angeltype * @param User[] $members * @param array $user_angeltype * @param bool $admin_user_angeltypes @@ -341,7 +350,7 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes) * @return string */ function AngelType_view( - $angeltype, + AngelType $angeltype, $members, $user_angeltype, $admin_user_angeltypes, @@ -353,7 +362,7 @@ function AngelType_view( ShiftCalendarRenderer $shiftCalendarRenderer, $tab ) { - return page_with_title(sprintf(__('Team %s'), $angeltype['name']), [ + return page_with_title(sprintf(__('Team %s'), $angeltype->name), [ AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, $supporter, $user_driver_license, $user), msg(), tabs([ @@ -374,46 +383,46 @@ function AngelType_view( } /** - * @param array $angeltype + * @param AngelType $angeltype * @param ShiftsFilterRenderer $shiftsFilterRenderer * @param ShiftCalendarRenderer $shiftCalendarRenderer * @return string HTML */ -function AngelType_view_shifts($angeltype, $shiftsFilterRenderer, $shiftCalendarRenderer) +function AngelType_view_shifts(AngelType $angeltype, $shiftsFilterRenderer, $shiftCalendarRenderer) { $shifts = $shiftsFilterRenderer->render(page_link_to('angeltypes', [ 'action' => 'view', - 'angeltype_id' => $angeltype['id'] - ]), ['type' => $angeltype['id']]); + 'angeltype_id' => $angeltype->id + ]), ['type' => $angeltype->id]); $shifts .= $shiftCalendarRenderer->render(); return div('first', $shifts); } /** - * @param array $angeltype - * @param User[] $members - * @param bool $admin_user_angeltypes - * @param bool $admin_angeltypes - * @param bool $supporter + * @param AngelType $angeltype + * @param User[] $members + * @param bool $admin_user_angeltypes + * @param bool $admin_angeltypes + * @param bool $supporter * @return string HTML */ function AngelType_view_info( - $angeltype, + AngelType $angeltype, $members, $admin_user_angeltypes, $admin_angeltypes, $supporter ) { $info = []; - if (AngelType_has_contact_info($angeltype)) { + if ($angeltype->hasContactInfo()) { $info[] = AngelTypes_render_contact_info($angeltype); } $info[] = '

' . __('Description') . '

'; $parsedown = new Parsedown(); - if ($angeltype['description'] != '') { - $info[] = $parsedown->parse((string)$angeltype['description']); + if ($angeltype->description != '') { + $info[] = $parsedown->parse($angeltype->description); } list($supporters, $members_confirmed, $members_unconfirmed) = AngelType_view_members( @@ -451,7 +460,7 @@ function AngelType_view_info( button( page_link_to( 'user_angeltypes', - ['action' => 'add', 'angeltype_id' => $angeltype['id']] + ['action' => 'add', 'angeltype_id' => $angeltype->id] ), __('Add'), 'add' @@ -460,15 +469,15 @@ function AngelType_view_info( } $info[] = table($table_headers, $members_confirmed); - if ($admin_user_angeltypes && $angeltype['restricted'] && count($members_unconfirmed) > 0) { + if ($admin_user_angeltypes && $angeltype->restricted && count($members_unconfirmed) > 0) { $info[] = '

' . __('Unconfirmed') . '

'; $info[] = buttons([ button( - page_link_to('user_angeltypes', ['action' => 'confirm_all', 'angeltype_id' => $angeltype['id']]), + page_link_to('user_angeltypes', ['action' => 'confirm_all', 'angeltype_id' => $angeltype->id]), icon('check-lg') . __('confirm all') ), button( - page_link_to('user_angeltypes', ['action' => 'delete_all', 'angeltype_id' => $angeltype['id']]), + page_link_to('user_angeltypes', ['action' => 'delete_all', 'angeltype_id' => $angeltype->id]), icon('trash') . __('deny all') ) ]); @@ -481,15 +490,15 @@ function AngelType_view_info( /** * Renders the contact info * - * @param array $angeltype + * @param AngelType $angeltype * @return string HTML */ -function AngelTypes_render_contact_info($angeltype) +function AngelTypes_render_contact_info(AngelType $angeltype) { $info = [ - __('Name') => [$angeltype['contact_name'], $angeltype['contact_name']], - __('DECT') => [sprintf('%1$s', $angeltype['contact_dect']), $angeltype['contact_dect']], - __('E-Mail') => [sprintf('%1$s', $angeltype['contact_email']), $angeltype['contact_email']], + __('Name') => [$angeltype->contact_name, $angeltype->contact_name], + __('DECT') => [sprintf('%1$s', $angeltype->contact_dect), $angeltype->contact_dect], + __('E-Mail') => [sprintf('%1$s', $angeltype->contact_email), $angeltype->contact_email], ]; $contactInfo = []; foreach ($info as $name => $data) { @@ -504,11 +513,11 @@ function AngelTypes_render_contact_info($angeltype) /** * Display the list of angeltypes. * - * @param array $angeltypes - * @param bool $admin_angeltypes + * @param AngelType[]|Collection $angeltypes + * @param bool $admin_angeltypes * @return string */ -function AngelTypes_list_view($angeltypes, $admin_angeltypes) +function AngelTypes_list_view($angeltypes, bool $admin_angeltypes) { return page_with_title(angeltypes_title(), [ msg(), @@ -519,44 +528,44 @@ function AngelTypes_list_view($angeltypes, $admin_angeltypes) button(page_link_to('angeltypes', ['action' => 'about']), __('Teams/Job description')) ]), table([ - 'name' => __('Name'), - 'restricted' => icon('book') . __('Requires introduction'), - 'no_self_signup' => icon('pencil-square') . __('Self Sign Up Allowed'), - 'membership' => __('Membership'), - 'actions' => '' + 'name' => __('Name'), + 'is_restricted' => icon('book') . __('Requires introduction'), + 'no_self_signup_allowed' => icon('pencil-square') . __('Self Sign Up Allowed'), + 'membership' => __('Membership'), + 'actions' => '' ], $angeltypes) ], true); } /** - * Renders the about info for an angeltype. + * Renders the about-info for an angeltype. * - * @param array $angeltype + * @param AngelType $angeltype * @return string */ -function AngelTypes_about_view_angeltype($angeltype) +function AngelTypes_about_view_angeltype(AngelType $angeltype) { $parsedown = new Parsedown(); - $html = '

' . $angeltype['name'] . '

'; + $html = '

' . $angeltype->name . '

'; - if (AngelType_has_contact_info($angeltype)) { + if ($angeltype->hasContactInfo()) { $html .= AngelTypes_render_contact_info($angeltype); } - if (isset($angeltype['user_angeltype_id'])) { + if (isset($angeltype->user_angeltype_id)) { $buttons = []; - if (!empty($angeltype['user_angeltype_id'])) { + if (!empty($angeltype->user_angeltype_id)) { $buttons[] = button( page_link_to( 'user_angeltypes', - ['action' => 'delete', 'user_angeltype_id' => $angeltype['user_angeltype_id']] + ['action' => 'delete', 'user_angeltype_id' => $angeltype->user_angeltype_id] ), __('leave') ); } else { $buttons[] = button( - page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype['id']]), + page_link_to('user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype->id]), __('join'), 'add' ); @@ -564,14 +573,14 @@ function AngelTypes_about_view_angeltype($angeltype) $html .= buttons($buttons); } - if ($angeltype['restricted']) { + if ($angeltype->restricted) { $html .= info( __('This angeltype requires the attendance at an introduction meeting. You might find additional information in the description.'), true ); } - if ($angeltype['description'] != '') { - $html .= $parsedown->parse((string)$angeltype['description']); + if ($angeltype->description != '') { + $html .= $parsedown->parse($angeltype->description); } $html .= '
'; @@ -581,8 +590,8 @@ function AngelTypes_about_view_angeltype($angeltype) /** * Renders a site that contains every angeltype and its description, basically as an overview of the needed help types. * - * @param array[] $angeltypes - * @param bool $user_logged_in + * @param Collection|AngelType[] $angeltypes + * @param bool $user_logged_in * @return string */ function AngelTypes_about_view($angeltypes, $user_logged_in) @@ -601,7 +610,11 @@ function AngelTypes_about_view($angeltypes, $user_logged_in) $footerConfig = config('footer_items'); if (!empty($footerConfig['FAQ'])) { - $buttons[] = button($footerConfig['FAQ'], __('FAQ'), 'btn-primary'); + $buttons[] = button( + Str::startsWith($footerConfig['FAQ'], '/') ? url($footerConfig['FAQ']) : $footerConfig['FAQ'], + __('FAQ'), + 'btn-primary' + ); } $content = [ diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php index b0b99d9c..19082ba1 100644 --- a/includes/view/ShiftCalendarShiftRenderer.php +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -2,6 +2,7 @@ namespace Engelsystem; +use Engelsystem\Models\AngelType; use Engelsystem\Models\Room; use Engelsystem\Models\User\User; @@ -15,10 +16,10 @@ class ShiftCalendarShiftRenderer /** * Renders a shift * - * @param array $shift The shift to render - * @param array $needed_angeltypes - * @param array $shift_entries - * @param User $user The user who is viewing the shift calendar + * @param array $shift The shift to render + * @param array[] $needed_angeltypes + * @param array $shift_entries + * @param User $user The user who is viewing the shift calendar * @return array */ public function render($shift, $needed_angeltypes, $shift_entries, $user) @@ -159,15 +160,16 @@ class ShiftCalendarShiftRenderer /** * Renders a list entry containing the needed angels for an angeltype * - * @param array $shift The shift which is rendered + * @param array $shift The shift which is rendered * @param array[] $shift_entries - * @param array[] $angeltype The angeltype, containing information about needed angeltypes + * @param array $angeltype The angeltype, containing information about needed angeltypes * and already signed up angels - * @param User $user The user who is viewing the shift calendar + * @param User $user The user who is viewing the shift calendar * @return array */ private function renderShiftNeededAngeltype($shift, $shift_entries, $angeltype, $user) { + $angeltype = (new AngelType())->forceFill($angeltype); $entry_list = []; foreach ($shift_entries as $entry) { $class = $entry['freeloaded'] ? 'text-decoration-line-through' : ''; @@ -216,7 +218,7 @@ class ShiftCalendarShiftRenderer break; case ShiftSignupState::ANGELTYPE: - if ($angeltype['restricted'] == 1) { + if ($angeltype->restricted) { // User has to be confirmed on the angeltype first $entry_list[] = $inner_text . icon('book'); } else { @@ -225,9 +227,9 @@ class ShiftCalendarShiftRenderer . button( page_link_to( 'user_angeltypes', - ['action' => 'add', 'angeltype_id' => $angeltype['id']] + ['action' => 'add', 'angeltype_id' => $angeltype->id] ), - sprintf(__('Become %s'), $angeltype['name']), + sprintf(__('Become %s'), $angeltype->name), 'btn-sm' ); } diff --git a/includes/view/ShiftEntry_view.php b/includes/view/ShiftEntry_view.php index 73fff1b8..fb63c45c 100644 --- a/includes/view/ShiftEntry_view.php +++ b/includes/view/ShiftEntry_view.php @@ -1,18 +1,19 @@ name ), true), form([ buttons([ @@ -36,12 +37,12 @@ function ShiftEntry_delete_view_admin($shift, $angeltype, $signoff_user) * Sign off from a shift, asking for ack. * * @param array $shift - * @param array $angeltype + * @param AngelType $angeltype * @param int $signoff_user_id * * @return string HTML */ -function ShiftEntry_delete_view($shift, $angeltype, $signoff_user_id) +function ShiftEntry_delete_view($shift, AngelType $angeltype, $signoff_user_id) { return page_with_title(ShiftEntry_delete_title(), [ info(sprintf( @@ -49,7 +50,7 @@ function ShiftEntry_delete_view($shift, $angeltype, $signoff_user_id) $shift['name'], date('Y-m-d H:i', $shift['start']), date('Y-m-d H:i', $shift['end']), - $angeltype['name'] + $angeltype->name ), true), form([ @@ -72,16 +73,22 @@ function ShiftEntry_delete_title() /** * Admin puts user into shift. * - * @param array $shift - * @param Room $room - * @param array $angeltype - * @param array $angeltypes_select - * @param User $signup_user - * @param array $users_select + * @param array $shift + * @param Room $room + * @param AngelType $angeltype + * @param array $angeltypes_select + * @param User $signup_user + * @param array $users_select * @return string */ -function ShiftEntry_create_view_admin($shift, Room $room, $angeltype, $angeltypes_select, $signup_user, $users_select) -{ +function ShiftEntry_create_view_admin( + $shift, + Room $room, + AngelType $angeltype, + $angeltypes_select, + $signup_user, + $users_select +) { return page_with_title( ShiftEntry_create_title() . ': ' . $shift['name'] . ' %c', @@ -89,7 +96,7 @@ function ShiftEntry_create_view_admin($shift, Room $room, $angeltype, $angeltype Shift_view_header($shift, $room), info(__('Do you want to sign up the following user for this shift?'), true), form([ - form_select('angeltype_id', __('Angeltype'), $angeltypes_select, $angeltype['id']), + form_select('angeltype_id', __('Angeltype'), $angeltypes_select, $angeltype->id), form_select('user_id', __('User'), $users_select, $signup_user->id), form_submit('submit', icon('check-lg') . __('Save')) ]) @@ -102,12 +109,12 @@ function ShiftEntry_create_view_admin($shift, Room $room, $angeltype, $angeltype * * @param array $shift * @param Room $room - * @param array $angeltype + * @param AngelType $angeltype * @param User $signup_user * @param array $users_select * @return string */ -function ShiftEntry_create_view_supporter($shift, Room $room, $angeltype, $signup_user, $users_select) +function ShiftEntry_create_view_supporter($shift, Room $room, AngelType $angeltype, $signup_user, $users_select) { return page_with_title( ShiftEntry_create_title() . ': ' . $shift['name'] @@ -131,11 +138,11 @@ function ShiftEntry_create_view_supporter($shift, Room $room, $angeltype, $signu * * @param array $shift * @param Room $room - * @param array $angeltype + * @param AngelType $angeltype * @param string $comment * @return string */ -function ShiftEntry_create_view_user($shift, Room $room, $angeltype, $comment) +function ShiftEntry_create_view_user($shift, Room $room, AngelType $angeltype, $comment) { return page_with_title( ShiftEntry_create_title() . ': ' . $shift['name'] diff --git a/includes/view/Shifts_view.php b/includes/view/Shifts_view.php index 27f478f2..e29d111e 100644 --- a/includes/view/Shifts_view.php +++ b/includes/view/Shifts_view.php @@ -1,5 +1,6 @@ user()->id, $angeltype); } if ( - isset($angeltype['shift_signup_state']) + isset($angeltype->shift_signup_state) && ( - $angeltype['shift_signup_state']->isSignupAllowed() + $angeltype->shift_signup_state->isSignupAllowed() || User_is_AngelType_supporter(auth()->user(), $angeltype) ) ) { return button(shift_entry_create_link($shift, $angeltype), __('Sign up')); } elseif (empty($user_angeltype)) { return button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]), sprintf( __('Become %s'), - $angeltype['name'] + $angeltype->name ) ); } @@ -104,11 +105,11 @@ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null) } /** - * @param array $shift - * @param ShiftType $shifttype - * @param Room $room - * @param array[] $angeltypes_source - * @param ShiftSignupState $shift_signup_state + * @param array $shift + * @param ShiftType $shifttype + * @param Room $room + * @param AngelType[]|Collection $angeltypes_source + * @param ShiftSignupState $shift_signup_state * @return string */ function Shift_view($shift, ShiftType $shifttype, Room $room, $angeltypes_source, ShiftSignupState $shift_signup_state) @@ -122,7 +123,7 @@ function Shift_view($shift, ShiftType $shifttype, Room $room, $angeltypes_source $angeltypes = []; foreach ($angeltypes_source as $angeltype) { - $angeltypes[$angeltype['id']] = $angeltype; + $angeltypes[$angeltype->id] = $angeltype; } $needed_angels = ''; @@ -198,10 +199,10 @@ function Shift_view($shift, ShiftType $shifttype, Room $room, $angeltypes_source } /** - * @param array $needed_angeltype - * @param array $angeltypes - * @param array[] $shift - * @param bool $user_shift_admin + * @param array $needed_angeltype + * @param AngelType[]|Collection $angeltypes + * @param array[] $shift + * @param bool $user_shift_admin * @return string */ function Shift_view_render_needed_angeltype($needed_angeltype, $angeltypes, $shift, $user_shift_admin) @@ -269,7 +270,7 @@ function Shift_view_render_shift_entry($shift_entry, $user_shift_admin, $angelty 'btn-sm' ); } - $angeltype = AngelType($shift_entry['TID']); + $angeltype = AngelType::find($shift_entry['TID']); $disabled = Shift_signout_allowed($shift, $angeltype, $shift_entry['UID']) ? '' : ' btn-disabled'; $entry .= button_icon(shift_entry_delete_link($shift_entry), 'trash', 'btn-sm' . $disabled); $entry .= ''; @@ -291,6 +292,7 @@ function shift_length($shift) 2, '0', STR_PAD_LEFT - ) . 'h'; + ); + $length .= 'h'; return $length; } diff --git a/includes/view/UserAngelTypes_view.php b/includes/view/UserAngelTypes_view.php index cbd498a5..5f42ca3a 100644 --- a/includes/view/UserAngelTypes_view.php +++ b/includes/view/UserAngelTypes_view.php @@ -1,15 +1,16 @@ name, User_Nick_render($user) ), true), form([ buttons([ button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]), icon('x-lg') . __('cancel') ), form_submit('submit', icon('check-lg') . __('yes'), 'btn-primary', false), @@ -37,65 +38,65 @@ function UserAngelType_update_view($user_angeltype, $user, $angeltype, $supporte } /** - * @param array $angeltype + * @param AngelType $angeltype * @return string */ -function UserAngelTypes_delete_all_view($angeltype) +function UserAngelTypes_delete_all_view(AngelType $angeltype) { return page_with_title(__('Deny all users'), [ msg(), - info(sprintf(__('Do you really want to deny all users for %s?'), $angeltype['name']), true), + info(sprintf(__('Do you really want to deny all users for %s?'), $angeltype->name), true), form([ buttons([ button( page_link_to( 'angeltypes', - ['action' => 'view', 'angeltype_id' => $angeltype['id']] + ['action' => 'view', 'angeltype_id' => $angeltype->id] ), icon('x-lg') . __('cancel') ), form_submit('deny_all', icon('check-lg') . __('yes'), 'btn-primary', false) ]), - ], page_link_to('user_angeltypes', ['action' => 'delete_all', 'angeltype_id' => $angeltype['id']])), + ], page_link_to('user_angeltypes', ['action' => 'delete_all', 'angeltype_id' => $angeltype->id])), ]); } /** - * @param array $angeltype + * @param AngelType $angeltype * @return string */ -function UserAngelTypes_confirm_all_view($angeltype) +function UserAngelTypes_confirm_all_view(AngelType $angeltype) { return page_with_title(__('Confirm all users'), [ msg(), - info(sprintf(__('Do you really want to confirm all users for %s?'), $angeltype['name']), true), + info(sprintf(__('Do you really want to confirm all users for %s?'), $angeltype->name), true), form([ buttons([ - button(angeltype_link($angeltype['id']), icon('x-lg') . __('cancel')), + button(angeltype_link($angeltype->id), icon('x-lg') . __('cancel')), form_submit('confirm_all', icon('check-lg') . __('yes'), 'btn-primary', false), ]), - ], page_link_to('user_angeltypes', ['action' => 'confirm_all', 'angeltype_id' => $angeltype['id']])), + ], page_link_to('user_angeltypes', ['action' => 'confirm_all', 'angeltype_id' => $angeltype->id])), ]); } /** - * @param array $user_angeltype - * @param User $user - * @param array $angeltype + * @param array $user_angeltype + * @param User $user + * @param AngelType $angeltype * @return string */ -function UserAngelType_confirm_view($user_angeltype, $user, $angeltype) +function UserAngelType_confirm_view($user_angeltype, $user, AngelType $angeltype) { return page_with_title(__('Confirm angeltype for user'), [ msg(), info(sprintf( __('Do you really want to confirm %s for %s?'), User_Nick_render($user), - $angeltype['name'] + $angeltype->name ), true), form([ buttons([ - button(angeltype_link($angeltype['id']), icon('x-lg') . __('cancel')), + button(angeltype_link($angeltype->id), icon('x-lg') . __('cancel')), form_submit('confirm_user', icon('check-lg') . __('yes'), 'btn-primary', false), ]), ], page_link_to('user_angeltypes', ['action' => 'confirm', 'user_angeltype_id' => $user_angeltype['id']])), @@ -103,23 +104,23 @@ function UserAngelType_confirm_view($user_angeltype, $user, $angeltype) } /** - * @param array $user_angeltype - * @param User $user - * @param array $angeltype + * @param array $user_angeltype + * @param User $user + * @param AngelType $angeltype * @return string */ -function UserAngelType_delete_view($user_angeltype, $user, $angeltype) +function UserAngelType_delete_view($user_angeltype, $user, AngelType $angeltype) { return page_with_title(__('Remove angeltype'), [ msg(), info(sprintf( __('Do you really want to delete %s from %s?'), User_Nick_render($user), - $angeltype['name'] + $angeltype->name ), true), form([ buttons([ - button(angeltype_link($angeltype['id']), icon('x-lg') . __('cancel')), + button(angeltype_link($angeltype->id), icon('x-lg') . __('cancel')), form_submit('delete', icon('check-lg') . __('yes'), 'btn-primary', false), ]), ], page_link_to('user_angeltypes', ['action' => 'delete', 'user_angeltype_id' => $user_angeltype['id']])), @@ -127,12 +128,12 @@ function UserAngelType_delete_view($user_angeltype, $user, $angeltype) } /** - * @param array $angeltype - * @param User[] $users_source - * @param int $user_id + * @param AngelType $angeltype + * @param User[] $users_source + * @param int $user_id * @return string */ -function UserAngelType_add_view($angeltype, $users_source, $user_id) +function UserAngelType_add_view(AngelType $angeltype, $users_source, $user_id) { $users = []; foreach ($users_source as $user_source) { @@ -143,13 +144,13 @@ function UserAngelType_add_view($angeltype, $users_source, $user_id) msg(), buttons([ button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), + page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]), __('back'), 'back' ) ]), form([ - form_info(__('Angeltype'), $angeltype['name']), + form_info(__('Angeltype'), $angeltype->name), form_checkbox('auto_confirm_user', __('Confirm user'), true), form_select('user_id', __('User'), $users, $user_id), form_submit('submit', __('Add')) @@ -158,28 +159,28 @@ function UserAngelType_add_view($angeltype, $users_source, $user_id) } /** - * @param User $user - * @param array $angeltype + * @param User $user + * @param AngelType $angeltype * @return string */ -function UserAngelType_join_view($user, $angeltype) +function UserAngelType_join_view($user, AngelType $angeltype) { - return page_with_title(sprintf(__('Become a %s'), $angeltype['name']), [ + return page_with_title(sprintf(__('Become a %s'), $angeltype->name), [ msg(), info(sprintf( __('Do you really want to add %s to %s?'), User_Nick_render($user), - $angeltype['name'] + $angeltype->name ), true), form([ auth()->can('admin_user_angeltypes') ? form_checkbox('auto_confirm_user', __('Confirm user'), true) : '', buttons([ - button(angeltype_link($angeltype['id']), icon('x-lg') . __('cancel')), + button(angeltype_link($angeltype->id), icon('x-lg') . __('cancel')), form_submit('submit', icon('check-lg') . __('save'), 'btn-primary', false) ]), ], page_link_to( 'user_angeltypes', - ['action' => 'add', 'angeltype_id' => $angeltype['id'], 'user_id' => $user->id] + ['action' => 'add', 'angeltype_id' => $angeltype->id, 'user_id' => $user->id] )), ]); } diff --git a/includes/view/User_view.php b/includes/view/User_view.php index 4ff712fe..01366519 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -1,6 +1,7 @@ $shift['TID']], $user_source->id)) { + if (Shift_signout_allowed($shift, (new AngelType())->forceFill(['id' => $shift['TID']]), $user_source->id)) { $myshift['actions'][] = button( shift_entry_delete_link($shift), icon('trash') . __('sign off'), diff --git a/src/Models/AngelType.php b/src/Models/AngelType.php new file mode 100644 index 00000000..b10d2eb4 --- /dev/null +++ b/src/Models/AngelType.php @@ -0,0 +1,77 @@ + */ + protected $casts = [ + 'restricted' => 'boolean', + 'requires_driver_license' => 'boolean', + 'no_self_signup' => 'boolean', + 'show_on_dashboard' => 'boolean', + 'hide_register' => 'boolean', + ]; + + protected static function boot(): void + { + parent::boot(); + static::addGlobalScope('order', fn(Builder $builder) => $builder->orderBy('name')); + } + + public function hasContactInfo(): bool + { + return !empty($this->contact_name) + || !empty($this->contact_dect) + || !empty($this->contact_email); + } +} diff --git a/tests/Unit/Models/AngelTypeTest.php b/tests/Unit/Models/AngelTypeTest.php new file mode 100644 index 00000000..f1135bc8 --- /dev/null +++ b/tests/Unit/Models/AngelTypeTest.php @@ -0,0 +1,54 @@ + + */ + public function hasContactInfoDataProvider(): array + { + return [ + [false, '', '', ''], + [true, 'Foo', '', ''], + [true, '', 'BAR', ''], + [true, '', '', 'baz@localhost'], + [true, 'Foo', 'BAR', 'baz@localhost'], + ]; + } + + /** + * @covers \Engelsystem\Models\AngelType::hasContactInfo + * @dataProvider hasContactInfoDataProvider + */ + public function testHasContactInfo(bool $expected, ?string $name, ?string $dect, ?string $email): void + { + $model = new AngelType([ + 'contact_name' => $name, + 'contact_dect' => $dect, + 'contact_email' => $email, + ]); + + $this->assertEquals($expected, $model->hasContactInfo()); + } + + /** + * @covers \Engelsystem\Models\AngelType::boot + */ + public function testBoot(): void + { + AngelType::factory()->create(['name' => 'foo']); + AngelType::factory()->create(['name' => 'bar']); + AngelType::factory()->create(['name' => 'baz']); + AngelType::factory()->create(['name' => 'lorem']); + AngelType::factory()->create(['name' => 'ipsum']); + + $this->assertEquals( + ['bar', 'baz', 'foo', 'ipsum', 'lorem'], + AngelType::all()->map(fn(AngelType $angelType) => $angelType->toArray())->pluck('name')->toArray() + ); + } +}