Moved themeMessage method from ThreadProcessor to Thread

This commit is contained in:
Dmitriy Simushev 2012-10-02 08:58:05 +00:00
parent c26fc8bb26
commit 1528c610d7
2 changed files with 50 additions and 50 deletions

View File

@ -381,6 +381,55 @@ Class Thread {
return $kind_names[$message_kind]; return $kind_names[$message_kind];
} }
/**
* Theme message to display in chat window
*
* @param array $message Message array
* @return string Ready to display themed message
*/
public static function themeMessage($message) {
global $webim_encoding;
// No theming for avatars
if ($message['ikind'] == Thread::KIND_AVATAR) {
return '';
}
// Prepare messages fields
$creation_date = date("H:i:s", $message['created']);
$kind_name = Thread::kindToString($message['ikind']);
$sender_name = $message['tname']
? "<span class='n{$kind_name}'>" . htmlspecialchars($message['tname']) . "</span>: "
: '';
// Prepare message text
// Escape special chars
$text = htmlspecialchars($message['tmessage']);
// Replace URL's by <a> tags
$text = preg_replace('/(https?|ftp):\/\/\S*/', '<a href="$0" target="_blank">$0</a>', $text);
// Add <br> tags instead of \n chars
$text = str_replace("\n", "<br/>", $text);
// Span and storng tags available for system messages
if ($message['ikind'] != Thread::KIND_USER && $message['ikind'] != Thread::KIND_AGENT) {
$text = preg_replace('/&lt;(span|strong)&gt;(.*)&lt;\/\1&gt;/U', '<$1>$2</$1>', $text);
$text = preg_replace(
'/&lt;span class=&quot;(.*)&quot;&gt;(.*)&lt;\/span&gt;/U',
'<span class="$1">$2</span>',
$text
);
}
// Build result message
$result = sprintf(
"<span>%s</span> %s<span class='m%s'>%s</span><br/>",
$creation_date,
$sender_name,
$kind_name,
$text
);
return myiconv($webim_encoding, "utf-8", $result);
}
/** /**
* Return next revision number (last revision number plus one) * Return next revision number (last revision number plus one)
* *

View File

@ -224,55 +224,6 @@ class ThreadProcessor extends RequestProcessor {
return $requests; return $requests;
} }
/**
* Theme message to display in chat window
*
* @param array $message Message array
* @return string Ready to display themed message
*/
protected function themeMessage($message) {
global $webim_encoding;
// No theming for avatars
if ($message['ikind'] == Thread::KIND_AVATAR) {
return '';
}
// Prepare messages fields
$creation_date = date("H:i:s", $message['created']);
$kind_name = Thread::kindToString($message['ikind']);
$sender_name = $message['tname']
? "<span class='n{$kind_name}'>" . htmlspecialchars($message['tname']) . "</span>: "
: '';
// Prepare message text
// Escape special chars
$text = htmlspecialchars($message['tmessage']);
// Replace URL's by <a> tags
$text = preg_replace('/(https?|ftp):\/\/\S*/', '<a href="$0" target="_blank">$0</a>', $text);
// Add <br> tags instead of \n chars
$text = str_replace("\n", "<br/>", $text);
// Span and storng tags available for system messages
if ($message['ikind'] != Thread::KIND_USER && $message['ikind'] != Thread::KIND_AGENT) {
$text = preg_replace('/&lt;(span|strong)&gt;(.*)&lt;\/\1&gt;/U', '<$1>$2</$1>', $text);
$text = preg_replace(
'/&lt;span class=&quot;(.*)&quot;&gt;(.*)&lt;\/span&gt;/U',
'<span class="$1">$2</span>',
$text
);
}
// Build result message
$result = sprintf(
"<span>%s</span> %s<span class='m%s'>%s</span><br/>",
$creation_date,
$sender_name,
$kind_name,
$text
);
return myiconv($webim_encoding, "utf-8", $result);
}
/** /**
* Send new messages to window * Send new messages to window
* *
@ -287,7 +238,7 @@ class ThreadProcessor extends RequestProcessor {
$messages = $thread->getMessages($is_user, $last_message_id); $messages = $thread->getMessages($is_user, $last_message_id);
if (! empty($messages)) { if (! empty($messages)) {
foreach($messages as $key => $msg) { foreach($messages as $key => $msg) {
$messages[$key] = $this->themeMessage($msg); $messages[$key] = Thread::themeMessage($msg);
} }
$this->responses[] = array( $this->responses[] = array(
'token' => md5(time() . rand()), 'token' => md5(time() . rand()),