Skip to content

Instantly share code, notes, and snippets.

@hhamon
Last active December 17, 2015 12:29
Show Gist options
  • Save hhamon/5609617 to your computer and use it in GitHub Desktop.
Save hhamon/5609617 to your computer and use it in GitHub Desktop.
<?php $view->extend('layout.php') ?>
<?php $view['slots']->set('title', 'Tasks Management') ?>
<form action="" method="post">
<div>
<label for="title">Title:</label>
<input type="text" id="title" name="title" size="45"/>
<input type="hidden" name="action" value="create"/>
<button type="submit">send</button>
</div>
</form>
<p>
There are <strong><?= $count ?></strong> tasks.
</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($tasks as $todo) : ?>
<tr>
<td class="center"><?= $todo['id'] ?></td>
<td>
<a href="">
<?= $view->escape($todo['title']) ?>
</a>
</td>
<td class="center">
<?php if ($todo['is_done']) : ?>
<span class="done">done</span>
<?php else : ?>
<a href="">
close
</a>
<?php endif ?>
</td>
<td class="center">
<a href="">
delete
</a>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php $view['slots']->output('title', 'Todo Application') ?></title>
<link rel="stylesheet" media="screen" type="text/css" href="/style.css"/>
</head>
<body>
<div id="container">
<h1>
<a href="">My Todos List</a>
</h1>
<div id="content">
<?php $view['slots']->output('_content') ?>
</div>
<div id="footer">
(c) copyright - not sensio
</div>
</div>
</body>
</html>
<?php
namespace Todo\Persistence;
class TodoGateway
{
private $dbh;
private $hostname;
private $username;
private $password;
private $database;
public function __construct($database, $username, $password, $hostname)
{
$this->database = $database;
$this->username = $username;
$this->password = $password;
$this->hostname = $hostname;
}
private function connect()
{
if (null !== $this->dbh) {
return;
}
$this->dbh = mysql_connect($this->hostname, $this->username, $this->password);
if (false === $this->dbh) {
throw new \RuntimeException('Cannot connect to MySQL server.');
}
if (false === mysql_select_db($this->database, $this->dbh)) {
throw new \RuntimeException('Cannot select database.');
}
}
private function query($query)
{
$this->connect();
$result = mysql_query($query, $this->dbh);
if (false === $result) {
throw new \RuntimeException('SQL query execution failed: '.$query);
}
return $result;
}
private function quote($data)
{
$this->connect();
return mysql_real_escape_string($data, $this->dbh);
}
public function deleteTask($id)
{
$this->query('DELETE FROM todo WHERE id = '. (int) $id);
if (!mysql_affected_rows($this->dbh)) {
throw new \RuntimeException('Unable to delete task #'.$id);
}
}
public function closeTask($id)
{
$this->query('UPDATE todo SET is_done = 1 WHERE id = '. (int) $id);
if (!mysql_affected_rows($this->dbh)) {
throw new \RuntimeException('Unable to close task #'.$id);
}
}
public function createTask($title)
{
if (empty($title)) {
throw new \InvalidArgumentException('Title must be filled.');
}
$this->query(sprintf(
"INSERT INTO todo (title) VALUES ('%s');",
$this->quote($title)
));
if (!mysql_affected_rows($this->dbh)) {
throw new \RuntimeException(sprintf('Unable to create new task "%s".', $title));
}
}
public function countTasks()
{
$result = $this->query('SELECT COUNT(*) FROM todo');
return (int) current(mysql_fetch_row($result));
}
public function getTask($id)
{
$result = $this->query('SELECT * FROM todo WHERE id = '. (int) $id);
return mysql_fetch_assoc($result);
}
public function getAllTasks()
{
$tasks = array();
$result = $this->query('SELECT * FROM todo');
while ($todo = mysql_fetch_assoc($result)) {
$tasks[] = $todo;
}
return $tasks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment