From a78659b2d49e1f5da65d8e924530da2c52f92a58 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Thu, 12 Dec 2019 21:31:09 +0100 Subject: [PATCH] Migrate message view --- includes/includes.php | 1 - includes/model/Message_model.php | 56 ------------------------ includes/pages/user_messages.php | 75 ++++++++++++++------------------ resources/lang/de_DE/default.po | 4 ++ 4 files changed, 36 insertions(+), 100 deletions(-) delete mode 100644 includes/model/Message_model.php diff --git a/includes/includes.php b/includes/includes.php index 50e98fe5..7ec1f768 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -12,7 +12,6 @@ $includeFiles = [ __DIR__ . '/../includes/sys_template.php', __DIR__ . '/../includes/model/AngelType_model.php', - __DIR__ . '/../includes/model/Message_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/Room_model.php', __DIR__ . '/../includes/model/ShiftEntry_model.php', diff --git a/includes/model/Message_model.php b/includes/model/Message_model.php deleted file mode 100644 index 640cb085..00000000 --- a/includes/model/Message_model.php +++ /dev/null @@ -1,56 +0,0 @@ -user(); - $receiver = User::find($receiver_user_id); - - if (empty($text) || !$receiver || $receiver->id == $user->id) { - return false; - } - - return DB::insert(' - INSERT INTO `Messages` (`Datum`, `SUID`, `RUID`, `Text`) - VALUES(?, ?, ?, ?) - ', - [ - time(), - $user->id, - $receiver->id, - $text - ] - ); -} diff --git a/includes/pages/user_messages.php b/includes/pages/user_messages.php index 5ec6fdfb..2485514d 100644 --- a/includes/pages/user_messages.php +++ b/includes/pages/user_messages.php @@ -1,6 +1,6 @@ user(); if ($user) { - $new_messages = count(DB::select( - 'SELECT `id` FROM `Messages` WHERE isRead=\'N\' AND `RUID`=?', - [$user->id] - )); + $new_messages = $user->messagesReceived() + ->where('read', false) + ->count(); + if ($new_messages > 0) { return ' ' . $new_messages . ''; } @@ -57,23 +57,12 @@ function user_messages() $to_select = html_select_key('to', 'to', $to_select_data, ''); - $messages = DB::select(' - SELECT * - FROM `Messages` - WHERE `SUID`=? - OR `RUID`=? - ORDER BY `isRead`,`Datum` DESC - ', - [ - $user->id, - $user->id, - ] - ); + $messages = $user->messages; $messages_table = [ [ 'news' => '', - 'timestamp' => date('Y-m-d H:i'), + 'timestamp' => date(__('Y-m-d H:i')), 'from' => User_Nick_render($user), 'to' => $to_select, 'text' => form_textarea('text', '', ''), @@ -82,28 +71,28 @@ function user_messages() ]; foreach ($messages as $message) { - $sender_user_source = User::find($message['SUID']); - $receiver_user_source = User::find($message['RUID']); + $sender_user_source = $message->user; + $receiver_user_source = $message->receiver; $messages_table_entry = [ - 'new' => $message['isRead'] == 'N' ? '' : '', - 'timestamp' => date('Y-m-d H:i', $message['Datum']), + 'new' => !$message->read ? '' : '', + 'timestamp' => $message->created_at->format(__('Y-m-d H:i')), 'from' => User_Nick_render($sender_user_source), 'to' => User_Nick_render($receiver_user_source), - 'text' => nl2br(htmlspecialchars($message['Text'])) + 'text' => nl2br(htmlspecialchars($message->text)) ]; - if ($message['RUID'] == $user->id) { - if ($message['isRead'] == 'N') { + if ($message->receiver_id == $user->id) { + if (!$message->read) { $messages_table_entry['actions'] = button( - page_link_to('user_messages', ['action' => 'read', 'id' => $message['id']]), + page_link_to('user_messages', ['action' => 'read', 'id' => $message->id]), __('mark as read'), 'btn-xs' ); } } else { $messages_table_entry['actions'] = button( - page_link_to('user_messages', ['action' => 'delete', 'id' => $message['id']]), + page_link_to('user_messages', ['action' => 'delete', 'id' => $message->id]), __('delete message'), 'btn-xs' ); @@ -134,15 +123,10 @@ function user_messages() return error(__('Incomplete call, missing Message ID.'), true); } - $message = DB::selectOne( - 'SELECT `RUID` FROM `Messages` WHERE `id`=? LIMIT 1', - [$message_id] - ); - if (!empty($message) && $message['RUID'] == $user->id) { - DB::update( - 'UPDATE `Messages` SET `isRead`=\'Y\' WHERE `id`=? LIMIT 1', - [$message_id] - ); + $message = Message::find($message_id); + if ($message !== null && $message->receiver_id == $user->id) { + $message->read = true; + $message->save(); throw_redirect(page_link_to('user_messages')); } else { return error(__('No Message found.'), true); @@ -156,12 +140,9 @@ function user_messages() return error(__('Incomplete call, missing Message ID.'), true); } - $message = DB::selectOne( - 'SELECT `SUID` FROM `Messages` WHERE `id`=? LIMIT 1', - [$message_id] - ); - if (!empty($message) && $message['SUID'] == $user->id) { - DB::delete('DELETE FROM `Messages` WHERE `id`=? LIMIT 1', [$message_id]); + $message = Message::find($message_id); + if ($message !== null && $message->user_id == $user->id) { + $message->delete(); throw_redirect(page_link_to('user_messages')); } else { return error(__('No Message found.'), true); @@ -169,7 +150,15 @@ function user_messages() break; case 'send': - if (Message_send($request->input('to'), $request->input('text'))) { + $receiver = User::find($request->input('to')); + $text = $request->input('text'); + + if ($receiver !== null && !empty($text)) { + Message::create([ + 'user_id' => $user->id, + 'receiver_id' => $request->input('to'), + 'text' => $request->input('text') + ]); throw_redirect(page_link_to('user_messages')); } else { return error(__('Transmitting was terminated with an Error.'), true); diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 70160f01..a389f478 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -1696,6 +1696,10 @@ msgstr "Nachrichten" msgid "Select recipient..." msgstr "Empfänger auswählen..." +#: includes/pages/user_messages.php:61 includes/pages/user_messages.php:75 +msgid "Y-m-d H:i" +msgstr "d.m.Y H:i" + #: includes/pages/user_messages.php:98 msgid "mark as read" msgstr "als gelesen markieren"