refactor goodie config option as enum (#1100)

* goodie config as enum
* Unified goodie check, updated test
* Changed tshirt to goodie in url

---------

Co-authored-by: Igor Scheller <igor.scheller@igorshp.de>
This commit is contained in:
xuwhite 2023-03-05 03:00:38 +01:00 committed by GitHub
parent 523e984122
commit 668a9e86f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 195 additions and 103 deletions

View File

@ -275,14 +275,11 @@ return [
// Enables the planned arrival/leave date // Enables the planned arrival/leave date
'enable_planned_arrival' => (bool) env('ENABLE_PLANNED_ARRIVAL', true), 'enable_planned_arrival' => (bool) env('ENABLE_PLANNED_ARRIVAL', true),
// Enables the T-Shirt configuration on signup and profile // Resembles the Goodie Type. There are three options:
'enable_tshirt_size' => (bool) env('ENABLE_TSHIRT_SIZE', true), // 'none' => no goodie at all
// 'goodie' => a goodie which has no sizing options
// When true changes everything from shirts to goodies and disables shirt size // 'tshirt' => goodie that is called tshirt and has sizing options
'other_goodie' => (bool) env('OTHER_GOODIE', false), 'goodie_type' => env('GOODIE_TYPE', 'goodie'),
// Enables the goody configuration on signup and profile
'enable_goody' => (bool) env('ENABLE_GOODY', false),
// Enables the food voucher in the user profile // Enables the food voucher in the user profile
'enable_voucher' => (bool) env('ENABLE_VOUCHER', true), 'enable_voucher' => (bool) env('ENABLE_VOUCHER', true),

View File

@ -174,7 +174,7 @@ $route->addGroup(
function (RouteCollector $route): void { function (RouteCollector $route): void {
// Shirts // Shirts
$route->addGroup( $route->addGroup(
'/shirt', '/goodie',
function (RouteCollector $route): void { function (RouteCollector $route): void {
$route->get('', 'Admin\\UserShirtController@editShirt'); $route->get('', 'Admin\\UserShirtController@editShirt');
$route->post('', 'Admin\\UserShirtController@saveShirt'); $route->post('', 'Admin\\UserShirtController@saveShirt');

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
class RenameShirtManager extends Migration
{
/**
* Run the migration
*/
public function up(): void
{
$db = $this->schema->getConnection();
$db->table('groups')
->where('name', 'Shirt Manager')
->update(['name' => 'Goodie Manager']);
}
/**
* Reverse the migration
*/
public function down(): void
{
$db = $this->schema->getConnection();
$db->table('groups')
->where('name', 'Goodie Manager')
->update(['name' => 'Shirt Manager']);
}
}

View File

@ -5,6 +5,7 @@ use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Engelsystem\Config\GoodieType;
/** /**
* @return string * @return string
@ -22,7 +23,9 @@ function admin_active()
$tshirt_sizes = config('tshirt_sizes'); $tshirt_sizes = config('tshirt_sizes');
$shift_sum_formula = User_get_shifts_sum_query(); $shift_sum_formula = User_get_shifts_sum_query();
$request = request(); $request = request();
$other_goodie = config('other_goodie'); $goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
$msg = ''; $msg = '';
$search = ''; $search = '';
@ -130,7 +133,7 @@ function admin_active()
$user_source->state->got_shirt = true; $user_source->state->got_shirt = true;
$user_source->state->save(); $user_source->state->save();
engelsystem_log('User ' . User_Nick_render($user_source, true) . ' has tshirt now.'); engelsystem_log('User ' . User_Nick_render($user_source, true) . ' has tshirt now.');
$msg = success(($other_goodie ? __('Angel has got a goodie.') : __('Angel has got a t-shirt.')), true); $msg = success(($goodie_tshirt ? __('Angel has got a t-shirt.') : __('Angel has got a goodie.')), true);
} else { } else {
$msg = error('Angel not found.', true); $msg = error('Angel not found.', true);
} }
@ -141,7 +144,7 @@ function admin_active()
$user_source->state->got_shirt = false; $user_source->state->got_shirt = false;
$user_source->state->save(); $user_source->state->save();
engelsystem_log('User ' . User_Nick_render($user_source, true) . ' has NO tshirt.'); engelsystem_log('User ' . User_Nick_render($user_source, true) . ' has NO tshirt.');
$msg = success(($other_goodie ? __('Angel has got no goodie.') : __('Angel has got no t-shirt.')), true); $msg = success(($goodie_tshirt ? __('Angel has got no t-shirt.') : __('Angel has got no goodie.')), true);
} else { } else {
$msg = error(__('Angel not found.'), true); $msg = error(__('Angel not found.'), true);
} }
@ -213,7 +216,7 @@ function admin_active()
$userData = []; $userData = [];
$userData['no'] = count($matched_users) + 1; $userData['no'] = count($matched_users) + 1;
$userData['nick'] = User_Nick_render($usr) . User_Pronoun_render($usr); $userData['nick'] = User_Nick_render($usr) . User_Pronoun_render($usr);
if (!$other_goodie) { if ($goodie_tshirt) {
$userData['shirt_size'] = (isset($tshirt_sizes[$shirtSize]) ? $tshirt_sizes[$shirtSize] : ''); $userData['shirt_size'] = (isset($tshirt_sizes[$shirtSize]) ? $tshirt_sizes[$shirtSize] : '');
} }
$userData['work_time'] = round($usr['shift_length'] / 60) $userData['work_time'] = round($usr['shift_length'] / 60)
@ -262,12 +265,15 @@ function admin_active()
if ($show_all_shifts) { if ($show_all_shifts) {
$parametersShirt['show_all_shifts'] = 1; $parametersShirt['show_all_shifts'] = 1;
} }
$actions[] = form(
[form_submit('submit', ($other_goodie ? __('got goodie') : __('got t-shirt')), 'btn-sm', false, 'secondary')], if ($goodie_enabled) {
page_link_to('admin_active', $parametersShirt), $actions[] = form(
false, [form_submit('submit', ($goodie_tshirt ? __('got t-shirt') : __('got goodie')), 'btn-sm', false, 'secondary')],
true page_link_to('admin_active', $parametersShirt),
); false,
true
);
}
} }
if ($usr->state->got_shirt) { if ($usr->state->got_shirt) {
$parameters = [ $parameters = [
@ -277,16 +283,19 @@ function admin_active()
if ($show_all_shifts) { if ($show_all_shifts) {
$parameters['show_all_shifts'] = 1; $parameters['show_all_shifts'] = 1;
} }
$actions[] = form(
[form_submit('submit', ($other_goodie ? __('remove goodie') : __('remove t-shirt')), 'btn-sm', false, 'secondary')], if ($goodie_enabled) {
page_link_to('admin_active', $parameters), $actions[] = form(
false, [form_submit('submit', ($goodie_tshirt ? __('remove t-shirt') : __('remove goodie')), 'btn-sm', false, 'secondary')],
true page_link_to('admin_active', $parameters),
); false,
true
);
}
} }
if (!$other_goodie) { if ($goodie_tshirt) {
$actions[] = button(url('/admin/user/' . $usr->id . '/shirt'), __('form.edit'), 'btn-secondary btn-sm'); $actions[] = button(url('/admin/user/' . $usr->id . '/goodie'), __('form.edit'), 'btn-secondary btn-sm');
} }
$userData['actions'] = buttons($actions); $userData['actions'] = buttons($actions);
@ -294,8 +303,8 @@ function admin_active()
$matched_users[] = $userData; $matched_users[] = $userData;
} }
$shirt_statistics = []; $goodie_statistics = [];
if (!$other_goodie) { if ($goodie_tshirt) {
foreach (array_keys($tshirt_sizes) as $size) { foreach (array_keys($tshirt_sizes) as $size) {
$gc = State::query() $gc = State::query()
->leftJoin('users_settings', 'users_state.user_id', '=', 'users_settings.user_id') ->leftJoin('users_settings', 'users_state.user_id', '=', 'users_settings.user_id')
@ -303,15 +312,15 @@ function admin_active()
->where('users_state.got_shirt', '=', true) ->where('users_state.got_shirt', '=', true)
->where('users_personal_data.shirt_size', '=', $size) ->where('users_personal_data.shirt_size', '=', $size)
->count(); ->count();
$shirt_statistics[] = [ $goodie_statistics[] = [
'size' => $size, 'size' => $size,
'given' => $gc, 'given' => $gc,
]; ];
} }
} }
$shirt_statistics[] = array_merge( $goodie_statistics[] = array_merge(
(!$other_goodie ? ['size' => '<b>' . __('Sum') . '</b>'] : []), ($goodie_tshirt ? ['size' => '<b>' . __('Sum') . '</b>'] : []),
['given' => '<b>' . State::whereGotShirt(true)->count() . '</b>'] ['given' => '<b>' . State::whereGotShirt(true)->count() . '</b>']
); );
@ -332,22 +341,24 @@ function admin_active()
'no' => __('No.'), 'no' => __('No.'),
'nick' => __('Nickname'), 'nick' => __('Nickname'),
], ],
(!$other_goodie ? ['shirt_size' => __('Size')] : []), ($goodie_tshirt ? ['shirt_size' => __('Size')] : []),
[ [
'shift_count' => __('Shifts'), 'shift_count' => __('Shifts'),
'work_time' => __('Length'), 'work_time' => __('Length'),
'active' => __('Active?'), 'active' => __('Active?'),
'force_active' => __('Forced'), 'force_active' => __('Forced'),
'tshirt' => ($other_goodie ? __('Goodie?') : __('T-shirt?')), ],
($goodie_enabled ? ['tshirt' => ($goodie_tshirt ? __('T-shirt?') : __('Goodie?'))] : []),
[
'actions' => '', 'actions' => '',
] ]
), ),
$matched_users $matched_users
), ),
'<h2>' . ($other_goodie ? __('Goodie statistic') : __('Shirt statistic')) . '</h2>', $goodie_enabled ? '<h2>' . ($goodie_tshirt ? __('Shirt statistic') : __('Goodie statistic')) . '</h2>' : '',
table(array_merge( $goodie_enabled ? table(array_merge(
(!$other_goodie ? ['size' => __('Size')] : []), ($goodie_tshirt ? ['size' => __('Size')] : []),
['given' => $other_goodie ? __('Given goodies') : __('Given shirts') ] ['given' => $goodie_tshirt ? __('Given shirts') : __('Given goodies') ]
), $shirt_statistics), ), $goodie_statistics) : '',
]); ]);
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use Engelsystem\Config\GoodieType;
use Engelsystem\Models\Group; use Engelsystem\Models\Group;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
@ -22,6 +23,9 @@ function admin_user()
$tshirt_sizes = config('tshirt_sizes'); $tshirt_sizes = config('tshirt_sizes');
$request = request(); $request = request();
$html = ''; $html = '';
$goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
if (!$request->has('id')) { if (!$request->has('id')) {
throw_redirect(users_link()); throw_redirect(users_link());
@ -36,11 +40,11 @@ function admin_user()
} }
$html .= __('Here you can change the user entry. Under the item \'Arrived\' the angel is marked as present, a yes at Active means that the angel was active.'); $html .= __('Here you can change the user entry. Under the item \'Arrived\' the angel is marked as present, a yes at Active means that the angel was active.');
if (config('enable_tshirt_size')) { if ($goodie_enabled) {
if (config('other_goodie')) { if ($goodie_tshirt) {
$html .= ' ' . __('If the angel is active, it can claim a goodie. If goodie is set to \'Yes\', the angel already got their goodie.');
} else {
$html .= ' ' . __('If the angel is active, it can claim a T-shirt. If T-shirt is set to \'Yes\', the angel already got their T-shirt.'); $html .= ' ' . __('If the angel is active, it can claim a T-shirt. If T-shirt is set to \'Yes\', the angel already got their T-shirt.');
} else {
$html .= ' ' . __('If the angel is active, it can claim a goodie. If goodie is set to \'Yes\', the angel already got their goodie.');
} }
} }
$html .= '<br /><br />'; $html .= '<br /><br />';
@ -67,7 +71,7 @@ function admin_user()
if ($user_source->settings->email_human) { if ($user_source->settings->email_human) {
$html .= ' <tr><td>' . __('settings.profile.email') . '</td><td>' . '<input type="email" size="40" name="eemail" value="' . $user_source->email . '" class="form-control" maxlength="254"></td></tr>' . "\n"; $html .= ' <tr><td>' . __('settings.profile.email') . '</td><td>' . '<input type="email" size="40" name="eemail" value="' . $user_source->email . '" class="form-control" maxlength="254"></td></tr>' . "\n";
} }
if (config('enable_tshirt_size') && !config('other_goodie')) { if ($goodie_tshirt) {
$html .= ' <tr><td>' . __('user.shirt_size') . '</td><td>' $html .= ' <tr><td>' . __('user.shirt_size') . '</td><td>'
. html_select_key( . html_select_key(
'size', 'size',
@ -103,12 +107,12 @@ function admin_user()
$html .= html_options('force_active', $options, $user_source->state->force_active) . '</td></tr>' . "\n"; $html .= html_options('force_active', $options, $user_source->state->force_active) . '</td></tr>' . "\n";
} }
if (config('enable_tshirt_size')) { if ($goodie_enabled) {
// T-Shirt bekommen? // T-Shirt bekommen?
if (config('other_goodie')) { if ($goodie_tshirt) {
$html .= ' <tr><td>' . __('Goodie') . '</td><td>' . "\n";
} else {
$html .= ' <tr><td>' . __('T-Shirt') . '</td><td>' . "\n"; $html .= ' <tr><td>' . __('T-Shirt') . '</td><td>' . "\n";
} else {
$html .= ' <tr><td>' . __('Goodie') . '</td><td>' . "\n";
} }
$html .= html_options('eTshirt', $options, $user_source->state->got_shirt) . '</td></tr>' . "\n"; $html .= html_options('eTshirt', $options, $user_source->state->got_shirt) . '</td></tr>' . "\n";
} }
@ -250,7 +254,7 @@ function admin_user()
$user_source->personalData->first_name = $request->postData('eVorname'); $user_source->personalData->first_name = $request->postData('eVorname');
$user_source->personalData->last_name = $request->postData('eName'); $user_source->personalData->last_name = $request->postData('eName');
} }
if (config('enable_tshirt_size') && !config('other_goodie')) { if ($goodie_tshirt) {
$user_source->personalData->shirt_size = $request->postData('eSize'); $user_source->personalData->shirt_size = $request->postData('eSize');
} }
$user_source->personalData->save(); $user_source->personalData->save();
@ -259,7 +263,7 @@ function admin_user()
$user_source->contact->dect = $request->postData('eDECT'); $user_source->contact->dect = $request->postData('eDECT');
$user_source->contact->save(); $user_source->contact->save();
if (config('enable_tshirt_size')) { if ($goodie_enabled) {
$user_source->state->got_shirt = $request->postData('eTshirt'); $user_source->state->got_shirt = $request->postData('eTshirt');
} }
$user_source->state->active = $request->postData('eAktiv'); $user_source->state->active = $request->postData('eAktiv');
@ -268,10 +272,10 @@ function admin_user()
engelsystem_log( engelsystem_log(
'Updated user: ' . $user_source->name . ' (' . $user_source->id . ')' 'Updated user: ' . $user_source->name . ' (' . $user_source->id . ')'
. (config('other_goodie') ? '' : ', t-shirt: ' . $user_source->personalData->shirt_size) . ($goodie_tshirt ? ', t-shirt: ' : '' . $user_source->personalData->shirt_size)
. ', active: ' . $user_source->state->active . ', active: ' . $user_source->state->active
. ', force-active: ' . $user_source->state->force_active . ', force-active: ' . $user_source->state->force_active
. (config('other_goodie') ? ', goodie: ' : ', tshirt: ' . $user_source->state->got_shirt) . ($goodie_tshirt ? ', tshirt: ' : ', goodie: ' . $user_source->state->got_shirt)
); );
$html .= success(__('Changes where saved.') . "\n", true); $html .= success(__('Changes where saved.') . "\n", true);
break; break;

View File

@ -3,6 +3,7 @@
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Database; use Engelsystem\Database\Database;
use Engelsystem\Events\Listener\OAuth2; use Engelsystem\Events\Listener\OAuth2;
use Engelsystem\Config\GoodieType;
use Engelsystem\Models\AngelType; use Engelsystem\Models\AngelType;
use Engelsystem\Models\Group; use Engelsystem\Models\Group;
use Engelsystem\Models\OAuth; use Engelsystem\Models\OAuth;
@ -30,8 +31,9 @@ function guest_register()
{ {
$authUser = auth()->user(); $authUser = auth()->user();
$tshirt_sizes = config('tshirt_sizes'); $tshirt_sizes = config('tshirt_sizes');
$enable_tshirt_size = config('enable_tshirt_size'); $goodie = GoodieType::from(config('goodie_type'));
$other_goodie = config('other_goodie'); $goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
$enable_user_name = config('enable_user_name'); $enable_user_name = config('enable_user_name');
$enable_dect = config('enable_dect'); $enable_dect = config('enable_dect');
$enable_planned_arrival = config('enable_planned_arrival'); $enable_planned_arrival = config('enable_planned_arrival');
@ -168,7 +170,7 @@ function guest_register()
$email_goody = true; $email_goody = true;
} }
if ($enable_tshirt_size && !$other_goodie) { if ($goodie_tshirt) {
if ($request->has('tshirt_size') && isset($tshirt_sizes[$request->input('tshirt_size')])) { if ($request->has('tshirt_size') && isset($tshirt_sizes[$request->input('tshirt_size')])) {
$tshirt_size = $request->input('tshirt_size'); $tshirt_size = $request->input('tshirt_size');
} else { } else {
@ -450,7 +452,7 @@ function guest_register()
__('Allow heaven angels to contact you by e-mail.'), __('Allow heaven angels to contact you by e-mail.'),
$email_by_human_allowed $email_by_human_allowed
), ),
config('enable_goody') ? $goodie_enabled ?
form_checkbox( form_checkbox(
'email_goody', 'email_goody',
__('To receive vouchers, give consent that nick, email address, worked hours and shirt size will be stored until the next similar event.') __('To receive vouchers, give consent that nick, email address, worked hours and shirt size will be stored until the next similar event.')
@ -495,7 +497,7 @@ function guest_register()
]) : '', ]) : '',
div('col', [ div('col', [
$enable_tshirt_size && !$other_goodie ? form_select( $goodie_tshirt ? form_select(
'tshirt_size', 'tshirt_size',
__('Shirt size') . ' ' . entry_required(), __('Shirt size') . ' ' . entry_required(),
$tshirt_sizes, $tshirt_sizes,

View File

@ -1,6 +1,7 @@
<?php <?php
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Config\GoodieType;
use Engelsystem\Models\AngelType; use Engelsystem\Models\AngelType;
use Engelsystem\Models\Group; use Engelsystem\Models\Group;
use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\Shift;
@ -84,6 +85,9 @@ function Users_view(
$tshirts_count, $tshirts_count,
$voucher_count $voucher_count
) { ) {
$goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
$usersList = []; $usersList = [];
foreach ($users as $user) { foreach ($users as $user) {
$u = []; $u = [];
@ -98,9 +102,9 @@ function Users_view(
$u['freeloads'] = $user->getAttribute('freeloads'); $u['freeloads'] = $user->getAttribute('freeloads');
$u['active'] = icon_bool($user->state->active); $u['active'] = icon_bool($user->state->active);
$u['force_active'] = icon_bool($user->state->force_active); $u['force_active'] = icon_bool($user->state->force_active);
if (config('enable_tshirt_size')) { if ($goodie_enabled) {
$u['got_shirt'] = icon_bool($user->state->got_shirt); $u['got_shirt'] = icon_bool($user->state->got_shirt);
if (!config('other_goodie')) { if ($goodie_tshirt) {
$u['shirt_size'] = $user->personalData->shirt_size; $u['shirt_size'] = $user->personalData->shirt_size;
} }
} }
@ -142,16 +146,14 @@ function Users_view(
$user_table_headers['freeloads'] = Users_table_header_link('freeloads', __('Freeloads'), $order_by); $user_table_headers['freeloads'] = Users_table_header_link('freeloads', __('Freeloads'), $order_by);
$user_table_headers['active'] = Users_table_header_link('active', __('Active'), $order_by); $user_table_headers['active'] = Users_table_header_link('active', __('Active'), $order_by);
$user_table_headers['force_active'] = Users_table_header_link('force_active', __('Forced'), $order_by); $user_table_headers['force_active'] = Users_table_header_link('force_active', __('Forced'), $order_by);
if (config('enable_tshirt_size')) { if ($goodie_enabled) {
if (config('other_goodie')) { if ($goodie_tshirt) {
$user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('Goodie'), $order_by);
} else {
$user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('T-Shirt'), $order_by); $user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('T-Shirt'), $order_by);
$user_table_headers['shirt_size'] = Users_table_header_link('shirt_size', __('Size'), $order_by);
} else {
$user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('Goodie'), $order_by);
} }
} }
if (config('enable_tshirt_size') && !config('other_goodie')) {
$user_table_headers['shirt_size'] = Users_table_header_link('shirt_size', __('Size'), $order_by);
}
$user_table_headers['arrival_date'] = Users_table_header_link( $user_table_headers['arrival_date'] = Users_table_header_link(
'planned_arrival_date', 'planned_arrival_date',
__('Planned arrival'), __('Planned arrival'),
@ -376,6 +378,9 @@ function User_view_myshifts(
$user_worklogs, $user_worklogs,
$admin_user_worklog_privilege $admin_user_worklog_privilege
) { ) {
$goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
$myshifts_table = []; $myshifts_table = [];
$timeSum = 0; $timeSum = 0;
foreach ($shifts as $shift) { foreach ($shifts as $shift) {
@ -405,9 +410,9 @@ function User_view_myshifts(
'comment' => '', 'comment' => '',
'actions' => '', 'actions' => '',
]; ];
if (config('enable_tshirt_size', false) && ($its_me || $tshirt_admin)) { if ($goodie_enabled && ($its_me || $tshirt_admin)) {
$myshifts_table[] = [ $myshifts_table[] = [
'date' => '<b>' . (config('other_goodie') ? __('Your goodie score') : __('Your t-shirt score')) . '&trade;:</b>', 'date' => '<b>' . ($goodie_tshirt ? __('Your t-shirt score') : __('Your goodie score')) . '&trade;:</b>',
'duration' => '<b>' . $tshirt_score . '</b>', 'duration' => '<b>' . $tshirt_score . '</b>',
'room' => '', 'room' => '',
'shift_info' => '', 'shift_info' => '',
@ -489,6 +494,9 @@ function User_view(
$admin_user_worklog_privilege, $admin_user_worklog_privilege,
$user_worklogs $user_worklogs
) { ) {
$goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
$auth = auth(); $auth = auth();
$nightShiftsConfig = config('night_shifts'); $nightShiftsConfig = config('night_shifts');
$user_name = htmlspecialchars( $user_name = htmlspecialchars(
@ -538,9 +546,9 @@ function User_view(
div('row', [ div('row', [
div('col-md-12', [ div('col-md-12', [
buttons([ buttons([
$auth->can('user.edit.shirt') && (config('enable_tshirt_size')) ? button( $auth->can('user.edit.shirt') && $goodie_enabled ? button(
url('/admin/user/' . $user_source->id . '/shirt'), url('/admin/user/' . $user_source->id . '/goodie'),
icon('person') . (config('other_goodie') ? __('Goodie') : __('Shirt')) icon('person') . ($goodie_tshirt ? __('Shirt') : __('Goodie'))
) : '', ) : '',
$admin_user_privilege ? button( $admin_user_privilege ? button(
page_link_to('admin_user', ['id' => $user_source->id]), page_link_to('admin_user', ['id' => $user_source->id]),
@ -627,7 +635,7 @@ function User_view(
]), ]),
($its_me || $admin_user_privilege) ? '<h2>' . __('Shifts') . '</h2>' : '', ($its_me || $admin_user_privilege) ? '<h2>' . __('Shifts') . '</h2>' : '',
$myshifts_table, $myshifts_table,
($its_me && $nightShiftsConfig['enabled'] && config('enable_tshirt_size')) ? info( ($its_me && $nightShiftsConfig['enabled'] && $goodie_enabled) ? info(
icon('info-circle') . sprintf( icon('info-circle') . sprintf(
__('Your night shifts between %d and %d am count twice.'), __('Your night shifts between %d and %d am count twice.'),
$nightShiftsConfig['start'], $nightShiftsConfig['start'],
@ -700,6 +708,9 @@ function User_view_state_user($user_source)
function User_view_state_admin($freeloader, $user_source) function User_view_state_admin($freeloader, $user_source)
{ {
$state = []; $state = [];
$goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
if ($freeloader) { if ($freeloader) {
$state[] = '<span class="text-danger">' . icon('exclamation-circle') . __('Freeloader') . '</span>'; $state[] = '<span class="text-danger">' . icon('exclamation-circle') . __('Freeloader') . '</span>';
@ -720,8 +731,8 @@ function User_view_state_admin($freeloader, $user_source)
} elseif ($user_source->state->active) { } elseif ($user_source->state->active) {
$state[] = '<span class="text-success">' . __('Active') . '</span>'; $state[] = '<span class="text-success">' . __('Active') . '</span>';
} }
if ($user_source->state->got_shirt) { if ($user_source->state->got_shirt && $goodie_enabled) {
$state[] = '<span class="text-success">' . __('T-Shirt') . '</span>'; $state[] = '<span class="text-success">' . ($goodie_tshirt ? __('T-Shirt') : __('Goodie')) . '</span>';
} }
} else { } else {
$arrivalDate = $user_source->personalData->planned_arrival_date; $arrivalDate = $user_source->personalData->planned_arrival_date;
@ -930,7 +941,9 @@ function render_user_arrived_hint()
*/ */
function render_user_tshirt_hint() function render_user_tshirt_hint()
{ {
if ((config('enable_tshirt_size') && !config('other_goodie')) && !auth()->user()->personalData->shirt_size) { $goodie = GoodieType::from(config('goodie_type'));
$goodie_tshirt = $goodie === GoodieType::Tshirt;
if ($goodie_tshirt && !auth()->user()->personalData->shirt_size) {
$text = __('You need to specify a tshirt size in your settings!'); $text = __('You need to specify a tshirt size in your settings!');
return render_profile_link($text, null, 'text-danger'); return render_profile_link($text, null, 'text-danger');
} }

View File

@ -380,6 +380,9 @@ msgstr "Answer"
msgid "user.edit.shirt" msgid "user.edit.shirt"
msgstr "Edit shirt" msgstr "Edit shirt"
msgid "user.edit.goodie"
msgstr "Edit goodie"
msgid "form.shirt" msgid "form.shirt"
msgstr "Shirt" msgstr "Shirt"
@ -398,6 +401,9 @@ msgstr "Arrived"
msgid "user.got_shirt" msgid "user.got_shirt"
msgstr "Got shirt" msgstr "Got shirt"
msgid "user.got_goodie"
msgstr "Got goodie"
msgid "message.title" msgid "message.title"
msgstr "Messages" msgstr "Messages"

View File

@ -3,11 +3,7 @@
{% import 'macros/form.twig' as f %} {% import 'macros/form.twig' as f %}
{% block title %} {% block title %}
{% if config('other_goodie') %} {{ is_tshirt ? __('user.edit.shirt') : __('user.edit.goodie') }}
{{ __('user.edit.goodie') }}
{% else %}
{{ __('user.edit.shirt') }}
{% endif %}
{% endblock %} {% endblock %}
{% block content %} {% block content %}
@ -20,7 +16,7 @@
{{ csrf() }} {{ csrf() }}
<div class="row"> <div class="row">
{% if not config('other_goodie') %} {% if is_tshirt %}
<div class="col-md-6"> <div class="col-md-6">
{{ f.select('shirt_size', config('tshirt_sizes'), __('user.shirt_size'), userdata.personalData.shirt_size) }} {{ f.select('shirt_size', config('tshirt_sizes'), __('user.shirt_size'), userdata.personalData.shirt_size) }}
</div> </div>
@ -34,11 +30,7 @@
{{ f.switch('active', __('user.active'), userdata.state.active) }} {{ f.switch('active', __('user.active'), userdata.state.active) }}
{% if config('other_goodie') %} {{ f.switch('got_shirt', is_tshirt ? __('user.got_shirt') : __('user.got_goodie'), userdata.state.got_shirt) }}
{{ f.switch('got_shirt', __('user.got_goodie'), userdata.state.got_shirt) }}
{% else %}
{{ f.switch('got_shirt', __('user.got_shirt'), userdata.state.got_shirt) }}
{% endif %}
</div> </div>
<div class="col-md-12"> <div class="col-md-12">
{{ f.submit(__('form.save')) }} {{ f.submit(__('form.save')) }}

View File

@ -127,7 +127,7 @@
__('settings.profile.email_by_human_allowed'), __('settings.profile.email_by_human_allowed'),
user.settings.email_human user.settings.email_human
) }} ) }}
{% if config('enable_goody') %} {% if goodie_enabled %}
{% set privacy_email = config('privacy_email') %} {% set privacy_email = config('privacy_email') %}
{% set email_goody_label = __('settings.profile.email_goody') ~ {% set email_goody_label = __('settings.profile.email_goody') ~
(privacy_email ? ' ' ~ __('settings.profile.privacy', [privacy_email]) : '') (privacy_email ? ' ' ~ __('settings.profile.privacy', [privacy_email]) : '')
@ -143,7 +143,7 @@
{% endif %} {% endif %}
</div> </div>
{% if config('enable_tshirt_size') and not config('other_goodie') %} {% if goodie_tshirt %}
<div class="col-md-12"> <div class="col-md-12">
{{ f.select( {{ f.select(
'shirt_size', 'shirt_size',

12
src/Config/GoodieType.php Normal file
View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Config;
enum GoodieType : string
{
case None = 'none';
case Goodie = 'goodie';
case Tshirt = 'tshirt';
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Engelsystem\Controllers\Admin; namespace Engelsystem\Controllers\Admin;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Config\GoodieType;
use Engelsystem\Controllers\BaseController; use Engelsystem\Controllers\BaseController;
use Engelsystem\Controllers\HasUserNotifications; use Engelsystem\Controllers\HasUserNotifications;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
@ -42,15 +43,17 @@ class UserShirtController extends BaseController
return $this->response->withView( return $this->response->withView(
'admin/user/edit-shirt.twig', 'admin/user/edit-shirt.twig',
['userdata' => $user] [
'userdata' => $user,
'is_tshirt' => $this->config->get('goodie_type') === GoodieType::Tshirt->value,
]
); );
} }
public function saveShirt(Request $request): Response public function saveShirt(Request $request): Response
{ {
$userId = (int) $request->getAttribute('user_id'); $userId = (int) $request->getAttribute('user_id');
$shirtEnabled = !$this->config->get('other_goodie'); $shirtEnabled = $this->config->get('goodie_type') === GoodieType::Tshirt->value;
/** @var User $user */ /** @var User $user */
$user = $this->user->findOrFail($userId); $user = $this->user->findOrFail($userId);

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Engelsystem\Controllers; namespace Engelsystem\Controllers;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Config\GoodieType;
use Engelsystem\Http\Exceptions\HttpNotFound; use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Response; use Engelsystem\Http\Response;
use Engelsystem\Http\Redirector; use Engelsystem\Http\Redirector;
@ -40,6 +41,8 @@ class SettingsController extends BaseController
[ [
'settings_menu' => $this->settingsMenu(), 'settings_menu' => $this->settingsMenu(),
'user' => $user, 'user' => $user,
'goodie_tshirt' => $this->config->get('goodie_type') === GoodieType::Tshirt->value,
'goodie_enabled' => $this->config->get('goodie_type') !== GoodieType::None->value,
] ]
); );
} }
@ -48,6 +51,9 @@ class SettingsController extends BaseController
{ {
$user = $this->auth->user(); $user = $this->auth->user();
$data = $this->validate($request, $this->getSaveProfileRules()); $data = $this->validate($request, $this->getSaveProfileRules());
$goodie = GoodieType::from(config('goodie_type'));
$goodie_enabled = $goodie !== GoodieType::None;
$goodie_tshirt = $goodie === GoodieType::Tshirt;
if (config('enable_pronoun')) { if (config('enable_pronoun')) {
$user->personalData->pronoun = $data['pronoun']; $user->personalData->pronoun = $data['pronoun'];
@ -87,13 +93,12 @@ class SettingsController extends BaseController
$user->settings->email_human = $data['email_human'] ?: false; $user->settings->email_human = $data['email_human'] ?: false;
$user->settings->email_messages = $data['email_messages'] ?: false; $user->settings->email_messages = $data['email_messages'] ?: false;
if (config('enable_goody')) { if ($goodie_enabled) {
$user->settings->email_goody = $data['email_goody'] ?: false; $user->settings->email_goody = $data['email_goody'] ?: false;
} }
if ( if (
(config('enable_tshirt_size') $goodie_tshirt
&& !config('other_goodie'))
&& isset(config('tshirt_sizes')[$data['shirt_size']]) && isset(config('tshirt_sizes')[$data['shirt_size']])
) { ) {
$user->personalData->shirt_size = $data['shirt_size']; $user->personalData->shirt_size = $data['shirt_size'];
@ -265,6 +270,7 @@ class SettingsController extends BaseController
*/ */
private function getSaveProfileRules(): array private function getSaveProfileRules(): array
{ {
$goodie_tshirt = $this->config->get('goodie_type') === GoodieType::Tshirt->value;
$rules = [ $rules = [
'pronoun' => 'optional|max:15', 'pronoun' => 'optional|max:15',
'first_name' => 'optional|max:64', 'first_name' => 'optional|max:64',
@ -283,7 +289,7 @@ class SettingsController extends BaseController
$rules['planned_arrival_date'] = 'required|date:Y-m-d'; $rules['planned_arrival_date'] = 'required|date:Y-m-d';
$rules['planned_departure_date'] = 'optional|date:Y-m-d'; $rules['planned_departure_date'] = 'optional|date:Y-m-d';
} }
if (config('enable_tshirt_size') && !config('other_goodie')) { if ($goodie_tshirt) {
$rules['shirt_size'] = 'required'; $rules['shirt_size'] = 'required';
} }
return $rules; return $rules;

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Engelsystem\Test\Unit\Controllers\Admin; namespace Engelsystem\Test\Unit\Controllers\Admin;
use Engelsystem\Config\GoodieType;
use Engelsystem\Controllers\Admin\UserShirtController; use Engelsystem\Controllers\Admin\UserShirtController;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Redirector; use Engelsystem\Http\Redirector;
@ -63,6 +64,7 @@ class UserShirtControllerTest extends ControllerTest
*/ */
public function testSaveShirt(): void public function testSaveShirt(): void
{ {
$this->config->set('goodie_type', GoodieType::Tshirt->value);
$request = $this->request $request = $this->request
->withAttribute('user_id', 1) ->withAttribute('user_id', 1)
->withParsedBody([ ->withParsedBody([
@ -79,11 +81,11 @@ class UserShirtControllerTest extends ControllerTest
->create(); ->create();
$auth $auth
->expects($this->exactly(5)) ->expects($this->exactly(6))
->method('can') ->method('can')
->with('admin_arrive') ->with('admin_arrive')
->willReturnOnConsecutiveCalls(true, true, true, false, true); ->willReturnOnConsecutiveCalls(true, true, true, false, false, true);
$this->setExpects($redirector, 'back', null, $this->response, $this->exactly(5)); $this->setExpects($redirector, 'back', null, $this->response, $this->exactly(6));
$controller = new UserShirtController( $controller = new UserShirtController(
$auth, $auth,
@ -146,7 +148,18 @@ class UserShirtControllerTest extends ControllerTest
$this->assertFalse($user->state->arrived); $this->assertFalse($user->state->arrived);
// Shirt disabled // Shirt disabled
$this->config->set('other_goodie'); $this->config->set('goodie_type', GoodieType::None->value);
$request = $request
->withParsedBody([
'shirt_size' => 'XS',
]);
$controller->saveShirt($request);
$user = User::find(1);
$this->assertEquals('S', $user->personalData->shirt_size);
// Shirt enabled
$this->config->set('goodie_type', GoodieType::Tshirt->value);
$request = $request $request = $request
->withParsedBody([ ->withParsedBody([
'shirt_size' => 'XS', 'shirt_size' => 'XS',

View File

@ -6,6 +6,7 @@ namespace Engelsystem\Test\Unit\Controllers;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Config\GoodieType;
use Engelsystem\Controllers\NotificationType; use Engelsystem\Controllers\NotificationType;
use Engelsystem\Controllers\SettingsController; use Engelsystem\Controllers\SettingsController;
use Engelsystem\Http\Exceptions\HttpNotFound; use Engelsystem\Http\Exceptions\HttpNotFound;
@ -64,8 +65,7 @@ class SettingsControllerTest extends ControllerTest
'enable_planned_arrival' => true, 'enable_planned_arrival' => true,
'enable_dect' => true, 'enable_dect' => true,
'enable_mobile_show' => true, 'enable_mobile_show' => true,
'enable_goody' => true, 'goodie_type' => GoodieType::Tshirt->value,
'enable_tshirt_size' => true,
]); ]);
$this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce()); $this->setExpects($this->auth, 'user', null, $this->user, $this->atLeastOnce());
@ -213,7 +213,7 @@ class SettingsControllerTest extends ControllerTest
public function testSaveProfileIgnoresEmailGoodyIfDisabled(): void public function testSaveProfileIgnoresEmailGoodyIfDisabled(): void
{ {
$this->setUpProfileTest(); $this->setUpProfileTest();
config(['enable_goody' => false]); $this->config->set('goodie_type', GoodieType::None->value);
$this->controller->saveProfile($this->request); $this->controller->saveProfile($this->request);
$this->assertFalse($this->user->settings->email_goody); $this->assertFalse($this->user->settings->email_goody);
} }
@ -224,7 +224,7 @@ class SettingsControllerTest extends ControllerTest
public function testSaveProfileIgnoresTShirtSizeIfDisabled(): void public function testSaveProfileIgnoresTShirtSizeIfDisabled(): void
{ {
$this->setUpProfileTest(); $this->setUpProfileTest();
config(['enable_tshirt_size' => false]); $this->config->set('goodie_type', GoodieType::None->value);
$this->controller->saveProfile($this->request); $this->controller->saveProfile($this->request);
$this->assertEquals('', $this->user->personalData->shirt_size); $this->assertEquals('', $this->user->personalData->shirt_size);
} }
@ -639,6 +639,7 @@ class SettingsControllerTest extends ControllerTest
'themes' => $themes, 'themes' => $themes,
'locales' => $languages, 'locales' => $languages,
'tshirt_sizes' => $tshirt_sizes, 'tshirt_sizes' => $tshirt_sizes,
'goodie_type' => GoodieType::Goodie->value,
]); ]);
$this->app->instance('config', $this->config); $this->app->instance('config', $this->config);
$this->app->instance(Config::class, $this->config); $this->app->instance(Config::class, $this->config);