Change keys of array return by Thread::getMessages

This commit is contained in:
Dmitriy Simushev 2012-10-10 13:33:25 +00:00
parent f5a970e50f
commit 38668789b9
5 changed files with 46 additions and 45 deletions

View File

@ -607,48 +607,48 @@ class ThreadTest extends PHPUnit_Framework_TestCase {
// Create messages // Create messages
// The first // The first
$first_message = array( $first_message = array(
'ikind' => Thread::KIND_USER, 'kind' => Thread::KIND_USER,
'tmessage' => 'The first message', 'message' => 'The first message',
'created' => time(), 'created' => time(),
'tname' => 'System message only for agent' 'name' => 'System message only for agent'
); );
// The second // The second
$second_message = array( $second_message = array(
'ikind' => Thread::KIND_AGENT, 'kind' => Thread::KIND_AGENT,
'tmessage' => 'The second message', 'message' => 'The second message',
'created' => time(), 'created' => time(),
'tname' => 'User' 'name' => 'User'
); );
// The third // The third
$third_message = array( $third_message = array(
'ikind' => Thread::KIND_FOR_AGENT, 'kind' => Thread::KIND_FOR_AGENT,
'tmessage' => 'The third message', 'message' => 'The third message',
'created' => time(), 'created' => time(),
'tname' => 'Agent' 'name' => 'Agent'
); );
// Send messages // Send messages
// The first // The first
$first_message['messageid'] = $thread->postMessage( $first_message['id'] = $thread->postMessage(
$first_message['ikind'], $first_message['kind'],
$first_message['tmessage'], $first_message['message'],
$first_message['tname'], $first_message['name'],
12, 12,
$first_message['created'] $first_message['created']
); );
// The second // The second
$second_message['messageid'] = $thread->postMessage( $second_message['id'] = $thread->postMessage(
$second_message['ikind'], $second_message['kind'],
$second_message['tmessage'], $second_message['message'],
$second_message['tname'], $second_message['name'],
14, 14,
$second_message['created'] $second_message['created']
); );
// The third // The third
$third_message['messageid'] = $thread->postMessage( $third_message['id'] = $thread->postMessage(
$third_message['ikind'], $third_message['kind'],
$third_message['tmessage'], $third_message['message'],
$third_message['tname'], $third_message['name'],
16, 16,
$third_message['created'] $third_message['created']
); );
@ -660,7 +660,7 @@ class ThreadTest extends PHPUnit_Framework_TestCase {
$thread->getMessages(false, $last_id) $thread->getMessages(false, $last_id)
); );
// Check last message id // Check last message id
$this->assertEquals($third_message['messageid'], $last_id); $this->assertEquals($third_message['id'], $last_id);
// Check messages for user with ids starts from $msg_id // Check messages for user with ids starts from $msg_id
$last_id = $msg_id; $last_id = $msg_id;
@ -669,16 +669,16 @@ class ThreadTest extends PHPUnit_Framework_TestCase {
$thread->getMessages(true, $last_id) $thread->getMessages(true, $last_id)
); );
// Check last message id // Check last message id
$this->assertEquals($second_message['messageid'], $last_id); $this->assertEquals($second_message['id'], $last_id);
// Check messages for agent with ids starts from first message's id // Check messages for agent with ids starts from first message's id
$last_id = $first_message['messageid']; $last_id = $first_message['id'];
$this->assertEquals( $this->assertEquals(
array($second_message, $third_message), array($second_message, $third_message),
$thread->getMessages(false, $last_id) $thread->getMessages(false, $last_id)
); );
// Check last message id // Check last message id
$this->assertEquals($third_message['messageid'], $last_id); $this->assertEquals($third_message['id'], $last_id);
// Delete thread // Delete thread
$thread->delete(); $thread->delete();

View File

@ -28,19 +28,19 @@ function get_user_id()
function message_to_text($msg) function message_to_text($msg)
{ {
if ($msg['ikind'] == Thread::KIND_AVATAR) { if ($msg['kind'] == Thread::KIND_AVATAR) {
return ""; return "";
} }
$message_time = date("H:i:s ", $msg['created']); $message_time = date("H:i:s ", $msg['created']);
if ($msg['ikind'] == Thread::KIND_USER || $msg['ikind'] == Thread::KIND_AGENT) { if ($msg['kind'] == Thread::KIND_USER || $msg['kind'] == Thread::KIND_AGENT) {
if ($msg['tname']) if ($msg['name'])
return $message_time . $msg['tname'] . ": " . $msg['tmessage'] . "\n"; return $message_time . $msg['name'] . ": " . $msg['message'] . "\n";
else else
return $message_time . $msg['tmessage'] . "\n"; return $message_time . $msg['message'] . "\n";
} else if ($msg['ikind'] == Thread::KIND_INFO) { } else if ($msg['kind'] == Thread::KIND_INFO) {
return $message_time . $msg['tmessage'] . "\n"; return $message_time . $msg['message'] . "\n";
} else { } else {
return $message_time . "[" . $msg['tmessage'] . "]\n"; return $message_time . "[" . $msg['message'] . "]\n";
} }
} }

