Fixed error log warning (missing installed_extensions.inc) during upgrade to 2.2.
[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         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
129 // To delete tag associations, we need to specify the tag type.
130 // Otherwise we may inadvertantly delete records for another type of tag
131 function delete_tag_associations($type, $recordid, $all=false)
132 {
133         $sql = "DELETE ta FROM ".TB_PREF."tag_associations AS ta 
134                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
135                                 WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
136         
137         if (!$all)
138                 $sql .= " AND tags.inactive = 0";
139
140         db_query($sql, "could not delete tag associations");
141 }
142
143 //--------------------------------------------------------------------------------------
144
145 function get_records_associated_with_tag($id)
146 {
147         // Which table we query is based on the tag type
148         $type = get_tag_type($id);
149         
150         $table = $key = '';
151         switch ($type) {
152                 case TAG_ACCOUNT:
153                         $table = TB_PREF."chart_master";
154                         $key = "account_code";
155                         break;
156                 case TAG_DIMENSION:
157                         $table = TB_PREF."dimensions";
158                         $key = "id";
159                         break;
160         }
161         
162         $sql = "SELECT $table.* FROM $table 
163                 INNER JOIN ".TB_PREF."tag_associations AS ta ON ta.record_id = $table.$key
164                 INNER JOIN ".TB_PREF."tags AS tags ON ta.tag_id = tags.id
165                 WHERE tags.id = ".db_escape($id);
166
167         return db_query($sql, "could not get tag associations for tag");
168 }
169
170 //--------------------------------------------------------------------------------------
171
172 function get_tags_associated_with_record($type, $recordid)
173 {
174         $sql = "SELECT tags.* FROM ".TB_PREF."tag_associations AS ta 
175                                 INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
176                                 WHERE tags.type = $type AND ta.record_id = ".db_escape($recordid);
177
178         return db_query($sql, "could not get tags associations for record");
179 }
180
181 //--------------------------------------------------------------------------------------
182
183 ?>