1<?php
2/**
3 * DokuWiki Plugin starred (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/**
13 * Class helper_plugin_starred
14 */
15class helper_plugin_starred extends DokuWiki_Plugin {
16
17    /**
18     * load the sqlite helper
19     *
20     * @return helper_plugin_sqlite|false
21     */
22    public function getDB() {
23        /** @var helper_plugin_sqlite $db */
24        $db = plugin_load('helper', 'sqlite');
25        if(!is_null($db) && $db->init('starred', dirname(__FILE__) . '/db/')) {
26            return $db;
27        } else {
28            msg($this->getLang('e_nosqlite'), -1);
29            return false;
30        }
31    }
32
33    /**
34     * toggle the star for the user and page
35     * @param string|null $user defaults to current user
36     * @param string|null $pageid defaults to current page
37     */
38    public function toggleStar($user = null, $pageid = null) {
39        global $ID;
40
41        // DB access
42        $db = $this->getDB();
43        if(!$db) return;
44
45        // param defaults
46        if($pageid === null) $pageid = $ID;
47        if($user === null) $user = $_SERVER['REMOTE_USER'];
48        if(blank($user)) return;
49
50        $on = $this->checkStar($user, $pageid); // currently on?
51
52        if($on) {
53            //delete
54            $sql = "DELETE FROM stars WHERE pid = ? AND login = ?";
55            $db->query($sql, $pageid, $user);
56        } else {
57            //add
58            $sql = "INSERT OR IGNORE INTO stars (pid,login,stardate) VALUES (?,?,?)";
59            $db->query($sql, $pageid, $user, time());
60        }
61    }
62
63    /**
64     * check the star for the current user and page
65     *
66     * @param string|null $user defaults to current user
67     * @param string|null $pageid defaults to current page
68     * @return bool|int the time the star was added or false if not
69     */
70    public function checkStar($user = null, $pageid = null) {
71        global $ID;
72
73        // DB access
74        $db = $this->getDB();
75        if(!$db) return false;
76
77        // param defaults
78        if($pageid === null) $pageid = $ID;
79        if($user === null) $user = $_SERVER['REMOTE_USER'];
80        if(blank($user)) return false;
81
82        $sql = "SELECT stardate FROM stars WHERE pid = ? AND login = ?";
83        $res = $db->query($sql, $pageid, $user);
84        $row = $db->res2row($res);
85        return (int) $row['stardate'];
86    }
87
88    /**
89     * Load the starred pages of a given user
90     *
91     * @param string|null $user defaults to current user
92     * @param int $limit defaults to all
93     * @return array|bool
94     */
95    public function loadStars($user = null, $limit = 0) {
96        $result = array();
97
98        $db = $this->getDB();
99        if(!$db) return $result;
100
101        if($user === null) $user = $_SERVER['REMOTE_USER'];
102        if(blank($user)) return $result;
103
104        /** @var DokuWiki_Auth_Plugin $auth */
105        global $auth;
106
107        $sql = "SELECT pid, stardate FROM stars WHERE ";
108
109        if($auth && !$auth->isCaseSensitive()) {
110            $sql .= 'lower(login) = lower(?)';
111        } else {
112            $sql .= 'login = ?';
113        }
114        $sql .= " ORDER BY stardate DESC";
115        if($limit) {
116            $sql .= ' LIMIT ' . $limit;
117        }
118        $res = $db->query($sql, $user);
119        $arr = $db->res2arr($res);
120
121        foreach($arr as $row) {
122            $result[$row['pid']] = $row['stardate'];
123        }
124
125        return $result;
126    }
127
128    /**
129     * Create the html for a star
130     *
131     * @param string $ID        The page where the star is supposed to appear.
132     * @param string $pageid The page which the star is supposed to toggle.
133     * @param bool   $inneronly
134     * @param bool   $setid        Must not be true more than once per page
135     * @return string The html for the star
136     */
137    public function starHtml($ID, $pageid, $inneronly=false, $setid=false) {
138        $result = '';
139        $dt = $this->checkStar(null, $pageid);
140        if($inneronly === false) {
141            $result .= '<a href="' . wl($ID, array('do' => 'startoggle_' . $pageid)) . '" data-pageid="'.$pageid.'" class="plugin__starred"';
142            if($setid === true) {
143                $result .= ' id="plugin__starred">';
144            } else {
145                $result .= '>';
146            }
147        }
148
149        if($dt){
150            $result .= '<span title="'.$this->getLang('star_on').'" class="starred on">';
151            $result .= inlineSVG(__DIR__ . '/pix/star.svg');
152            $result .= '</span>';
153            //$result .= '<img src="'.DOKU_BASE.'lib/plugins/starred/pix/star.png" width="16" height="16" title="'.$this->getLang('star_on').'" alt="★" />';
154        }else{
155            $result .= '<span title="'.$this->getLang('star_off').'" class="starred off">';
156            $result .= inlineSVG(__DIR__ . '/pix/star-outline.svg');
157            $result .= '</span>';
158            //$result .= '<img src="'.DOKU_BASE.'lib/plugins/starred/pix/star_grey.png" width="16" height="16" title="'.$this->getLang('star_off').'" alt="☆" />';
159        }
160        if(!$inneronly) {
161            $result .=  '</a>';
162        }
163        return $result;
164    }
165}
166