Implemented AngelTypes model

This commit is contained in:
Igor Scheller 2022-11-09 00:02:30 +01:00 committed by Michael Weimann
parent bdc62eaac3
commit 6686d58c06
35 changed files with 910 additions and 841 deletions

View File

@ -0,0 +1,33 @@
<?php
namespace Database\Factories\Engelsystem\Models;
use Engelsystem\Models\AngelType;
use Illuminate\Database\Eloquent\Factories\Factory;
class AngelTypeFactory extends Factory
{
/** @var string */
protected $model = AngelType::class;
/**
* @return array
*/
public function definition(): array
{
return [
'name' => $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(),
];
}
}

View File

@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Support\Collection;
use Illuminate\Database\Schema\Blueprint;
use stdClass;
class CreateAngelTypesTable extends Migration
{
use ChangesReferences;
/**
* Creates the new table, copies the data and drops the old one
*/
public function up(): void
{
$connection = $this->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');
}
}

View File

@ -1,7 +1,11 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftsFilterRenderer; use Engelsystem\ShiftsFilterRenderer;
use Engelsystem\ValidationResult;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
/** /**
* Text for Angeltype related links. * Text for Angeltype related links.
@ -62,7 +66,7 @@ function angeltypes_about_controller()
if ($user) { if ($user) {
$angeltypes = AngelTypes_with_user($user->id); $angeltypes = AngelTypes_with_user($user->id);
} else { } else {
$angeltypes = AngelTypes(); $angeltypes = AngelType::all();
} }
return [ return [
@ -82,16 +86,17 @@ function angeltype_delete_controller()
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = load_angeltype(); $angeltype = AngelType::findOrFail(request()->input('angeltype_id'));
if (request()->hasPostData('delete')) { 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))); success(sprintf(__('Angeltype %s deleted.'), AngelType_name_render($angeltype)));
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
return [ return [
sprintf(__('Delete angeltype %s'), $angeltype['name']), sprintf(__('Delete angeltype %s'), $angeltype->name),
AngelType_delete_view($angeltype) AngelType_delete_view($angeltype)
]; ];
} }
@ -109,7 +114,7 @@ function angeltype_edit_controller()
if ($request->has('angeltype_id')) { if ($request->has('angeltype_id')) {
// Edit existing angeltype // Edit existing angeltype
$angeltype = load_angeltype(); $angeltype = AngelType::findOrFail($request->input('angeltype_id'));
if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) { if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) {
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
@ -120,7 +125,7 @@ function angeltype_edit_controller()
// Supporters aren't allowed to create new angeltypes. // Supporters aren't allowed to create new angeltypes.
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType_new(); $angeltype = new AngelType();
} }
if ($request->hasPostData('submit')) { if ($request->hasPostData('submit')) {
@ -129,41 +134,47 @@ function angeltype_edit_controller()
if (!$supporter_mode) { if (!$supporter_mode) {
if ($request->has('name')) { if ($request->has('name')) {
$result = AngelType_validate_name($request->postData('name'), $angeltype); $result = AngelType_validate_name($request->postData('name'), $angeltype);
$angeltype['name'] = $result->getValue(); $angeltype->name = $result->getValue();
if (!$result->isValid()) { if (!$result->isValid()) {
$valid = false; $valid = false;
error(__('Please check the name. Maybe it already exists.')); error(__('Please check the name. Maybe it already exists.'));
} }
} }
$angeltype['restricted'] = $request->has('restricted'); $angeltype->restricted = $request->has('restricted');
$angeltype['no_self_signup'] = $request->has('no_self_signup'); $angeltype->no_self_signup = $request->has('no_self_signup');
$angeltype['show_on_dashboard'] = $request->has('show_on_dashboard'); $angeltype->show_on_dashboard = $request->has('show_on_dashboard');
$angeltype['hide_register'] = $request->has('hide_register'); $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_name = strip_request_item('contact_name', $angeltype->contact_name);
$angeltype['contact_dect'] = strip_request_item('contact_dect', $angeltype['contact_dect']); $angeltype->contact_dect = strip_request_item('contact_dect', $angeltype->contact_dect);
$angeltype['contact_email'] = strip_request_item('contact_email', $angeltype['contact_email']); $angeltype->contact_email = strip_request_item('contact_email', $angeltype->contact_email);
if ($valid) { if ($valid) {
if (!empty($angeltype['id'])) { $angeltype->save();
AngelType_update($angeltype);
} else {
$angeltype = AngelType_create($angeltype);
}
success('Angel type saved.'); 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 [ return [
sprintf(__('Edit %s'), $angeltype['name']), sprintf(__('Edit %s'), $angeltype->name),
AngelType_edit_view($angeltype, $supporter_mode) AngelType_edit_view($angeltype, $supporter_mode)
]; ];
} }
@ -181,7 +192,7 @@ function angeltype_controller()
throw_redirect(page_link_to('/')); 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); $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype);
$members = Users_by_angeltype($angeltype); $members = Users_by_angeltype($angeltype);
@ -201,7 +212,7 @@ function angeltype_controller()
$isSupporter = !is_null($user_angeltype) && $user_angeltype['supporter']; $isSupporter = !is_null($user_angeltype) && $user_angeltype['supporter'];
return [ return [
sprintf(__('Team %s'), $angeltype['name']), sprintf(__('Team %s'), $angeltype->name),
AngelType_view( AngelType_view(
$angeltype, $angeltype,
$members, $members,
@ -221,10 +232,10 @@ function angeltype_controller()
/** /**
* On which days do shifts for this angeltype occur? Needed for shiftCalendar. * On which days do shifts for this angeltype occur? Needed for shiftCalendar.
* *
* @param array $angeltype * @param AngelType $angeltype
* @return array * @return array
*/ */
function angeltype_controller_shiftsFilterDays($angeltype) function angeltype_controller_shiftsFilterDays(AngelType $angeltype)
{ {
$all_shifts = Shifts_by_angeltype($angeltype); $all_shifts = Shifts_by_angeltype($angeltype);
$days = []; $days = [];
@ -241,17 +252,17 @@ function angeltype_controller_shiftsFilterDays($angeltype)
/** /**
* Sets up the shift filter for the angeltype. * Sets up the shift filter for the angeltype.
* *
* @param array $angeltype * @param AngelType $angeltype
* @param array $days * @param array $days
* @return ShiftsFilter * @return ShiftsFilter
*/ */
function angeltype_controller_shiftsFilter($angeltype, $days) function angeltype_controller_shiftsFilter(AngelType $angeltype, $days)
{ {
$request = request(); $request = request();
$shiftsFilter = new ShiftsFilter( $shiftsFilter = new ShiftsFilter(
auth()->can('user_shifts_admin'), auth()->can('user_shifts_admin'),
Room_ids(), Room_ids(),
[$angeltype['id']] [$angeltype->id]
); );
$selected_day = date('Y-m-d'); $selected_day = date('Y-m-d');
if (!empty($days) && !in_array($selected_day, $days)) { if (!empty($days) && !in_array($selected_day, $days)) {
@ -281,10 +292,10 @@ function angeltypes_list_controller()
$angeltypes = AngelTypes_with_user($user->id); $angeltypes = AngelTypes_with_user($user->id);
foreach ($angeltypes as &$angeltype) { foreach ($angeltypes as $angeltype) {
$actions = [ $actions = [
button( button(
page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]),
__('view'), __('view'),
'btn-sm' 'btn-sm'
) )
@ -292,45 +303,45 @@ function angeltypes_list_controller()
if (auth()->can('admin_angel_types')) { if (auth()->can('admin_angel_types')) {
$actions[] = button( $actions[] = button(
page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype->id]),
__('edit'), __('edit'),
'btn-sm' 'btn-sm'
); );
$actions[] = button( $actions[] = button(
page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype->id]),
__('delete'), __('delete'),
'btn-sm' 'btn-sm'
); );
} }
$angeltype['membership'] = AngelType_render_membership($angeltype); $angeltype->membership = AngelType_render_membership($angeltype);
if (!empty($angeltype['user_angeltype_id'])) { if (!empty($angeltype->user_angeltype_id)) {
$actions[] = button( $actions[] = button(
page_link_to( page_link_to(
'user_angeltypes', 'user_angeltypes',
['action' => 'delete', 'user_angeltype_id' => $angeltype['user_angeltype_id']] ['action' => 'delete', 'user_angeltype_id' => $angeltype->user_angeltype_id]
), ),
__('leave'), __('leave'),
'btn-sm' 'btn-sm'
); );
} else { } else {
$actions[] = button( $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'), __('join'),
'btn-sm' 'btn-sm'
); );
} }
$angeltype['restricted'] = $angeltype['restricted'] ? icon('book') : ''; $angeltype->is_restricted = $angeltype->restricted ? icon('book') : '';
$angeltype['no_self_signup'] = $angeltype['no_self_signup'] ? '' : icon('pencil-square'); $angeltype->no_self_signup_allowed = $angeltype->no_self_signup ? '' : icon('pencil-square');
$angeltype['name'] = '<a href="' $angeltype->name = '<a href="'
. page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]) . page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id])
. '">' . '">'
. $angeltype['name'] . $angeltype->name
. '</a>'; . '</a>';
$angeltype['actions'] = table_buttons($actions); $angeltype->actions = table_buttons($actions);
} }
return [ 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(); $name = strip_item($name);
if (!$request->has('angeltype_id')) { if ($name == '') {
throw_redirect(page_link_to('angeltypes')); 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')); $valid = AngelType::whereName($name)->count() == 0;
if (empty($angeltype)) { return new ValidationResult($valid, $name);
error(__('Angeltype doesn\'t exist . ')); }
throw_redirect(page_link_to('angeltypes'));
} /**
* Returns all angeltypes and subscription state to each of them for given user.
return $angeltype; *
* @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();
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
@ -109,11 +110,11 @@ function public_dashboard_needed_angels($needed_angels, ShiftsFilter $filter = n
foreach ($needed_angels as $needed_angel) { foreach ($needed_angels as $needed_angel) {
$need = $needed_angel['count'] - $needed_angel['taken']; $need = $needed_angel['count'] - $needed_angel['taken'];
if ($need > 0 && (!$filter || in_array($needed_angel['TID'], $filter->getTypes()))) { if ($need > 0 && (!$filter || in_array($needed_angel['TID'], $filter->getTypes()))) {
$angeltype = AngelType($needed_angel['TID']); $angeltype = AngelType::find($needed_angel['TID']);
if ($angeltype['show_on_dashboard']) { if ($angeltype->show_on_dashboard) {
$result[] = [ $result[] = [
'need' => $need, 'need' => $need,
'angeltype_name' => $angeltype['name'] 'angeltype_name' => $angeltype->name
]; ];
} }
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftsFilterRenderer; use Engelsystem\ShiftsFilterRenderer;
@ -34,7 +35,7 @@ function room_controller(): array
$shiftsFilter = new ShiftsFilter( $shiftsFilter = new ShiftsFilter(
true, true,
[$room->id], [$room->id],
AngelType_ids() AngelType::query()->get('id')->pluck('id')->toArray()
); );
$selected_day = date('Y-m-d'); $selected_day = date('Y-m-d');
if (!empty($days) && !in_array($selected_day, $days)) { if (!empty($days) && !in_array($selected_day, $days)) {

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
@ -51,7 +52,7 @@ function shift_entry_create_controller(): array
throw_redirect(user_link($user->id)); throw_redirect(user_link($user->id));
} }
$angeltype = AngelType($request->input('angeltype_id')); $angeltype = AngelType::find($request->input('angeltype_id'));
if (auth()->can('user_shifts_admin')) { if (auth()->can('user_shifts_admin')) {
return shift_entry_create_controller_admin($shift, $angeltype); return shift_entry_create_controller_admin($shift, $angeltype);
@ -73,10 +74,10 @@ function shift_entry_create_controller(): array
* Case: Admin * Case: Admin
* *
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType|null $angeltype
* @return array * @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(); $signup_user = auth()->user();
$request = request(); $request = request();
@ -88,9 +89,9 @@ function shift_entry_create_controller_admin($shift, $angeltype): array
throw_redirect(shift_link($shift)); throw_redirect(shift_link($shift));
} }
$angeltypes = AngelTypes(); $angeltypes = AngelType::all();
if ($request->hasPostData('angeltype_id')) { if ($request->hasPostData('angeltype_id')) {
$angeltype = AngelType($request->postData('angeltype_id')); $angeltype = AngelType::find($request->postData('angeltype_id'));
} }
if (empty($angeltype)) { if (empty($angeltype)) {
if (count($angeltypes) == 0) { if (count($angeltypes) == 0) {
@ -102,7 +103,7 @@ function shift_entry_create_controller_admin($shift, $angeltype): array
if ($request->hasPostData('submit')) { if ($request->hasPostData('submit')) {
ShiftEntry_create([ ShiftEntry_create([
'SID' => $shift['SID'], 'SID' => $shift['SID'],
'TID' => $angeltype['id'], 'TID' => $angeltype->id,
'UID' => $signup_user->id, 'UID' => $signup_user->id,
'Comment' => '', 'Comment' => '',
'freeloaded' => false, 'freeloaded' => false,
@ -120,11 +121,7 @@ function shift_entry_create_controller_admin($shift, $angeltype): array
$users_select[$user->id] = $user->name; $users_select[$user->id] = $user->name;
} }
$angeltypes_select = []; $angeltypes_select = $angeltypes->pluck('name', 'id')->toArray();
foreach ($angeltypes as $a) {
$angeltypes_select[$a['id']] = $a['name'];
}
$room = Room::find($shift['RID']); $room = Room::find($shift['RID']);
return [ return [
ShiftEntry_create_title(), ShiftEntry_create_title(),
@ -137,10 +134,10 @@ function shift_entry_create_controller_admin($shift, $angeltype): array
* Case: Supporter * Case: Supporter
* *
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @return array * @return array
*/ */
function shift_entry_create_controller_supporter($shift, $angeltype): array function shift_entry_create_controller_supporter($shift, AngelType $angeltype): array
{ {
$request = request(); $request = request();
$signup_user = auth()->user(); $signup_user = auth()->user();
@ -156,7 +153,7 @@ function shift_entry_create_controller_supporter($shift, $angeltype): array
if ($request->hasPostData('submit')) { if ($request->hasPostData('submit')) {
ShiftEntry_create([ ShiftEntry_create([
'SID' => $shift['SID'], 'SID' => $shift['SID'],
'TID' => $angeltype['id'], 'TID' => $angeltype->id,
'UID' => $signup_user->id, 'UID' => $signup_user->id,
'Comment' => '', 'Comment' => '',
'freeloaded' => false, 'freeloaded' => false,
@ -209,16 +206,16 @@ function shift_entry_error_message(ShiftSignupState $shift_signup_state)
* Case: User * Case: User
* *
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @return array * @return array
*/ */
function shift_entry_create_controller_user($shift, $angeltype): array function shift_entry_create_controller_user($shift, AngelType $angeltype): array
{ {
$request = request(); $request = request();
$signup_user = auth()->user(); $signup_user = auth()->user();
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype); $needed_angeltype = (new AngelType())->forceFill(NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype));
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']); $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype->id);
$shift_signup_state = Shift_signup_allowed( $shift_signup_state = Shift_signup_allowed(
$signup_user, $signup_user,
$shift, $shift,
@ -238,14 +235,14 @@ function shift_entry_create_controller_user($shift, $angeltype): array
$comment = strip_request_item_nl('comment'); $comment = strip_request_item_nl('comment');
ShiftEntry_create([ ShiftEntry_create([
'SID' => $shift['SID'], 'SID' => $shift['SID'],
'TID' => $angeltype['id'], 'TID' => $angeltype->id,
'UID' => $signup_user->id, 'UID' => $signup_user->id,
'Comment' => $comment, 'Comment' => $comment,
'freeloaded' => false, 'freeloaded' => false,
'freeload_comment' => '' '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); 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. * Link to create a shift entry.
* *
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @param array $params * @param array $params
* @return string URL * @return string URL
*/ */
function shift_entry_create_link($shift, $angeltype, $params = []) function shift_entry_create_link($shift, AngelType $angeltype, $params = [])
{ {
$params = array_merge([ $params = array_merge([
'action' => 'create', 'action' => 'create',
'shift_id' => $shift['SID'], 'shift_id' => $shift['SID'],
'angeltype_id' => $angeltype['id'] 'angeltype_id' => $angeltype->id
], $params); ], $params);
return page_link_to('shift_entries', $params); return page_link_to('shift_entries', $params);
} }
@ -327,7 +324,7 @@ function shift_entry_delete_controller()
$shiftEntry = shift_entry_load(); $shiftEntry = shift_entry_load();
$shift = Shift($shiftEntry['SID']); $shift = Shift($shiftEntry['SID']);
$angeltype = AngelType($shiftEntry['TID']); $angeltype = AngelType::find($shiftEntry['TID']);
$signout_user = User::find($shiftEntry['UID']); $signout_user = User::find($shiftEntry['UID']);
if (!Shift_signout_allowed($shift, $angeltype, $signout_user->id)) { if (!Shift_signout_allowed($shift, $angeltype, $signout_user->id)) {
error(__( error(__(

View File

@ -2,6 +2,7 @@
use Engelsystem\Helpers\Carbon; use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\ScheduleShift; use Engelsystem\Models\Shifts\ScheduleShift;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
@ -70,14 +71,10 @@ function shift_edit_controller()
foreach (Rooms() as $room) { foreach (Rooms() as $room) {
$rooms[$room->id] = $room->name; $rooms[$room->id] = $room->name;
} }
$angeltypes = select_array(AngelTypes(), 'id', 'name'); $angeltypes = AngelType::all()->pluck('name', 'id')->toArray();
$shifttypes = select_array(ShiftType::all(), 'id', 'name'); $shifttypes = ShiftType::all()->pluck('name', 'id')->toArray();
$needed_angel_types = select_array( $needed_angel_types = collect(NeededAngelTypes_by_shift($shift_id))->pluck('count', 'angel_type_id')->toArray();
NeededAngelTypes_by_shift($shift_id),
'angel_type_id',
'count'
);
foreach (array_keys($angeltypes) as $angeltype_id) { foreach (array_keys($angeltypes) as $angeltype_id) {
if (!isset($needed_angel_types[$angeltype_id])) { if (!isset($needed_angel_types[$angeltype_id])) {
$needed_angel_types[$angeltype_id] = 0; $needed_angel_types[$angeltype_id] = 0;
@ -242,14 +239,14 @@ function shift_delete_controller()
if ($request->hasPostData('delete')) { if ($request->hasPostData('delete')) {
$room = Room::find($shift['RID']); $room = Room::find($shift['RID']);
foreach ($shift['ShiftEntry'] as $entry) { foreach ($shift['ShiftEntry'] as $entry) {
$type = AngelType($entry['TID']); $type = AngelType::find($entry['TID']);
event('shift.entry.deleting', [ event('shift.entry.deleting', [
'user' => User::find($entry['user_id']), 'user' => User::find($entry['user_id']),
'start' => Carbon::createFromTimestamp($shift['start']), 'start' => Carbon::createFromTimestamp($shift['start']),
'end' => Carbon::createFromTimestamp($shift['end']), 'end' => Carbon::createFromTimestamp($shift['end']),
'name' => $shift['name'], 'name' => $shift['name'],
'title' => $shift['title'], 'title' => $shift['title'],
'type' => $type['name'], 'type' => $type->name,
'room' => $room, 'room' => $room,
'freeloaded' => (bool)$entry['freeloaded'], 'freeloaded' => (bool)$entry['freeloaded'],
]); ]);
@ -304,17 +301,18 @@ function shift_controller()
$shifttype = ShiftType::find($shift['shifttype_id']); $shifttype = ShiftType::find($shift['shifttype_id']);
$room = Room::find($shift['RID']); $room = Room::find($shift['RID']);
$angeltypes = AngelTypes(); $angeltypes = AngelType::all();
$user_shifts = Shifts_by_user($user->id); $user_shifts = Shifts_by_user($user->id);
$shift_signup_state = new ShiftSignupState(ShiftSignupState::OCCUPIED, 0); $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); $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
if (empty($needed_angeltype)) { if (empty($needed_angeltype)) {
continue; 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( $angeltype_signup_state = Shift_signup_allowed(
$user, $user,
@ -326,7 +324,7 @@ function shift_controller()
$shift_entries $shift_entries
); );
$shift_signup_state->combineWith($angeltype_signup_state); $shift_signup_state->combineWith($angeltype_signup_state);
$angeltype['shift_signup_state'] = $angeltype_signup_state; $angeltype->shift_signup_state = $angeltype_signup_state;
} }
return [ return [

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Mail\EngelsystemMailer; use Engelsystem\Mail\EngelsystemMailer;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Exception\TransportException;
@ -27,7 +28,8 @@ function user_angeltypes_unconfirmed_hint()
} }
$count = count($unconfirmed_user_angeltypes); $count = count($unconfirmed_user_angeltypes);
return _e( return
_e(
'There is %d unconfirmed angeltype.', 'There is %d unconfirmed angeltype.',
'There are %d unconfirmed angeltypes.', 'There are %d unconfirmed angeltypes.',
$count, $count,
@ -51,7 +53,7 @@ function user_angeltypes_delete_all_controller(): array
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType($request->input('angeltype_id')); $angeltype = AngelType::find($request->input('angeltype_id'));
if (empty($angeltype)) { if (empty($angeltype)) {
error(__('Angeltype doesn\'t exist.')); error(__('Angeltype doesn\'t exist.'));
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
@ -63,11 +65,11 @@ function user_angeltypes_delete_all_controller(): array
} }
if ($request->hasPostData('deny_all')) { 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))); 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))); 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 [ return [
@ -91,20 +93,15 @@ function user_angeltypes_confirm_all_controller(): array
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType($request->input('angeltype_id')); $angeltype = AngelType::findOrFail($request->input('angeltype_id'));
if (empty($angeltype)) {
error(__('Angeltype doesn\'t exist.'));
throw_redirect(page_link_to('angeltypes'));
}
if (!auth()->can('admin_user_angeltypes') && !User_is_AngelType_supporter($user, $angeltype)) { if (!auth()->can('admin_user_angeltypes') && !User_is_AngelType_supporter($user, $angeltype)) {
error(__('You are not allowed to confirm all users for this angeltype.')); error(__('You are not allowed to confirm all users for this angeltype.'));
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
if ($request->hasPostData('confirm_all')) { if ($request->hasPostData('confirm_all')) {
$users = UserAngelTypes_all_unconfirmed($angeltype['id']); $users = UserAngelTypes_all_unconfirmed($angeltype->id);
UserAngelTypes_confirm_all($angeltype['id'], $user->id); UserAngelTypes_confirm_all($angeltype->id, $user->id);
engelsystem_log(sprintf('Confirmed all users for angeltype %s', AngelType_name_render($angeltype, true))); 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))); 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); 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 [ 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 * @return array
*/ */
@ -144,12 +141,7 @@ function user_angeltype_confirm_controller(): array
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType($user_angeltype['angeltype_id']); $angeltype = AngelType::findOrFail($user_angeltype['angeltype_id']);
if (empty($angeltype)) {
error(__('Angeltype doesn\'t exist.'));
throw_redirect(page_link_to('angeltypes'));
}
if (!User_is_AngelType_supporter($user, $angeltype)) { if (!User_is_AngelType_supporter($user, $angeltype)) {
error(__('You are not allowed to confirm this users angeltype.')); error(__('You are not allowed to confirm this users angeltype.'));
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
@ -177,7 +169,7 @@ function user_angeltype_confirm_controller(): array
user_angeltype_confirm_email($user_source, $angeltype); 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 [ return [
@ -188,10 +180,10 @@ function user_angeltype_confirm_controller(): array
/** /**
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @return void * @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) { if (!$user->settings->email_shiftinfo) {
return; return;
@ -204,7 +196,7 @@ function user_angeltype_confirm_email(User $user, array $angeltype): void
$user, $user,
'notification.angeltype.confirmed', 'notification.angeltype.confirmed',
'emails/angeltype-confirmed', 'emails/angeltype-confirmed',
['name' => $angeltype['name'], 'angeltype' => $angeltype, 'username' => $user->name] ['name' => $angeltype->name, 'angeltype' => $angeltype, 'username' => $user->name]
); );
} catch (TransportException $e) { } catch (TransportException $e) {
/** @var LoggerInterface $logger */ /** @var LoggerInterface $logger */
@ -218,10 +210,10 @@ function user_angeltype_confirm_email(User $user, array $angeltype): void
/** /**
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @return void * @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) { if (!$user->settings->email_shiftinfo || $user->id == auth()->user()->id) {
return; return;
@ -234,7 +226,7 @@ function user_angeltype_add_email(User $user, array $angeltype): void
$user, $user,
'notification.angeltype.added', 'notification.angeltype.added',
'emails/angeltype-added', 'emails/angeltype-added',
['name' => $angeltype['name'], 'angeltype' => $angeltype, 'username' => $user->name] ['name' => $angeltype->name, 'angeltype' => $angeltype, 'username' => $user->name]
); );
} catch (TransportException $e) { } catch (TransportException $e) {
/** @var LoggerInterface $logger */ /** @var LoggerInterface $logger */
@ -267,18 +259,8 @@ function user_angeltype_delete_controller(): array
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType($user_angeltype['angeltype_id']); $angeltype = AngelType::findOrFail($user_angeltype['angeltype_id']);
if (empty($angeltype)) { $user_source = User::findOrFail($user_angeltype['user_id']);
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'));
}
if ($user->id != $user_angeltype['user_id'] && !User_is_AngelType_supporter($user, $angeltype)) { if ($user->id != $user_angeltype['user_id'] && !User_is_AngelType_supporter($user, $angeltype)) {
error(__('You are not allowed to delete this users angeltype.')); error(__('You are not allowed to delete this users angeltype.'));
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
@ -287,10 +269,10 @@ function user_angeltype_delete_controller(): array
if ($request->hasPostData('delete')) { if ($request->hasPostData('delete')) {
UserAngelType_delete($user_angeltype); UserAngelType_delete($user_angeltype);
engelsystem_log(sprintf('User %s removed from %s.', User_Nick_render($user_source, true), $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'])); 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 [ return [
@ -332,17 +314,8 @@ function user_angeltype_update_controller(): array
throw_redirect(page_link_to('angeltypes')); throw_redirect(page_link_to('angeltypes'));
} }
$angeltype = AngelType($user_angeltype['angeltype_id']); $angeltype = AngelType::findOrFail($user_angeltype['angeltype_id']);
if (empty($angeltype)) { $user_source = User::findOrFail($user_angeltype['user_id']);
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'));
}
if ($request->hasPostData('submit')) { if ($request->hasPostData('submit')) {
UserAngelType_update($user_angeltype['id'], $supporter); UserAngelType_update($user_angeltype['id'], $supporter);
@ -361,7 +334,7 @@ function user_angeltype_update_controller(): array
User_Nick_render($user_source) 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 [ return [
@ -377,7 +350,7 @@ function user_angeltype_update_controller(): array
*/ */
function user_angeltype_add_controller(): array function user_angeltype_add_controller(): array
{ {
$angeltype = load_angeltype(); $angeltype = AngelType::findOrFail(request()->input('angeltype_id'));
// User is joining by itself // User is joining by itself
if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) { 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); 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. * A user joins an angeltype.
* *
* @param array $angeltype * @param AngelType $angeltype
* @return array * @return array
*/ */
function user_angeltype_join_controller($angeltype) function user_angeltype_join_controller(AngelType $angeltype)
{ {
$user = auth()->user(); $user = auth()->user();
$user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype); $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype);
if (!empty($user_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')); throw_redirect(page_link_to('angeltypes'));
} }
@ -451,7 +424,7 @@ function user_angeltype_join_controller($angeltype)
if ($request->hasPostData('submit')) { if ($request->hasPostData('submit')) {
$user_angeltype_id = UserAngelType_create($user->id, $angeltype); $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( engelsystem_log(sprintf(
'User %s joined %s.', 'User %s joined %s.',
User_Nick_render($user, true), 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 [ return [
sprintf(__('Become a %s'), $angeltype['name']), sprintf(__('Become a %s'), $angeltype->name),
UserAngelType_join_view($user, $angeltype) UserAngelType_join_view($user, $angeltype)
]; ];
} }

View File

@ -212,11 +212,11 @@ function user_controller()
// TODO: Move queries to model // TODO: Move queries to model
$shift['needed_angeltypes'] = Db::select( $shift['needed_angeltypes'] = Db::select(
' '
SELECT DISTINCT `AngelTypes`.* SELECT DISTINCT `angel_types`.*
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` JOIN `angel_types` ON `ShiftEntry`.`TID`=`angel_types`.`id`
WHERE `ShiftEntry`.`SID` = ? WHERE `ShiftEntry`.`SID` = ?
ORDER BY `AngelTypes`.`name` ORDER BY `angel_types`.`name`
', ',
[$shift['SID']] [$shift['SID']]
); );

View File

@ -4,6 +4,7 @@ namespace Engelsystem\Events\Listener;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Database\Database; use Engelsystem\Database\Database;
use Engelsystem\Models\AngelType;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -55,7 +56,7 @@ class OAuth2
'SSO {provider}: Added to angeltype {angeltype}, confirmed: {confirmed}, supporter: {supporter}', 'SSO {provider}: Added to angeltype {angeltype}, confirmed: {confirmed}, supporter: {supporter}',
[ [
'provider' => $provider, 'provider' => $provider,
'angeltype' => AngelType($team['id'])['name'], 'angeltype' => AngelType::find($team['id'])->name,
'confirmed' => $confirmed ? 'yes' : 'no', 'confirmed' => $confirmed ? 'yes' : 'no',
'supporter' => $supporter ? 'yes' : 'no', 'supporter' => $supporter ? 'yes' : 'no',
] ]
@ -81,7 +82,7 @@ class OAuth2
'SSO {provider}: Set supporter state for angeltype {angeltype}', 'SSO {provider}: Set supporter state for angeltype {angeltype}',
[ [
'provider' => $provider, '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}', 'SSO {provider}: Set confirmed state for angeltype {angeltype}',
[ [
'provider' => $provider, 'provider' => $provider,
'angeltype' => AngelType($userAngeltype->angeltype_id)['name'], 'angeltype' => AngelType::find($userAngeltype->angeltype_id)->name,
] ]
); );
} }

View File

@ -11,7 +11,6 @@ $includeFiles = [
__DIR__ . '/../includes/sys_page.php', __DIR__ . '/../includes/sys_page.php',
__DIR__ . '/../includes/sys_template.php', __DIR__ . '/../includes/sys_template.php',
__DIR__ . '/../includes/model/AngelType_model.php',
__DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php',
__DIR__ . '/../includes/model/Room_model.php', __DIR__ . '/../includes/model/Room_model.php',
__DIR__ . '/../includes/model/ShiftEntry_model.php', __DIR__ . '/../includes/model/ShiftEntry_model.php',

View File

@ -1,249 +0,0 @@
<?php
use Engelsystem\Database\Db;
use Engelsystem\ValidationResult;
/**
* Returns an array containing the basic attributes of angeltypes.
* FIXME! This is the big sign for needing entity objects
*/
function AngelType_new()
{
return [
'id' => 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;
}

View File

@ -9,7 +9,7 @@ use Engelsystem\Database\Db;
/** /**
* Insert a new needed angel type. * 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 $angeltype_id The angeltype
* @param int|null $room_id The room. Can be null, but then a shift_id must be given. * @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? * @param int $count How many angels are needed?
@ -83,12 +83,12 @@ function NeededAngelTypes_by_shift($shiftId)
' '
SELECT SELECT
`NeededAngelTypes`.*, `NeededAngelTypes`.*,
`AngelTypes`.`id`, `angel_types`.`id`,
`AngelTypes`.`name`, `angel_types`.`name`,
`AngelTypes`.`restricted`, `angel_types`.`restricted`,
`AngelTypes`.`no_self_signup` `angel_types`.`no_self_signup`
FROM `NeededAngelTypes` 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` = ? WHERE `shift_id` = ?
AND `count` > 0 AND `count` > 0
ORDER BY `room_id` DESC', ORDER BY `room_id` DESC',
@ -98,9 +98,9 @@ function NeededAngelTypes_by_shift($shiftId)
// Use settings from room // Use settings from room
if (count($needed_angeltypes_source) == 0) { if (count($needed_angeltypes_source) == 0) {
$needed_angeltypes_source = Db::select(' $needed_angeltypes_source = Db::select('
SELECT `NeededAngelTypes`.*, `AngelTypes`.`name`, `AngelTypes`.`restricted` SELECT `NeededAngelTypes`.*, `angel_types`.`name`, `angel_types`.`restricted`
FROM `NeededAngelTypes` 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` JOIN `Shifts` ON `Shifts`.`RID` = `NeededAngelTypes`.`room_id`
WHERE `Shifts`.`SID` = ? WHERE `Shifts`.`SID` = ?
AND `count` > 0 AND `count` > 0

View File

@ -2,6 +2,7 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -37,12 +38,12 @@ function ShiftEntries_by_shift($shift_id)
`ShiftEntry`.`UID`, `ShiftEntry`.`UID`,
`ShiftEntry`.`TID`, `ShiftEntry`.`TID`,
`ShiftEntry`.`SID`, `ShiftEntry`.`SID`,
`AngelTypes`.`name` AS `angel_type_name`, `angel_types`.`name` AS `angel_type_name`,
`ShiftEntry`.`Comment`, `ShiftEntry`.`Comment`,
`ShiftEntry`.`freeloaded` `ShiftEntry`.`freeloaded`
FROM `ShiftEntry` FROM `ShiftEntry`
JOIN `users` ON `ShiftEntry`.`UID`=`users`.`id` 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` = ? WHERE `ShiftEntry`.`SID` = ?
', ',
[$shift_id] [$shift_id]
@ -61,7 +62,7 @@ function ShiftEntry_create($shift_entry)
$shift = Shift($shift_entry['SID']); $shift = Shift($shift_entry['SID']);
$shifttype = ShiftType::find($shift['shifttype_id']); $shifttype = ShiftType::find($shift['shifttype_id']);
$room = Room::find($shift['RID']); $room = Room::find($shift['RID']);
$angeltype = AngelType($shift_entry['TID']); $angeltype = AngelType::find($shift_entry['TID']);
$result = Db::insert( $result = Db::insert(
' '
INSERT INTO `ShiftEntry` ( INSERT INTO `ShiftEntry` (
@ -90,7 +91,7 @@ function ShiftEntry_create($shift_entry)
. ' at ' . $room->name . ' at ' . $room->name
. ' from ' . date('Y-m-d H:i', $shift['start']) . ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end']) . ' to ' . date('Y-m-d H:i', $shift['end'])
. ' as ' . $angeltype['name'] . ' as ' . $angeltype->name
); );
mail_shift_assign($user, $shift); mail_shift_assign($user, $shift);
@ -148,7 +149,7 @@ function ShiftEntry_delete($shiftEntry)
$shift = Shift($shiftEntry['SID']); $shift = Shift($shiftEntry['SID']);
$shifttype = ShiftType::find($shift['shifttype_id']); $shifttype = ShiftType::find($shift['shifttype_id']);
$room = Room::find($shift['RID']); $room = Room::find($shift['RID']);
$angeltype = AngelType($shiftEntry['TID']); $angeltype = AngelType::find($shiftEntry['TID']);
engelsystem_log( engelsystem_log(
'Shift signout: ' . User_Nick_render($signout_user, true) 'Shift signout: ' . User_Nick_render($signout_user, true)
@ -157,7 +158,7 @@ function ShiftEntry_delete($shiftEntry)
. ' at ' . $room->name . ' at ' . $room->name
. ' from ' . date('Y-m-d H:i', $shift['start']) . ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end']) . ' 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'])); mail_shift_removed(User::find($shiftEntry['UID']), Shift($shiftEntry['SID']));

View File

@ -46,12 +46,12 @@ class ShiftsFilter
* *
* @param bool $user_shifts_admin * @param bool $user_shifts_admin
* @param int[] $rooms * @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->rooms = $rooms;
$this->types = $types; $this->types = $angelTypes;
$this->filled = [ $this->filled = [
ShiftsFilter::FILLED_FREE ShiftsFilter::FILLED_FREE

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -8,10 +9,10 @@ use Engelsystem\ShiftsFilter;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
/** /**
* @param array $angeltype * @param AngelType $angeltype
* @return array * @return array
*/ */
function Shifts_by_angeltype($angeltype) function Shifts_by_angeltype(AngelType $angeltype)
{ {
return Db::select(' return Db::select('
SELECT DISTINCT `Shifts`.* FROM `Shifts` SELECT DISTINCT `Shifts`.* FROM `Shifts`
@ -29,7 +30,7 @@ function Shifts_by_angeltype($angeltype)
WHERE `NeededAngelTypes`.`angel_type_id` = ? WHERE `NeededAngelTypes`.`angel_type_id` = ?
AND `NeededAngelTypes`.`count` > 0 AND `NeededAngelTypes`.`count` > 0
AND NOT s.shift_id IS NULL AND NOT s.shift_id IS NULL
', [$angeltype['id'], $angeltype['id']]); ', [$angeltype->id, $angeltype->id]);
} }
/** /**
@ -150,13 +151,13 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
SELECT SELECT
`NeededAngelTypes`.*, `NeededAngelTypes`.*,
`Shifts`.`SID`, `Shifts`.`SID`,
`AngelTypes`.`id`, `angel_types`.`id`,
`AngelTypes`.`name`, `angel_types`.`name`,
`AngelTypes`.`restricted`, `angel_types`.`restricted`,
`AngelTypes`.`no_self_signup` `angel_types`.`no_self_signup`
FROM `Shifts` FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` 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 LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ? AND `start` BETWEEN ? AND ?
@ -167,13 +168,13 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
SELECT SELECT
`NeededAngelTypes`.*, `NeededAngelTypes`.*,
`Shifts`.`SID`, `Shifts`.`SID`,
`AngelTypes`.`id`, `angel_types`.`id`,
`AngelTypes`.`name`, `angel_types`.`name`,
`AngelTypes`.`restricted`, `angel_types`.`restricted`,
`AngelTypes`.`no_self_signup` `angel_types`.`no_self_signup`
FROM `Shifts` FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` 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 LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ') WHERE `Shifts`.`RID` IN (' . implode(',', $shiftsFilter->getRooms()) . ')
AND `start` BETWEEN ? AND ? AND `start` BETWEEN ? AND ?
@ -193,26 +194,26 @@ function NeededAngeltypes_by_ShiftsFilter(ShiftsFilter $shiftsFilter)
/** /**
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @return array|null * @return array|null
*/ */
function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype) function NeededAngeltype_by_Shift_and_Angeltype($shift, AngelType $angeltype)
{ {
return Db::selectOne( return Db::selectOne(
' '
SELECT SELECT
`NeededAngelTypes`.*, `NeededAngelTypes`.*,
`Shifts`.`SID`, `Shifts`.`SID`,
`AngelTypes`.`id`, `angel_types`.`id`,
`AngelTypes`.`name`, `angel_types`.`name`,
`AngelTypes`.`restricted`, `angel_types`.`restricted`,
`AngelTypes`.`no_self_signup` `angel_types`.`no_self_signup`
FROM `Shifts` FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` 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 LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`SID`=? WHERE `Shifts`.`SID`=?
AND `AngelTypes`.`id`=? AND `angel_types`.`id`=?
AND s.shift_id IS NULL AND s.shift_id IS NULL
UNION UNION
@ -220,23 +221,23 @@ function NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype)
SELECT SELECT
`NeededAngelTypes`.*, `NeededAngelTypes`.*,
`Shifts`.`SID`, `Shifts`.`SID`,
`AngelTypes`.`id`, `angel_types`.`id`,
`AngelTypes`.`name`, `angel_types`.`name`,
`AngelTypes`.`restricted`, `angel_types`.`restricted`,
`AngelTypes`.`no_self_signup` `angel_types`.`no_self_signup`
FROM `Shifts` FROM `Shifts`
JOIN `NeededAngelTypes` ON `NeededAngelTypes`.`room_id`=`Shifts`.`RID` 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 LEFT JOIN schedule_shift AS s on Shifts.SID = s.shift_id
WHERE `Shifts`.`SID`=? WHERE `Shifts`.`SID`=?
AND `AngelTypes`.`id`=? AND `angel_types`.`id`=?
AND NOT s.shift_id IS NULL AND NOT s.shift_id IS NULL
', ',
[ [
$shift['SID'], $shift['SID'],
$angeltype['id'], $angeltype->id,
$shift['SID'], $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. * Returns the number of needed angels/free shift entries for an angeltype.
* *
* @param array $needed_angeltype * @param AngelType $needed_angeltype
* @param array[] $shift_entries * @param array[] $shift_entries
* @return int * @return int
*/ */
function Shift_free_entries($needed_angeltype, $shift_entries) function Shift_free_entries(AngelType $needed_angeltype, $shift_entries)
{ {
$taken = 0; $taken = 0;
foreach ($shift_entries as $shift_entry) { 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); return max(0, $neededAngels - $taken);
} }
@ -318,20 +319,20 @@ function Shift_free_entries($needed_angeltype, $shift_entries)
* *
* @param User $user * @param User $user
* @param array $shift The shift * @param array $shift The shift
* @param array $angeltype The angeltype to which the user wants to sign up * @param AngelType $angeltype The angeltype to which the user wants to sign up
* @param array|null $user_angeltype * @param array|null $user_angeltype
* @param array|null $user_shifts List of the users shifts * @param array|null $user_shifts List of the users shifts
* @param array $needed_angeltype * @param AngelType $needed_angeltype
* @param array[] $shift_entries * @param array[] $shift_entries
* @return ShiftSignupState * @return ShiftSignupState
*/ */
function Shift_signup_allowed_angel( function Shift_signup_allowed_angel(
$user, $user,
$shift, $shift,
$angeltype, AngelType $angeltype,
$user_angeltype, $user_angeltype,
$user_shifts, $user_shifts,
$needed_angeltype, AngelType $needed_angeltype,
$shift_entries $shift_entries
) { ) {
$free_entries = Shift_free_entries($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); 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) { if (time() > $shift['start'] + $shift_post_signup_total_allowed_seconds) {
// you can only join if the shift is in future // you can only join if the shift is in future
@ -378,8 +381,8 @@ function Shift_signup_allowed_angel(
if ( if (
empty($user_angeltype) empty($user_angeltype)
|| $angeltype['no_self_signup'] == 1 || $angeltype->no_self_signup == 1
|| ($angeltype['restricted'] == 1 && !isset($user_angeltype['confirm_user_id'])) || ($angeltype->restricted == 1 && !isset($user_angeltype['confirm_user_id']))
) { ) {
// you cannot join if user is not of this angel type // you cannot join if user is not of this angel type
// you cannot join if you are not confirmed // 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. * Check if an angeltype supporter can sign up a user to a shift.
* *
* @param array $needed_angeltype * @param AngelType $needed_angeltype
* @param array[] $shift_entries * @param array[] $shift_entries
* @return ShiftSignupState * @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); $free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
if ($free_entries == 0) { 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. * Check if an admin can sign up a user to a shift.
* *
* @param array $needed_angeltype * @param AngelType $needed_angeltype
* @param array[] $shift_entries * @param array[] $shift_entries
* @return ShiftSignupState * @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); $free_entries = Shift_free_entries($needed_angeltype, $shift_entries);
@ -437,11 +440,11 @@ function Shift_signup_allowed_admin($needed_angeltype, $shift_entries)
* Check if an angel can signout from a shift. * Check if an angel can signout from a shift.
* *
* @param array $shift The shift * @param array $shift The shift
* @param array $angeltype The angeltype * @param AngelType $angeltype The angeltype
* @param int $signout_user_id The user that was signed up for the shift * @param int $signout_user_id The user that was signed up for the shift
* @return bool * @return bool
*/ */
function Shift_signout_allowed($shift, $angeltype, $signout_user_id) function Shift_signout_allowed($shift, AngelType $angeltype, $signout_user_id)
{ {
$user = auth()->user(); $user = auth()->user();
@ -470,20 +473,20 @@ function Shift_signout_allowed($shift, $angeltype, $signout_user_id)
* *
* @param User $signup_user * @param User $signup_user
* @param array $shift The shift * @param array $shift The shift
* @param array $angeltype The angeltype to which the user wants to sign up * @param AngelType $angeltype The angeltype to which the user wants to sign up
* @param array|null $user_angeltype * @param array|null $user_angeltype
* @param array|null $user_shifts List of the users shifts * @param array|null $user_shifts List of the users shifts
* @param array $needed_angeltype * @param AngelType $needed_angeltype
* @param array[] $shift_entries * @param array[] $shift_entries
* @return ShiftSignupState * @return ShiftSignupState
*/ */
function Shift_signup_allowed( function Shift_signup_allowed(
$signup_user, $signup_user,
$shift, $shift,
$angeltype, AngelType $angeltype,
$user_angeltype, $user_angeltype,
$user_shifts, $user_shifts,
$needed_angeltype, AngelType $needed_angeltype,
$shift_entries $shift_entries
) { ) {
if (auth()->can('user_shifts_admin')) { if (auth()->can('user_shifts_admin')) {

View File

@ -84,14 +84,14 @@ function stats_angels_needed_three_hours(ShiftsFilter $filter = null)
( (
SELECT SUM(`count`) SELECT SUM(`count`)
FROM `NeededAngelTypes` FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
) - ( ) - (
SELECT COUNT(*) FROM `ShiftEntry` SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0 AND `freeloaded`=0
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
@ -111,14 +111,14 @@ function stats_angels_needed_three_hours(ShiftsFilter $filter = null)
( (
SELECT SUM(`count`) SELECT SUM(`count`)
FROM `NeededAngelTypes` FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID` AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID`
' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
) - ( ) - (
SELECT COUNT(*) FROM `ShiftEntry` SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0 AND `freeloaded`=0
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
@ -163,14 +163,14 @@ function stats_angels_needed_for_nightshifts(ShiftsFilter $filter = null)
( (
SELECT SUM(`count`) SELECT SUM(`count`)
FROM `NeededAngelTypes` FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID` AND `NeededAngelTypes`.`shift_id`=`Shifts`.`SID`
' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND NeededAngelTypes.angel_type_id IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
) - ( ) - (
SELECT COUNT(*) FROM `ShiftEntry` SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0 AND `freeloaded`=0
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '
@ -190,14 +190,14 @@ function stats_angels_needed_for_nightshifts(ShiftsFilter $filter = null)
( (
SELECT SUM(`count`) SELECT SUM(`count`)
FROM `NeededAngelTypes` FROM `NeededAngelTypes`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`NeededAngelTypes`.`angel_type_id` JOIN `angel_types` ON `angel_types`.`id`=`NeededAngelTypes`.`angel_type_id`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `NeededAngelTypes`.`room_id`=`Shifts`.`RID` 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` SELECT COUNT(*) FROM `ShiftEntry`
JOIN `AngelTypes` ON `AngelTypes`.`id`=`ShiftEntry`.`TID` JOIN `angel_types` ON `angel_types`.`id`=`ShiftEntry`.`TID`
WHERE `AngelTypes`.`show_on_dashboard`=TRUE WHERE `angel_types`.`show_on_dashboard`=TRUE
AND `ShiftEntry`.`SID`=`Shifts`.`SID` AND `ShiftEntry`.`SID`=`Shifts`.`SID`
AND `freeloaded`=0 AND `freeloaded`=0
' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . ' ' . ($filter ? 'AND ShiftEntry.TID IN (' . implode(',', $filter->getTypes()) . ')' : '') . '

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
@ -11,17 +12,17 @@ use Engelsystem\Models\User\User;
* Checks if a user joined an angeltype. * Checks if a user joined an angeltype.
* *
* @param int $userId The user to be checked * @param int $userId The user to be checked
* @param array $angeltype The angeltype to be checked * @param AngelType $angeltype The angeltype to be checked
* @return boolean * @return boolean
*/ */
function UserAngelType_exists($userId, $angeltype) function UserAngelType_exists($userId, AngelType $angeltype)
{ {
return count(Db::select(' return count(Db::select('
SELECT `id` SELECT `id`
FROM `UserAngelTypes` FROM `UserAngelTypes`
WHERE `UserAngelTypes`.`user_id`=? WHERE `UserAngelTypes`.`user_id`=?
AND `angeltype_id`=? AND `angeltype_id`=?
', [$userId, $angeltype['id']])) > 0; ', [$userId, $angeltype->id])) > 0;
} }
/** /**
@ -33,9 +34,9 @@ function UserAngelType_exists($userId, $angeltype)
function User_angeltypes($userId) function User_angeltypes($userId)
{ {
return Db::select(' return Db::select('
SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter` SELECT `angel_types`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`supporter`
FROM `UserAngelTypes` FROM `UserAngelTypes`
JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id` JOIN `angel_types` ON `UserAngelTypes`.`angeltype_id` = `angel_types`.`id`
WHERE `UserAngelTypes`.`user_id`=? WHERE `UserAngelTypes`.`user_id`=?
', [$userId]); ', [$userId]);
} }
@ -51,17 +52,17 @@ function User_unconfirmed_AngelTypes($userId)
return Db::select(' return Db::select('
SELECT SELECT
`UserAngelTypes`.*, `UserAngelTypes`.*,
`AngelTypes`.`name`, `angel_types`.`name`,
count(`UnconfirmedMembers`.`user_id`) AS `count` count(`UnconfirmedMembers`.`user_id`) AS `count`
FROM `UserAngelTypes` 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` JOIN `UserAngelTypes` AS `UnconfirmedMembers` ON `UserAngelTypes`.`angeltype_id`=`UnconfirmedMembers`.`angeltype_id`
WHERE `UserAngelTypes`.`user_id`=? WHERE `UserAngelTypes`.`user_id`=?
AND `UserAngelTypes`.`supporter`=TRUE AND `UserAngelTypes`.`supporter`=TRUE
AND `AngelTypes`.`restricted`=TRUE AND `angel_types`.`restricted`=TRUE
AND `UnconfirmedMembers`.`confirm_user_id` IS NULL 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 GROUP BY `UserAngelTypes`.`angeltype_id`, `UserAngelTypes`.`id`, angel_types.name, UserAngelTypes.user_id, UserAngelTypes.confirm_user_id, UserAngelTypes.supporter
ORDER BY `AngelTypes`.`name` ORDER BY `angel_types`.`name`
', [$userId]); ', [$userId]);
} }
@ -69,10 +70,10 @@ function User_unconfirmed_AngelTypes($userId)
* Returns true if user is angeltype supporter or has privilege admin_user_angeltypes. * Returns true if user is angeltype supporter or has privilege admin_user_angeltypes.
* *
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @return bool * @return bool
*/ */
function User_is_AngelType_supporter($user, $angeltype) function User_is_AngelType_supporter($user, AngelType $angeltype)
{ {
if (!$user) { if (!$user) {
return false; return false;
@ -80,7 +81,9 @@ function User_is_AngelType_supporter($user, $angeltype)
$privileges = $user->privileges->pluck('name')->toArray(); $privileges = $user->privileges->pluck('name')->toArray();
return (count(Db::select( return
count(
Db::select(
' '
SELECT `id` SELECT `id`
FROM `UserAngelTypes` FROM `UserAngelTypes`
@ -91,9 +94,10 @@ function User_is_AngelType_supporter($user, $angeltype)
', ',
[ [
$user->id, $user->id,
$angeltype['id'] $angeltype->id
] ]
)) > 0) )
) > 0
|| in_array('admin_user_angeltypes', $privileges); || in_array('admin_user_angeltypes', $privileges);
} }
@ -118,7 +122,7 @@ function UserAngelType_update($user_angeltype_id, $supporter)
* *
* @param int $angeltype_id * @param int $angeltype_id
*/ */
function UserAngelTypes_delete_all($angeltype_id) function UserAngelTypes_delete_all_unconfirmed(int $angeltype_id)
{ {
Db::delete(' Db::delete('
DELETE FROM `UserAngelTypes` DELETE FROM `UserAngelTypes`
@ -190,10 +194,10 @@ function UserAngelType_delete($user_angeltype)
* Create an UserAngelType. * Create an UserAngelType.
* *
* @param int $userId * @param int $userId
* @param array $angeltype * @param AngelType $angeltype
* @return int * @return int
*/ */
function UserAngelType_create($userId, $angeltype) function UserAngelType_create($userId, AngelType $angeltype)
{ {
Db::insert( Db::insert(
' '
@ -202,7 +206,7 @@ function UserAngelType_create($userId, $angeltype)
', ',
[ [
$userId, $userId,
$angeltype['id'] $angeltype->id
] ]
); );
@ -217,25 +221,25 @@ function UserAngelType_create($userId, $angeltype)
*/ */
function UserAngelType($user_angeltype_id) function UserAngelType($user_angeltype_id)
{ {
$angelType = Db::selectOne(' $userAngelType = Db::selectOne('
SELECT * SELECT *
FROM `UserAngelTypes` FROM `UserAngelTypes`
WHERE `id`=? WHERE `id`=?
LIMIT 1', [$user_angeltype_id]); LIMIT 1', [$user_angeltype_id]);
return empty($angelType) ? null : $angelType; return empty($userAngelType) ? null : $userAngelType;
} }
/** /**
* Get an UserAngelType by user and angeltype. * Get an UserAngelType by user and angeltype.
* *
* @param int $userId * @param int $userId
* @param array $angeltype * @param AngelType $angeltype
* @return array|null * @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 * SELECT *
FROM `UserAngelTypes` FROM `UserAngelTypes`
@ -245,11 +249,9 @@ function UserAngelType_by_User_and_AngelType($userId, $angeltype)
', ',
[ [
$userId, $userId,
$angeltype['id'] $angeltype->id
] ]
); );
return empty($angelType) ? null : $angelType;
} }
/** /**
@ -265,7 +267,7 @@ function UserAngelTypes_by_User($userId, $onlyConfirmed = false)
' '
SELECT * SELECT *
FROM `UserAngelTypes` 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`=? WHERE `user_id`=?
' '
. ( . (

View File

@ -2,6 +2,7 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\Models\Worklog; use Engelsystem\Models\Worklog;
use Engelsystem\ValidationResult; use Engelsystem\ValidationResult;
@ -81,10 +82,10 @@ function Users_by_angeltype_inverted($angeltype)
/** /**
* Returns all members of given angeltype. * Returns all members of given angeltype.
* *
* @param array $angeltype * @param AngelType $angeltype
* @return User[]|Collection * @return User[]|Collection
*/ */
function Users_by_angeltype($angeltype) function Users_by_angeltype(AngelType $angeltype)
{ {
return User::query() return User::query()
->select( ->select(
@ -96,7 +97,7 @@ function Users_by_angeltype($angeltype)
) )
->join('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id') ->join('UserAngelTypes', 'users.id', '=', 'UserAngelTypes.user_id')
->leftJoin('users_licenses', 'users.id', '=', 'users_licenses.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') ->orderBy('users.name')
->get(); ->get();
} }
@ -122,18 +123,6 @@ function User_validate_Nick($nick)
return new ValidationResult(true, $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 * Validate the planned arrival date
* *

View File

@ -1,6 +1,6 @@
<?php <?php
use Engelsystem\Database\Db; use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
@ -24,12 +24,12 @@ function admin_free()
$search = strip_request_item('search'); $search = strip_request_item('search');
} }
$angel_types_source = Db::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`'); $angel_types_source = AngelType::all(['id', 'name']);
$angel_types = [ $angel_types = [
'' => __('All') '' => __('All')
]; ];
foreach ($angel_types_source as $angel_type) { 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', ''); $angelType = $request->input('angeltype', '');
@ -58,11 +58,11 @@ function admin_free()
->where('UserAngelTypes.angeltype_id', '=', $angelType); ->where('UserAngelTypes.angeltype_id', '=', $angelType);
}); });
$query->join('AngelTypes', function ($join) { $query->join('angel_types', function ($join) {
/** @var JoinClause $join */ /** @var JoinClause $join */
$join->on('UserAngelTypes.angeltype_id', '=', 'AngelTypes.id') $join->on('UserAngelTypes.angeltype_id', '=', 'angel_types.id')
->whereNotNull('UserAngelTypes.confirm_user_id') ->whereNotNull('UserAngelTypes.confirm_user_id')
->orWhere('AngelTypes.restricted', '=', '0'); ->orWhere('angel_types.restricted', '=', '0');
}); });
} }

View File

@ -1,6 +1,7 @@
<?php <?php
use Engelsystem\Helpers\Carbon; use Engelsystem\Helpers\Carbon;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -50,12 +51,12 @@ function admin_rooms()
$dect = null; $dect = null;
$room_id = 0; $room_id = 0;
$angeltypes_source = AngelTypes(); $angeltypes_source = AngelType::all();
$angeltypes = []; $angeltypes = [];
$angeltypes_count = []; $angeltypes_count = [];
foreach ($angeltypes_source as $angeltype) { foreach ($angeltypes_source as $angeltype) {
$angeltypes[$angeltype['id']] = $angeltype['name']; $angeltypes[$angeltype->id] = $angeltype->name;
$angeltypes_count[$angeltype['id']] = 0; $angeltypes_count[$angeltype->id] = 0;
} }
if (test_request_int('id')) { if (test_request_int('id')) {
@ -132,11 +133,11 @@ function admin_rooms()
NeededAngelTypes_delete_by_room($room_id); NeededAngelTypes_delete_by_room($room_id);
$needed_angeltype_info = []; $needed_angeltype_info = [];
foreach ($angeltypes_count as $angeltype_id => $angeltype_count) { foreach ($angeltypes_count as $angeltype_id => $angeltype_count) {
$angeltype = AngelType($angeltype_id); $angeltype = AngelType::find($angeltype_id);
if (!empty($angeltype)) { if (!empty($angeltype)) {
NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count); NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count);
if ($angeltype_count > 0) { 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 = []; $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', [ $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) { foreach ($shifts as $shift) {
$shift = Shift($shift['SID']); $shift = Shift($shift['SID']);
foreach ($shift['ShiftEntry'] as $entry) { foreach ($shift['ShiftEntry'] as $entry) {
$type = AngelType($entry['TID']); $type = AngelType::find($entry['TID']);
event('shift.entry.deleting', [ event('shift.entry.deleting', [
'user' => User::find($entry['user_id']), 'user' => User::find($entry['user_id']),
'start' => Carbon::createFromTimestamp($shift['start']), 'start' => Carbon::createFromTimestamp($shift['start']),
'end' => Carbon::createFromTimestamp($shift['end']), 'end' => Carbon::createFromTimestamp($shift['end']),
'name' => $shift['name'], 'name' => $shift['name'],
'title' => $shift['title'], 'title' => $shift['title'],
'type' => $type['name'], 'type' => $type->name,
'room' => $room, 'room' => $room,
'freeloaded' => (bool)$entry['freeloaded'], 'freeloaded' => (bool)$entry['freeloaded'],
]); ]);

View File

@ -3,6 +3,7 @@
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Helpers\Carbon; use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -46,10 +47,10 @@ function admin_shifts()
} }
// Engeltypen laden // Engeltypen laden
$types = Db::select('SELECT * FROM `AngelTypes` ORDER BY `name`'); $types = AngelType::all();
$needed_angel_types = []; $needed_angel_types = [];
foreach ($types as $type) { foreach ($types as $type) {
$needed_angel_types[$type['id']] = 0; $needed_angel_types[$type->id] = 0;
} }
// Load shift types // Load shift types
@ -152,11 +153,11 @@ function admin_shifts()
$angelmode = 'location'; $angelmode = 'location';
} elseif ($request->input('angelmode') == 'manually') { } elseif ($request->input('angelmode') == 'manually') {
foreach ($types as $type) { foreach ($types as $type) {
if (preg_match('/^\d+$/', 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)); $needed_angel_types[$type->id] = trim($request->input('type_' . $type->id, 0));
} else { } else {
$valid = false; $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' => '' 'needed_angels' => ''
]; ];
foreach ($types as $type) { 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'] .= '<b>' . AngelType_name_render($type) . ':</b> ' $shifts_table_entry['needed_angels'] .= '<b>' . AngelType_name_render($type) . ':</b> '
. $needed_angel_types[$type['id']] . '<br />'; . $needed_angel_types[$type->id] . '<br />';
} }
} }
$shifts_table[] = $shifts_table_entry; $shifts_table[] = $shifts_table_entry;
@ -382,12 +383,7 @@ function admin_shifts()
$needed_angel_types_info = []; $needed_angel_types_info = [];
foreach ($session->get('admin_shifts_types', []) as $type_id => $count) { foreach ($session->get('admin_shifts_types', []) as $type_id => $count) {
$angel_type_source = Db::selectOne(' $angel_type_source = AngelType::find($type_id);
SELECT *
FROM `AngelTypes`
WHERE `id` = ?
LIMIT 1', [$type_id]);
if (!empty($angel_type_source)) { if (!empty($angel_type_source)) {
Db::insert( Db::insert(
' '
@ -402,7 +398,7 @@ function admin_shifts()
); );
if ($count > 0) { 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) { foreach ($types as $type) {
$angel_types .= '<div class="col-sm-6 col-md-8 col-lg-6 col-xl-4 col-xxl-3">' $angel_types .= '<div class="col-sm-6 col-md-8 col-lg-6 col-xl-4 col-xxl-3">'
. form_spinner( . form_spinner(
'type_' . $type['id'], 'type_' . $type->id,
$type['name'], $type->name,
$needed_angel_types[$type['id']] $needed_angel_types[$type->id]
) )
. '</div>'; . '</div>';
} }
@ -549,14 +545,14 @@ function admin_shifts_history(): string
$shift = Shift($shift['SID']); $shift = Shift($shift['SID']);
$room = Room::find($shift['RID']); $room = Room::find($shift['RID']);
foreach ($shift['ShiftEntry'] as $entry) { foreach ($shift['ShiftEntry'] as $entry) {
$type = AngelType($entry['TID']); $type = AngelType::find($entry['TID']);
event('shift.entry.deleting', [ event('shift.entry.deleting', [
'user' => User::find($entry['user_id']), 'user' => User::find($entry['user_id']),
'start' => Carbon::createFromTimestamp($shift['start']), 'start' => Carbon::createFromTimestamp($shift['start']),
'end' => Carbon::createFromTimestamp($shift['end']), 'end' => Carbon::createFromTimestamp($shift['end']),
'name' => $shift['name'], 'name' => $shift['name'],
'title' => $shift['title'], 'title' => $shift['title'],
'type' => $type['name'], 'type' => $type->name,
'room' => $room, 'room' => $room,
'freeloaded' => (bool)$entry['freeloaded'], 'freeloaded' => (bool)$entry['freeloaded'],
]); ]);

View File

@ -4,6 +4,7 @@ use Carbon\Carbon;
use Engelsystem\Database\Database; use Engelsystem\Database\Database;
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Events\Listener\OAuth2; use Engelsystem\Events\Listener\OAuth2;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Group; use Engelsystem\Models\Group;
use Engelsystem\Models\OAuth; use Engelsystem\Models\OAuth;
use Engelsystem\Models\User\Contact; use Engelsystem\Models\User\Contact;
@ -63,7 +64,7 @@ function guest_register()
$selected_angel_types = []; $selected_angel_types = [];
$planned_arrival_date = null; $planned_arrival_date = null;
$angel_types_source = AngelTypes(); $angel_types_source = AngelType::all();
$angel_types = []; $angel_types = [];
if (!empty($session->get('oauth2_groups'))) { if (!empty($session->get('oauth2_groups'))) {
/** @var OAuth2 $oauth */ /** @var OAuth2 $oauth */
@ -76,13 +77,13 @@ function guest_register()
} }
} }
foreach ($angel_types_source as $angel_type) { foreach ($angel_types_source as $angel_type) {
if ($angel_type['hide_register']) { if ($angel_type->hide_register) {
continue; continue;
} }
$angel_types[$angel_type['id']] = $angel_type['name'] $angel_types[$angel_type->id] = $angel_type->name
. ($angel_type['restricted'] ? ' (' . __('Requires introduction') . ')' : ''); . ($angel_type->restricted ? ' (' . __('Requires introduction') . ')' : '');
if (!$angel_type['restricted']) { if (!$angel_type->restricted) {
$selected_angel_types[] = $angel_type['id']; $selected_angel_types[] = $angel_type->id;
} }
} }

View File

@ -300,13 +300,13 @@ class ImportSchedule extends BaseController
$shiftEntries = $this->db $shiftEntries = $this->db
->table('ShiftEntry') ->table('ShiftEntry')
->select([ ->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' 'Shifts.start', 'Shifts.end', 'ShiftEntry.UID as user_id', 'ShiftEntry.freeloaded'
]) ])
->join('Shifts', 'Shifts.SID', 'ShiftEntry.SID') ->join('Shifts', 'Shifts.SID', 'ShiftEntry.SID')
->join('schedule_shift', 'Shifts.SID', 'schedule_shift.shift_id') ->join('schedule_shift', 'Shifts.SID', 'schedule_shift.shift_id')
->join('rooms', 'rooms.id', 'Shifts.RID') ->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') ->join('shift_types', 'shift_types.id', 'Shifts.shifttype_id')
->where('schedule_shift.guid', $event->getGuid()) ->where('schedule_shift.guid', $event->getGuid())
->get(); ->get();

View File

@ -58,9 +58,9 @@ function user_myshifts()
`shift_types`.`name`, `shift_types`.`name`,
`Shifts`.*, `Shifts`.*,
`rooms`.`name` as room_name, `rooms`.`name` as room_name,
`AngelTypes`.`name` AS `angel_type` `angel_types`.`name` AS `angel_type`
FROM `ShiftEntry` 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 `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
JOIN `shift_types` ON (`shift_types`.`id` = `Shifts`.`shifttype_id`) JOIN `shift_types` ON (`shift_types`.`id` = `Shifts`.`shifttype_id`)
JOIN `rooms` ON (`Shifts`.`RID` = `rooms`.`id`) JOIN `rooms` ON (`Shifts`.`RID` = `rooms`.`id`)

View File

@ -2,6 +2,7 @@
use Engelsystem\Database\Db; use Engelsystem\Database\Db;
use Engelsystem\Helpers\Carbon; use Engelsystem\Helpers\Carbon;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -152,29 +153,29 @@ function load_types()
{ {
$user = auth()->user(); $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.')); error(__('The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype.'));
throw_redirect(page_link_to('/')); throw_redirect(page_link_to('/'));
} }
$types = Db::select( $types = Db::select(
' '
SELECT SELECT
`AngelTypes`.`id`, `angel_types`.`id`,
`AngelTypes`.`name`, `angel_types`.`name`,
( (
`AngelTypes`.`restricted`=0 `angel_types`.`restricted`=0
OR ( OR (
NOT `UserAngelTypes`.`confirm_user_id` IS NULL NOT `UserAngelTypes`.`confirm_user_id` IS NULL
OR `UserAngelTypes`.`id` IS NULL OR `UserAngelTypes`.`id` IS NULL
) )
) AS `enabled` ) AS `enabled`
FROM `AngelTypes` FROM `angel_types`
LEFT JOIN `UserAngelTypes` LEFT JOIN `UserAngelTypes`
ON ( ON (
`UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` `UserAngelTypes`.`angeltype_id`=`angel_types`.`id`
AND `UserAngelTypes`.`user_id`=? AND `UserAngelTypes`.`user_id`=?
) )
ORDER BY `AngelTypes`.`name` ORDER BY `angel_types`.`name`
', ',
[ [
$user->id, $user->id,
@ -191,7 +192,7 @@ function load_types()
*/ */
function unrestricted_angeltypes() function unrestricted_angeltypes()
{ {
return Db::select('SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0'); return AngelType::whereRestricted(0)->get(['id', 'name'])->toArray();
} }
/** /**

View File

@ -323,7 +323,7 @@ function description($data)
* Rendert eine Datentabelle * Rendert eine Datentabelle
* *
* @param array|string $columns * @param array|string $columns
* @param array[] $rows_raw * @param array[]|ArrayAccess $rows_raw
* @param bool $data * @param bool $data
* @return string * @return string
*/ */

View File

@ -1,9 +1,12 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\License; use Engelsystem\Models\User\License;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\ShiftCalendarRenderer; use Engelsystem\ShiftCalendarRenderer;
use Engelsystem\ShiftsFilterRenderer; use Engelsystem\ShiftsFilterRenderer;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
/** /**
* AngelTypes * AngelTypes
@ -12,38 +15,38 @@ use Engelsystem\ShiftsFilterRenderer;
/** /**
* Renders the angeltypes name as link. * Renders the angeltypes name as link.
* *
* @param array $angeltype * @param AngelType $angeltype
* @param bool $plain * @param bool $plain
* @return string * @return string
*/ */
function AngelType_name_render($angeltype, $plain = false) function AngelType_name_render(AngelType $angeltype, $plain = false)
{ {
if ($plain) { if ($plain) {
return sprintf('%s (%u)', $angeltype['name'], $angeltype['id']); return sprintf('%s (%u)', $angeltype->name, $angeltype->id);
} }
return '<a href="' . angeltype_link($angeltype['id']) . '">' return '<a href="' . angeltype_link($angeltype->id) . '">'
. ($angeltype['restricted'] ? icon('book') : '') . $angeltype['name'] . ($angeltype->restricted ? icon('book') : '') . $angeltype->name
. '</a>'; . '</a>';
} }
/** /**
* Render angeltype membership state * Render angeltype membership state
* *
* @param array $user_angeltype UserAngelType and AngelType * @param AngelType $user_angeltype UserAngelType and AngelType
* @return string * @return string
*/ */
function AngelType_render_membership($user_angeltype) function AngelType_render_membership(AngelType $user_angeltype)
{ {
if (!empty($user_angeltype['user_angeltype_id'])) { if (!empty($user_angeltype->user_angeltype_id)) {
if ($user_angeltype['restricted']) { if ($user_angeltype->restricted) {
if (empty($user_angeltype['confirm_user_id'])) { if (empty($user_angeltype->confirm_user_id)) {
return icon('book') . __('Unconfirmed'); return icon('book') . __('Unconfirmed');
} elseif ($user_angeltype['supporter']) { } elseif ($user_angeltype->supporter) {
return icon_bool(true) . __('Supporter'); return icon_bool(true) . __('Supporter');
} }
return icon_bool(true) . __('Member'); return icon_bool(true) . __('Member');
} elseif ($user_angeltype['supporter']) { } elseif ($user_angeltype->supporter) {
return icon_bool(true) . __('Supporter'); return icon_bool(true) . __('Supporter');
} }
return icon_bool(true) . __('Member'); return icon_bool(true) . __('Member');
@ -52,13 +55,13 @@ function AngelType_render_membership($user_angeltype)
} }
/** /**
* @param array $angeltype * @param AngelType $angeltype
* @return string * @return string
*/ */
function AngelType_delete_view($angeltype) function AngelType_delete_view(AngelType $angeltype)
{ {
return page_with_title(sprintf(__('Delete angeltype %s'), $angeltype['name']), [ return page_with_title(sprintf(__('Delete angeltype %s'), $angeltype->name), [
info(sprintf(__('Do you want to delete angeltype %s?'), $angeltype['name']), true), info(sprintf(__('Do you want to delete angeltype %s?'), $angeltype->name), true),
form([ form([
buttons([ buttons([
button(page_link_to('angeltypes'), icon('x-lg') . __('cancel')), button(page_link_to('angeltypes'), icon('x-lg') . __('cancel')),
@ -71,59 +74,59 @@ function AngelType_delete_view($angeltype)
/** /**
* Render angeltype edit form. * Render angeltype edit form.
* *
* @param array $angeltype The angeltype to edit * @param AngelType $angeltype The angeltype to edit
* @param boolean $supporter_mode Is the user a supporter of this angeltype? * @param boolean $supporter_mode Is the user a supporter of this angeltype?
* @return string * @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([ buttons([
button(page_link_to('angeltypes'), __('Angeltypes'), 'back') button(page_link_to('angeltypes'), __('Angeltypes'), 'back')
]), ]),
msg(), msg(),
form([ form([
$supporter_mode $supporter_mode
? form_info(__('Name'), $angeltype['name']) ? form_info(__('Name'), $angeltype->name)
: form_text('name', __('Name'), $angeltype['name']), : form_text('name', __('Name'), $angeltype->name),
$supporter_mode $supporter_mode
? form_info(__('Requires introduction'), $angeltype['restricted'] ? __('Yes') : __('No')) ? form_info(__('Requires introduction'), $angeltype->restricted ? __('Yes') : __('No'))
: form_checkbox('restricted', __('Requires introduction'), $angeltype['restricted']), : form_checkbox('restricted', __('Requires introduction'), $angeltype->restricted),
form_info( form_info(
'', '',
__('Angel types which require introduction can only be used by an angel if enabled by a supporter (double opt-in).') __('Angel types which require introduction can only be used by an angel if enabled by a supporter (double opt-in).')
), ),
$supporter_mode $supporter_mode
? form_info(__('No Self Sign Up allowed'), $angeltype['no_self_signup'] ? __('Yes') : __('No')) ? 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']), : form_checkbox('no_self_signup', __('No Self Sign Up allowed'), $angeltype->no_self_signup),
$supporter_mode $supporter_mode ?
? form_info( form_info(
__('Requires driver license'), __('Requires driver license'),
$angeltype['requires_driver_license'] $angeltype->requires_driver_license
? __('Yes') ? __('Yes')
: __('No') : __('No')
) ) :
: form_checkbox( form_checkbox(
'requires_driver_license', 'requires_driver_license',
__('Requires driver license'), __('Requires driver license'),
$angeltype['requires_driver_license'] $angeltype->requires_driver_license
), ),
$supporter_mode $supporter_mode
? form_info(__('Show on dashboard'), $angeltype['show_on_dashboard'] ? __('Yes') : __('No')) ? form_info(__('Show on dashboard'), $angeltype->show_on_dashboard ? __('Yes') : __('No'))
: form_checkbox('show_on_dashboard', __('Show on dashboard'), $angeltype['show_on_dashboard']), : form_checkbox('show_on_dashboard', __('Show on dashboard'), $angeltype->show_on_dashboard),
$supporter_mode $supporter_mode
? form_info(__('Hide at Registration'), $angeltype['hide_register'] ? __('Yes') : __('No')) ? form_info(__('Hide at Registration'), $angeltype->hide_register ? __('Yes') : __('No'))
: form_checkbox('hide_register', __('Hide at Registration'), $angeltype['hide_register']), : form_checkbox('hide_register', __('Hide at Registration'), $angeltype->hide_register),
form_textarea('description', __('Description'), $angeltype['description']), form_textarea('description', __('Description'), $angeltype->description),
form_info('', __('Please use markdown for the description.')), form_info('', __('Please use markdown for the description.')),
heading(__('Contact'), 3), heading(__('Contact'), 3),
form_info( form_info(
'', '',
__('Primary contact person/desk for user questions.') __('Primary contact person/desk for user questions.')
), ),
form_text('contact_name', __('Name'), $angeltype['contact_name']), form_text('contact_name', __('Name'), $angeltype->contact_name),
form_text('contact_dect', __('DECT'), $angeltype['contact_dect']), form_text('contact_dect', __('DECT'), $angeltype->contact_dect),
form_text('contact_email', __('E-Mail'), $angeltype['contact_email']), form_text('contact_email', __('E-Mail'), $angeltype->contact_email),
form_submit('submit', __('Save')) form_submit('submit', __('Save'))
]) ])
]); ]);
@ -132,7 +135,7 @@ function AngelType_edit_view($angeltype, $supporter_mode)
/** /**
* Renders the buttons for the angeltype view. * Renders the buttons for the angeltype view.
* *
* @param array $angeltype * @param AngelType $angeltype
* @param array|null $user_angeltype * @param array|null $user_angeltype
* @param bool $admin_angeltypes * @param bool $admin_angeltypes
* @param bool $supporter * @param bool $supporter
@ -140,13 +143,19 @@ function AngelType_edit_view($angeltype, $supporter_mode)
* @param User|null $user * @param User|null $user
* @return string * @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 = [ $buttons = [
button(page_link_to('angeltypes'), __('Angeltypes'), 'back') button(page_link_to('angeltypes'), __('Angeltypes'), 'back')
]; ];
if ($angeltype['requires_driver_license']) { if ($angeltype->requires_driver_license) {
$buttons[] = button( $buttons[] = button(
user_driver_license_edit_link($user), user_driver_license_edit_link($user),
icon('wallet2') . __('my driving license') icon('wallet2') . __('my driving license')
@ -155,19 +164,19 @@ function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes,
if (is_null($user_angeltype)) { if (is_null($user_angeltype)) {
$buttons[] = button( $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'), __('join'),
'add' 'add'
); );
} else { } 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!')); 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( error(sprintf(
__('You are unconfirmed for this angeltype. Please go to the introduction for %s to get confirmed.'), __('You are unconfirmed for this angeltype. Please go to the introduction for %s to get confirmed.'),
$angeltype['name'] $angeltype->name
)); ));
} }
$buttons[] = button( $buttons[] = button(
@ -178,14 +187,14 @@ function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes,
if ($admin_angeltypes || $supporter) { if ($admin_angeltypes || $supporter) {
$buttons[] = button( $buttons[] = button(
page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'edit', 'angeltype_id' => $angeltype->id]),
__('edit'), __('edit'),
'edit' 'edit'
); );
} }
if ($admin_angeltypes) { if ($admin_angeltypes) {
$buttons[] = button( $buttons[] = button(
page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype->id]),
__('delete'), __('delete'),
'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. * Renders and sorts the members of an angeltype into supporters, members and unconfirmed members.
* *
* @param array $angeltype * @param AngelType $angeltype
* @param User[] $members * @param User[] $members
* @param bool $admin_user_angeltypes * @param bool $admin_user_angeltypes
* @param bool $admin_angeltypes * @param bool $admin_angeltypes
* @return array [supporters, members, unconfirmed members] * @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 = []; $supporters = [];
$members_confirmed = []; $members_confirmed = [];
@ -211,7 +220,7 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a
foreach ($members as $member) { foreach ($members as $member) {
$member->name = User_Nick_render($member) . User_Pronoun_render($member); $member->name = User_Nick_render($member) . User_Pronoun_render($member);
$member['dect'] = $member->contact->dect; $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['wants_to_drive'] = icon_bool($member->license->wantsToDrive());
$member['has_car'] = icon_bool($member->license->has_car); $member['has_car'] = icon_bool($member->license->has_car);
$member['has_license_car'] = icon_bool($member->license->drive_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); $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([ $member['actions'] = table_buttons([
button( button(
page_link_to( page_link_to(
@ -261,8 +270,8 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a
} else { } else {
if ($admin_user_angeltypes) { if ($admin_user_angeltypes) {
$member['actions'] = table_buttons([ $member['actions'] = table_buttons([
$admin_angeltypes $admin_angeltypes ?
? button( button(
page_link_to('user_angeltypes', [ page_link_to('user_angeltypes', [
'action' => 'update', 'action' => 'update',
'user_angeltype_id' => $member['user_angeltype_id'], 'user_angeltype_id' => $member['user_angeltype_id'],
@ -270,8 +279,8 @@ function AngelType_view_members($angeltype, $members, $admin_user_angeltypes, $a
]), ]),
__('Add supporter rights'), __('Add supporter rights'),
'btn-sm' 'btn-sm'
) ) :
: '', '',
button( button(
page_link_to('user_angeltypes', [ page_link_to('user_angeltypes', [
'action' => 'delete', '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. * Creates the needed member table headers according to given rights and settings from the angeltype.
* *
* @param array $angeltype * @param AngelType $angeltype
* @param bool $supporter * @param bool $supporter
* @param bool $admin_angeltypes * @param bool $admin_angeltypes
* @return array * @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 [ return [
'name' => __('Nick'), 'name' => __('Nick'),
'dect' => __('DECT'), 'dect' => __('DECT'),
@ -327,7 +336,7 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes)
/** /**
* Render an angeltype page containing the member lists. * Render an angeltype page containing the member lists.
* *
* @param array $angeltype * @param AngelType $angeltype
* @param User[] $members * @param User[] $members
* @param array $user_angeltype * @param array $user_angeltype
* @param bool $admin_user_angeltypes * @param bool $admin_user_angeltypes
@ -341,7 +350,7 @@ function AngelType_view_table_headers($angeltype, $supporter, $admin_angeltypes)
* @return string * @return string
*/ */
function AngelType_view( function AngelType_view(
$angeltype, AngelType $angeltype,
$members, $members,
$user_angeltype, $user_angeltype,
$admin_user_angeltypes, $admin_user_angeltypes,
@ -353,7 +362,7 @@ function AngelType_view(
ShiftCalendarRenderer $shiftCalendarRenderer, ShiftCalendarRenderer $shiftCalendarRenderer,
$tab $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), AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, $supporter, $user_driver_license, $user),
msg(), msg(),
tabs([ tabs([
@ -374,24 +383,24 @@ function AngelType_view(
} }
/** /**
* @param array $angeltype * @param AngelType $angeltype
* @param ShiftsFilterRenderer $shiftsFilterRenderer * @param ShiftsFilterRenderer $shiftsFilterRenderer
* @param ShiftCalendarRenderer $shiftCalendarRenderer * @param ShiftCalendarRenderer $shiftCalendarRenderer
* @return string HTML * @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', [ $shifts = $shiftsFilterRenderer->render(page_link_to('angeltypes', [
'action' => 'view', 'action' => 'view',
'angeltype_id' => $angeltype['id'] 'angeltype_id' => $angeltype->id
]), ['type' => $angeltype['id']]); ]), ['type' => $angeltype->id]);
$shifts .= $shiftCalendarRenderer->render(); $shifts .= $shiftCalendarRenderer->render();
return div('first', $shifts); return div('first', $shifts);
} }
/** /**
* @param array $angeltype * @param AngelType $angeltype
* @param User[] $members * @param User[] $members
* @param bool $admin_user_angeltypes * @param bool $admin_user_angeltypes
* @param bool $admin_angeltypes * @param bool $admin_angeltypes
@ -399,21 +408,21 @@ function AngelType_view_shifts($angeltype, $shiftsFilterRenderer, $shiftCalendar
* @return string HTML * @return string HTML
*/ */
function AngelType_view_info( function AngelType_view_info(
$angeltype, AngelType $angeltype,
$members, $members,
$admin_user_angeltypes, $admin_user_angeltypes,
$admin_angeltypes, $admin_angeltypes,
$supporter $supporter
) { ) {
$info = []; $info = [];
if (AngelType_has_contact_info($angeltype)) { if ($angeltype->hasContactInfo()) {
$info[] = AngelTypes_render_contact_info($angeltype); $info[] = AngelTypes_render_contact_info($angeltype);
} }
$info[] = '<h3>' . __('Description') . '</h3>'; $info[] = '<h3>' . __('Description') . '</h3>';
$parsedown = new Parsedown(); $parsedown = new Parsedown();
if ($angeltype['description'] != '') { if ($angeltype->description != '') {
$info[] = $parsedown->parse((string)$angeltype['description']); $info[] = $parsedown->parse($angeltype->description);
} }
list($supporters, $members_confirmed, $members_unconfirmed) = AngelType_view_members( list($supporters, $members_confirmed, $members_unconfirmed) = AngelType_view_members(
@ -451,7 +460,7 @@ function AngelType_view_info(
button( button(
page_link_to( page_link_to(
'user_angeltypes', 'user_angeltypes',
['action' => 'add', 'angeltype_id' => $angeltype['id']] ['action' => 'add', 'angeltype_id' => $angeltype->id]
), ),
__('Add'), __('Add'),
'add' 'add'
@ -460,15 +469,15 @@ function AngelType_view_info(
} }
$info[] = table($table_headers, $members_confirmed); $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[] = '<h3>' . __('Unconfirmed') . '</h3>'; $info[] = '<h3>' . __('Unconfirmed') . '</h3>';
$info[] = buttons([ $info[] = buttons([
button( 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') icon('check-lg') . __('confirm all')
), ),
button( 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') icon('trash') . __('deny all')
) )
]); ]);
@ -481,15 +490,15 @@ function AngelType_view_info(
/** /**
* Renders the contact info * Renders the contact info
* *
* @param array $angeltype * @param AngelType $angeltype
* @return string HTML * @return string HTML
*/ */
function AngelTypes_render_contact_info($angeltype) function AngelTypes_render_contact_info(AngelType $angeltype)
{ {
$info = [ $info = [
__('Name') => [$angeltype['contact_name'], $angeltype['contact_name']], __('Name') => [$angeltype->contact_name, $angeltype->contact_name],
__('DECT') => [sprintf('<a href="tel:%s">%1$s</a>', $angeltype['contact_dect']), $angeltype['contact_dect']], __('DECT') => [sprintf('<a href="tel:%s">%1$s</a>', $angeltype->contact_dect), $angeltype->contact_dect],
__('E-Mail') => [sprintf('<a href="mailto:%s">%1$s</a>', $angeltype['contact_email']), $angeltype['contact_email']], __('E-Mail') => [sprintf('<a href="mailto:%s">%1$s</a>', $angeltype->contact_email), $angeltype->contact_email],
]; ];
$contactInfo = []; $contactInfo = [];
foreach ($info as $name => $data) { foreach ($info as $name => $data) {
@ -504,11 +513,11 @@ function AngelTypes_render_contact_info($angeltype)
/** /**
* Display the list of angeltypes. * Display the list of angeltypes.
* *
* @param array $angeltypes * @param AngelType[]|Collection $angeltypes
* @param bool $admin_angeltypes * @param bool $admin_angeltypes
* @return string * @return string
*/ */
function AngelTypes_list_view($angeltypes, $admin_angeltypes) function AngelTypes_list_view($angeltypes, bool $admin_angeltypes)
{ {
return page_with_title(angeltypes_title(), [ return page_with_title(angeltypes_title(), [
msg(), msg(),
@ -520,8 +529,8 @@ function AngelTypes_list_view($angeltypes, $admin_angeltypes)
]), ]),
table([ table([
'name' => __('Name'), 'name' => __('Name'),
'restricted' => icon('book') . __('Requires introduction'), 'is_restricted' => icon('book') . __('Requires introduction'),
'no_self_signup' => icon('pencil-square') . __('Self Sign Up Allowed'), 'no_self_signup_allowed' => icon('pencil-square') . __('Self Sign Up Allowed'),
'membership' => __('Membership'), 'membership' => __('Membership'),
'actions' => '' 'actions' => ''
], $angeltypes) ], $angeltypes)
@ -529,34 +538,34 @@ function AngelTypes_list_view($angeltypes, $admin_angeltypes)
} }
/** /**
* Renders the about info for an angeltype. * Renders the about-info for an angeltype.
* *
* @param array $angeltype * @param AngelType $angeltype
* @return string * @return string
*/ */
function AngelTypes_about_view_angeltype($angeltype) function AngelTypes_about_view_angeltype(AngelType $angeltype)
{ {
$parsedown = new Parsedown(); $parsedown = new Parsedown();
$html = '<h2>' . $angeltype['name'] . '</h2>'; $html = '<h2>' . $angeltype->name . '</h2>';
if (AngelType_has_contact_info($angeltype)) { if ($angeltype->hasContactInfo()) {
$html .= AngelTypes_render_contact_info($angeltype); $html .= AngelTypes_render_contact_info($angeltype);
} }
if (isset($angeltype['user_angeltype_id'])) { if (isset($angeltype->user_angeltype_id)) {
$buttons = []; $buttons = [];
if (!empty($angeltype['user_angeltype_id'])) { if (!empty($angeltype->user_angeltype_id)) {
$buttons[] = button( $buttons[] = button(
page_link_to( page_link_to(
'user_angeltypes', 'user_angeltypes',
['action' => 'delete', 'user_angeltype_id' => $angeltype['user_angeltype_id']] ['action' => 'delete', 'user_angeltype_id' => $angeltype->user_angeltype_id]
), ),
__('leave') __('leave')
); );
} else { } else {
$buttons[] = button( $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'), __('join'),
'add' 'add'
); );
@ -564,14 +573,14 @@ function AngelTypes_about_view_angeltype($angeltype)
$html .= buttons($buttons); $html .= buttons($buttons);
} }
if ($angeltype['restricted']) { if ($angeltype->restricted) {
$html .= info( $html .= info(
__('This angeltype requires the attendance at an introduction meeting. You might find additional information in the description.'), __('This angeltype requires the attendance at an introduction meeting. You might find additional information in the description.'),
true true
); );
} }
if ($angeltype['description'] != '') { if ($angeltype->description != '') {
$html .= $parsedown->parse((string)$angeltype['description']); $html .= $parsedown->parse($angeltype->description);
} }
$html .= '<hr />'; $html .= '<hr />';
@ -581,7 +590,7 @@ 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. * Renders a site that contains every angeltype and its description, basically as an overview of the needed help types.
* *
* @param array[] $angeltypes * @param Collection|AngelType[] $angeltypes
* @param bool $user_logged_in * @param bool $user_logged_in
* @return string * @return string
*/ */
@ -601,7 +610,11 @@ function AngelTypes_about_view($angeltypes, $user_logged_in)
$footerConfig = config('footer_items'); $footerConfig = config('footer_items');
if (!empty($footerConfig['FAQ'])) { 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 = [ $content = [

View File

@ -2,6 +2,7 @@
namespace Engelsystem; namespace Engelsystem;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -16,7 +17,7 @@ class ShiftCalendarShiftRenderer
* Renders a shift * Renders a shift
* *
* @param array $shift The shift to render * @param array $shift The shift to render
* @param array $needed_angeltypes * @param array[] $needed_angeltypes
* @param array $shift_entries * @param array $shift_entries
* @param User $user The user who is viewing the shift calendar * @param User $user The user who is viewing the shift calendar
* @return array * @return array
@ -161,13 +162,14 @@ class ShiftCalendarShiftRenderer
* *
* @param array $shift The shift which is rendered * @param array $shift The shift which is rendered
* @param array[] $shift_entries * @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 * 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 * @return array
*/ */
private function renderShiftNeededAngeltype($shift, $shift_entries, $angeltype, $user) private function renderShiftNeededAngeltype($shift, $shift_entries, $angeltype, $user)
{ {
$angeltype = (new AngelType())->forceFill($angeltype);
$entry_list = []; $entry_list = [];
foreach ($shift_entries as $entry) { foreach ($shift_entries as $entry) {
$class = $entry['freeloaded'] ? 'text-decoration-line-through' : ''; $class = $entry['freeloaded'] ? 'text-decoration-line-through' : '';
@ -216,7 +218,7 @@ class ShiftCalendarShiftRenderer
break; break;
case ShiftSignupState::ANGELTYPE: case ShiftSignupState::ANGELTYPE:
if ($angeltype['restricted'] == 1) { if ($angeltype->restricted) {
// User has to be confirmed on the angeltype first // User has to be confirmed on the angeltype first
$entry_list[] = $inner_text . icon('book'); $entry_list[] = $inner_text . icon('book');
} else { } else {
@ -225,9 +227,9 @@ class ShiftCalendarShiftRenderer
. button( . button(
page_link_to( page_link_to(
'user_angeltypes', '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' 'btn-sm'
); );
} }

View File

@ -1,18 +1,19 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
* Sign off from an user from a shift with admin permissions, asking for ack. * Sign off from a user from a shift with admin permissions, asking for ack.
* *
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @param User $signoff_user * @param User $signoff_user
* *
* @return string HTML * @return string HTML
*/ */
function ShiftEntry_delete_view_admin($shift, $angeltype, $signoff_user) function ShiftEntry_delete_view_admin($shift, AngelType $angeltype, $signoff_user)
{ {
return page_with_title(ShiftEntry_delete_title(), [ return page_with_title(ShiftEntry_delete_title(), [
info(sprintf( info(sprintf(
@ -21,7 +22,7 @@ function ShiftEntry_delete_view_admin($shift, $angeltype, $signoff_user)
$shift['name'], $shift['name'],
date('Y-m-d H:i', $shift['start']), date('Y-m-d H:i', $shift['start']),
date('Y-m-d H:i', $shift['end']), date('Y-m-d H:i', $shift['end']),
$angeltype['name'] $angeltype->name
), true), ), true),
form([ form([
buttons([ buttons([
@ -36,12 +37,12 @@ function ShiftEntry_delete_view_admin($shift, $angeltype, $signoff_user)
* Sign off from a shift, asking for ack. * Sign off from a shift, asking for ack.
* *
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @param int $signoff_user_id * @param int $signoff_user_id
* *
* @return string HTML * @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(), [ return page_with_title(ShiftEntry_delete_title(), [
info(sprintf( info(sprintf(
@ -49,7 +50,7 @@ function ShiftEntry_delete_view($shift, $angeltype, $signoff_user_id)
$shift['name'], $shift['name'],
date('Y-m-d H:i', $shift['start']), date('Y-m-d H:i', $shift['start']),
date('Y-m-d H:i', $shift['end']), date('Y-m-d H:i', $shift['end']),
$angeltype['name'] $angeltype->name
), true), ), true),
form([ form([
@ -74,14 +75,20 @@ function ShiftEntry_delete_title()
* *
* @param array $shift * @param array $shift
* @param Room $room * @param Room $room
* @param array $angeltype * @param AngelType $angeltype
* @param array $angeltypes_select * @param array $angeltypes_select
* @param User $signup_user * @param User $signup_user
* @param array $users_select * @param array $users_select
* @return string * @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( return page_with_title(
ShiftEntry_create_title() . ': ' . $shift['name'] ShiftEntry_create_title() . ': ' . $shift['name']
. ' <small data-countdown-ts="' . $shift['start'] . '">%c</small>', . ' <small data-countdown-ts="' . $shift['start'] . '">%c</small>',
@ -89,7 +96,7 @@ function ShiftEntry_create_view_admin($shift, Room $room, $angeltype, $angeltype
Shift_view_header($shift, $room), Shift_view_header($shift, $room),
info(__('Do you want to sign up the following user for this shift?'), true), info(__('Do you want to sign up the following user for this shift?'), true),
form([ 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_select('user_id', __('User'), $users_select, $signup_user->id),
form_submit('submit', icon('check-lg') . __('Save')) 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 array $shift
* @param Room $room * @param Room $room
* @param array $angeltype * @param AngelType $angeltype
* @param User $signup_user * @param User $signup_user
* @param array $users_select * @param array $users_select
* @return string * @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( return page_with_title(
ShiftEntry_create_title() . ': ' . $shift['name'] ShiftEntry_create_title() . ': ' . $shift['name']
@ -131,11 +138,11 @@ function ShiftEntry_create_view_supporter($shift, Room $room, $angeltype, $signu
* *
* @param array $shift * @param array $shift
* @param Room $room * @param Room $room
* @param array $angeltype * @param AngelType $angeltype
* @param string $comment * @param string $comment
* @return string * @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( return page_with_title(
ShiftEntry_create_title() . ': ' . $shift['name'] ShiftEntry_create_title() . ': ' . $shift['name']

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -73,30 +74,30 @@ function Shift_editor_info_render($shift)
/** /**
* @param array $shift * @param array $shift
* @param array $angeltype * @param AngelType $angeltype
* @param array $user_angeltype * @param array $user_angeltype
* @return string * @return string
*/ */
function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null) function Shift_signup_button_render($shift, AngelType $angeltype, $user_angeltype = null)
{ {
if (empty($user_angeltype)) { if (empty($user_angeltype)) {
$user_angeltype = UserAngelType_by_User_and_AngelType(auth()->user()->id, $angeltype); $user_angeltype = UserAngelType_by_User_and_AngelType(auth()->user()->id, $angeltype);
} }
if ( 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) || User_is_AngelType_supporter(auth()->user(), $angeltype)
) )
) { ) {
return button(shift_entry_create_link($shift, $angeltype), __('Sign up')); return button(shift_entry_create_link($shift, $angeltype), __('Sign up'));
} elseif (empty($user_angeltype)) { } elseif (empty($user_angeltype)) {
return button( return button(
page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]),
sprintf( sprintf(
__('Become %s'), __('Become %s'),
$angeltype['name'] $angeltype->name
) )
); );
} }
@ -107,7 +108,7 @@ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null)
* @param array $shift * @param array $shift
* @param ShiftType $shifttype * @param ShiftType $shifttype
* @param Room $room * @param Room $room
* @param array[] $angeltypes_source * @param AngelType[]|Collection $angeltypes_source
* @param ShiftSignupState $shift_signup_state * @param ShiftSignupState $shift_signup_state
* @return string * @return string
*/ */
@ -122,7 +123,7 @@ function Shift_view($shift, ShiftType $shifttype, Room $room, $angeltypes_source
$angeltypes = []; $angeltypes = [];
foreach ($angeltypes_source as $angeltype) { foreach ($angeltypes_source as $angeltype) {
$angeltypes[$angeltype['id']] = $angeltype; $angeltypes[$angeltype->id] = $angeltype;
} }
$needed_angels = ''; $needed_angels = '';
@ -199,7 +200,7 @@ function Shift_view($shift, ShiftType $shifttype, Room $room, $angeltypes_source
/** /**
* @param array $needed_angeltype * @param array $needed_angeltype
* @param array $angeltypes * @param AngelType[]|Collection $angeltypes
* @param array[] $shift * @param array[] $shift
* @param bool $user_shift_admin * @param bool $user_shift_admin
* @return string * @return string
@ -269,7 +270,7 @@ function Shift_view_render_shift_entry($shift_entry, $user_shift_admin, $angelty
'btn-sm' 'btn-sm'
); );
} }
$angeltype = AngelType($shift_entry['TID']); $angeltype = AngelType::find($shift_entry['TID']);
$disabled = Shift_signout_allowed($shift, $angeltype, $shift_entry['UID']) ? '' : ' btn-disabled'; $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 .= button_icon(shift_entry_delete_link($shift_entry), 'trash', 'btn-sm' . $disabled);
$entry .= '</div>'; $entry .= '</div>';
@ -291,6 +292,7 @@ function shift_length($shift)
2, 2,
'0', '0',
STR_PAD_LEFT STR_PAD_LEFT
) . 'h'; );
$length .= 'h';
return $length; return $length;
} }

View File

@ -1,15 +1,16 @@
<?php <?php
use Engelsystem\Models\AngelType;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
/** /**
* @param array $user_angeltype * @param array $user_angeltype
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @param bool $supporter * @param bool $supporter
* @return string * @return string
*/ */
function UserAngelType_update_view($user_angeltype, $user, $angeltype, $supporter) function UserAngelType_update_view($user_angeltype, User $user, AngelType $angeltype, bool $supporter)
{ {
return page_with_title($supporter ? __('Add supporter rights') : __('Remove supporter rights'), [ return page_with_title($supporter ? __('Add supporter rights') : __('Remove supporter rights'), [
msg(), msg(),
@ -17,13 +18,13 @@ function UserAngelType_update_view($user_angeltype, $user, $angeltype, $supporte
$supporter $supporter
? __('Do you really want to add supporter rights for %s to %s?') ? __('Do you really want to add supporter rights for %s to %s?')
: __('Do you really want to remove supporter rights for %s from %s?'), : __('Do you really want to remove supporter rights for %s from %s?'),
$angeltype['name'], $angeltype->name,
User_Nick_render($user) User_Nick_render($user)
), true), ), true),
form([ form([
buttons([ buttons([
button( 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') icon('x-lg') . __('cancel')
), ),
form_submit('submit', icon('check-lg') . __('yes'), 'btn-primary', false), 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 * @return string
*/ */
function UserAngelTypes_delete_all_view($angeltype) function UserAngelTypes_delete_all_view(AngelType $angeltype)
{ {
return page_with_title(__('Deny all users'), [ return page_with_title(__('Deny all users'), [
msg(), 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([ form([
buttons([ buttons([
button( button(
page_link_to( page_link_to(
'angeltypes', 'angeltypes',
['action' => 'view', 'angeltype_id' => $angeltype['id']] ['action' => 'view', 'angeltype_id' => $angeltype->id]
), ),
icon('x-lg') . __('cancel') icon('x-lg') . __('cancel')
), ),
form_submit('deny_all', icon('check-lg') . __('yes'), 'btn-primary', false) 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 * @return string
*/ */
function UserAngelTypes_confirm_all_view($angeltype) function UserAngelTypes_confirm_all_view(AngelType $angeltype)
{ {
return page_with_title(__('Confirm all users'), [ return page_with_title(__('Confirm all users'), [
msg(), 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([ form([
buttons([ 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), 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 array $user_angeltype
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @return string * @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'), [ return page_with_title(__('Confirm angeltype for user'), [
msg(), msg(),
info(sprintf( info(sprintf(
__('Do you really want to confirm %s for %s?'), __('Do you really want to confirm %s for %s?'),
User_Nick_render($user), User_Nick_render($user),
$angeltype['name'] $angeltype->name
), true), ), true),
form([ form([
buttons([ 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), form_submit('confirm_user', icon('check-lg') . __('yes'), 'btn-primary', false),
]), ]),
], page_link_to('user_angeltypes', ['action' => 'confirm', 'user_angeltype_id' => $user_angeltype['id']])), ], page_link_to('user_angeltypes', ['action' => 'confirm', 'user_angeltype_id' => $user_angeltype['id']])),
@ -105,21 +106,21 @@ function UserAngelType_confirm_view($user_angeltype, $user, $angeltype)
/** /**
* @param array $user_angeltype * @param array $user_angeltype
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @return string * @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'), [ return page_with_title(__('Remove angeltype'), [
msg(), msg(),
info(sprintf( info(sprintf(
__('Do you really want to delete %s from %s?'), __('Do you really want to delete %s from %s?'),
User_Nick_render($user), User_Nick_render($user),
$angeltype['name'] $angeltype->name
), true), ), true),
form([ form([
buttons([ 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), form_submit('delete', icon('check-lg') . __('yes'), 'btn-primary', false),
]), ]),
], page_link_to('user_angeltypes', ['action' => 'delete', 'user_angeltype_id' => $user_angeltype['id']])), ], 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 AngelType $angeltype
* @param User[] $users_source * @param User[] $users_source
* @param int $user_id * @param int $user_id
* @return string * @return string
*/ */
function UserAngelType_add_view($angeltype, $users_source, $user_id) function UserAngelType_add_view(AngelType $angeltype, $users_source, $user_id)
{ {
$users = []; $users = [];
foreach ($users_source as $user_source) { foreach ($users_source as $user_source) {
@ -143,13 +144,13 @@ function UserAngelType_add_view($angeltype, $users_source, $user_id)
msg(), msg(),
buttons([ buttons([
button( button(
page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype->id]),
__('back'), __('back'),
'back' 'back'
) )
]), ]),
form([ form([
form_info(__('Angeltype'), $angeltype['name']), form_info(__('Angeltype'), $angeltype->name),
form_checkbox('auto_confirm_user', __('Confirm user'), true), form_checkbox('auto_confirm_user', __('Confirm user'), true),
form_select('user_id', __('User'), $users, $user_id), form_select('user_id', __('User'), $users, $user_id),
form_submit('submit', __('Add')) form_submit('submit', __('Add'))
@ -159,27 +160,27 @@ function UserAngelType_add_view($angeltype, $users_source, $user_id)
/** /**
* @param User $user * @param User $user
* @param array $angeltype * @param AngelType $angeltype
* @return string * @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(), msg(),
info(sprintf( info(sprintf(
__('Do you really want to add %s to %s?'), __('Do you really want to add %s to %s?'),
User_Nick_render($user), User_Nick_render($user),
$angeltype['name'] $angeltype->name
), true), ), true),
form([ form([
auth()->can('admin_user_angeltypes') ? form_checkbox('auto_confirm_user', __('Confirm user'), true) : '', auth()->can('admin_user_angeltypes') ? form_checkbox('auto_confirm_user', __('Confirm user'), true) : '',
buttons([ 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) form_submit('submit', icon('check-lg') . __('save'), 'btn-primary', false)
]), ]),
], page_link_to( ], page_link_to(
'user_angeltypes', 'user_angeltypes',
['action' => 'add', 'angeltype_id' => $angeltype['id'], 'user_id' => $user->id] ['action' => 'add', 'angeltype_id' => $angeltype->id, 'user_id' => $user->id]
)), )),
]); ]);
} }

View File

@ -1,6 +1,7 @@
<?php <?php
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Group; use Engelsystem\Models\Group;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
@ -308,7 +309,7 @@ function User_view_myshift($shift, $user_source, $its_me)
'btn-sm' 'btn-sm'
); );
} }
if (Shift_signout_allowed($shift, ['id' => $shift['TID']], $user_source->id)) { if (Shift_signout_allowed($shift, (new AngelType())->forceFill(['id' => $shift['TID']]), $user_source->id)) {
$myshift['actions'][] = button( $myshift['actions'][] = button(
shift_entry_delete_link($shift), shift_entry_delete_link($shift),
icon('trash') . __('sign off'), icon('trash') . __('sign off'),

77
src/Models/AngelType.php Normal file
View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* @property int $id
* @property string $name
* @property string $description
* @property string $contact_name
* @property string $contact_dect
* @property string $contact_email
* @property boolean $restricted # If users need an introduction
* @property boolean $requires_driver_license # If users must have a driver license
* @property boolean $no_self_signup # Users can sign up for shifts
* @property boolean $show_on_dashboard # Show on public dashboard
* @property boolean $hide_register # Hide from registration page
*
* @method static QueryBuilder|AngelType[] whereId($value)
* @method static QueryBuilder|AngelType[] whereName($value)
* @method static QueryBuilder|AngelType[] whereDescription($value)
* @method static QueryBuilder|AngelType[] whereContactName($value)
* @method static QueryBuilder|AngelType[] whereContactDect($value)
* @method static QueryBuilder|AngelType[] whereContactEmail($value)
* @method static QueryBuilder|AngelType[] whereRestricted($value)
* @method static QueryBuilder|AngelType[] whereRequiresDriverLicense($value)
* @method static QueryBuilder|AngelType[] whereNoSelfSignup($value)
* @method static QueryBuilder|AngelType[] whereShowOnDashboard($value)
* @method static QueryBuilder|AngelType[] whereHideRegister($value)
*/
class AngelType extends BaseModel
{
use HasFactory;
/** @var string[] */
protected $fillable = [
'name',
'description',
'contact_name',
'contact_dect',
'contact_email',
'restricted',
'requires_driver_license',
'no_self_signup',
'show_on_dashboard',
'hide_register',
];
/** @var array<string, string> */
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);
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Models\AngelType;
class AngelTypeTest extends ModelTest
{
/**
* @return array<array{boolean, string, string, string}>
*/
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()
);
}
}