View File

@ -391,26 +391,26 @@ Class Thread {
global $webim_encoding; global $webim_encoding;
// No theming for avatars // No theming for avatars
if ($message['ikind'] == Thread::KIND_AVATAR) { if ($message['kind'] == Thread::KIND_AVATAR) {
return ''; return '';
} }
// Prepare messages fields // Prepare messages fields
$creation_date = date("H:i:s", $message['created']); $creation_date = date("H:i:s", $message['created']);
$kind_name = Thread::kindToString($message['ikind']); $kind_name = Thread::kindToString($message['kind']);
$sender_name = $message['tname'] $sender_name = $message['name']
? "<span class='n{$kind_name}'>" . htmlspecialchars($message['tname']) . "</span>: " ? "<span class='n{$kind_name}'>" . htmlspecialchars($message['name']) . "</span>: "
: ''; : '';
// Prepare message text // Prepare message text
// Escape special chars // Escape special chars
$text = htmlspecialchars($message['tmessage']); $text = htmlspecialchars($message['message']);
// Replace URL's by <a> tags // Replace URL's by <a> tags
$text = preg_replace('/(https?|ftp):\/\/\S*/', '<a href="$0" target="_blank">$0</a>', $text); $text = preg_replace('/(https?|ftp):\/\/\S*/', '<a href="$0" target="_blank">$0</a>', $text);
// Add <br> tags instead of \n chars // Add <br> tags instead of \n chars
$text = str_replace("\n", "<br/>", $text); $text = str_replace("\n", "<br/>", $text);
// Span and storng tags available for system messages // Span and storng tags available for system messages
if ($message['ikind'] != Thread::KIND_USER && $message['ikind'] != Thread::KIND_AGENT) { if ($message['kind'] != Thread::KIND_USER && $message['kind'] != Thread::KIND_AGENT) {
$text = preg_replace('/&lt;(span|strong)&gt;(.*)&lt;\/\1&gt;/U', '<$1>$2</$1>', $text); $text = preg_replace('/&lt;(span|strong)&gt;(.*)&lt;\/\1&gt;/U', '<$1>$2</$1>', $text);
$text = preg_replace( $text = preg_replace(
'/&lt;span class=&quot;(.*)&quot;&gt;(.*)&lt;\/span&gt;/U', '/&lt;span class=&quot;(.*)&quot;&gt;(.*)&lt;\/span&gt;/U',
@ -675,7 +675,8 @@ Class Thread {
// Load messages // Load messages
$messages = $db->query( $messages = $db->query(
"select messageid,ikind,dtmcreated as created,tname,tmessage from {chatmessage} " . "select messageid as id, ikind as kind, dtmcreated as created, tname as name, tmessage as message " .
"from {chatmessage} " .
"where threadid = :threadid and messageid > :lastid " . "where threadid = :threadid and messageid > :lastid " .
($is_user ? "and ikind <> " . self::KIND_FOR_AGENT : "") . ($is_user ? "and ikind <> " . self::KIND_FOR_AGENT : "") .
" order by messageid", " order by messageid",
@ -688,8 +689,8 @@ Class Thread {
foreach ($messages as $msg) { foreach ($messages as $msg) {
// Get last message ID // Get last message ID
if ($msg['messageid'] > $last_id) { if ($msg['id'] > $last_id) {
$last_id = $msg['messageid']; $last_id = $msg['id'];
} }
} }

View File

@ -239,7 +239,7 @@ class ThreadProcessor extends RequestProcessor {
if (! empty($messages)) { if (! empty($messages)) {
foreach($messages as $key => $msg) { foreach($messages as $key => $msg) {
// Check if message is avatar // Check if message is avatar
if ($msg['ikind'] == Thread::KIND_AVATAR) { if ($msg['kind'] == Thread::KIND_AVATAR) {
// Update avatar // Update avatar
$this->responses[] = array( $this->responses[] = array(
'token' => md5(time() . rand()), 'token' => md5(time() . rand()),
@ -251,7 +251,7 @@ class ThreadProcessor extends RequestProcessor {
'token' => $thread->lastToken, 'token' => $thread->lastToken,
'return' => array(), 'return' => array(),
'references' => array(), 'references' => array(),
'imageLink' => $msg['tmessage'] 'imageLink' => $msg['message']
) )
) )
) )

View File

@ -54,7 +54,7 @@ if (isset($_GET['threadid'])) {
$lastid = -1; $lastid = -1;
$messages = $thread_info['thread']->getMessages(false, $lastid); $messages = $thread_info['thread']->getMessages(false, $lastid);
foreach ($messages as $msg) { foreach ($messages as $msg) {
if ($msg['ikind'] == Thread::KIND_AVATAR) { if ($msg['kind'] == Thread::KIND_AVATAR) {
continue; continue;
} }
$page['threadMessages'][] = Thread::themeMessage($msg); $page['threadMessages'][] = Thread::themeMessage($msg);