From dd028bec3822d1d9c28c35d599d687e038c7705f Mon Sep 17 00:00:00 2001 From: wei <> Date: Thu, 4 Jan 2007 11:23:26 +0000 Subject: Add chat demo and tutorial. --- demos/chat/index.php | 29 ++++++++ demos/chat/protected/.htaccess | 1 + demos/chat/protected/App_Code/ChatBufferRecord.php | 64 ++++++++++++++++ demos/chat/protected/App_Code/ChatUserManager.php | 63 ++++++++++++++++ demos/chat/protected/App_Code/ChatUserRecord.php | 41 +++++++++++ demos/chat/protected/App_Code/chat.db | Bin 0 -> 4096 bytes demos/chat/protected/application.xml | 24 ++++++ demos/chat/protected/pages/Home.page | 51 +++++++++++++ demos/chat/protected/pages/Home.php | 56 ++++++++++++++ demos/chat/protected/pages/Login.page | 35 +++++++++ demos/chat/protected/pages/Login.php | 40 ++++++++++ demos/chat/protected/pages/config.xml | 15 ++++ demos/chat/protected/pages/send.gif | Bin 0 -> 993 bytes demos/chat/protected/pages/send.png | Bin 0 -> 32586 bytes demos/chat/protected/pages/style.css | 82 +++++++++++++++++++++ 15 files changed, 501 insertions(+) create mode 100644 demos/chat/index.php create mode 100644 demos/chat/protected/.htaccess create mode 100644 demos/chat/protected/App_Code/ChatBufferRecord.php create mode 100644 demos/chat/protected/App_Code/ChatUserManager.php create mode 100644 demos/chat/protected/App_Code/ChatUserRecord.php create mode 100644 demos/chat/protected/App_Code/chat.db create mode 100644 demos/chat/protected/application.xml create mode 100644 demos/chat/protected/pages/Home.page create mode 100644 demos/chat/protected/pages/Home.php create mode 100644 demos/chat/protected/pages/Login.page create mode 100644 demos/chat/protected/pages/Login.php create mode 100644 demos/chat/protected/pages/config.xml create mode 100644 demos/chat/protected/pages/send.gif create mode 100644 demos/chat/protected/pages/send.png create mode 100644 demos/chat/protected/pages/style.css (limited to 'demos/chat') diff --git a/demos/chat/index.php b/demos/chat/index.php new file mode 100644 index 00000000..17ed21d0 --- /dev/null +++ b/demos/chat/index.php @@ -0,0 +1,29 @@ +run(); + +?> \ No newline at end of file diff --git a/demos/chat/protected/.htaccess b/demos/chat/protected/.htaccess new file mode 100644 index 00000000..3418e55a --- /dev/null +++ b/demos/chat/protected/.htaccess @@ -0,0 +1 @@ +deny from all \ No newline at end of file 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 @@ +_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 "
{$user}: {$content}
"; + } +} + +?> \ No newline at end of file diff --git a/demos/chat/protected/App_Code/ChatUserManager.php b/demos/chat/protected/App_Code/ChatUserManager.php new file mode 100644 index 00000000..f8fe09cc --- /dev/null +++ b/demos/chat/protected/App_Code/ChatUserManager.php @@ -0,0 +1,63 @@ +setIsGuest(true); + if($username !== null) + { + $user->setIsGuest(false); + $user->setName($username); + $user->setRoles(array('normal')); + } + return $user; + } + + /** + * Add a new user to the database. + * @param string username. + */ + public function addNewUser($username) + { + $user = new ChatUserRecord(); + $user->username = $username; + $user->save(); + } + + /** + * @return boolean true if username already exists, false otherwise. + */ + public function usernameExists($username) + { + return ChatUserRecord::finder()->findByUsername($username) instanceof ChatUserRecord; + } + + /** + * Validates if the username exists. + * @param string user name + * @param string password + * @return boolean true if validation is successful, false otherwise. + */ + public function validateUser($username,$password) + { + return $this->usernameExists($username); + } +} + + +?> \ No newline at end of file diff --git a/demos/chat/protected/App_Code/ChatUserRecord.php b/demos/chat/protected/App_Code/ChatUserRecord.php new file mode 100644 index 00000000..9bfb11cd --- /dev/null +++ b/demos/chat/protected/App_Code/ChatUserRecord.php @@ -0,0 +1,41 @@ +_last_activity === null) + $this->_last_activity = time(); + return $this->_last_activity; + } + + public function setLast_Activity($value) + { + $this->_last_activity = $value; + } + + public static function finder() + { + return parent::getRecordFinder('ChatUserRecord'); + } + + public function getUserList() + { + $this->deleteAll('last_activity < ?', time()-300); //5 min inactivity + $content = ''; + + return $content; + } +} + +?> \ No newline at end of file diff --git a/demos/chat/protected/App_Code/chat.db b/demos/chat/protected/App_Code/chat.db new file mode 100644 index 00000000..f8e3660f Binary files /dev/null and b/demos/chat/protected/App_Code/chat.db differ diff --git a/demos/chat/protected/application.xml b/demos/chat/protected/application.xml new file mode 100644 index 00000000..7153d5af --- /dev/null +++ b/demos/chat/protected/application.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demos/chat/protected/pages/Home.page b/demos/chat/protected/pages/Home.page new file mode 100644 index 00000000..b896cd28 --- /dev/null +++ b/demos/chat/protected/pages/Home.page @@ -0,0 +1,51 @@ + + + + + Prado Chat Demo + + + + + +

