summaryrefslogtreecommitdiff
path: root/demos/chat/protected/App_Code/ChatBufferRecord.php
diff options
context:
space:
mode:
authorwei <>2007-01-04 11:23:26 +0000
committerwei <>2007-01-04 11:23:26 +0000
commitdd028bec3822d1d9c28c35d599d687e038c7705f (patch)
treef3d2fb7f95073ea481a4dec86f0f0d30c7fe3588 /demos/chat/protected/App_Code/ChatBufferRecord.php
parentcac90ea6547fe194ab6ab101dfe11a0b751823ca (diff)
Add chat demo and tutorial.
Diffstat (limited to 'demos/chat/protected/App_Code/ChatBufferRecord.php')
-rw-r--r--demos/chat/protected/App_Code/ChatBufferRecord.php64
1 files changed, 64 insertions, 0 deletions
diff --git a/demos/chat/protected/App_Code/ChatBufferRecord.php b/demos/chat/protected/App_Code/ChatBufferRecord.php
new file mode 100644
index 00000000..cf3c651f
--- /dev/null
+++ b/demos/chat/protected/App_Code/ChatBufferRecord.php
@@ -0,0 +1,64 @@
+<?php
+
+class ChatBufferRecord extends TActiveRecord
+{
+ public $id;
+ public $for_user;
+ public $from_user;
+ public $message;
+ private $_created_on;
+
+ public static $_tablename='chat_buffer';
+
+ public function getCreated_On()
+ {
+ if($this->_created_on === null)
+ $this->_created_on = time();
+ return $this->_created_on;
+ }
+
+ public function setCreated_On($value)
+ {
+ $this->_created_on = $value;
+ }
+
+ public static function finder()
+ {
+ return parent::getRecordFinder('ChatBufferRecord');
+ }
+
+ public function saveMessage()
+ {
+ foreach(ChatUserRecord::finder()->findAll() as $user)
+ {
+ $message = new self;
+ $message->for_user = $user->username;
+ $message->from_user = $this->from_user;
+ $message->message = $this->message;
+ $message->save();
+ if($user->username == $this->from_user)
+ {
+ $user->last_activity = time(); //update the last activity;
+ $user->save();
+ }
+ }
+ }
+
+ public function getUserMessages($user)
+ {
+ $content = '';
+ foreach($this->findAll('for_user = ?', $user) as $message)
+ $content .= $this->formatMessage($message);
+ $this->deleteAll('for_user = ? OR created_on < ?', $user, time() - 300); //5 min inactivity
+ return $content;
+ }
+
+ protected function formatMessage($message)
+ {
+ $user = htmlspecialchars($message->from_user);
+ $content = htmlspecialchars($message->message);
+ return "<div class=\"message\"><strong>{$user}:</strong> <span>{$content}</span></div>";
+ }
+}
+
+?> \ No newline at end of file