diff --git a/assets/css/style.css b/assets/css/style.css index b5de946..ec77635 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -202,11 +202,25 @@ body { color: white; } + .main .messageBox .message a { color: inherit; text-decoration: none; } +img.dialogImage { + max-width: 300px; + max-height: 300px; +} + +a.fileLink { + text-decoration: underline !important; +} + +a.fileLink:before { + content: "📂 " +} + .main .messageBox .message.fromCompanion:before { content: ""; border: solid transparent; diff --git a/core/lib/Core/Models/Dialog.class.php b/core/lib/Core/Models/Dialog.class.php index 6439b10..3795aa3 100644 --- a/core/lib/Core/Models/Dialog.class.php +++ b/core/lib/Core/Models/Dialog.class.php @@ -14,6 +14,15 @@ /** @var string Таблица с сообщениями */ const TABLE_MESSAGES = DB_TABLE_PREFIX . 'messages'; + /** @var string MESSAGE_TYPE_TEXT Тип сообщения: текст */ + const MESSAGE_TYPE_TEXT = 'text'; + + /** @var string MESSAGE_TYPE_FILE Тип сообщения: файл */ + const MESSAGE_TYPE_FILE = 'file'; + + /** @var string MESSAGE_TYPE_IMAGE Тип сообщения: изображение */ + const MESSAGE_TYPE_IMAGE = 'image'; + /** * @var User $user Объект пользователя */ @@ -139,11 +148,11 @@ } /** - * Отправка сообщения + * Отправка тестовых сообщения * * @throws CoreException */ - public function sendMessage(int $to, string $message) + public function sendMessage(int $to, string $message): bool { if (!User::isUserExistsByParams(['id' => $to])) { throw new CoreException('Пользователь с идентификатором ' . $to . ' отсутствует в базе', CoreException::ERROR_USER_NOT_FOUND); @@ -157,7 +166,33 @@ $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]); } - $result = $DB->addItem(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'user_from' => $this->user->getId(), 'user_to' => $to, 'text' => $message]); + $result = $DB->addItem(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'type' => self::MESSAGE_TYPE_TEXT, 'user_from' => $this->user->getId(), 'user_to' => $to, 'text' => $message]); + return (int)$result > 0; + } + + /** + * Отправка файлов + * + * @param int $fileId + * + * + * @throws CoreException + */ + public function sendFile(int $to, int $fileId): bool + { + if (!User::isUserExistsByParams(['id' => $to])) { + throw new CoreException('Пользователь с идентификатором ' . $to . ' отсутствует в базе', CoreException::ERROR_USER_NOT_FOUND); + } + /** @var $DB DB Объект базы данных */ + $DB = DB::getInstance(); + $dialogId = self::getDialogId($this->user->getId(), $to); + if (empty($dialogId)) { + $dialogId = $this->createDialog($to); + } else { + $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]); + } + + $result = $DB->addItem(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'type' => self::MESSAGE_TYPE_FILE, 'user_from' => $this->user->getId(), 'user_to' => $to, 'text' => $fileId]); return (int)$result > 0; } diff --git a/core/lib/Core/Models/File.class.php b/core/lib/Core/Models/File.class.php index ff26fe1..9ab3ac6 100644 --- a/core/lib/Core/Models/File.class.php +++ b/core/lib/Core/Models/File.class.php @@ -70,7 +70,7 @@ { $this->folder = $_SERVER['DOCUMENT_ROOT'] . self::FOLDER; $this->DB = DB::getInstance(); - if (!empty($id)) { + if (!empty($id) && $id > 0) { $this->id = $id; $this->loadFileProps(); } diff --git a/core/lib/Core/Models/User.class.php b/core/lib/Core/Models/User.class.php index 902ec95..ea4c53f 100644 --- a/core/lib/Core/Models/User.class.php +++ b/core/lib/Core/Models/User.class.php @@ -28,6 +28,7 @@ use Core\CoreException; use Core\Helpers\{Cache, Log, Mail, Sanitize, SystemFunctions}; use Core\DataBases\DB; + use Throwable; class User { @@ -925,6 +926,14 @@ foreach ($messages as $key => $message) { $messages[$key]['user_from_data'] = (new self((int)$message['user_from']))->getAllUserData(true); $messages[$key]['user_to_data'] = (new self((int)$message['user_to']))->getAllUserData(true); + if ($message['type'] !== Dialog::MESSAGE_TYPE_TEXT) { + $messages[$key]['file'] = (new File((int)$message['text']))->getAllProps(); + if ($messages[$key]['file'] === null) { + $messages[$key]['file']['error'] = true; + } else { + $messages[$key]['file']['error'] = false; + } + } } return $messages; diff --git a/db/migrations/20230405185037_users.php b/db/migrations/20230405185037_users.php index 176e934..9720024 100644 --- a/db/migrations/20230405185037_users.php +++ b/db/migrations/20230405185037_users.php @@ -5,6 +5,8 @@ use Phinx\Migration\AbstractMigration; final class Users extends AbstractMigration { + private $table; + private $data; public function init(): void { diff --git a/db/migrations/20230405185040_roles.php b/db/migrations/20230405185040_roles.php index 3d98daa..5f156b0 100644 --- a/db/migrations/20230405185040_roles.php +++ b/db/migrations/20230405185040_roles.php @@ -5,6 +5,8 @@ use Phinx\Migration\AbstractMigration; final class Roles extends AbstractMigration { + private $table; + private $data; public function init(): void { $this->table = 'd_roles'; diff --git a/db/migrations/20230405185041_user_roles.php b/db/migrations/20230405185041_user_roles.php index e319a98..b30e0d7 100644 --- a/db/migrations/20230405185041_user_roles.php +++ b/db/migrations/20230405185041_user_roles.php @@ -5,6 +5,9 @@ use Phinx\Migration\AbstractMigration; final class UserRoles extends AbstractMigration { + private $table; + private $data; + public function init(): void { $this->table = 'd_user_roles'; diff --git a/db/migrations/20230405185044_user_meta.php b/db/migrations/20230405185044_user_meta.php index 209c838..8f19ca7 100644 --- a/db/migrations/20230405185044_user_meta.php +++ b/db/migrations/20230405185044_user_meta.php @@ -5,6 +5,8 @@ use Phinx\Migration\AbstractMigration; final class UserMeta extends AbstractMigration { + private $table; + public function init(): void { $this->table = 'd_user_meta'; diff --git a/db/migrations/20230405185104_threads.php b/db/migrations/20230405185104_threads.php index 6406c38..69c714d 100644 --- a/db/migrations/20230405185104_threads.php +++ b/db/migrations/20230405185104_threads.php @@ -5,6 +5,8 @@ use Phinx\Migration\AbstractMigration; final class Threads extends AbstractMigration { + private $table; + public function init() { $this->table = 'd_threads'; diff --git a/db/migrations/20230405185109_threads_history.php b/db/migrations/20230405185109_threads_history.php index 95e978a..994d467 100644 --- a/db/migrations/20230405185109_threads_history.php +++ b/db/migrations/20230405185109_threads_history.php @@ -5,6 +5,8 @@ use Phinx\Migration\AbstractMigration; final class ThreadsHistory extends AbstractMigration { + private $table; + public function init() { $this->table = 'd_threads_history'; diff --git a/db/migrations/20230405185125_files.php b/db/migrations/20230405185125_files.php index e663565..260a0ce 100644 --- a/db/migrations/20230405185125_files.php +++ b/db/migrations/20230405185125_files.php @@ -5,12 +5,16 @@ use Phinx\Migration\AbstractMigration; final class Files extends AbstractMigration { + private $table; + private $data; + public function init() { $this->table = 'd_files'; $this->data = [ [ 'name' => 'system.png', + 'size' => 1024, 'path' => '/uploads/users/system.png', ], ]; @@ -20,6 +24,7 @@ final class Files extends AbstractMigration { $table = $this->table($this->table); $table->addColumn('name', 'string', ['limit' => 255, 'default' => null]) + ->addColumn('size', 'integer', ['signed' => false, 'null' => true, 'default' => null]) ->addColumn('path', 'text', ['default' => null]) ->create(); $table->insert($this->data); diff --git a/db/migrations/20230503194401_mail_history.php b/db/migrations/20230503194401_mail_history.php index 2d10042..9600796 100644 --- a/db/migrations/20230503194401_mail_history.php +++ b/db/migrations/20230503194401_mail_history.php @@ -5,6 +5,8 @@ use Phinx\Migration\AbstractMigration; final class MailHistory extends AbstractMigration { + private $table; + public function init() { $this->table = 'd_mail_history'; diff --git a/db/migrations/20230514002045_utm_history.php b/db/migrations/20230514002045_utm_history.php index 177430c..c1d3660 100644 --- a/db/migrations/20230514002045_utm_history.php +++ b/db/migrations/20230514002045_utm_history.php @@ -5,6 +5,8 @@ final class UtmHistory extends AbstractMigration { + private $table; + public function init() { $this->table = 'd_utm_history'; diff --git a/templates/dialog.twig b/templates/dialog.twig index edbbc13..be4c3d1 100644 --- a/templates/dialog.twig +++ b/templates/dialog.twig @@ -8,9 +8,27 @@ {% for message in messages %}
{{ message.user_from_data.name }}
-
{{ message.text }}
+
+ {% if message.type == 'text' %} + {{ message.text }} + {% elseif message.type == 'image' %} + {% if message.file.error == false %} + + {{ message.file.name }} + + {% else %} + Некорректное изображение + {% endif %} + {% elseif message.type == 'file' %} + {% if message.file.error == false %} + {{ message.file.name }} + {% else %} + Некорректный файл + {% endif %} + {% endif %} +
{{ message.date }}
- {% if message.user_from == userId%} + {% if message.user_from == userId %}
 
{% endif %}