08cb13c6eda1f37906c6a5d79297ecefd71bfae0
[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 function get_tag_names($tags)
85 {
86         $str = "";
87         if ($tags == -1)
88                 return $str;
89         foreach($tags as $id)
90         {
91                 $tag = get_tag_name($id);
92                 if ($str == "")
93                         $str .= $tag;
94                 else    
95                         $str .= ", ".$tag;
96         }
97         return $str;
98 }
99 //--------------------------------------------------------------------------------------
100
101 function get_tag_description($id)
102 {
103         $sql = "SELECT description FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
104
105         $result = db_query($sql, "could not get tag description");
106
107         $row = db_fetch_row($result);
108         return $row[0];
109 }
110
111 //--------------------------------------------------------------------------------------
112
113 function delete_tag($id)
114 {
115         $sql = "DELETE FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
116
117         db_query($sql, "could not delete tag");
118 }
119
120 //--------------------------------------------------------------------------------------
121
122 function add_tag_associations($recordid, $tagids)
123 {
124         foreach($tagids as $tagid) {
125                 if (!$tagid) continue;
126                 $sql = "INSERT INTO ".TB_PREF."tag_associations (record_id, tag_id)
127                         VALUES (".db_escape($recordid).", ".db_escape($tagid).")";
128
129                 db_query($sql, "could not add tag association");
130         }
131 }
132
133 //--------------------------------------------------------------------------------------
134
135 function update_tag_associations($type, $recordid, $tagids)
136 {
137         // Delete the old associations
138         delete_tag_associations($type, $recordid, false);
139         // Add the new associations
140         add_tag_associations($recordid, $tagids);
141 }
142
143 //--------------------------------------------------------------------------------------
144 // To delete tag associations, we need to specify the tag type.
145 // Otherwise we may inadvertantly delete records for another type of tag
146 //
147 function delete_tag_associations($type, $recordid, $all=false)
148 {
149 /* multiply table DELETE syntax available since MySQL 4.0.0:
150         $sql = "DELETE ta FROM ".TB_PREF."tag_associations ta 
151                                 INNER JOIN ".TB_PREF."tags tags ON tags.id = ta.tag_id 
152                                 WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
153 */
154         // To support MySQL 3.xx we have to use multiply queries
155         $sql = "SELECT * FROM ".TB_PREF."tag_associations ta 
156                         INNER JOIN ".TB_PREF."tags tags ON tags.id = ta.tag_id 
157                         WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
158         if (!$all)
159                 $sql .= " AND tags.inactive = 0";
160         $result = db_query($sql, "could not select tag associations");
161
162         while($ta = db_fetch($result)) {
163                 $sql2 = "DELETE FROM ".TB_PREF."tag_associations WHERE 
164                         record_id = '".$ta['record_id']."' AND tag_id=".$ta['tag_id'];
165                 db_query($sql2, "could not delete tag associations");
166         }
167 }
168
169 //--------------------------------------------------------------------------------------
170
171 function get_records_associated_with_tag($id)
172 {
173         // Which table we query is based on the tag type
174         $type = get_tag_type($id);
175         
176         $table = $key = '';
177         switch ($type) {
178                 case TAG_ACCOUNT:
179                         $table = TB_PREF."chart_master";
180                         $key = "account_code";
181                         break;
182                 case TAG_DIMENSION:
183                         $table = TB_PREF."dimensions";
184                         $key = "id";
185                         break;
186         }
187         
188         $sql = "SELECT $table.* FROM $table 
189                 INNER JOIN ".TB_PREF."tag_associations AS ta ON ta.record_id = $table.$key
190                 INNER JOIN ".TB_PREF."tags AS tags ON ta.tag_id = tags.id
191                 WHERE tags.id = ".db_escape($id);
192
193         return db_query($sql, "could not get tag associations for tag");
194 }
195
196 //--------------------------------------------------------------------------------------
197
198 function get_tags_associated_with_record($type, $recordid)
199 {
200         $sql = "SELECT tags.* FROM ".TB_PREF."tag_associations AS ta 
201                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
202                                 WHERE tags.type = $type AND ta.record_id = ".db_escape($recordid);
203
204         return db_query($sql, "could not get tags associations for record");
205 }
206
207 //--------------------------------------------------------------------------------------
208
209 function is_record_in_tags($tags, $type, $recordid)
210 {
211         foreach($tags as $id)
212         {
213                 $sql = "SELECT ta.record_id FROM ".TB_PREF."tag_associations AS ta 
214                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
215                                 WHERE tags.type = $type AND tags.id = $id AND ta.record_id = ".db_escape($recordid);
216                 $res = db_query($sql, "could not get tags associations for record");
217                 if (db_num_rows($res) == 0)
218                         return false;
219         }
220         return true;
221 }
222