summaryrefslogtreecommitdiff
path: root/demos/chat/protected/pages/Home.php
blob: bdcb2d3b2f3f9ba882a8eb1b4f283181853b81c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

class Home extends TPage
{
	public function onLoad($param)
	{
		$username = $this->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 .= "<a href=\"#\" id=\"{$anchor}\"> </a>";
			$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);
		}
	}
}

?>