-
Notifications
You must be signed in to change notification settings - Fork 0
Intervals, Database and Registry
Let's create something more advanced. We will create the chat application with the login information.
First let's create our login view.
app/view/chat/chatLogin.php
:
<!DOCTYPE html>
<html>
<head>
<title>EOSS Introduction</title>
</head>
<body>
<div id="error" style="color: red"></div>
<input type="text" id="username" /><br>
<input type="password" id="password" /><br>
<input type="button" id="next" value="Next" />
</body>
</html>
Inside the #error
we will print the Invalid credentials
when the user enters wrong username or password.
Now let's create indexEOSS
.
app/controller/indexEOSS.php
:
<?php
use \EOSS\EOSS;
class indexEOSS extends EOSS
{
public function load()
{
$this->csi->setFile("chat/chatLogin.php");
}
public function bind()
{
$this->csi->next->onclick[] = "next";
}
public function next($sender) {
$database = new \Database\PDOWrapper('localhost', 'username', 'password', 'testdatabase');
if($row = $database->prepareExecuteAndFetch("SELECT * FROM users WHERE username = ? AND password = ?", $this->csi->username->value, $this->csi->password->value)) {
$this->csi->error->html = "";
\EOSS\Registry::getInstance()->username = $this->csi->username->value;
$this->redirect("chatEOSS");
} else {
$this->csi->error->html = "Invalid credentials.";
}
}
}
We will pass the username into the chatEOSS
through the Registry
which is the Singleton pattern and is stored inside Sessions.
Let's create a chat view.
app/view/chat/chat.php
:
<!DOCTYPE html>
<html>
<head>
<title>EOSS Introduction</title>
</head>
<body>
<div id="chat"><?= isset($chat) ? $chat : "" ?></div>
<input type="text" id="message">
<input type="button" id="send" value="Send">
<input type="button" id="back" value="Back" />
</body>
</html>
Now we need to create a ChatModel
which will look like this:
app/models/ChatModel.php
:
<?php
class ChatModel
{
/**
* @var \Database\PDOWrapper
*/
private $database;
public function __construct() {
$this->database = new \Database\PDOWrapper('localhost', 'username', 'password', 'testdatabase');
}
public function getChatMessagesFormatted() {
$chat = "";
if($row = $this->database->queryAndFetchAll("SELECT * FROM chat LIMIT 10")) {
foreach($row as $r) {
$chat .= "<b>" . $r["username"] . "</b>: " . $r["message"] . "<br>";
}
}
return $chat;
}
public function sendMessage($username, $message) {
$this->database->prepareAndExecute("INSERT INTO chat(username, message) VALUES(?, ?)", $username, $message);
}
}
Now let's create
Now we can finally create our chatEOSS
.
app/controller/chatEOSS.php
:
<?php
use \EOSS\EOSS;
class chatEOSS extends EOSS
{
/**
* @var ChatModel|null
*/
private $chatModel = NULL;
public function load()
{
$this->chatModel = new ChatModel();
$this->csi->setFile("chat/chat.php");
$this->csi->params->chat = $this->chatModel->getChatMessagesFormatted();
}
public function bind()
{
$this->csi->send->onclick[] = "sendMsg";
$this->csi->back->onclick[] = "back";
$this->csi->intervals["reloadPosts"] = 500;
}
public function reloadPosts() {
$this->csi->chat->html = $this->chatModel->getChatMessagesFormatted();
}
public function sendMsg($sender) {
$this->chatModel->sendMessage(\EOSS\Registry::getInstance()->username, $this->csi->message->value);
$this->csi->message->value = "";
}
public function back($sender) {
$this->redirect("chatLoginEOSS");
}
}
We've set the interval to call reloadPosts
every 500 ms. The syntax is $this->csi->intervals["functionYouWantToCall"] = 500
.
And now we're done...
Welcome to EOSS, we are glad to see you here.
We are looking for contributors, come and join us, write me an email on [email protected] or post something on a http://eoss.wz.sk .