aboutsummaryrefslogtreecommitdiff
path: root/services/tagservice.php
blob: fbb7fa3fe3eed7fe99cc669f729608ae01070fc9 (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
<?php
class TagService {
    var $db;
    var $tablename;

    function &getInstance(&$db) {
        static $instance;
        if (!isset($instance))
            $instance =& new TagService($db);
        return $instance;
    }

    function TagService(&$db) {
        $this->db =& $db;
        $this->tablename = $GLOBALS['tableprefix'] .'tags';
    }

    function getDescription($tag, $uId) {
	$query = 'SELECT tag, uId, tDescription';
	$query.= ' FROM '.$this->getTableName();
	$query.= ' WHERE tag = "'.$tag.'"';
        $query.= ' AND uId = "'.$uId.'"';

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

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

    function getAllDescriptions($tag) {
	$query = 'SELECT tag, uId, tDescription';
	$query.= ' FROM '.$this->getTableName();
	$query.= ' WHERE tag = "'.$tag.'"';

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

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

    function updateDescription($tag, $uId, $desc) {
	if(count($this->getDescription($tag, $uId))>0) {
	    $query = 'UPDATE '.$this->getTableName();
	    $query.= ' SET tDescription="'.$this->db->sql_escape($desc).'"';
	    $query.= ' WHERE tag="'.$tag.'" AND uId="'.$uId.'"';
	} else {
	    $values = array('tag'=>$tag, 'uId'=>$uId, 'tDescription'=>$desc);
	    $query = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
	}

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

    function renameTag($uId, $oldName, $newName) {
	$query = 'UPDATE `'. $this->getTableName() .'`';
	$query.= ' SET tag="'.$newName.'"';
	$query.= ' WHERE tag="'.$oldName.'"';
	$query.= ' AND uId="'.$uId.'"';
	$this->db->sql_query($query);
    }

   

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

    // Properties
    function getTableName()       { return $this->tablename; }
    function setTableName($value) { $this->tablename = $value; }
}
?>