From 96277dcfc44a2eba5edb9ce53b563bfd557373c4 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 3 Jun 2022 21:46:16 +0200 Subject: [PATCH] Added transaction id to shifts for bulk deletion --- ...06_03_000000_shifts_add_transaction_id.php | 39 ++++++++ includes/model/Shifts_model.php | 15 ++- includes/pages/admin_shifts.php | 99 ++++++++++++++++++- resources/lang/de_DE/default.po | 12 +++ src/Middleware/LegacyMiddleware.php | 3 + tests/Unit/HasDatabase.php | 1 + 6 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 db/migrations/2022_06_03_000000_shifts_add_transaction_id.php diff --git a/db/migrations/2022_06_03_000000_shifts_add_transaction_id.php b/db/migrations/2022_06_03_000000_shifts_add_transaction_id.php new file mode 100644 index 00000000..79a3c256 --- /dev/null +++ b/db/migrations/2022_06_03_000000_shifts_add_transaction_id.php @@ -0,0 +1,39 @@ +schema->hasTable('Shifts')) { + return; + } + + $this->schema->table('Shifts', function (Blueprint $table) { + $table->unsignedInteger('transaction_id')->nullable()->default(null); + }); + } + + /** + * Reverse the migration + */ + public function down(): void + { + if (!$this->schema->hasTable('Shifts')) { + return; + } + + $this->schema->table('Shifts', function (Blueprint $table) { + $table->dropColumn('transaction_id'); + }); + } +} diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 2717ebcb..9c70ec72 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -556,13 +556,22 @@ function Shift_update($shift) ); } +/** + * Get the next free shifts transaction id + */ +function Shift_get_next_transaction_id(): int +{ + return Db::selectOne('SELECT MAX(transaction_id) + 1 AS transaction_id FROM Shifts')['transaction_id'] ?? 1; +} + /** * Create a new shift. * * @param array $shift + * @param int $transactionId * @return int ID of the new created shift */ -function Shift_create($shift) +function Shift_create($shift, $transactionId = null) { DB::insert(' INSERT INTO `Shifts` ( @@ -573,11 +582,12 @@ function Shift_create($shift) `title`, `description`, `URL`, + `transaction_id`, `created_by_user_id`, `edited_at_timestamp`, `created_at_timestamp` ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ', [ $shift['shifttype_id'], @@ -587,6 +597,7 @@ function Shift_create($shift) $shift['title'], $shift['description'], $shift['URL'], + $transactionId, auth()->user()->id, time(), time(), diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index adb8c66e..ec1f79c6 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -2,7 +2,9 @@ use Engelsystem\Database\Db; use Engelsystem\Helpers\Carbon; +use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Models\Room; +use Engelsystem\Models\User\User; /** * @return string @@ -363,9 +365,10 @@ function admin_shifts() throw_redirect(page_link_to('admin_shifts')); } + $transactionId = Shift_get_next_transaction_id(); foreach ($session->get('admin_shifts_shifts', []) as $shift) { $shift['URL'] = null; - $shift_id = Shift_create($shift); + $shift_id = Shift_create($shift, $transactionId); engelsystem_log( 'Shift created: ' . $shifttypes[$shift['shifttype_id']] @@ -424,7 +427,12 @@ function admin_shifts() . ''; } - return page_with_title(admin_shifts_title(), [ + return page_with_title( + admin_shifts_title() . ' ' . sprintf( + '%s', + page_link_to('admin_shifts_history'), + icon('clock-history') + ), [ msg(), form([ div('row',[ @@ -494,3 +502,90 @@ function admin_shifts() ]) ]); } + +/** + * @return string + */ +function admin_shifts_history_title(): string +{ + return __('Shifts history'); +} + +/** + * Display shifts transaction history + * + * @return string + */ +function admin_shifts_history(): string +{ + if (!auth()->can('admin_shifts')) { + throw new HttpForbidden(); + } + + $request = request(); + $transactionId = $request->postData('transaction_id'); + if ($request->hasPostData('delete') && $transactionId) { + $shifts = Db::select(' + SELECT SID + FROM Shifts + WHERE transaction_id = ? + ', [$transactionId]); + + engelsystem_log('Deleting ' . count($shifts) . ' shifts (transaction id ' . $transactionId . ')'); + + foreach ($shifts as $shift) { + $shift = Shift($shift['SID']); + UserWorkLog_from_shift($shift); + shift_delete($shift['SID']); + + engelsystem_log( + 'Deleted shift ' . $shift['name'] + . ' from ' . date('Y-m-d H:i', $shift['start']) + . ' to ' . date('Y-m-d H:i', $shift['end']) + ); + } + + success(sprintf(__('%s shifts deleted.'), count($shifts))); + throw_redirect(page_link_to('admin_shifts_history')); + } + + $shifts = Db::select(' + SELECT + transaction_id, + title, + COUNT(SID) AS count, + MIN(start) AS start, + MAX(end) AS end, + created_by_user_id AS user_id, + created_at_timestamp AS created_at + FROM Shifts + WHERE transaction_id IS NOT NULL + GROUP BY transaction_id + ORDER BY transaction_id DESC + '); + + foreach ($shifts as &$shift) { + $shift['user'] = User_Nick_render(User::find($shift['user_id'])); + $shift['start'] = Carbon::createFromTimestamp($shift['start'])->format(__('Y-m-d H:i')); + $shift['end'] = Carbon::createFromTimestamp($shift['end'])->format(__('Y-m-d H:i')); + $shift['created_at'] = Carbon::createFromTimestamp($shift['created_at'])->format(__('Y-m-d H:i')); + $shift['actions'] = form([ + form_hidden('transaction_id', $shift['transaction_id']), + form_submit('delete', icon('trash') . __('delete all'), 'btn-sm', true, 'danger'), + ]); + } + + return page_with_title(admin_shifts_history_title(), [ + msg(), + table([ + 'transaction_id' => __('ID'), + 'title' => __('Title'), + 'count' => __('Count'), + 'start' => __('Start'), + 'end' => __('End'), + 'user' => __('User'), + 'created_at' => __('Created'), + 'actions' => '' + ], $shifts) + ], true); +} diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index c71ca20e..8f7d0e4f 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -531,6 +531,18 @@ msgstr "Benötigte Engel" msgid "Shift deleted." msgstr "Schicht gelöscht." +msgid "Shifts history" +msgstr "Schichten historie" + +msgid "%s shifts deleted." +msgstr "%s Schichten gelöscht." + +msgid "Created" +msgstr "Erstellt" + +msgid "delete all" +msgstr "alle löschen" + #: includes/controller/shifts_controller.php:238 #, php-format msgid "Do you want to delete the shift %s from %s to %s?" diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index 4ed9e122..e86a818a 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -27,6 +27,7 @@ class LegacyMiddleware implements MiddlewareInterface 'users', 'user_driver_licenses', 'user_worklog', + 'admin_shifts_history', ]; /** @var ContainerInterface */ @@ -176,6 +177,8 @@ class LegacyMiddleware implements MiddlewareInterface $title = admin_shifts_title(); $content = admin_shifts(); return [$title, $content]; + case 'admin_shifts_history': + return [admin_shifts_history_title(), admin_shifts_history()]; } throw_redirect(page_link_to('login')); diff --git a/tests/Unit/HasDatabase.php b/tests/Unit/HasDatabase.php index ef60ffc0..359198a1 100644 --- a/tests/Unit/HasDatabase.php +++ b/tests/Unit/HasDatabase.php @@ -57,6 +57,7 @@ trait HasDatabase ['migration' => '2021_10_12_000000_add_shifts_description'], ['migration' => '2021_12_30_000000_remove_admin_news_html_privilege'], ['migration' => '2022_06_02_000000_create_voucher_edit_permission'], + ['migration' => '2022_06_03_000000_shifts_add_transaction_id'], ] );