1<?php
2/**
3 * DokuWiki Plugin ac (activecollab Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Adrian Lang <lang@cosmocode.de>
7 */
8
9require_once DOKU_INC . 'inc/HTTPClient.php';
10require_once DOKU_INC . 'inc/JSON.php';
11
12class syntax_plugin_ac_ac {
13
14    public function __construct($url, $token) {
15        $this->base_url = "{$url}?token={$token}&format=json";
16    }
17
18    public function get($path, $data = array()) {
19        $client = new DokuHTTPClient();
20        $json = new JSON();
21        return $json->decode($client->get($this->base_url . '&' .
22                                          "path_info=/{$path}&" .
23                                          buildURLparams($data, '&')));
24    }
25
26    public function objToString($obj) {
27        return "<a href='$obj->permalink'>$obj->name</a>";
28    }
29
30    public function fetch($type, $values) {
31        $all = $this->get($type);
32        $ret = array();
33        foreach($all as $elem) {
34            $match = true;
35            foreach($values as $prop => $val) {
36                if ($elem->$prop !== $val) {
37                    $match = false;
38                    break;
39                }
40            }
41            if ($match) {
42                $ret[] = $elem;
43            }
44        }
45        return $ret;
46    }
47
48    public function fetchSingle($type, $values) {
49        $ret = $this->fetch($type, $values);
50        return (count($ret) === 0) ?  false : $ret[0];
51    }
52}
53
54// vim:ts=4:sw=4:et:enc=utf-8:
55