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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
31 // Definitions of custom field types.
33 const TYPE_TEXT = 1; // A text field.
34 const TYPE_DROPDOWN = 2; // A dropdown field with pre-defined values.
36 var $fields = array(); // Array of custom fields for group.
37 var $options = array(); // Array of options for a dropdown custom field.
40 function __construct($group_id) {
41 $mdb2 = getConnection();
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'=>'');
52 // If we have a dropdown obtain options for it.
53 if ((count($this->fields) > 0) && ($this->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)) {
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'];
65 function insert($log_id, $field_id, $option_id, $value) {
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'));
73 function update($log_id, $field_id, $option_id, $value) {
75 return true; // Nothing to update.
77 // Remove older custom field values, if any.
78 $res = $this->delete($log_id);
82 if (!$value && !$option_id)
83 return true; // Do not insert NULL values.
85 return $this->insert($log_id, $field_id, $option_id, $value);
88 function delete($log_id) {
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'));
96 function get($log_id) {
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()) {
111 // insertOption adds a new option to a custom field.
112 static function insertOption($field_id, $option_name) {
114 $mdb2 = getConnection();
116 $group_id = $user->getActiveGroup();
117 $org_id = $user->org_id;
119 // Check if the option exists.
121 $sql = "select id from tt_custom_field_options where field_id = $field_id and value = ".$mdb2->quote($option_name);
122 $res = $mdb2->query($sql);
123 if (is_a($res, 'PEAR_Error'))
125 if ($val = $res->fetchRow()) $id = $val['id'];
129 $sql = "insert into tt_custom_field_options (group_id, org_id, field_id, value)".
130 " values($group_id, $org_id, $field_id, ".$mdb2->quote($option_name).")";
131 $affected = $mdb2->exec($sql);
132 if (is_a($affected, 'PEAR_Error'))
138 // updateOption updates option name.
139 static function updateOption($id, $option_name) {
141 $mdb2 = getConnection();
143 $sql = "update tt_custom_field_options set value = ".$mdb2->quote($option_name)." where id = $id";
144 $affected = $mdb2->exec($sql);
145 return (!is_a($affected, 'PEAR_Error'));
148 // delete Option deletes an option and all custom field log entries that used it.
149 static function deleteOption($id) {
151 $mdb2 = getConnection();
153 $field_id = CustomFields::getFieldIdForOption($id);
155 // First make sure that the field is ours.
156 $sql = "select group_id from tt_custom_fields where id = $field_id";
157 $res = $mdb2->query($sql);
158 if (is_a($res, 'PEAR_Error'))
160 $val = $res->fetchRow();
161 if ($user->group_id != $val['group_id'])
164 // Delete log entries with this option.
165 $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id and value = ".$mdb2->quote($id);
166 $affected = $mdb2->exec($sql);
167 if (is_a($affected, 'PEAR_Error'))
170 // Delete the option.
171 $sql = "delete from tt_custom_field_options where id = $id";
172 $affected = $mdb2->exec($sql);
173 return (!is_a($affected, 'PEAR_Error'));
176 // getOptions returns an array of options for a custom field.
177 static function getOptions($field_id) {
179 $mdb2 = getConnection();
182 // First make sure that the field is ours.
183 $sql = "select group_id from tt_custom_fields where id = $field_id";
184 $res = $mdb2->query($sql);
185 if (is_a($res, 'PEAR_Error'))
187 $val = $res->fetchRow();
188 if ($user->group_id != $val['group_id'])
192 $sql = "select id, value from tt_custom_field_options where field_id = $field_id order by value";
193 $res = $mdb2->query($sql);
194 if (!is_a($res, 'PEAR_Error')) {
195 while ($val = $res->fetchRow()) {
196 $options[$val['id']] = $val['value'];
203 // getOptionName returns an option name for a custom field.
204 static function getOptionName($id) {
206 $mdb2 = getConnection();
208 $field_id = CustomFields::getFieldIdForOption($id);
210 // First make sure that the field is ours.
211 $sql = "select group_id from tt_custom_fields where id = $field_id";
212 $res = $mdb2->query($sql);
213 if (is_a($res, 'PEAR_Error'))
215 $val = $res->fetchRow();
216 if ($user->group_id != $val['group_id'])
220 $sql = "select value from tt_custom_field_options where id = $id";
221 $res = $mdb2->query($sql);
222 if (!is_a($res, 'PEAR_Error')) {
223 $val = $res->fetchRow();
224 $name = $val['value'];
230 // getFields returns an array of custom fields for group.
231 static function getFields() {
233 $mdb2 = getConnection();
236 $sql = "select id, type, label from tt_custom_fields where group_id = $user->group_id and status = 1 and type > 0";
237 $res = $mdb2->query($sql);
238 if (!is_a($res, 'PEAR_Error')) {
239 while ($val = $res->fetchRow()) {
240 $fields[] = array('id'=>$val['id'],'type'=>$val['type'],'label'=>$val['label']);
247 // getField returns a custom field.
248 static function getField($id) {
250 $mdb2 = getConnection();
252 $sql = "select label, type, required from tt_custom_fields where id = $id and group_id = $user->group_id";
253 $res = $mdb2->query($sql);
254 if (!is_a($res, 'PEAR_Error')) {
255 $val = $res->fetchRow();
263 // getFieldIdForOption returns field id from an associated option id.
264 static function getFieldIdForOption($option_id) {
265 $mdb2 = getConnection();
267 $sql = "select field_id from tt_custom_field_options where id = $option_id";
268 $res = $mdb2->query($sql);
269 if (!is_a($res, 'PEAR_Error')) {
270 $val = $res->fetchRow();
271 $field_id = $val['field_id'];
277 // The insertField inserts a custom field for group.
278 static function insertField($field_name, $field_type, $required) {
280 $mdb2 = getConnection();
281 $group_id = $user->getActiveGroup();
282 $org_id = $user->org_id;
283 $sql = "insert into tt_custom_fields (group_id, org_id, type, label, required, status)".
284 " values($group_id, $org_id, $field_type, ".$mdb2->quote($field_name).", $required, 1)";
285 $affected = $mdb2->exec($sql);
286 return (!is_a($affected, 'PEAR_Error'));
289 // The updateField updates custom field for group.
290 static function updateField($id, $name, $type, $required) {
292 $mdb2 = getConnection();
293 $sql = "update tt_custom_fields set label = ".$mdb2->quote($name).", type = $type, required = $required where id = $id and group_id = $user->group_id";
294 $affected = $mdb2->exec($sql);
295 return (!is_a($affected, 'PEAR_Error'));
298 // The deleteField deletes a custom field, its options and log entries for group.
299 static function deleteField($field_id) {
301 // Our overall intention is to keep the code simple and manageable.
302 // If a user wishes to delete a field, we will delete all its options and log entries.
303 // Otherwise we have to do conditional queries depending on field status (this complicates things).
306 $mdb2 = getConnection();
308 // First make sure that the field is ours so that we can safely delete it.
309 $sql = "select group_id from tt_custom_fields where id = $field_id";
310 $res = $mdb2->query($sql);
311 if (is_a($res, 'PEAR_Error'))
313 $val = $res->fetchRow();
314 if ($user->group_id != $val['group_id'])
317 // Mark log entries as deleted.
318 $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id";
319 $affected = $mdb2->exec($sql);
320 if (is_a($affected, 'PEAR_Error'))
323 // Delete field options.
324 $sql = "delete from tt_custom_field_options where field_id = $field_id";
325 $affected = $mdb2->exec($sql);
326 if (is_a($affected, 'PEAR_Error'))
330 $sql = "delete from tt_custom_fields where id = $field_id and group_id = $user->group_id";
331 $affected = $mdb2->exec($sql);
332 return (!is_a($affected, 'PEAR_Error'));