first work on rendering
[stapibas.git] / src / stapibas / Renderer / Html.php
1 <?php
2 namespace stapibas;
3
4 class Renderer_Html
5 {
6     public $db;
7     public $log;
8
9     public function __construct(Dependencies $deps)
10     {
11         $this->deps = $deps;
12         $this->db   = $deps->db;
13         $this->log  = $deps->log;
14
15         \Twig_Autoloader::register();
16         $loader = new \Twig_Loader_Filesystem($this->deps->options['template_dir']);
17         $this->deps->twig = new \Twig_Environment(
18             $loader,
19             array(
20                 //'cache' => '/path/to/compilation_cache',
21                 'debug' => true
22             )
23         );
24     }
25
26     public function render($url)
27     {
28         $arData = $this->loadData($url);
29         header('Content-type: text/html');
30         $this->renderHtml('mentions', array('arData' => $arData));
31     }
32
33     /**
34      * Fetches all bookmarks, comments and links
35      */
36     protected function loadData($url)
37     {
38         $arData = array(
39             'bookmarks' => array(),
40             'comments'  => array(),
41             'links'     => array(),
42         );
43
44         $stmt = $this->db->query(
45             'SELECT * FROM pingbacks, rbookmarks'
46             . ' WHERE p_id = rb_p_id AND p_use = 1'
47             . ' AND p_target = ' . $this->db->quote($url)
48             . ' ORDER BY p_time ASC'
49         );
50         $arData['bookmarks'] = $stmt->fetchAll();
51
52         $stmt = $this->db->query(
53             'SELECT * FROM pingbacks, rcomments'
54             . ' WHERE p_id = rc_p_id AND p_use = 1'
55             . ' AND p_target = ' . $this->db->quote($url)
56             . ' ORDER BY p_time ASC'
57         );
58         $arData['comments'] = $stmt->fetchAll();
59
60         $stmt = $this->db->query(
61             'SELECT * FROM pingbacks, rlinks'
62             . ' WHERE p_id = rl_p_id AND p_use = 1'
63             . ' AND p_target = ' . $this->db->quote($url)
64             . ' ORDER BY p_time ASC'
65         );
66         $arData['links'] = $stmt->fetchAll();
67
68         return $arData;
69     }
70
71     protected function renderHtml($tplname, $vars = array())
72     {
73         $template = $this->deps->twig->loadTemplate($tplname . '.htm');
74         echo $template->render($vars);
75     }
76
77 }
78 ?>