diff --git a/assets/css/style.css b/assets/css/style.css index 577188f..b5de946 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -175,10 +175,18 @@ body { .main .messageBox { display: flex; flex-direction: column; + + /*подумать + overflow-y: scroll; + overflow-x: hidden; + height: 300px; + */ } .main .messageBox .message { - width: 300px; + width: fit-content; + min-width: 300px; + max-width: 80%; padding: 20px 10px; position: relative; background: #eee; @@ -194,6 +202,11 @@ body { color: white; } +.main .messageBox .message a { + color: inherit; + text-decoration: none; +} + .main .messageBox .message.fromCompanion:before { content: ""; border: solid transparent; @@ -221,6 +234,7 @@ body { font-weight: bold; position: absolute; top: 5px; + text-decoration: none; } .main .messageBox .message .text { @@ -244,4 +258,17 @@ body { .main .header .mainMenu a { color: white; text-decoration: none; -} \ No newline at end of file +} + +.deliveryStatus { + background: #cecece; + width: 10px; + height: 10px; + border-radius: 45px; + position: absolute; + right: 8px; + top: 8px; +} +.deliveryStatus.viewed { + background: #00f700; +} diff --git a/core/bootstrap.php b/core/bootstrap.php index 141382a..4b89e4e 100644 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -33,6 +33,8 @@ } date_default_timezone_set('Europe/Moscow'); + define('START_MEMORY', memory_get_usage()); + require_once __DIR__ . '/../vendor/autoload.php'; \Sentry\init( [ diff --git a/core/lib/Core/App.class.php b/core/lib/Core/App.class.php index 61516a1..92311f4 100644 --- a/core/lib/Core/App.class.php +++ b/core/lib/Core/App.class.php @@ -36,6 +36,7 @@ */ private static function getLayoutParams(): array { + /** @var User $USER */ global $USER; $finish_time = microtime(true); @@ -45,10 +46,12 @@ } $currentPage = explode('::', Registry::get('currentPage'))[1]; + $memoryUse = memory_get_usage() - START_MEMORY; return [ 'salt' => md5(random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999)), 'currentPage' => $currentPage, + 'memoryUse' => SystemFunctions::convertBytes($memoryUse), 'executionTime' => $delta . ' cek.', 'isAuthorized' => User::isAuthorized(), 'isAdmin' => User::isAuthorized() && $USER->isAdmin(), @@ -93,12 +96,14 @@ public static function dialogs() { + /** @var User $USER */ global $USER; self::render('dialogs.twig', ['dialogs' => User::isAuthorized() ? $USER->getDialogs() : []]); } public static function dialog(int $id) { + /** @var User $USER */ global $USER; self::render( 'dialog.twig', @@ -114,6 +119,7 @@ public static function sendMessage(int $userId) { + /** @var User $USER */ global $USER; $USER->getDialogObject()->sendMessage($userId, $_REQUEST['message']); header('Location: /dialog/' . (int)$_REQUEST['dialogId']); @@ -168,4 +174,15 @@ $userData = (new User($id))->getAllUserData(true); self::render('userProfile.twig', ['userData' => $userData]); } + + public static function goToDialog(int $userId) + { + /** @var User $USER */ + global $USER; + $dialogId = $USER->getDialogObject()->getDialogId($USER->getId(), $userId); + if (empty($dialogId)) { + $dialogId = $USER->getDialogObject()->createDialog($userId); + } + header('Location: /dialog/' . $dialogId); + } } \ No newline at end of file diff --git a/core/lib/Core/DataBases/DB.class.php b/core/lib/Core/DataBases/DB.class.php index 1a7586b..50c284e 100644 --- a/core/lib/Core/DataBases/DB.class.php +++ b/core/lib/Core/DataBases/DB.class.php @@ -499,6 +499,29 @@ return null; } + /** + * Получение количества элементов + * + * @param string $table Имя таблицы + * @param array|string $where Массив where + * + * @return int Количество элементов + */ + public function getCountItems(string $table, $where): int + { + self::$quantity++; + $count = 0; + try { + $result = $this->query('SELECT COUNT(*) as count FROM `' . $table . '` WHERE ' . $this->createWhere($where)); + } catch (Throwable $e) { + $result = false; + } + if ($result) { + $count = (int)$result[0]['count']; + } + return $count; + } + /** * Получить все данные из таблицы diff --git a/core/lib/Core/Helpers/SystemFunctions.class.php b/core/lib/Core/Helpers/SystemFunctions.class.php index 80e73ad..eb0d88b 100644 --- a/core/lib/Core/Helpers/SystemFunctions.class.php +++ b/core/lib/Core/Helpers/SystemFunctions.class.php @@ -71,15 +71,15 @@ $size = str_replace('.', ',', round($size, 2)); switch ($i) { case 0: - return $size .= ' Б'; + return $size .= ' b'; case 1: - return $size .= ' КБ'; + return $size .= ' Kb'; case 2: - return $size .= ' МБ'; + return $size .= ' Mb'; case 3: - return $size .= ' ГБ'; + return $size .= ' Gb'; case 4: - return $size .= 'ТБ'; + return $size .= 'Tb'; } return 'Неизвестно'; } diff --git a/core/lib/Core/Models/Dialog.class.php b/core/lib/Core/Models/Dialog.class.php index e600a37..6439b10 100644 --- a/core/lib/Core/Models/Dialog.class.php +++ b/core/lib/Core/Models/Dialog.class.php @@ -33,7 +33,7 @@ * @return int|null * @throws CoreException */ - private static function getDialogId(int $userOne, int $userTwo): ?int + public static function getDialogId(int $userOne, int $userTwo): ?int { $DB = DB::getInstance(); $dialogId = $DB->query( @@ -53,12 +53,23 @@ * @return void * @throws CoreException */ - private static function markDialogViewed(int $dialogId): void + private function markDialogViewed(int $dialogId): void { + /** @var $DB DB Объект базы данных */ $DB = DB::getInstance(); - $dialogId = $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed' => CODE_VALUE_Y]); + $dialogData = $DB->getItem(self::TABLE_DIALOGS, ['id' => $dialogId]); + if ($this->user->getId() === (int)$dialogData['receive']) { + $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed' => CODE_VALUE_Y]); + $DB->update(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'user_to' => $this->user->getId(), 'viewed' => CODE_VALUE_N], ['viewed' => CODE_VALUE_Y]); + } } + public function createDialog(int $userId): ?int + { + /** @var $DB DB Объект базы данных */ + $DB = DB::getInstance(); + return $DB->addItem(self::TABLE_DIALOGS, ['viewed' => CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $userId]); + } /** * Получение диалогов * @@ -119,7 +130,7 @@ $DB = DB::getInstance(); $messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '"'); if ($markDialogViewed) { - self::markDialogViewed($dialogId); + $this->markDialogViewed($dialogId); } if (empty($messages)) { return []; @@ -141,13 +152,28 @@ $DB = DB::getInstance(); $dialogId = self::getDialogId($this->user->getId(), $to); if (empty($dialogId)) { - $dialogId = $DB->addItem(self::TABLE_DIALOGS, ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]); + $dialogId = $this->createDialog($to); } else { $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]); } - $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, 'user_from' => $this->user->getId(), 'user_to' => $to, 'text' => $message]); + return (int)$result > 0; } + /** + * Получение количества непрочитанных сообщений в диалоге + * + * @param int $dialogId Идентификатор диалога + * + * @return int Количество + */ + public function getDialogUnviewedMessagesCount(int $dialogId): int + { + /** @var $DB DB Объект базы данных */ + $DB = DB::getInstance(); + return $DB->getCountItems(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'viewed' => CODE_VALUE_N, 'user_to' => $this->user->getId()]); + } + + } diff --git a/core/lib/Core/Models/User.class.php b/core/lib/Core/Models/User.class.php index 746d962..902ec95 100644 --- a/core/lib/Core/Models/User.class.php +++ b/core/lib/Core/Models/User.class.php @@ -904,6 +904,7 @@ foreach ($dialogs as $key => $dialog) { $dialogs[$key]['companionId'] = $this->getDialogObject()->getDialogCompanionId((int)$dialog['id']); $dialogs[$key]['companionData'] = (new self((int)$dialogs[$key]['companionId']))->getAllUserData(true); + $dialogs[$key]['unviewedCount'] = $this->getDialogObject()->getDialogUnviewedMessagesCount((int)$dialog['id']); } return $dialogs; diff --git a/core/routes.php b/core/routes.php index 6a71e15..46e37b7 100644 --- a/core/routes.php +++ b/core/routes.php @@ -41,6 +41,8 @@ Router::route('/users', '\Core\App::users'); Router::route('/users/(\d+)', '\Core\App::userProfile'); Router::route('/users/(\d+)/sendMessage', '\Core\App::sendMessage'); + Router::route('/users/(\d+)/dialog', '\Core\App::goToDialog'); + Router::route('/test', '\Core\App::test', true); Router::route('/test/(\d+)', '\Core\App::test'); diff --git a/templates/dialog.twig b/templates/dialog.twig index e7c5d55..edbbc13 100644 --- a/templates/dialog.twig +++ b/templates/dialog.twig @@ -4,48 +4,19 @@ {% if isAuthorized %}

Диалог # {{ dialog_id }} с "{{ companionName }} ({{ companionId }})"

- - - - -
{% for message in messages %}
-
{{ message.user_from_data.name }}
+
{{ message.text }}
{{ message.date }}
+ {% if message.user_from == userId%} +
 
+ {% endif %}
{% endfor %}
- - - -

diff --git a/templates/dialogs.twig b/templates/dialogs.twig index 80dc630..b37abdf 100644 --- a/templates/dialogs.twig +++ b/templates/dialogs.twig @@ -9,6 +9,7 @@ ID Прочитано + Непрочитанных С кем диалог Действия @@ -18,6 +19,7 @@ {{ dialog.id }} {{ dialog.viewed }} + {{ dialog.unviewedCount }} {{ dialog.companionData.nameForDisplay }} Открыть диалог diff --git a/templates/layout.twig b/templates/layout.twig index cec6fca..3e9ea05 100644 --- a/templates/layout.twig +++ b/templates/layout.twig @@ -41,7 +41,7 @@ diff --git a/templates/userProfile.twig b/templates/userProfile.twig index b0348ed..1383af6 100644 --- a/templates/userProfile.twig +++ b/templates/userProfile.twig @@ -5,11 +5,14 @@ {% block main %}

Профиль пользователя {{ userData.login }}

- ID: {{ userData.id }}
- Логин: {{ userData.login }}
- Имя: {{ userData.name }}
- E-Mail: {{ userData.email }}
- Был активен: {{ userData.last_active }}
+ ID: {{ userData.id }}
+ Логин: {{ userData.login }}
+ Имя: {{ userData.name }}
+ E-Mail: {{ userData.email }}
+ Был активен: {{ userData.last_active }}
+ + +

{% endblock %} \ No newline at end of file