1<?php
2/**
3 * DokuWiki Plugin ffindr (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Christian Schneider <cschneid@cschneid.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'syntax.php';
17require_once(DOKU_INC.'inc/FeedParser.php');
18
19class FFindrParser extends FeedParser {
20    function FFindrParser() {
21        parent::__construct();
22        $this->set_item_class($class = 'FFindrParser_Item');
23    }
24
25    static function sort_items($a, $b) {
26        return strcmp($a->get_tag('dateStart'), $b->get_tag('dateStart'));
27    }
28}
29
30class FFindrParser_Item extends SimplePie_Item {
31    function get_tag($name) {
32        $tag = $this->get_item_tags("", $name);
33        return $this->sanitize($tag[0]['data'], SIMPLEPIE_CONSTRUCT_TEXT);
34    }
35}
36
37class syntax_plugin_ffindr_ffindr extends DokuWiki_Syntax_Plugin {
38    function getType() {
39        return 'substition';
40    }
41
42    function getPType() {
43        return 'block';
44    }
45
46    function getSort() {
47        return 310;
48    }
49
50    function connectTo($mode) {
51        $this->Lexer->addSpecialPattern('{{ffindr>.*?}}',$mode,'plugin_ffindr_ffindr');
52    }
53
54    function handle($match, $dummy_state, $dummy_pos, &$dummy_handler){
55        $data = array();
56        $match = substr($match, 9, -2);
57        list($url, $dummy_header)   = explode('|', $match, 2);
58        list($url, $params) = explode(' ', $url, 2);
59
60        foreach (explode(' ', $params) as $param) {
61            list($key, $value) = explode('=', $param, 2);
62            if ($value)
63                $data[$key] = $value;
64            else
65                $format[] = $key;
66        }
67
68        $format = array_filter((array)$format);
69
70        return $data + array(
71            'url' => $url,
72            'max' => 100,
73            'format' => $format ? $format : explode(' ', "date title location"),
74        );
75    }
76
77    function render($mode, &$renderer, $data) {
78        if($mode != 'xhtml') return false;
79
80        if ($data['url']) {
81            $feed = new FFindrParser();
82            $feed->set_feed_url($data['url']);
83            // disable warning while fetching
84            if (!defined('DOKU_E_LEVEL'))
85                $elvl = error_reporting(E_ERROR);
86            $rc = $feed->init();
87            if (!defined('DOKU_E_LEVEL'))
88              error_reporting($elvl);
89
90            // decide on start and end
91            $max = max(1, intval($data['max'] - 1));
92            if ($data['sort'] == "reverse") {
93                $start = $feed->get_item_quantity() - 1;
94                $end   = max(0, $start - $max);
95                $step = -1;
96            } else {
97                $start = 0;
98                $end   = min($max, $feed->get_item_quantity() - 1);
99                $step   = 1;
100            }
101
102            if($rc) {
103                $dateformat = "%d.%m.";
104                $renderer->doc .= '<table class="inline">';
105
106                foreach (range($start, $end, $step) as $i) {
107                    $item = $feed->get_item($i);
108                    $renderer->doc .= '<tr>';
109
110                    foreach ($data['format'] as $cell) {
111                        $renderer->doc .= "<td>";
112
113                        switch (trim($cell)) {
114                        case 'date': {
115                            $renderer->doc .= strftime($dateformat, strtotime($item->get_tag('dateStart'))) . "&nbsp;-&nbsp;" . strftime($dateformat, strtotime($item->get_tag('dateEnd')));
116                            break;
117                        }
118
119                        case 'title': {
120                            // support feeds without links
121                            if ($linkurl = $item->get_permalink()) {
122                                // title is escaped by SimplePie, we unescape here because it
123                                // is escaped again in externallink() FS#1705
124                                $renderer->externallink($linkurl, htmlspecialchars_decode($item->get_title()));
125                            }
126                            else
127                                $renderer->doc .= $item->get_title();
128                            break;
129                        }
130
131                        case  'author': {
132                            if ($author = $item->get_author(0)) {
133                                $name = $author->get_name();
134                                if(!$name)
135                                    $name = $author->get_email();
136                                if($name)
137                                    $renderer->doc .= $lang['by'].' '.$name ;
138                            }
139                            break;
140                        }
141
142                        case  'location': {
143                            $renderer->doc .= '<a href="http://' . ($item->get_tag('country') == "Switzerland" ? "map.search.ch/" : "maps.google.com?q=") . $item->get_latitude() . "," . $item->get_longitude() . '">' . $item->get_tag('location') . "</a>";
144                            break;
145                        }
146                        }
147
148                        $renderer->doc .= "</td>";
149                    }
150
151                    $renderer->doc .= "</tr>\n";
152                }
153
154                $renderer->doc .= "</table>";
155            } else {
156                $renderer->doc .= '<ul><li><div class="li"><em>'.$lang['rssfailed'].'</em>';
157                $renderer->externallink($data['url']);
158                if($GLOBALS['conf']['allowdebug'])
159                    $renderer->doc .= '<!--' . hsc($feed->error) . '-->';
160                $renderer->doc .= '</div></li></ul>';
161            }
162        } else
163            $renderer->doc .= '<div class="ffindr">No FFindr URL specified</div>';
164
165        return true;
166    }
167}
168
169// vim:ts=4:sw=4:et:
170