diff options
Diffstat (limited to 'db.php')
-rw-r--r-- | db.php | 63 |
1 files changed, 59 insertions, 4 deletions
@@ -1,13 +1,40 @@ <?php +function url_to_a($url, $title = NULL) { + if(!isset($title)) { + $title = htmlspecialchars($url); + } + return "<a href='" . htmlspecialchars($url) . "'>$title</a>"; +} + class Queue { public $name; public $endpoint_url; function toPlainText($url_generator) { return "Name: " . $this->name . "\n" . + "Queue URL: " . $url_generator->queue($this->name) . "\n" . "Endpoint URL: " . (isset($this->endpoint_url) ? $this->endpoint_url : "Not set") . "\n" . - "Enqueue: " . $url_generator->enqueue($this->name) . "\n"; + "Enqueue: " . $url_generator->enqueue($this->name) . "\n" . + "Items: " . $url_generator->queue_items($this->name) . "\n"; + } + + function toHtml($url_generator, $detail) { + $queue_url = url_to_a($url_generator->queue($this->name)); + $endpoint = !isset($this->endpoint_url) ? "Not set" : htmlspecialchars($this->endpoint_url); + $enqueue_url = url_to_a($url_generator->enqueue($this->name)); + $items_url = url_to_a($url_generator->queue_items($this->name)); + $html = + "<h2>Queue: $this->name</h2>" . + "<table>" . + "<tr><td>Queue URL</td><td>$queue_url</td></tr>"; + if($detail) $html .= + "<tr><td>Endpoint URL</td><td>$endpoint</td></tr>" . + "<tr><td>Enqueue</td><td>$enqueue_url</td></tr>" . + "<tr><td>Items</td><td>$items_url</td></tr>"; + $html .= + "</table>"; + return $html; } static function valid_name($str) { @@ -39,6 +66,24 @@ class Item { } return "Headers:\n" . $headers; } + + function toHtml($url_generator) { + $item_url = url_to_a($url_generator->item($this->queue_name, $this->id)); + $html = + "<h2>Item</h2>" . + "<h3>Meta</h3>" . + "<ul>" . + "<li>Id: $this->id</li>" . + "<li>Created: $this->created</li>" . + "<li>Link: $item_url</li>" . + "</ul>" . + "<h3>Headers</h3>" . + "<ul>\n"; + foreach($this->headers as $k => $v) { + $html .= "<li>" . $k . ": " . $v . "</li>\n"; + } + return $html . "</ul>\n"; + } } class DB { @@ -100,7 +145,15 @@ class DB { $stmt->setFetchMode(PDO::FETCH_CLASS, "Queue"); $stmt->bindParam(":name", $queue); $stmt->execute(); - return $stmt->fetch(PDO::FETCH_CLASS); + $x = $stmt->fetch(PDO::FETCH_CLASS); + return $x === FALSE ? NULL : $x; + } + + function delete_item($queue_name, $id) { + $stmt = $this->db->prepare("DELETE FROM item WHERE queue_name = :queue_name AND id = :id"); + $stmt->bindParam(":queue_name", $queue_name); + $stmt->bindParam(":id", $id); + $stmt->execute(); } function select_item($queue_name, $id) { @@ -108,7 +161,8 @@ class DB { $stmt->bindParam(":queue_name", $queue_name); $stmt->bindParam(":id", $id); $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_FUNC, 'item_from_db'); + $x = $stmt->fetchAll(PDO::FETCH_FUNC, 'item_from_db'); + return count($x) > 0 ? $x[0] : NULL; } function select_items($queue_name, $limit = 10, $offset = 0) { @@ -117,7 +171,8 @@ class DB { $stmt->bindParam(":limit", $limit); $stmt->bindParam(":offset", $offset); $stmt->execute(); - return $stmt->fetchAll(PDO::FETCH_FUNC, 'item_from_db'); + $x = $stmt->fetchAll(PDO::FETCH_FUNC, 'item_from_db'); + return $x; } } |