Prado Chat Demo

+ +
+ +
+
+ +
+
+ + + OnClick="processMessage"/> +
+ + + + + Event.observe($("<%= $this->userinput->ClientID %>"), "keypress", function(ev) + { + if(Event.keyCode(ev) == Event.KEY_RETURN) + { + if(Event.element(ev).value.length > 0) + new Prado.Callback("<%= $this->sendButton->UniqueID %>"); + Event.stop(ev); + } + }); + +
+ + + + + + \ No newline at end of file diff --git a/demos/chat/protected/pages/Home.php b/demos/chat/protected/pages/Home.php new file mode 100644 index 00000000..65cb22dc --- /dev/null +++ b/demos/chat/protected/pages/Home.php @@ -0,0 +1,56 @@ +Application->User->Name; + if(!$this->Application->Modules['users']->usernameExists($username)) + { + $auth = $this->Application->Modules['auth']; + $auth->logout(); + + //redirect to login page. + $this->Response->Redirect($this->Service->ConstructUrl($auth->LoginPage)); + } + } + + function processMessage($sender, $param) + { + if(strlen($this->userinput->Text) > 0) + { + $record = new ChatBufferRecord(); + $record->message = $this->userinput->Text; + $record->from_user = $this->Application->User->Name; + $record->saveMessage(); + $this->userinput->Text = ''; + $this->refresh($sender, $param); + $this->CallbackClient->focus($this->userinput); + } + } + + function refresh($sender, $param) + { + //refresh the message list + $content = ChatBufferRecord::finder()->getUserMessages($this->Application->User->Name); + if(strlen($content) > 0) + { + $client = $this->Page->CallbackClient; + $anchor = (string)time(); + $content .= " "; + $client->appendContent("messages", $content); + $client->focus($anchor); + } + + //refresh the user list + $lastUpdate = $this->getViewState('userList',''); + $users = ChatUserRecord::finder()->getUserList(); + if($lastUpdate != $users) + { + $this->Page->CallbackClient->update('users', $users); + $this->setViewstate('userList', $users); + } + } +} + +?> \ No newline at end of file diff --git a/demos/chat/protected/pages/Login.page b/demos/chat/protected/pages/Login.page new file mode 100644 index 00000000..0d0a3fa8 --- /dev/null +++ b/demos/chat/protected/pages/Login.page @@ -0,0 +1,35 @@ + + + + + Prado Chat Demo Login + + + + + + +

Prado Chat Demo Login

+