-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-126 Move reply form data fetching to utilities
- Loading branch information
Showing
5 changed files
with
194 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
modules/messages/utils/createReplyMessageSubject.utils.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace UniEngine\Engine\Modules\Messages\Utils; | ||
|
||
/** | ||
* Creates new subject by increasing reply counter on the previous subject. | ||
* | ||
* @param array $params | ||
* @param string $params['previousSubject'] | ||
* @param number $params['replyCounter'] | ||
*/ | ||
function createReplyMessageSubject($params) { | ||
global $_Lang; | ||
|
||
$previousSubject = $params['previousSubject']; | ||
$replyCounter = $params['replyCounter']; | ||
|
||
$cleanSubject = preg_replace( | ||
'#' . $_Lang['mess_answer_prefix'] . '\[[0-9]{1,}\]\: #si', | ||
'', | ||
$previousSubject | ||
); | ||
$newReplySubject = "{$_Lang['mess_answer_prefix']} [{$replyCounter}]: {$cleanSubject}"; | ||
|
||
return $newReplySubject; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace UniEngine\Engine\Modules\Messages\Utils; | ||
|
||
use UniEngine\Engine\Modules\Messages; | ||
|
||
/** | ||
* Fetches necessary data when trying to reply to an existing message. | ||
* | ||
* @param array $params | ||
* @param number $params['replyToMessageId'] | ||
* @param &array $params['senderUser'] | ||
*/ | ||
function fetchFormDataForReply($params) { | ||
$senderUser = &$params['senderUser']; | ||
$replyToMessageId = $params['replyToMessageId']; | ||
|
||
$senderUserId = $senderUser['id']; | ||
|
||
$query_GetReferencedMessage = ( | ||
"SELECT " . | ||
"`m`.`id`, `m`.`Thread_ID`, `m`.`subject`, `m`.`text`, `u`.`id` AS `user_id`, `u`.`username`, `u`.`authlevel` " . | ||
"FROM " . | ||
"{{table}} AS `m` " . | ||
"LEFT JOIN " . | ||
"`{{prefix}}users` AS `u` ON `u`.`id` = IF(`m`.`id_owner` != {$senderUserId}, `m`.`id_owner`, `m`.`id_sender`) " . | ||
"WHERE " . | ||
"( " . | ||
"`m`.`id` = {$replyToMessageId} OR " . | ||
"`m`.`Thread_ID` = {$replyToMessageId} " . | ||
") AND ( " . | ||
"`m`.`id_owner` = {$senderUserId} OR " . | ||
"`m`.`id_sender` = {$senderUserId} " . | ||
") AND " . | ||
"`deleted` = false " . | ||
"LIMIT 1 " . | ||
";" | ||
); | ||
|
||
$referencedMessage = doquery($query_GetReferencedMessage, 'messages', true); | ||
|
||
if (!$referencedMessage || $referencedMessage['id'] <= 0) { | ||
return [ | ||
'isSuccess' => false, | ||
'error' => [ | ||
'messageNotFound' => true, | ||
], | ||
]; | ||
} | ||
|
||
|
||
$isInOngoingThread = ($referencedMessage['Thread_ID'] > 0); | ||
$previousSubject = $referencedMessage['subject']; | ||
$replyCounter = 1; | ||
|
||
$formData = [ | ||
'username' => $referencedMessage['username'], | ||
'uid' => $referencedMessage['user_id'], | ||
'authlevel' => $referencedMessage['authlevel'], | ||
'subject' => null, | ||
'replyto' => $replyToMessageId, | ||
'Thread_Started' => $isInOngoingThread, | ||
'lock_username' => true, | ||
'lock_subject' => true, | ||
]; | ||
|
||
$checkIsMessageCopy = Messages\Utils\getMessageCopyId([ | ||
'messageData' => &$referencedMessage, | ||
]); | ||
|
||
if ($checkIsMessageCopy['isSuccess']) { | ||
$query_GetOriginalMessage = ( | ||
"SELECT `subject` " . | ||
"FROM {{table}} " . | ||
"WHERE `id` = {$checkIsMessageCopy['payload']['originalMessageId']} " . | ||
"LIMIT 1 " . | ||
";" | ||
); | ||
|
||
$originalMessage = doquery($query_GetOriginalMessage, 'messages', true); | ||
|
||
$previousSubject = $originalMessage['subject']; | ||
} | ||
|
||
if ($isInOngoingThread) { | ||
$query_GetThreadLength = ( | ||
"SELECT " . | ||
"COUNT(*) AS `Count` " . | ||
"FROM {{table}} " . | ||
"WHERE " . | ||
"`Thread_ID` = {$replyToMessageId} " . | ||
";" | ||
); | ||
|
||
$result_GetThreadLength = doquery($query_GetThreadLength, 'messages', true); | ||
|
||
$replyCounter = $result_GetThreadLength['Count']; | ||
} | ||
|
||
$formData['subject'] = Messages\Utils\createReplyMessageSubject([ | ||
'previousSubject' => $previousSubject, | ||
'replyCounter' => $replyCounter, | ||
]); | ||
|
||
return [ | ||
'isSuccess' => true, | ||
'payload' => $formData, | ||
]; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace UniEngine\Engine\Modules\Messages\Utils; | ||
|
||
/** | ||
* Extracts message's carbon copy reference ID, as long as the message is indeed | ||
* a copy of another message. Otherwise, returns an error. | ||
* | ||
* @param array $params | ||
* @param &array $params['messageData'] | ||
* @param string $params['messageData']['text'] | ||
*/ | ||
function getMessageCopyId($params) { | ||
$messageContent = $params['messageData']['text']; | ||
|
||
$extractedMatches = null; | ||
|
||
$isCopy = preg_match('/^\{COPY\_MSG\_\#([0-9]{1,}){1}\}$/D', $messageContent, $extractedMatches); | ||
|
||
if ($isCopy !== 1) { | ||
return [ | ||
'isSuccess' => false, | ||
'error' => [ | ||
'isNotCopy' => true, | ||
], | ||
]; | ||
} | ||
|
||
return [ | ||
'isSuccess' => true, | ||
'payload' => [ | ||
'originalMessageId' => $extractedMatches[1], | ||
], | ||
]; | ||
} | ||
|
||
?> |