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']." and status = 1 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();
69 $group_id = $user->getGroup();
70 $org_id = $user->org_id;
72 $sql = "insert into tt_custom_field_log (group_id, org_id, log_id, field_id, option_id, value)".
73 " values($group_id, $org_id, $log_id, $field_id, ".$mdb2->quote($option_id).", ".$mdb2->quote($value).")";
74 $affected = $mdb2->exec($sql);
75 return (!is_a($affected, 'PEAR_Error'));
78 function update($log_id, $field_id, $option_id, $value) {
80 return true; // Nothing to update.
82 // Remove older custom field values, if any.
83 $res = $this->delete($log_id);
87 if (!$value && !$option_id)
88 return true; // Do not insert NULL values.
90 return $this->insert($log_id, $field_id, $option_id, $value);
93 function delete($log_id) {
95 $mdb2 = getConnection();
96 $sql = "update tt_custom_field_log set status = NULL where log_id = $log_id";
97 $affected = $mdb2->exec($sql);
98 return (!is_a($affected, 'PEAR_Error'));
101 function get($log_id) {
104 $mdb2 = getConnection();
105 $sql = "select id, field_id, option_id, value from tt_custom_field_log where log_id = $log_id and status = 1";
106 $res = $mdb2->query($sql);
107 if (!is_a($res, 'PEAR_Error')) {
108 while ($val = $res->fetchRow()) {
116 // insertOption adds a new option to a custom field.
117 static function insertOption($field_id, $option_name) {
119 $mdb2 = getConnection();
121 $group_id = $user->getGroup();
122 $org_id = $user->org_id;
124 // Check if the option exists.
126 $sql = "select id from tt_custom_field_options where field_id = $field_id and value = ".$mdb2->quote($option_name);
127 $res = $mdb2->query($sql);
128 if (is_a($res, 'PEAR_Error'))
130 if ($val = $res->fetchRow()) $id = $val['id'];
134 $sql = "insert into tt_custom_field_options (group_id, org_id, field_id, value)".
135 " values($group_id, $org_id, $field_id, ".$mdb2->quote($option_name).")";
136 $affected = $mdb2->exec($sql);
137 if (is_a($affected, 'PEAR_Error'))
143 // updateOption updates option name.
144 static function updateOption($id, $option_name) {
146 $mdb2 = getConnection();
148 $sql = "update tt_custom_field_options set value = ".$mdb2->quote($option_name)." where id = $id";
149 $affected = $mdb2->exec($sql);
150 return (!is_a($affected, 'PEAR_Error'));
153 // delete Option deletes an option and all custom field log entries that used it.
154 static function deleteOption($id) {
156 $mdb2 = getConnection();
158 $field_id = CustomFields::getFieldIdForOption($id);
160 // First make sure that the field is ours.
161 $sql = "select group_id from tt_custom_fields where id = $field_id";
162 $res = $mdb2->query($sql);
163 if (is_a($res, 'PEAR_Error'))
165 $val = $res->fetchRow();
166 if ($user->group_id != $val['group_id'])
169 // Delete log entries with this option.
170 $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id and value = ".$mdb2->quote($id);
171 $affected = $mdb2->exec($sql);
172 if (is_a($affected, 'PEAR_Error'))
175 // Delete the option.
176 $sql = "update tt_custom_field_options set status = NULL where id = $id";
177 $affected = $mdb2->exec($sql);
178 return (!is_a($affected, 'PEAR_Error'));
181 // getOptions returns an array of options for a custom field.
182 static function getOptions($field_id) {
184 $mdb2 = getConnection();
187 // First make sure that the field is ours.
188 $sql = "select group_id from tt_custom_fields where id = $field_id";
189 $res = $mdb2->query($sql);
190 if (is_a($res, 'PEAR_Error'))
192 $val = $res->fetchRow();
193 if ($user->group_id != $val['group_id'])
197 $sql = "select id, value from tt_custom_field_options where field_id = $field_id and status = 1 order by value";
198 $res = $mdb2->query($sql);
199 if (!is_a($res, 'PEAR_Error')) {
200 while ($val = $res->fetchRow()) {
201 $options[$val['id']] = $val['value'];
208 // getOptionName returns an option name for a custom field.
209 static function getOptionName($id) {
211 $mdb2 = getConnection();
213 $field_id = CustomFields::getFieldIdForOption($id);
215 // First make sure that the field is ours.
216 $sql = "select group_id from tt_custom_fields where id = $field_id";
217 $res = $mdb2->query($sql);
218 if (is_a($res, 'PEAR_Error'))
220 $val = $res->fetchRow();
221 if ($user->group_id != $val['group_id'])
225 $sql = "select value from tt_custom_field_options where id = $id";
226 $res = $mdb2->query($sql);
227 if (!is_a($res, 'PEAR_Error')) {
228 $val = $res->fetchRow();
229 $name = $val['value'];
235 // getFields returns an array of custom fields for group.
236 static function getFields() {
238 $mdb2 = getConnection();
241 $sql = "select id, type, label from tt_custom_fields where group_id = $user->group_id and status = 1 and type > 0";
242 $res = $mdb2->query($sql);
243 if (!is_a($res, 'PEAR_Error')) {
244 while ($val = $res->fetchRow()) {
245 $fields[] = array('id'=>$val['id'],'type'=>$val['type'],'label'=>$val['label']);
252 // getField returns a custom field.
253 static function getField($id) {
255 $mdb2 = getConnection();
257 $sql = "select label, type, required from tt_custom_fields where id = $id and group_id = $user->group_id";
258 $res = $mdb2->query($sql);
259 if (!is_a($res, 'PEAR_Error')) {
260 $val = $res->fetchRow();
268 // getFieldIdForOption returns field id from an associated option id.
269 static function getFieldIdForOption($option_id) {
270 $mdb2 = getConnection();
272 $sql = "select field_id from tt_custom_field_options where id = $option_id";
273 $res = $mdb2->query($sql);
274 if (!is_a($res, 'PEAR_Error')) {
275 $val = $res->fetchRow();
276 $field_id = $val['field_id'];
282 // The insertField inserts a custom field for group.
283 static function insertField($field_name, $field_type, $required) {
285 $mdb2 = getConnection();
286 $group_id = $user->getGroup();
287 $org_id = $user->org_id;
288 $sql = "insert into tt_custom_fields (group_id, org_id, type, label, required, status)".
289 " values($group_id, $org_id, $field_type, ".$mdb2->quote($field_name).", $required, 1)";
290 $affected = $mdb2->exec($sql);
291 return (!is_a($affected, 'PEAR_Error'));
294 // The updateField updates custom field for group.
295 static function updateField($id, $name, $type, $required) {
297 $mdb2 = getConnection();
298 $sql = "update tt_custom_fields set label = ".$mdb2->quote($name).", type = $type, required = $required where id = $id and group_id = $user->group_id";
299 $affected = $mdb2->exec($sql);
300 return (!is_a($affected, 'PEAR_Error'));
303 // The deleteField deletes a custom field, its options and log entries for group.
304 static function deleteField($field_id) {
307 $mdb2 = getConnection();
309 // First make sure that the field is ours so that we can safely delete it.
310 $sql = "select group_id from tt_custom_fields where id = $field_id";
311 $res = $mdb2->query($sql);
312 if (is_a($res, 'PEAR_Error'))
314 $val = $res->fetchRow();
315 if ($user->group_id != $val['group_id'])
318 // Mark log entries as deleted.
319 $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id";
320 $affected = $mdb2->exec($sql);
321 if (is_a($affected, 'PEAR_Error'))
324 // Mark field options as deleted.
325 $sql = "update tt_custom_field_options set status = NULL where field_id = $field_id";
326 $affected = $mdb2->exec($sql);
327 if (is_a($affected, 'PEAR_Error'))
330 // Mark custom field as deleted.
331 $sql = "update tt_custom_fields set status = NULL where id = $field_id and group_id = $user->group_id";
332 $affected = $mdb2->exec($sql);
333 return (!is_a($affected, 'PEAR_Error'));