first work on rendering
authorChristian Weiske <cweiske@cweiske.de>
Wed, 19 Jun 2013 21:55:52 +0000 (23:55 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 19 Jun 2013 21:55:52 +0000 (23:55 +0200)
data/templates/default/mentions-comment.htm [new file with mode: 0644]
data/templates/default/mentions-link.htm [new file with mode: 0644]
data/templates/default/mentions.htm [new file with mode: 0644]
src/stapibas/Renderer/Html.php [new file with mode: 0644]
www/render.php [new file with mode: 0644]

diff --git a/data/templates/default/mentions-comment.htm b/data/templates/default/mentions-comment.htm
new file mode 100644 (file)
index 0000000..929f144
--- /dev/null
@@ -0,0 +1,10 @@
+{% if commentRow.rc_author_image %}<img src="{{commentRow.rc_author_image}}" alt="Avatar for {{commentRow.rc_author_name}}" style="max-width: 48px; max-height: 48px"/>{% endif %}
+
+{% if linkRow.rc_author_url %}<a href="{{linkRow.rc_author_url}}">{% endif %}
+{{commentRow.rc_author_name}}
+{% if linkRow.rc_author_url %}</a>{% endif %}
+
+<div>
+ {% if commentRow.rc_title %}<h3>{{commentRow.rc_title}}</h3>{% endif %}
+ {{commentRow.rl_comment|raw}}
+</div>
diff --git a/data/templates/default/mentions-link.htm b/data/templates/default/mentions-link.htm
new file mode 100644 (file)
index 0000000..7dfad64
--- /dev/null
@@ -0,0 +1,7 @@
+<a href="{{linkRow.rl_source}}">{{linkRow.rl_title}}</a>
+{% if linkRow.rl_author_name %}
+  by 
+  {% if linkRow.rl_author_url %}<a href="{{linkRow.rl_author_url}}">{% endif %}
+  {{linkRow.rl_author_name}}
+  {% if linkRow.rl_author_url %}</a>{% endif %}
+{% endif %}
diff --git a/data/templates/default/mentions.htm b/data/templates/default/mentions.htm
new file mode 100644 (file)
index 0000000..7f57d32
--- /dev/null
@@ -0,0 +1,17 @@
+{% if arData.comments %}
+<h3 id="m-comments">Comments</h3>
+<ul class="m-container m-comments">
+ {% for commentRow in arData.comments %}
+   {% include 'mentions-comment.htm' %}
+ {% endfor %}
+</ul>
+{% endif %}
+
+{% if arData.links %}
+<h3 id="m-links">Links to this page</h3>
+<ul class="m-container m-links">
+ {% for linkRow in arData.links %}
+   <li>{% include 'mentions-link.htm' %}</li>
+ {% endfor %}
+</ul>
+{% endif %}
diff --git a/src/stapibas/Renderer/Html.php b/src/stapibas/Renderer/Html.php
new file mode 100644 (file)
index 0000000..ca26584
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+namespace stapibas;
+
+class Renderer_Html
+{
+    public $db;
+    public $log;
+
+    public function __construct(Dependencies $deps)
+    {
+        $this->deps = $deps;
+        $this->db   = $deps->db;
+        $this->log  = $deps->log;
+
+        \Twig_Autoloader::register();
+        $loader = new \Twig_Loader_Filesystem($this->deps->options['template_dir']);
+        $this->deps->twig = new \Twig_Environment(
+            $loader,
+            array(
+                //'cache' => '/path/to/compilation_cache',
+                'debug' => true
+            )
+        );
+    }
+
+    public function render($url)
+    {
+        $arData = $this->loadData($url);
+        header('Content-type: text/html');
+        $this->renderHtml('mentions', array('arData' => $arData));
+    }
+
+    /**
+     * Fetches all bookmarks, comments and links
+     */
+    protected function loadData($url)
+    {
+        $arData = array(
+            'bookmarks' => array(),
+            'comments'  => array(),
+            'links'     => array(),
+        );
+
+        $stmt = $this->db->query(
+            'SELECT * FROM pingbacks, rbookmarks'
+            . ' WHERE p_id = rb_p_id AND p_use = 1'
+            . ' AND p_target = ' . $this->db->quote($url)
+            . ' ORDER BY p_time ASC'
+        );
+        $arData['bookmarks'] = $stmt->fetchAll();
+
+        $stmt = $this->db->query(
+            'SELECT * FROM pingbacks, rcomments'
+            . ' WHERE p_id = rc_p_id AND p_use = 1'
+            . ' AND p_target = ' . $this->db->quote($url)
+            . ' ORDER BY p_time ASC'
+        );
+        $arData['comments'] = $stmt->fetchAll();
+
+        $stmt = $this->db->query(
+            'SELECT * FROM pingbacks, rlinks'
+            . ' WHERE p_id = rl_p_id AND p_use = 1'
+            . ' AND p_target = ' . $this->db->quote($url)
+            . ' ORDER BY p_time ASC'
+        );
+        $arData['links'] = $stmt->fetchAll();
+
+        return $arData;
+    }
+
+    protected function renderHtml($tplname, $vars = array())
+    {
+        $template = $this->deps->twig->loadTemplate($tplname . '.htm');
+        echo $template->render($vars);
+    }
+
+}
+?>
diff --git a/www/render.php b/www/render.php
new file mode 100644 (file)
index 0000000..e460b42
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+namespace stapibas;
+/**
+ * Render the bookmarks/comments/links for a given URL
+ *
+ * @param string $url URL to get content for
+ */
+header('HTTP/1.0 500 Internal Server Error');
+header('Content-type: text/plain');
+
+require_once __DIR__ . '/../data/config.php';
+require_once 'stapibas/autoloader.php';
+
+if (!isset($_GET['url'])) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "HTTP POST 'url' parameter missing\n";
+    exit(1);
+}
+$url = $_GET['url'];
+if ($url === '') {
+    header('HTTP/1.0 400 Bad Request');
+    echo "'url' parameter is empty\n";
+    exit(1);
+}
+if (filter_var($url, FILTER_VALIDATE_URL) === false) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Invalid URL given\n";
+    exit(1);
+}
+
+
+$deps = new Dependencies();
+$deps->db = new PDO($dbdsn, $dbuser, $dbpass);
+$deps->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$deps->options = array(
+    'template_dir' => __DIR__ . '/../data/templates/default/'
+);
+
+$r = new Renderer_Html($deps);
+echo $r->render($url);
+?>