Added generic tags support; tags for dimensions/gl accounts.
[fa-stable.git] / admin / db / tags_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function add_tag($type, $name, $description)
14 {
15         $sql = "INSERT INTO ".TB_PREF."tags (type, name, description)
16                 VALUES (".db_escape($type).", ".db_escape($name).", ".db_escape($description).")";
17
18         return db_query($sql);
19 }
20
21 //--------------------------------------------------------------------------------------
22
23 function update_tag($id, $name, $description, $type=null)
24 {
25         $sql = "UPDATE ".TB_PREF."tags SET name=".db_escape($name).", 
26                                        description=".db_escape($description);
27         if ($type != null)
28                 $sql .= ", type=".db_escape($type);
29
30         $sql .= " WHERE id = ".db_escape($id);
31
32         return db_query($sql);
33 }
34
35 //--------------------------------------------------------------------------------------
36
37 function get_tags($type, $all=false)
38 {
39         $sql = "SELECT * FROM ".TB_PREF."tags WHERE type=".db_escape($type);
40         
41         if (!$all) $sql .= " AND !inactive";
42         
43         $sql .= " ORDER BY name";
44
45         return db_query($sql, "could not get tags");
46 }
47
48 //--------------------------------------------------------------------------------------
49
50 function get_tag($id)
51 {
52         $sql = "SELECT * FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
53
54         $result = db_query($sql, "could not get tag");
55
56         return db_fetch($result);
57 }
58
59 //--------------------------------------------------------------------------------------
60
61 function get_tag_type($id)
62 {
63         $sql = "SELECT type FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
64
65         $result = db_query($sql, "could not get tag type");
66
67         $row = db_fetch_row($result);
68         return $row[0];
69 }
70
71 //--------------------------------------------------------------------------------------
72
73 function get_tag_name($id)
74 {
75         $sql = "SELECT name FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
76
77         $result = db_query($sql, "could not get tag name");
78
79         $row = db_fetch_row($result);
80         return $row[0];
81 }
82
83 //--------------------------------------------------------------------------------------
84
85 function get_tag_description($id)
86 {
87         $sql = "SELECT description FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
88
89         $result = db_query($sql, "could not get tag description");
90
91         $row = db_fetch_row($result);
92         return $row[0];
93 }
94
95 //--------------------------------------------------------------------------------------
96
97 function delete_tag($id)
98 {
99         $sql = "DELETE FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
100
101         db_query($sql, "could not delete tag");
102 }
103
104 //--------------------------------------------------------------------------------------
105
106 function add_tag_associations($recordid, $tagids)
107 {
108         _vd($tagids);
109         foreach($tagids as $tagid) {
110                 if (!$tagid) continue;
111                 $sql = "INSERT INTO ".TB_PREF."tag_associations (record_id, tag_id)
112                         VALUES (".db_escape($recordid).", ".db_escape($tagid).")";
113
114                 db_query($sql, "could not add tag association");
115         }
116 }
117
118 //--------------------------------------------------------------------------------------
119
120 function update_tag_associations($type, $recordid, $tagids)
121 {
122         // Delete the old associations
123         delete_tag_associations($type, $recordid, false);
124         // Add the new associations
125         add_tag_associations($recordid, $tagids);
126 }
127
128 //--------------------------------------------------------------------------------------
129
130 // To delete tag associations, we need to specify the tag type.
131 // Otherwise we may inadvertantly delete records for another type of tag
132 function delete_tag_associations($type, $recordid, $all=false)
133 {
134         $sql = "DELETE ta FROM ".TB_PREF."tag_associations AS ta 
135                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
136                                 WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
137         
138         if (!$all)
139                 $sql .= " AND tags.inactive = 0";
140
141         db_query($sql, "could not delete tag associations");
142 }
143
144 //--------------------------------------------------------------------------------------
145
146 function get_records_associated_with_tag($id)
147 {
148         // Which table we query is based on the tag type
149         $type = get_tag_type($id);
150         
151         $table = $key = '';
152         switch ($type) {
153                 case TAG_ACCOUNT:
154                         $table = TB_PREF."chart_master";
155                         $key = "account_code";
156                         break;
157                 case TAG_DIMENSION:
158                         $table = TB_PREF."dimensions";
159                         $key = "id";
160                         break;
161         }
162         
163         $sql = "SELECT $table.* FROM $table 
164                 INNER JOIN ".TB_PREF."tag_associations AS ta ON ta.record_id = $table.$key
165                 INNER JOIN ".TB_PREF."tags AS tags ON ta.tag_id = tags.id
166                 WHERE tags.id = ".db_escape($id);
167
168         return db_query($sql, "could not get tag associations for tag");
169 }
170
171 //--------------------------------------------------------------------------------------
172
173 function get_tags_associated_with_record($type, $recordid)
174 {
175         $sql = "SELECT tags.* FROM ".TB_PREF."tag_associations AS ta 
176                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
177                                 WHERE tags.type = $type AND ta.record_id = ".db_escape($recordid);
178
179         return db_query($sql, "could not get tags associations for record");
180 }
181
182 //--------------------------------------------------------------------------------------
183
184 ?>