summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/CommonDescription.php
blob: 39c5826bfb57f2e35bc7cdb7c3c650ef0d989203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * SemanticScuttle common bookmark description service.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Service_CommonDescription extends SemanticScuttle_DbService
{
    /**
     * Returns the single service instance
     *
     * @param DB $db Database object
     *
     * @return SemanticScuttle_Service
     */
    public static function getInstance($db)
    {
        static $instance;
        if (!isset($instance)) {
            $instance = new self($db);
        }
        return $instance;
    }

    public function __construct($db)
    {
        $this->db = $db;
        $this->tablename = $GLOBALS['tableprefix'] .'commondescription';
    }

    function addTagDescription($tag, $desc, $uId, $time) {
        $tag = self::getSortedTag($tag);

        // Check if no modification
        $lastDesc = $this->getLastTagDescription($tag);
        if($lastDesc['cdDescription'] == $desc) {
            return true;
        }

        // If modification
        $datetime = gmdate('Y-m-d H:i:s', $time);
        $values = array('tag'=>$tag, 'cdDescription'=>$desc, 'uId'=>$uId, 'cdDatetime'=>$datetime);
        $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);

        if (!($dbresult =& $this->db->sql_query($sql))) {
            $this->db->sql_transaction('rollback');
            message_die(GENERAL_ERROR, 'Could not add tag description', '', __LINE__, __FILE__, $sql, $this->db);
            return false;
        }

        return true;
    }

    function getLastTagDescription($tag) {
        $tag = self::getSortedTag($tag);

        $query = "SELECT *";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag='".$tag."'";
        $query.= " ORDER BY cdDatetime DESC";

        if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) {
            message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        $row = $this->db->sql_fetchrow($dbresult);
        $this->db->sql_freeresult($dbresult);
        if ($row) {
            return $row;
        } else {
            return false;
        }
    }

    function getAllTagsDescription($tag) {
        $query = "SELECT *";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag='".$tag."'";
        $query.= " ORDER BY cdDatetime DESC";

        if (!($dbresult = & $this->db->sql_query($query))) {
            message_die(GENERAL_ERROR, 'Could not get tag descriptions', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        $res = $this->db->sql_fetchrowset($dbresult);
        $this->db->sql_freeresult($dbresult);
        return $res;
    }

    function getDescriptionById($cdId) {
        $query = "SELECT *";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE cdId='".$cdId."'";

        if (!($dbresult = & $this->db->sql_query($query))) {
            message_die(GENERAL_ERROR, 'Could not get tag descriptions', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        if ($row =& $this->db->sql_fetchrow($dbresult)) {
            $this->db->sql_freeresult($dbresult);
            return $row;
        } else {
            return false;
        }

    }

    function addBookmarkDescription($bHash, $title, $desc, $uId, $time) {
        // Check if no modification
        $lastDesc = $this->getLastBookmarkDescription($bHash);
        if($lastDesc['cdTitle'] == $title && $lastDesc['cdDescription'] == $desc) {
            return true;
        }

        // If modification
        $datetime = gmdate('Y-m-d H:i:s', $time);
        $values = array('bHash'=>$bHash, 'cdTitle'=>$title, 'cdDescription'=>$desc, 'uId'=>$uId, 'cdDatetime'=>$datetime);
        $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);

        if (!($dbresult =& $this->db->sql_query($sql))) {
            $this->db->sql_transaction('rollback');
            message_die(GENERAL_ERROR, 'Could not add bookmark description', '', __LINE__, __FILE__, $sql, $this->db);
            return false;
        }
        return true;
    }

    function getLastBookmarkDescription($bHash) {
        $query = "SELECT *";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE bHash='".$bHash."'";
        $query.= " ORDER BY cdDatetime DESC";

        if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) {
            message_die(GENERAL_ERROR, 'Could not get bookmark description', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        $row = $this->db->sql_fetchrow($dbresult);
        $this->db->sql_freeresult($dbresult);
        if ($row) {
            return $row;
        } else {
            return false;
        }
    }

    function getAllBookmarksDescription($bHash) {
        $query = "SELECT *";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE bHash='".$bHash."'";
        $query.= " ORDER BY cdDatetime DESC";

        if (!($dbresult = & $this->db->sql_query($query))) {
            message_die(GENERAL_ERROR, 'Could not get bookmark descriptions', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        $rowset = $this->db->sql_fetchrowset($dbresult);
        $this->db->sql_freeresult($dbresult);
        return $rowset;
    }

    function deleteDescriptionsForUser($uId){
        $query = 'DELETE FROM '. $this->getTableName() . ' WHERE uId = '. intval($uId);

        $this->db->sql_transaction('begin');
        if (!($dbresult = & $this->db->sql_query($query))) {
            $this->db->sql_transaction('rollback');
            message_die(GENERAL_ERROR, 'Could not delete user descriptions', '',
            __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        return true;
    }


    function deleteAll() {
        $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';
        $this->db->sql_query($query);
    }



    /**
     * Sorts a tag combination.
     *
     * Multiple tags are separated by "+" signs. Semantically,
     * tag combinations like "blue+flower" and "flower+blue" are identical.
     * This method sorts the single tags so i.e. the common bookmark description
     * of "blue+flower" is also shown for "flower+blue".
     *
     * @param string $tag Single tag or tag combination ("+")
     *
     * @return string Sorted tag combination
     */
    protected static function getSortedTag($tag)
    {
        $tags = explode('+', $tag);
        sort($tags);
        return implode('+', $tags);
    }

}
?>