Rerun of connect_db_mysqli.inc.
[fa-stable.git] / 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         foreach($tagids as $tagid) {
109                 if (!$tagid) continue;
110                 $sql = "INSERT INTO ".TB_PREF."tag_associations (record_id, tag_id)
111                         VALUES (".db_escape($recordid).", ".db_escape($tagid).")";
112
113                 db_query($sql, "could not add tag association");
114         }
115 }
116
117 //--------------------------------------------------------------------------------------
118
119 function update_tag_associations($type, $recordid, $tagids)
120 {
121         // Delete the old associations
122         delete_tag_associations($type, $recordid, false);
123         // Add the new associations
124         add_tag_associations($recordid, $tagids);
125 }
126
127 //--------------------------------------------------------------------------------------
128 // To delete tag associations, we need to specify the tag type.
129 // Otherwise we may inadvertantly delete records for another type of tag
130 //
131 function delete_tag_associations($type, $recordid, $all=false)
132 {
133 /* multiply table DELETE syntax available since MySQL 4.0.0:
134         $sql = "DELETE ta FROM ".TB_PREF."tag_associations ta 
135                                 INNER JOIN ".TB_PREF."tags tags ON tags.id = ta.tag_id 
136                                 WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
137 */
138         // To support MySQL 3.xx we have to use multiply queries
139         $sql = "SELECT * FROM ".TB_PREF."tag_associations ta 
140                         INNER JOIN ".TB_PREF."tags tags ON tags.id = ta.tag_id 
141                         WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
142         if (!$all)
143                 $sql .= " AND tags.inactive = 0";
144         $result = db_query($sql, "could not select tag associations");
145
146         while($ta = db_fetch($result)) {
147                 $sql2 = "DELETE FROM ".TB_PREF."tag_associations WHERE 
148                         record_id = '".$ta['record_id']."' AND tag_id=".$ta['tag_id'];
149                 db_query($sql2, "could not delete tag associations");
150         }
151 }
152
153 //--------------------------------------------------------------------------------------
154
155 function get_records_associated_with_tag($id)
156 {
157         // Which table we query is based on the tag type
158         $type = get_tag_type($id);
159         
160         $table = $key = '';
161         switch ($type) {
162                 case TAG_ACCOUNT:
163                         $table = TB_PREF."chart_master";
164                         $key = "account_code";
165                         break;
166                 case TAG_DIMENSION:
167                         $table = TB_PREF."dimensions";
168                         $key = "id";
169                         break;
170         }
171         
172         $sql = "SELECT $table.* FROM $table 
173                 INNER JOIN ".TB_PREF."tag_associations AS ta ON ta.record_id = $table.$key
174                 INNER JOIN ".TB_PREF."tags AS tags ON ta.tag_id = tags.id
175                 WHERE tags.id = ".db_escape($id);
176
177         return db_query($sql, "could not get tag associations for tag");
178 }
179
180 //--------------------------------------------------------------------------------------
181
182 function get_tags_associated_with_record($type, $recordid)
183 {
184         $sql = "SELECT tags.* FROM ".TB_PREF."tag_associations AS ta 
185                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
186                                 WHERE tags.type = $type AND ta.record_id = ".db_escape($recordid);
187
188         return db_query($sql, "could not get tags associations for record");
189 }
190
191 //--------------------------------------------------------------------------------------
192
193 ?>