Refactoring. Renamed team_id fields to become group_id.
[timetracker.git] / plugins / CustomFields.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 class CustomFields {
30
31   // Definitions of custom field types.
32
33   const TYPE_TEXT = 1;     // A text field.
34   const TYPE_DROPDOWN = 2; // A dropdown field with pre-defined values.
35
36   var $fields = array();  // Array of custom fields for team.
37   var $options = array(); // Array of options for a dropdown custom field.
38
39   // Constructor.
40   function __construct($group_id) {
41     $mdb2 = getConnection();
42
43     // Get fields.
44     $sql = "select id, type, label, required from tt_custom_fields where group_id = $group_id and status = 1 and type > 0";
45     $res = $mdb2->query($sql);
46     if (!is_a($res, 'PEAR_Error')) {
47       while ($val = $res->fetchRow()) {
48         $this->fields[] = array('id'=>$val['id'],'type'=>$val['type'],'label'=>$val['label'],'required'=>$val['required'],'value'=>'');
49       }
50     }
51
52     // If we have a dropdown obtain options for it.
53     if ((count($this->fields) > 0) && ($this->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)) {
54
55       $sql = "select id, value from tt_custom_field_options where field_id = ".$this->fields[0]['id']." order by value";
56       $res = $mdb2->query($sql);
57       if (!is_a($res, 'PEAR_Error')) {
58         while ($val = $res->fetchRow()) {
59           $this->options[$val['id']] = $val['value'];
60         }
61       }
62     }
63   }
64
65   function insert($log_id, $field_id, $option_id, $value) {
66
67     $mdb2 = getConnection();
68     $sql = "insert into tt_custom_field_log (log_id, field_id, option_id, value) values($log_id, $field_id, ".$mdb2->quote($option_id).", ".$mdb2->quote($value).")";
69     $affected = $mdb2->exec($sql);
70     return (!is_a($affected, 'PEAR_Error'));
71   }
72
73   function update($log_id, $field_id, $option_id, $value) {
74     if (!$field_id)
75       return true; // Nothing to update.
76
77     // Remove older custom field values, if any.
78     $res = $this->delete($log_id);
79     if (!$res)
80       return false;
81
82     if (!$value && !$option_id)
83       return true; // Do not insert NULL values.
84
85     return $this->insert($log_id, $field_id, $option_id, $value);
86   }
87
88   function delete($log_id) {
89
90     $mdb2 = getConnection();
91     $sql = "update tt_custom_field_log set status = NULL where log_id = $log_id";
92     $affected = $mdb2->exec($sql);
93     return (!is_a($affected, 'PEAR_Error'));
94   }
95
96   function get($log_id) {
97     $fields = array();
98
99     $mdb2 = getConnection();
100     $sql = "select id, field_id, option_id, value from tt_custom_field_log where log_id = $log_id and status = 1";
101     $res = $mdb2->query($sql);
102     if (!is_a($res, 'PEAR_Error')) {
103       while ($val = $res->fetchRow()) {
104         $fields[] = $val;
105       }
106       return $fields;
107     }
108     return false;
109   }
110
111   // insertOption adds a new option to a custom field.
112   static function insertOption($field_id, $option_name) {
113
114     $mdb2 = getConnection();
115
116     // Check if the option exists.
117     $id = 0;
118     $sql = "select id from tt_custom_field_options where field_id = $field_id and value = ".$mdb2->quote($option_name);
119     $res = $mdb2->query($sql);
120     if (is_a($res, 'PEAR_Error'))
121       return false;
122     if ($val = $res->fetchRow()) $id = $val['id'];
123
124     // Insert option.
125     if (!$id) {
126       $sql = "insert into tt_custom_field_options (field_id, value) values($field_id, ".$mdb2->quote($option_name).")";
127       $affected = $mdb2->exec($sql);
128       if (is_a($affected, 'PEAR_Error'))
129         return false;
130     }
131     return true;
132   }
133
134   // updateOption updates option name.
135   static function updateOption($id, $option_name) {
136
137     $mdb2 = getConnection();
138
139     $sql = "update tt_custom_field_options set value = ".$mdb2->quote($option_name)." where id = $id";
140     $affected = $mdb2->exec($sql);
141     return (!is_a($affected, 'PEAR_Error'));
142   }
143
144   // delete Option deletes an option and all custom field log entries that used it.
145   static function deleteOption($id) {
146     global $user;
147     $mdb2 = getConnection();
148
149     $field_id = CustomFields::getFieldIdForOption($id);
150
151     // First make sure that the field is ours.
152     $sql = "select group_id from tt_custom_fields where id = $field_id";
153     $res = $mdb2->query($sql);
154     if (is_a($res, 'PEAR_Error'))
155       return false;
156     $val = $res->fetchRow();
157     if ($user->group_id != $val['group_id'])
158       return false;
159
160     // Delete log entries with this option.
161     $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id and value = ".$mdb2->quote($id);
162     $affected = $mdb2->exec($sql);
163     if (is_a($affected, 'PEAR_Error'))
164       return false;
165
166     // Delete the option.
167     $sql = "delete from tt_custom_field_options where id = $id";
168     $affected = $mdb2->exec($sql);
169     return (!is_a($affected, 'PEAR_Error'));
170   }
171
172   // getOptions returns an array of options for a custom field.
173   static function getOptions($field_id) {
174     global $user;
175     $mdb2 = getConnection();
176     $options = array();
177
178     // First make sure that the field is ours.
179     $sql = "select group_id from tt_custom_fields where id = $field_id";
180     $res = $mdb2->query($sql);
181     if (is_a($res, 'PEAR_Error'))
182       return false;
183     $val = $res->fetchRow();
184     if ($user->group_id != $val['group_id'])
185       return false;
186
187     // Get options.
188     $sql = "select id, value from tt_custom_field_options where field_id = $field_id order by value";
189     $res = $mdb2->query($sql);
190     if (!is_a($res, 'PEAR_Error')) {
191       while ($val = $res->fetchRow()) {
192         $options[$val['id']] = $val['value'];
193       }
194       return $options;
195     }
196     return false;
197   }
198
199   // getOptionName returns an option name for a custom field.
200   static function getOptionName($id) {
201     global $user;
202     $mdb2 = getConnection();
203
204     $field_id = CustomFields::getFieldIdForOption($id);
205
206     // First make sure that the field is ours.
207     $sql = "select group_id from tt_custom_fields where id = $field_id";
208     $res = $mdb2->query($sql);
209     if (is_a($res, 'PEAR_Error'))
210       return false;
211     $val = $res->fetchRow();
212     if ($user->group_id != $val['group_id'])
213       return false;
214
215     // Get option name.
216     $sql = "select value from tt_custom_field_options where id = $id";
217     $res = $mdb2->query($sql);
218     if (!is_a($res, 'PEAR_Error')) {
219       $val = $res->fetchRow();
220       $name = $val['value'];
221       return $name;
222     }
223     return false;
224   }
225
226   // getFields returns an array of custom fields for team.
227   static function getFields() {
228     global $user;
229     $mdb2 = getConnection();
230
231     $fields = array();
232     $sql = "select id, type, label from tt_custom_fields where group_id = $user->group_id and status = 1 and type > 0";
233     $res = $mdb2->query($sql);
234     if (!is_a($res, 'PEAR_Error')) {
235       while ($val = $res->fetchRow()) {
236         $fields[] = array('id'=>$val['id'],'type'=>$val['type'],'label'=>$val['label']);
237       }
238       return $fields;
239     }
240     return false;
241   }
242
243   // getField returns a custom field.
244   static function getField($id) {
245     global $user;
246     $mdb2 = getConnection();
247
248     $sql = "select label, type, required from tt_custom_fields where id = $id and group_id = $user->group_id";
249     $res = $mdb2->query($sql);
250     if (!is_a($res, 'PEAR_Error')) {
251       $val = $res->fetchRow();
252       if (!$val)
253         return false;
254       return $val;
255     }
256     return false;
257   }
258
259   // getFieldIdForOption returns field id from an associated option id.
260   static function getFieldIdForOption($option_id) {
261     $mdb2 = getConnection();
262
263     $sql = "select field_id from tt_custom_field_options where id = $option_id";
264     $res = $mdb2->query($sql);
265     if (!is_a($res, 'PEAR_Error')) {
266       $val = $res->fetchRow();
267       $field_id = $val['field_id'];
268       return $field_id;
269     }
270     return false;
271   }
272
273   // The insertField inserts a custom field for team.
274   static function insertField($field_name, $field_type, $required) {
275     global $user;
276     $mdb2 = getConnection();
277     $sql = "insert into tt_custom_fields (group_id, type, label, required, status) values($user->group_id, $field_type, ".$mdb2->quote($field_name).", $required, 1)";
278     $affected = $mdb2->exec($sql);
279     return (!is_a($affected, 'PEAR_Error'));
280   }
281
282   // The updateField updates custom field for team.
283   static function updateField($id, $name, $type, $required) {
284     global $user;
285     $mdb2 = getConnection();
286     $sql = "update tt_custom_fields set label = ".$mdb2->quote($name).", type = $type, required = $required where id = $id and group_id = $user->group_id";
287     $affected = $mdb2->exec($sql);
288     return (!is_a($affected, 'PEAR_Error'));
289   }
290
291   // The deleteField deletes a custom field, its options and log entries for team.
292   static function deleteField($field_id) {
293
294     // Our overall intention is to keep the code simple and manageable.
295     // If a user wishes to delete a field, we will delete all its options and log entries.
296     // Otherwise we have to do conditional queries depending on field status (this complicates things).
297
298     global $user;
299     $mdb2 = getConnection();
300
301     // First make sure that the field is ours so that we can safely delete it.
302     $sql = "select group_id from tt_custom_fields where id = $field_id";
303     $res = $mdb2->query($sql);
304     if (is_a($res, 'PEAR_Error'))
305       return false;
306     $val = $res->fetchRow();
307     if ($user->group_id != $val['group_id'])
308       return false;
309
310     // Mark log entries as deleted.
311     $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id";
312     $affected = $mdb2->exec($sql);
313     if (is_a($affected, 'PEAR_Error'))
314       return false;
315
316     // Delete field options.
317     $sql = "delete from tt_custom_field_options where field_id = $field_id";
318     $affected = $mdb2->exec($sql);
319     if (is_a($affected, 'PEAR_Error'))
320       return false;
321
322     // Delete the field.
323     $sql = "delete from tt_custom_fields where id = $field_id and group_id = $user->group_id";
324     $affected = $mdb2->exec($sql);
325     return (!is_a($affected, 'PEAR_Error'));
326   }
327 }