доработки диалогов
This commit is contained in:
1 parent
6576be0c61
commit
95f0611022
12 files changed
+127
-53
No files matched your search
+29
-2
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
.deliveryStatus {
|
||||
background: #cecece;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 45px;
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
top: 8px;
|
||||
}
|
||||
.deliveryStatus.viewed {
|
||||
background: #00f700;
|
||||
}
|
||||
@@ -33,6 +33,8 @@
|
||||
}
|
||||
date_default_timezone_set('Europe/Moscow');
|
||||
|
||||
define('START_MEMORY', memory_get_usage());
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
\Sentry\init(
|
||||
[
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Получить все данные из таблицы
|
||||
|
||||
@@ -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 'Неизвестно';
|
||||
}
|
||||
|
||||
@@ -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()]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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');
|
||||
|
||||
+4
-33
@@ -4,48 +4,19 @@
|
||||
{% if isAuthorized %}
|
||||
<h1>Диалог # {{ dialog_id }} с "{{ companionName }} ({{ companionId }})"</h1>
|
||||
|
||||
<!--
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<td>ID</td>
|
||||
<td>От</td>
|
||||
<td>Кому</td>
|
||||
<td>Дата</td>
|
||||
<td>Сообщение</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for message in messages %}
|
||||
<tr>
|
||||
<td>{{ message.id }}</td>
|
||||
<td>{{ message.user_from_data.name }} ({{ message.user_from_data.login }})</td>
|
||||
<td>{{ message.user_to_data.name }} ({{ message.user_to_data.login }})</td>
|
||||
<td>{{ message.date }}</td>
|
||||
<td>{{ message.text }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
-->
|
||||
|
||||
|
||||
|
||||
|
||||
<div class="messageBox">
|
||||
{% for message in messages %}
|
||||
<div class="message {% if message.user_from == userId %}fromMe{% else %}fromCompanion{% endif %}">
|
||||
<div class="name">{{ message.user_from_data.name }}</div>
|
||||
<div class="name"><a href="/users/{{ message.user_from_data.id }}">{{ message.user_from_data.name }}</a></div>
|
||||
<div class="text">{{ message.text }}</div>
|
||||
<div class="date">{{ message.date }}</div>
|
||||
{% if message.user_from == userId%}
|
||||
<div class="deliveryStatus {% if message.user_from == userId and message.viewed == "Y" %}viewed{% endif %}"> </div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<p>
|
||||
<form method="POST" action="/users/{{ companionId }}/sendMessage">
|
||||
<input type="hidden" name="dialogId" value="{{ dialog_id }}">
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<tr>
|
||||
<td>ID</td>
|
||||
<td>Прочитано</td>
|
||||
<td>Непрочитанных</td>
|
||||
<td>С кем диалог</td>
|
||||
<td>Действия</td>
|
||||
</tr>
|
||||
@@ -18,6 +19,7 @@
|
||||
<tr>
|
||||
<td>{{ dialog.id }}</td>
|
||||
<td>{{ dialog.viewed }}</td>
|
||||
<td>{{ dialog.unviewedCount }}</td>
|
||||
<td>{{ dialog.companionData.nameForDisplay }}</td>
|
||||
<td><a href="/dialog/{{ dialog.id }}">Открыть диалог</a></td>
|
||||
</tr>
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<a href="https://github.com/rsgrinko/delta_framework" target="_blank">DeltaFramework</a> by <a href="https://it-stories.ru/about" target="_blank">Roman S Grinko</a>, {{ currentYear }} | Generation time: {{ executionTime }}
|
||||
<a href="https://github.com/rsgrinko/delta_framework" target="_blank">DeltaFramework</a> by <a href="https://it-stories.ru/about" target="_blank">Roman S Grinko</a>, {{ currentYear }} | Generation time: {{ executionTime }} | Memory use: {{ memoryUse }}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -5,11 +5,14 @@
|
||||
{% block main %}
|
||||
<h1>Профиль пользователя {{ userData.login }}</h1>
|
||||
<p>
|
||||
<span>ID: {{ userData.id }}</span><br>
|
||||
<span>Логин: {{ userData.login }}</span><br>
|
||||
<span>Имя: {{ userData.name }}</span><br>
|
||||
<span>E-Mail: {{ userData.email }}</span><br>
|
||||
<span>Был активен: {{ userData.last_active }}</span><br>
|
||||
<span><b>ID:</b> {{ userData.id }}</span><br>
|
||||
<span><b>Логин:</b> {{ userData.login }}</span><br>
|
||||
<span><b>Имя:</b> {{ userData.name }}</span><br>
|
||||
<span><b>E-Mail:</b> {{ userData.email }}</span><br>
|
||||
<span><b>Был активен:</b> {{ userData.last_active }}</span><br>
|
||||
<form action="/users/{{ userData.id }}/dialog" method="POST">
|
||||
<button class="button" type="submit">Написать</button>
|
||||
</form>
|
||||
|
||||
</p>
|
||||
{% endblock %}
|
||||
Reference in new issue
Block a user