Skip to content

Instantly share code, notes, and snippets.

@hhamon
Last active December 17, 2015 12:29
Show Gist options
  • Select an option

  • Save hhamon/5609617 to your computer and use it in GitHub Desktop.

Select an option

Save hhamon/5609617 to your computer and use it in GitHub Desktop.

Revisions

  1. Hugo Hamon revised this gist May 20, 2013. 1 changed file with 112 additions and 0 deletions.
    112 changes: 112 additions & 0 deletions TodoGateway.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    <?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;
    }
    }
  2. Hugo Hamon revised this gist May 19, 2013. No changes.
  3. Hugo Hamon revised this gist May 19, 2013. No changes.
  4. Hugo Hamon revised this gist May 19, 2013. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?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>
  5. Hugo Hamon created this gist May 19, 2013.
    21 changes: 21 additions & 0 deletions layout.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <!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>