Skip to content

Commit

Permalink
Merge pull request #130 from mdziekon/mdziekon/gh-126/msgsys-refactor…
Browse files Browse the repository at this point in the history
…-part4

Refactor message categories displaying code
  • Loading branch information
mdziekon authored Nov 11, 2020
2 parents ac9daec + b7bed0b commit 6f093a2
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 143 deletions.
131 changes: 12 additions & 119 deletions messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,15 @@
includeLang('FleetMission_MissileAttack');

$parse = &$_Lang;
$parse['Insert_Styles'] = '';
$parse['Insert_CategoryList'] = '';

$CreateSimForms = '';
$SetTitle = $_Lang['mess_pagetitle_read'];

$Now = time();

$MessageType = array(100, 0, 1, 2, 3, 4, 5, 15, 50, 70, 80);
foreach($MessageType as $TypeID)
{
$MsgCounter['total'][$TypeID] = 0;
$MsgCounter['threaded'][$TypeID] = 0;
$MsgCounter['unread'][$TypeID] = 0;
}
$_CanBeThreaded = array(1, 80, 100);
$TitleColor = array
(
0 => '#FFFF00', 1 => '#FF6699', 2 => '#FF3300', 3 => '#FF9900', 4 => '#9540BF', 5 => '#009933',
15 => '#6661FF', 80 => 'white', 50 => 'skyblue', 70 => '#75F121', 100 => '#ABABAB'
);

$_MaxLength_Subject = 100;
$_MaxLength_Text = 5000;

$_UseThreads = ($_User['settings_UseMsgThreads'] == 1 ? true : false);

$_ThisCategory = (isset($_GET['messcat']) ? intval($_GET['messcat']) : 0);
$DeleteWhat = (isset($_POST['deletemessages']) ? $_POST['deletemessages'] : '');
if(!empty($DeleteWhat) || (isset($_POST['delid']) && $_POST['delid'] > 0))
Expand Down Expand Up @@ -275,7 +258,6 @@
[
'user' => &$_User,
'timestamp' => $Now,
'knownMessageTypes' => $MessageType,
]
);

Expand Down Expand Up @@ -326,7 +308,7 @@
$parse['SpyDisplay'] = 'display: none;';
}

if (!in_array($_ThisCategory, $MessageType)) {
if (!Messages\Utils\isValidMessageType($_ThisCategory)) {
$_ThisCategory = 100;
}

Expand Down Expand Up @@ -374,7 +356,7 @@

$parse['ThisPage'] = $_ThisPage;
$parse['MessCategory'] = $_ThisCategory;
$parse['MessCategoryColor'] = $TitleColor[$_ThisCategory];
$parse['MessCategoryColor'] = Messages\Utils\getMessageTypeColor($_ThisCategory);
$parse['Pagination'] = $Pagination;
$page = '';

Expand Down Expand Up @@ -501,9 +483,13 @@
unset($Messages[$ThisKey]);
}

$isThreadViewEnabled = Messages\Utils\_isMessagesThreadViewEnabled([
'user' => &$_User,
]);

foreach ($Messages as $MessageData) {
if (
$_UseThreads &&
$isThreadViewEnabled &&
isset($MessageData['Thread_ID']) &&
$MessageData['Thread_ID'] > 0
) {
Expand Down Expand Up @@ -551,103 +537,10 @@
break;

default:
// Show Message Categories
$SQLResult_GetMessageCategoriesCounters = doquery(
"SELECT `type`, `read`, `Thread_ID`, `Thread_IsLast`, COUNT(*) AS `Count` FROM {{table}} WHERE `id_owner` = {$_User['id']} AND `deleted` = false GROUP BY `type`, `read`, `Thread_ID`, `Thread_IsLast` ORDER BY `Thread_IsLast` DESC;",
'messages'
);

if($SQLResult_GetMessageCategoriesCounters->num_rows > 0)
{
$SeenThreads = array();
$ThreadMainTypes = array();
while($Counter = $SQLResult_GetMessageCategoriesCounters->fetch_assoc())
{
// Handler TypeCount
if($_UseThreads AND $Counter['Thread_ID'] > 0)
{
if($Counter['Thread_IsLast'] == 1)
{
$ThreadMainTypes[$Counter['Thread_ID']] = $Counter['type'];
}
else
{
$Counter['type'] = $ThreadMainTypes[$Counter['Thread_ID']];
}
if(!in_array($Counter['Thread_ID'], $SeenThreads))
{
$MsgCounter['threaded'][$Counter['type']] += 1;
}
}
else
{

$MsgCounter['threaded'][$Counter['type']] += $Counter['Count'];
}
$MsgCounter['total'][$Counter['type']] += $Counter['Count'];
if($Counter['read'] == 0)
{
$MsgCounter['unread'][$Counter['type']] += $Counter['Count'];
}
// Handle TotalCount
if($Counter['type'] != 80)
{
$MsgCounter['total'][100] += $Counter['Count'];
if($Counter['read'] == 0)
{
$MsgCounter['unread'][100] += $Counter['Count'];
}
if($_UseThreads AND $Counter['Thread_ID'] > 0)
{
if(!in_array($Counter['Thread_ID'], $SeenThreads))
{
$MsgCounter['threaded'][100] += 1;
}
}
else
{
$MsgCounter['threaded'][100] += $Counter['Count'];
}
}

if($_UseThreads AND $Counter['Thread_ID'] > 0)
{
if(!in_array($Counter['Thread_ID'], $SeenThreads))
{
$SeenThreads[] = $Counter['Thread_ID'];
}
}
}
}

$TPL_CatList_Body = gettemplate('messages_catlist_body');
$TPL_CatList_Row = gettemplate('messages_catlist_row');

foreach($MessageType as $TypeID)
{
$ThisClass = 'c'.(string)($TypeID + 0);
$parse['Insert_Styles'] .= ".{$ThisClass} { color: {$TitleColor[$TypeID]}; } ";
$ThisArray = array
(
'Insert_CatID' => $TypeID,
'Insert_CatClass' => $ThisClass,
'Insert_CatName' => $_Lang['type'][$TypeID],
'Insert_CatUnread' => prettyNumber($MsgCounter['unread'][$TypeID]),
'Insert_CatTotal' => (($_UseThreads AND in_array($TypeID, $_CanBeThreaded) AND $MsgCounter['threaded'][$TypeID] < $MsgCounter['total'][$TypeID]) ? prettyNumber($MsgCounter['threaded'][$TypeID]).'/' : '').prettyNumber($MsgCounter['total'][$TypeID]),
);
$parse['Insert_CategoryList'] .= parsetemplate($TPL_CatList_Row, $ThisArray);
}

if($_UseThreads)
{
$parse['Insert_Hide_ThreadEnabled'] = 'display: none;';
}
else
{
$parse['Insert_Hide_ThreadDisabled'] = 'display: none;';
}

$page = parsetemplate($TPL_CatList_Body, $parse);
$renderResult = Messages\Screens\CategoriesListView\render([
'readerUser' => &$_User,
]);
$page = $renderResult['componentHTML'];

break;
}
Expand Down
3 changes: 3 additions & 0 deletions modules/messages/_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
include($includePath . './commands/batchMarkMessagesAsRead.commands.php');
include($includePath . './input/batchActions.userCommands.php');
include($includePath . './input/sendMessage.userCommands.php');
include($includePath . './screens/CategoriesListView/CategoriesListView.component.php');
include($includePath . './screens/CategoriesListView/utils/fetchMessagesCounters.utils.php');
include($includePath . './utils/batchDeleteMessages.utils.php');
include($includePath . './utils/batchMessageUpdates.utils.php');
include($includePath . './utils/buildMessageDetails.utils.php');
Expand All @@ -23,6 +25,7 @@
include($includePath . './utils/fetchUserMessagesCount.utils.php');
include($includePath . './utils/fetchUserThreads.utils.php');
include($includePath . './utils/formatUserMessageDetails.utils.php');
include($includePath . './utils/getConstants.utils.php');
include($includePath . './utils/getMessageCopyId.utils.php');
include($includePath . './utils/getUnreadMessageIds.utils.php');
include($includePath . './utils/messageObject.utils.php');
Expand Down
3 changes: 1 addition & 2 deletions modules/messages/input/batchActions.userCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @param array $params
* @param objectRef $params['user']
* @param number $params['timestamp']
* @param array<number> $params['knownMessageTypes']
*/
function handleBatchAction(&$input, $params) {
global $_Lang;
Expand All @@ -38,7 +37,7 @@ function handleBatchAction(&$input, $params) {

$user = &$params['user'];
$currentTimestamp = $params['timestamp'];
$knownMessageTypes = $params['knownMessageTypes'];
$knownMessageTypes = Messages\Utils\getMessageTypes();

$parsedInput = [
'command' => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace UniEngine\Engine\Modules\Messages\Screens\CategoriesListView;

use UniEngine\Engine\Modules\Messages\Screens\CategoriesListView;
use UniEngine\Engine\Modules\Messages;

// Arguments
// - $props (Object)
// - readerUser (&Object)
//
// Returns: Object
// - componentHTML (String)
//
function render ($props) {
global $_Lang;

$readerUser = &$props['readerUser'];

$isReaderUsingThreads = Messages\Utils\_isMessagesThreadViewEnabled([ 'user' => &$readerUser ]);

$localTemplateLoader = createLocalTemplateLoader(__DIR__);

$tplBodyCache = [
'body' => $localTemplateLoader('body'),
'categoryEntryRow' => $localTemplateLoader('categoryEntryRow'),
];

$messageTypes = Messages\Utils\getMessageTypes();

$counters = [
'total' => [],
'threaded' => [],
'unread' => [],
];

foreach ($messageTypes as $typeId) {
$counters['total'][$typeId] = 0;
$counters['threaded'][$typeId] = 0;
$counters['unread'][$typeId] = 0;
}

$internalCaches = [
'seenThreads' => [],
'threadMainTypes' => [],
];

$messageCountersResult = CategoriesListView\Utils\fetchMessagesCounters([
'readerId' => $readerUser['id'],
]);

while ($counterEntry = $messageCountersResult->fetch_assoc()) {
$typeId = $counterEntry['type'];

$thisCounterUpdates = [
'total' => 0,
'threaded' => 0,
'unread' => 0,
];

if (
$isReaderUsingThreads &&
$counterEntry['Thread_ID'] > 0
) {
$threadId = $counterEntry['Thread_ID'];

if ($counterEntry['Thread_IsLast'] == 1) {
$internalCaches['threadMainTypes'][$threadId] = $typeId;
}

// For all messages in a thread, assume they are of the same type
// as the last message
$typeId = $internalCaches['threadMainTypes'][$threadId];

if (!in_array($threadId, $internalCaches['seenThreads'])) {
$thisCounterUpdates['threaded'] += 1;

$internalCaches['seenThreads'][] = $threadId;
}
} else {
$thisCounterUpdates['threaded'] += $counterEntry['Count'];
}

$thisCounterUpdates['total'] += $counterEntry['Count'];
$thisCounterUpdates['unread'] += (
$counterEntry['read'] == 0 ?
$counterEntry['Count'] :
0
);

foreach ($thisCounterUpdates as $updateKey => $updateIncrement) {
$counters[$updateKey][$typeId] += $updateIncrement;

// Don't count admin messages towards total counter
if ($typeId == 80) {
continue;
}

// Update total counter as well
$counters[$updateKey][100] += $updateIncrement;
}
}

$componentTPLData = [
'Insert_Styles' => [],
'Insert_CategoryList' => [],
'Insert_Hide_ThreadEnabled' => (
$isReaderUsingThreads ?
'display: none;' :
''
),
'Insert_Hide_ThreadDisabled' => (
!$isReaderUsingThreads ?
'display: none;' :
''
),
];

foreach ($messageTypes as $typeId) {
$typeCSSClass = 'c' . (string)($typeId + 0);
$typeColor = Messages\Utils\getMessageTypeColor($typeId);

$componentTPLData['Insert_Styles'][] = ".{$typeCSSClass} { color: {$typeColor}; } ";

$categoryEntryRowTPLData = [
'Insert_CatID' => $typeId,
'Insert_CatClass' => $typeCSSClass,
'Insert_CatName' => $_Lang['type'][$typeId],
'Insert_CatUnread' => prettyNumber($counters['unread'][$typeId]),
'Insert_CatTotal' => (
(
$isReaderUsingThreads &&
Messages\Utils\_isMessageTypeThreadable($typeId) &&
$counters['threaded'][$typeId] < $counters['total'][$typeId]
) ?
prettyNumber($counters['threaded'][$typeId]) . '/' :
''
) . prettyNumber($counters['total'][$typeId]),
];

$componentTPLData['Insert_CategoryList'][] = parsetemplate(
$tplBodyCache['categoryEntryRow'],
$categoryEntryRowTPLData
);
}

$componentTPLData['Insert_Styles'] = implode('', $componentTPLData['Insert_Styles']);
$componentTPLData['Insert_CategoryList'] = implode('', $componentTPLData['Insert_CategoryList']);

$componentTPLBody = $tplBodyCache['body'];
$componentHTML = parsetemplate($componentTPLBody, array_merge($_Lang, $componentTPLData));

return [
'componentHTML' => $componentHTML,
];
}

?>
Loading

0 comments on commit 6f093a2

Please sign in to comment.