From ccdf1aa8d5f017de1950a1f6ff076db43350165c Mon Sep 17 00:00:00 2001 From: Xu Date: Fri, 27 Jan 2023 21:01:23 +0100 Subject: [PATCH] add config option for other goodies --- config/config.default.php | 3 + includes/pages/admin_active.php | 88 +++++++++++-------- includes/pages/admin_user.php | 20 +++-- includes/pages/guest_login.php | 5 +- includes/view/User_view.php | 20 +++-- resources/lang/de_DE/default.po | 49 +++++++++-- resources/views/admin/user/edit-shirt.twig | 22 +++-- resources/views/pages/settings/profile.twig | 2 +- src/Controllers/Admin/UserShirtController.php | 5 +- src/Controllers/SettingsController.php | 8 +- .../Admin/UserShirtControllerTest.php | 19 +++- 11 files changed, 169 insertions(+), 72 deletions(-) diff --git a/config/config.default.php b/config/config.default.php index 66a8b287..62bfcdbd 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -276,6 +276,9 @@ return [ // Enables the T-Shirt configuration on signup and profile 'enable_tshirt_size' => (bool) env('ENABLE_TSHIRT_SIZE', true), + // When true changes everything from shirts to goodies and disables shirt size + 'other_goodie' => (bool) env('OTHER_GOODIE', false), + // Enables the goody configuration on signup and profile 'enable_goody' => (bool) env('ENABLE_GOODY', false), diff --git a/includes/pages/admin_active.php b/includes/pages/admin_active.php index 73105b74..0103f4d8 100644 --- a/includes/pages/admin_active.php +++ b/includes/pages/admin_active.php @@ -22,6 +22,7 @@ function admin_active() $tshirt_sizes = config('tshirt_sizes'); $shift_sum_formula = User_get_shifts_sum_query(); $request = request(); + $other_goodie = config('other_goodie'); $msg = ''; $search = ''; @@ -129,7 +130,7 @@ function admin_active() $user_source->state->got_shirt = true; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source, true) . ' has tshirt now.'); - $msg = success(__('Angel has got a t-shirt.'), true); + $msg = success(($other_goodie ? __('Angel has got a goodie.') : __('Angel has got a t-shirt.')), true); } else { $msg = error('Angel not found.', true); } @@ -140,7 +141,7 @@ function admin_active() $user_source->state->got_shirt = false; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source, true) . ' has NO tshirt.'); - $msg = success(__('Angel has got no t-shirt.'), true); + $msg = success(($other_goodie ? __('Angel has got no goodie.') : __('Angel has got no t-shirt.')), true); } else { $msg = error(__('Angel not found.'), true); } @@ -212,7 +213,9 @@ function admin_active() $userData = []; $userData['no'] = count($matched_users) + 1; $userData['nick'] = User_Nick_render($usr) . User_Pronoun_render($usr); - $userData['shirt_size'] = (isset($tshirt_sizes[$shirtSize]) ? $tshirt_sizes[$shirtSize] : ''); + if (!$other_goodie) { + $userData['shirt_size'] = (isset($tshirt_sizes[$shirtSize]) ? $tshirt_sizes[$shirtSize] : ''); + } $userData['work_time'] = round($usr['shift_length'] / 60) . ' min (' . sprintf('%.2f', $usr['shift_length'] / 3600) . ' h)'; $userData['active'] = icon_bool($usr->state->active == 1); @@ -260,7 +263,7 @@ function admin_active() $parametersShirt['show_all_shifts'] = 1; } $actions[] = form( - [form_submit('submit', __('got t-shirt'), 'btn-sm', false, 'secondary')], + [form_submit('submit', ($other_goodie ? __('got goodie') : __('got t-shirt')), 'btn-sm', false, 'secondary')], page_link_to('admin_active', $parametersShirt), false, true @@ -275,14 +278,16 @@ function admin_active() $parameters['show_all_shifts'] = 1; } $actions[] = form( - [form_submit('submit', __('remove t-shirt'), 'btn-sm', false, 'secondary')], + [form_submit('submit', ($other_goodie ? __('remove goodie') : __('remove t-shirt')), 'btn-sm', false, 'secondary')], page_link_to('admin_active', $parameters), false, true ); } - $actions[] = button(url('/admin/user/' . $usr->id . '/shirt'), __('form.edit'), 'btn-secondary btn-sm'); + if (!$other_goodie) { + $actions[] = button(url('/admin/user/' . $usr->id . '/shirt'), __('form.edit'), 'btn-secondary btn-sm'); + } $userData['actions'] = buttons($actions); @@ -290,23 +295,25 @@ function admin_active() } $shirt_statistics = []; - foreach (array_keys($tshirt_sizes) as $size) { - $gc = State::query() - ->leftJoin('users_settings', 'users_state.user_id', '=', 'users_settings.user_id') - ->leftJoin('users_personal_data', 'users_state.user_id', '=', 'users_personal_data.user_id') - ->where('users_state.got_shirt', '=', true) - ->where('users_personal_data.shirt_size', '=', $size) - ->count(); - $shirt_statistics[] = [ - 'size' => $size, - 'given' => $gc - ]; + if (!$other_goodie) { + foreach (array_keys($tshirt_sizes) as $size) { + $gc = State::query() + ->leftJoin('users_settings', 'users_state.user_id', '=', 'users_settings.user_id') + ->leftJoin('users_personal_data', 'users_state.user_id', '=', 'users_personal_data.user_id') + ->where('users_state.got_shirt', '=', true) + ->where('users_personal_data.shirt_size', '=', $size) + ->count(); + $shirt_statistics[] = [ + 'size' => $size, + 'given' => $gc + ]; + } } - $shirt_statistics[] = [ - 'size' => '' . __('Sum') . '', - 'given' => '' . State::whereGotShirt(true)->count() . '' - ]; + $shirt_statistics[] = array_merge( + (!$other_goodie ? ['size' => '' . __('Sum') . ''] : []), + ['given' => '' . State::whereGotShirt(true)->count() . ''] + ); return page_with_title(admin_active_title(), [ form([ @@ -319,21 +326,28 @@ function admin_active() form_submit('set_active', __('Preview')) ]) : $set_active, $msg . msg(), - table([ - 'no' => __('No.'), - 'nick' => __('Nickname'), - 'shirt_size' => __('Size'), - 'shift_count' => __('Shifts'), - 'work_time' => __('Length'), - 'active' => __('Active?'), - 'force_active' => __('Forced'), - 'tshirt' => __('T-shirt?'), - 'actions' => '' - ], $matched_users), - '

' . __('Shirt statistics') . '

', - table([ - 'size' => __('Size'), - 'given' => __('Given shirts') - ], $shirt_statistics) + table( + array_merge( + [ + 'no' => __('No.'), + 'nick' => __('Nickname'), + ], + (!$other_goodie ? ['shirt_size' => __('Size')] : []), + [ + 'shift_count' => __('Shifts'), + 'work_time' => __('Length'), + 'active' => __('Active?'), + 'force_active' => __('Forced'), + 'tshirt' => ($other_goodie ? __('Goodie?') : __('T-shirt?')), + 'actions' => '' + ] + ), + $matched_users + ), + '

' . ($other_goodie ? __('Goodie statistic') : __('Shirt statistic')) . '

', + table(array_merge( + (!$other_goodie ? ['size' => __('Size')] : []), + ['given' => $other_goodie ? __('Given goodies') : __('Given shirts') ] + ), $shirt_statistics) ]); } diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 29dd623e..d562677d 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -37,7 +37,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.'); if (config('enable_tshirt_size')) { - $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.'); + if (config('other_goodie')) { + $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 .= '

'; $html .= '
" . __('settings.profile.email') . "" . '' . "\n"; } - if (config('enable_tshirt_size')) { + if (config('enable_tshirt_size') && !config('other_goodie')) { $html .= ' ' . __('user.shirt_size') . '' . html_select_key( 'size', @@ -101,7 +105,11 @@ function admin_user() if (config('enable_tshirt_size')) { // T-Shirt bekommen? - $html .= ' ' . __('T-Shirt') . '' . "\n"; + if (config('other_goodie')) { + $html .= ' ' . __('Goodie') . '' . "\n"; + } else { + $html .= ' ' . __('T-Shirt') . '' . "\n"; + } $html .= html_options('eTshirt', $options, $user_source->state->got_shirt) . '' . "\n"; } $html .= '' . "\n" . ''; @@ -242,7 +250,7 @@ function admin_user() $user_source->personalData->first_name = $request->postData('eVorname'); $user_source->personalData->last_name = $request->postData('eName'); } - if (config('enable_tshirt_size')) { + if (config('enable_tshirt_size') && !config('other_goodie')) { $user_source->personalData->shirt_size = $request->postData('eSize'); } $user_source->personalData->save(); @@ -260,10 +268,10 @@ function admin_user() engelsystem_log( 'Updated user: ' . $user_source->name . ' (' . $user_source->id . ')' - . ', t-shirt: ' . $user_source->personalData->shirt_size + . (config('other_goodie') ? '' : ', t-shirt: ' . $user_source->personalData->shirt_size) . ', active: ' . $user_source->state->active . ', force-active: ' . $user_source->state->force_active - . ', tshirt: ' . $user_source->state->got_shirt + . (config('other_goodie') ? ', goodie: ' : ', tshirt: ' . $user_source->state->got_shirt) ); $html .= success(__('Changes where saved.') . "\n", true); break; diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index 9d4f3d75..1fcecda3 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -31,6 +31,7 @@ function guest_register() $authUser = auth()->user(); $tshirt_sizes = config('tshirt_sizes'); $enable_tshirt_size = config('enable_tshirt_size'); + $other_goodie = config('other_goodie'); $enable_user_name = config('enable_user_name'); $enable_dect = config('enable_dect'); $enable_planned_arrival = config('enable_planned_arrival'); @@ -162,7 +163,7 @@ function guest_register() $email_goody = true; } - if ($enable_tshirt_size) { + if ($enable_tshirt_size && !$other_goodie) { if ($request->has('tshirt_size') && isset($tshirt_sizes[$request->input('tshirt_size')])) { $tshirt_size = $request->input('tshirt_size'); } else { @@ -483,7 +484,7 @@ function guest_register() ]) : '', div('col', [ - $enable_tshirt_size ? form_select( + $enable_tshirt_size && !$other_goodie ? form_select( 'tshirt_size', __('Shirt size') . ' ' . entry_required(), $tshirt_sizes, diff --git a/includes/view/User_view.php b/includes/view/User_view.php index 8316f2df..076c5742 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -100,7 +100,9 @@ function Users_view( $u['force_active'] = icon_bool($user->state->force_active); if (config('enable_tshirt_size')) { $u['got_shirt'] = icon_bool($user->state->got_shirt); - $u['shirt_size'] = $user->personalData->shirt_size; + if (!config('other_goodie')) { + $u['shirt_size'] = $user->personalData->shirt_size; + } } $u['arrival_date'] = $user->personalData->planned_arrival_date ? $user->personalData->planned_arrival_date->format(__('Y-m-d')) : ''; @@ -141,9 +143,13 @@ function Users_view( $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); if (config('enable_tshirt_size')) { - $user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('T-Shirt'), $order_by); + if (config('other_goodie')) { + $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); + } } - if (config('enable_tshirt_size')) { + 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( @@ -401,7 +407,7 @@ function User_view_myshifts( ]; if (config('enable_tshirt_size', false) && ($its_me || $tshirt_admin)) { $myshifts_table[] = [ - 'date' => '' . __('Your t-shirt score') . '™:', + 'date' => '' . (config('other_goodie') ? __('Your goodie score') : __('Your t-shirt score')) . '™:', 'duration' => '' . $tshirt_score . '', 'room' => '', 'shift_info' => '', @@ -532,9 +538,9 @@ function User_view( div('row', [ div('col-md-12', [ buttons([ - $auth->can('user.edit.shirt') && config('enable_tshirt_size') ? button( + $auth->can('user.edit.shirt') && (config('enable_tshirt_size')) ? button( url('/admin/user/' . $user_source->id . '/shirt'), - icon('person') . __('Shirt') + icon('person') . (config('other_goodie') ? __('Goodie') : __('Shirt')) ) : '', $admin_user_privilege ? button( page_link_to('admin_user', ['id' => $user_source->id]), @@ -924,7 +930,7 @@ function render_user_arrived_hint() */ function render_user_tshirt_hint() { - if (config('enable_tshirt_size') && !auth()->user()->personalData->shirt_size) { + if ((config('enable_tshirt_size') && !config('other_goodie')) && !auth()->user()->personalData->shirt_size) { $text = __('You need to specify a tshirt size in your settings!'); return render_profile_link($text, null, 'text-danger'); } diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 84fe485e..e24ae915 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -961,11 +961,11 @@ msgstr "entferne aktiv" #: includes/pages/admin_active.php:261 msgid "got t-shirt" -msgstr "hat t-shirt" +msgstr "T-Shirt bekommen" #: includes/pages/admin_active.php:274 msgid "remove t-shirt" -msgstr "entferne t-shirt" +msgstr "entferne T-Shirt" #: includes/pages/admin_active.php:299 includes/pages/admin_arrive.php:202 #: includes/pages/admin_arrive.php:217 includes/pages/admin_arrive.php:232 @@ -1031,7 +1031,7 @@ msgid "T-shirt?" msgstr "T-Shirt?" #: includes/pages/admin_active.php:324 -msgid "Shirt statistics" +msgid "Shirt statistic" msgstr "T-Shirt Statistik" #: includes/pages/admin_active.php:327 @@ -2753,6 +2753,40 @@ msgstr "Passwort zurückgesetzt." msgid "The entries must match and must not be empty!" msgstr "Die Einträge müssen übereinstimmen und dürfen nicht leer sein!" +msgid "Number of shifts: %s" +msgstr "Anzahl an Schichten: %s" + +msgid "If the angel is active, it can claim a goodie. If goodie is set to 'Yes', the angel already got their goodie." +msgstr "Ist der Engel Aktiv, hat er damit Anspruch auf ein Goodie. Wenn Goodie ein 'Ja' enthält, bedeutet dies, " +"dass der Engel bereits sein Goodie erhalten hat." + +msgid "Goodie" +msgstr "Goodie" + +msgid "Your goodie score" +msgstr "Dein Goodie Score" + +msgid "Given goodies" +msgstr "Ausgegebene Goodies" + +msgid "Goodie statistic" +msgstr "Goodie Statistik" + +msgid "remove goodie" +msgstr "entferne Goodie" + +msgid "got goodie" +msgstr "Goodie bekommen" + +msgid "Goodie?" +msgstr "Goodie?" + +msgid "Angel has got a goodie." +msgstr "Engel hat ein Goodie bekommen." + +msgid "Angel has got no goodie." +msgstr "Engel hat kein Goodie bekommen." + #: src/Middleware/LegacyMiddleware.php:83 msgid "page.404.text" msgstr "" @@ -3091,6 +3125,9 @@ msgstr "Antwort" msgid "user.edit.shirt" msgstr "Shirt bearbeiten" +msgid "user.edit.goodie" +msgstr "Goodie bearbeiten" + msgid "form.shirt" msgstr "Shirt" @@ -3109,6 +3146,9 @@ msgstr "Angekommen" msgid "user.got_shirt" msgstr "Shirt bekommen" +msgid "user.got_goodie" +msgstr "Goodie bekommen" + msgid "message.title" msgstr "Nachrichten" @@ -3147,6 +3187,3 @@ msgstr "Arbeitseinsatz löschen" msgid "worklog.delete.info" msgstr "Möchtest du den Arbeitseinsatz von %s wirklich löschen?" - -msgid "Number of shifts: %s" -msgstr "Anzahl an Schichten: %s" diff --git a/resources/views/admin/user/edit-shirt.twig b/resources/views/admin/user/edit-shirt.twig index 5c46ac55..885f56ad 100644 --- a/resources/views/admin/user/edit-shirt.twig +++ b/resources/views/admin/user/edit-shirt.twig @@ -2,7 +2,13 @@ {% import 'macros/base.twig' as m %} {% import 'macros/form.twig' as f %} -{% block title %}{{ __('user.edit.shirt') }}{% endblock %} +{% block title %} + {% if config('other_goodie') %} + {{ __('user.edit.goodie') }} + {% else %} + {{ __('user.edit.shirt') }} + {% endif %} +{% endblock %} {% block content %}
@@ -14,9 +20,11 @@ {{ csrf() }}
-
- {{ f.select('shirt_size', config('tshirt_sizes'), __('user.shirt_size'), userdata.personalData.shirt_size) }} -
+ {% if not config('other_goodie') %} +
+ {{ f.select('shirt_size', config('tshirt_sizes'), __('user.shirt_size'), userdata.personalData.shirt_size) }} +
+ {% endif %}
{{ f.switch('arrived', __('user.arrived'), userdata.state.arrived, {'disabled': not has_permission_to('admin_arrive')}) }} @@ -26,7 +34,11 @@ {{ f.switch('active', __('user.active'), userdata.state.active) }} - {{ f.switch('got_shirt', __('user.got_shirt'), userdata.state.got_shirt) }} + {% if config('other_goodie') %} + {{ f.switch('got_shirt', __('user.got_goodie'), userdata.state.got_shirt) }} + {% else %} + {{ f.switch('got_shirt', __('user.got_shirt'), userdata.state.got_shirt) }} + {% endif %}
{{ f.submit(__('form.save')) }} diff --git a/resources/views/pages/settings/profile.twig b/resources/views/pages/settings/profile.twig index 8aaeff8e..8fa87421 100644 --- a/resources/views/pages/settings/profile.twig +++ b/resources/views/pages/settings/profile.twig @@ -138,7 +138,7 @@ {% endif %}
- {% if config('enable_tshirt_size') %} + {% if config('enable_tshirt_size') and not config('other_goodie') %}
{{ f.select( 'shirt_size', diff --git a/src/Controllers/Admin/UserShirtController.php b/src/Controllers/Admin/UserShirtController.php index ebc94c85..a35524fb 100644 --- a/src/Controllers/Admin/UserShirtController.php +++ b/src/Controllers/Admin/UserShirtController.php @@ -47,18 +47,19 @@ class UserShirtController extends BaseController public function saveShirt(Request $request): Response { $userId = (int) $request->getAttribute('user_id'); + $shirtEnabled = !$this->config->get('other_goodie'); /** @var User $user */ $user = $this->user->findOrFail($userId); $data = $this->validate($request, [ - 'shirt_size' => 'required', + 'shirt_size' => $shirtEnabled ? 'required' : 'optional', 'arrived' => 'optional|checked', 'active' => 'optional|checked', 'got_shirt' => 'optional|checked', ]); - if (isset($this->config->get('tshirt_sizes')[$data['shirt_size']])) { + if ($shirtEnabled && isset($this->config->get('tshirt_sizes')[$data['shirt_size']])) { $user->personalData->shirt_size = $data['shirt_size']; $user->personalData->save(); } diff --git a/src/Controllers/SettingsController.php b/src/Controllers/SettingsController.php index 54f3e9a0..382e9eaf 100644 --- a/src/Controllers/SettingsController.php +++ b/src/Controllers/SettingsController.php @@ -88,7 +88,11 @@ class SettingsController extends BaseController $user->settings->email_goody = $data['email_goody'] ?: false; } - if (config('enable_tshirt_size') && isset(config('tshirt_sizes')[$data['shirt_size']])) { + if ( + (config('enable_tshirt_size') + && !config('other_goodie')) + && isset(config('tshirt_sizes')[$data['shirt_size']]) + ) { $user->personalData->shirt_size = $data['shirt_size']; } @@ -275,7 +279,7 @@ class SettingsController extends BaseController $rules['planned_arrival_date'] = 'required|date:Y-m-d'; $rules['planned_departure_date'] = 'optional|date:Y-m-d'; } - if (config('enable_tshirt_size')) { + if (config('enable_tshirt_size') && !config('other_goodie')) { $rules['shirt_size'] = 'required'; } return $rules; diff --git a/tests/Unit/Controllers/Admin/UserShirtControllerTest.php b/tests/Unit/Controllers/Admin/UserShirtControllerTest.php index 0217e14a..a0526817 100644 --- a/tests/Unit/Controllers/Admin/UserShirtControllerTest.php +++ b/tests/Unit/Controllers/Admin/UserShirtControllerTest.php @@ -68,7 +68,7 @@ class UserShirtControllerTest extends ControllerTest ]); /** @var Authenticator|MockObject $auth */ $auth = $this->createMock(Authenticator::class); - $this->config->set('tshirt_sizes', ['S' => 'Small']); + $this->config->set('tshirt_sizes', ['S' => 'Small', 'XS' => 'Extra Small']); /** @var Redirector|MockObject $redirector */ $redirector = $this->createMock(Redirector::class); User::factory() @@ -77,11 +77,11 @@ class UserShirtControllerTest extends ControllerTest ->create(); $auth - ->expects($this->exactly(4)) + ->expects($this->exactly(5)) ->method('can') ->with('admin_arrive') - ->willReturnOnConsecutiveCalls(true, true, true, false); - $this->setExpects($redirector, 'back', null, $this->response, $this->exactly(4)); + ->willReturnOnConsecutiveCalls(true, true, true, false, true); + $this->setExpects($redirector, 'back', null, $this->response, $this->exactly(5)); $controller = new UserShirtController( $auth, @@ -142,6 +142,17 @@ class UserShirtControllerTest extends ControllerTest $controller->saveShirt($request); $user = User::find(1); $this->assertFalse($user->state->arrived); + + // Shirt disabled + $this->config->set('other_goodie'); + $request = $request + ->withParsedBody([ + 'shirt_size' => 'XS', + ]); + + $controller->saveShirt($request); + $user = User::find(1); + $this->assertEquals('XS', $user->personalData->shirt_size); } /**