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();
97 $group_id = $user->getGroup();
98 $org_id = $user->org_id;
100 $sql = "update tt_custom_field_log set status = null".
101 " where log_id = $log_id and group_id = $group_id and org_id = $org_id";
102 $affected = $mdb2->exec($sql);
103 return (!is_a($affected, 'PEAR_Error'));
106 function get($log_id) {
108 $mdb2 = getConnection();
110 $group_id = $user->getGroup();
111 $org_id = $user->org_id;
113 $sql = "select id, field_id, option_id, value from tt_custom_field_log".
114 " where log_id = $log_id and group_id = $group_id and org_id = $org_id and status = 1";
115 $res = $mdb2->query($sql);
116 if (!is_a($res, 'PEAR_Error')) {
118 while ($val = $res->fetchRow()) {
126 // insertOption adds a new option to a custom field.
127 static function insertOption($field_id, $option_name) {
129 $mdb2 = getConnection();
131 $group_id = $user->getGroup();
132 $org_id = $user->org_id;
134 // Check if the option exists.
136 $sql = "select id from tt_custom_field_options".
137 " where field_id = $field_id and group_id = $group_id and org_id = $org_id and value = ".$mdb2->quote($option_name);
138 $res = $mdb2->query($sql);
139 if (is_a($res, 'PEAR_Error'))
141 if ($val = $res->fetchRow()) $id = $val['id'];
145 $sql = "insert into tt_custom_field_options (group_id, org_id, field_id, value)".
146 " values($group_id, $org_id, $field_id, ".$mdb2->quote($option_name).")";
147 $affected = $mdb2->exec($sql);
148 if (is_a($affected, 'PEAR_Error'))
154 // updateOption updates option name.
155 static function updateOption($id, $option_name) {
157 $mdb2 = getConnection();
159 $group_id = $user->getGroup();
160 $org_id = $user->org_id;
162 $sql = "update tt_custom_field_options set value = ".$mdb2->quote($option_name).
163 " where id = $id and group_id = $group_id and org_id = $org_id";
164 $affected = $mdb2->exec($sql);
165 return (!is_a($affected, 'PEAR_Error'));
168 // delete Option deletes an option and all custom field log entries that used it.
169 static function deleteOption($id) {
171 $mdb2 = getConnection();
173 $group_id = $user->getGroup();
174 $org_id = $user->org_id;
176 $field_id = CustomFields::getFieldIdForOption($id);
177 if (!$field_id) return false;
179 // Delete log entries with this option. TODO: why? Research impact.
180 $sql = "update tt_custom_field_log set status = null".
181 " where field_id = $field_id and group_id = $group_id and org_id = $org_id and value = ".$mdb2->quote($id);
182 $affected = $mdb2->exec($sql);
183 if (is_a($affected, 'PEAR_Error'))
186 // Delete the option.
187 $sql = "update tt_custom_field_options set status = null".
188 " where id = $id and group_id = $group_id and org_id = $org_id";
189 $affected = $mdb2->exec($sql);
190 return (!is_a($affected, 'PEAR_Error'));
193 // getOptions returns an array of options for a custom field.
194 static function getOptions($field_id) {
196 $mdb2 = getConnection();
198 $group_id = $user->getGroup();
199 $org_id = $user->org_id;
202 $sql = "select id, value from tt_custom_field_options".
203 " where field_id = $field_id and group_id = $group_id and org_id = $org_id and status = 1 order by value";
204 $res = $mdb2->query($sql);
205 if (!is_a($res, 'PEAR_Error')) {
207 while ($val = $res->fetchRow()) {
208 $options[$val['id']] = $val['value'];
215 // getOptionName returns an option name for a custom field.
216 static function getOptionName($id) {
218 $mdb2 = getConnection();
220 $group_id = $user->getGroup();
221 $org_id = $user->org_id;
223 $sql = "select value from tt_custom_field_options".
224 " where id = $id and group_id = $group_id and org_id = $org_id";
225 $res = $mdb2->query($sql);
226 if (!is_a($res, 'PEAR_Error')) {
227 $val = $res->fetchRow();
228 $name = $val['value'];
234 // getFields returns an array of custom fields for group.
235 static function getFields() {
237 $mdb2 = getConnection();
239 $group_id = $user->getGroup();
240 $org_id = $user->org_id;
243 $sql = "select id, type, label from tt_custom_fields".
244 " where group_id = $group_id and org_id = $org_id and status = 1 and type > 0";
245 $res = $mdb2->query($sql);
246 if (!is_a($res, 'PEAR_Error')) {
247 while ($val = $res->fetchRow()) {
248 $fields[] = array('id'=>$val['id'],'type'=>$val['type'],'label'=>$val['label']);
255 // getField returns a custom field.
256 static function getField($id) {
258 $mdb2 = getConnection();
260 $group_id = $user->getGroup();
261 $org_id = $user->org_id;
263 $sql = "select label, type, required from tt_custom_fields".
264 " where id = $id and group_id = $group_id and org_id = $org_id";
265 $res = $mdb2->query($sql);
266 if (!is_a($res, 'PEAR_Error')) {
267 $val = $res->fetchRow();
275 // getFieldIdForOption returns field id from an associated option id.
276 static function getFieldIdForOption($option_id) {
278 $mdb2 = getConnection();
280 $group_id = $user->getGroup();
281 $org_id = $user->org_id;
283 $sql = "select field_id from tt_custom_field_options".
284 " where id = $option_id and group_id = $group_id and org_id = $org_id";
285 $res = $mdb2->query($sql);
286 if (!is_a($res, 'PEAR_Error')) {
287 $val = $res->fetchRow();
288 $field_id = $val['field_id'];
294 // The insertField inserts a custom field for group.
295 static function insertField($field_name, $field_type, $required) {
297 $mdb2 = getConnection();
299 $group_id = $user->getGroup();
300 $org_id = $user->org_id;
302 $sql = "insert into tt_custom_fields (group_id, org_id, type, label, required, status)".
303 " values($group_id, $org_id, $field_type, ".$mdb2->quote($field_name).", $required, 1)";
304 $affected = $mdb2->exec($sql);
305 return (!is_a($affected, 'PEAR_Error'));
308 // The updateField updates custom field for group.
309 static function updateField($id, $name, $type, $required) {
311 $mdb2 = getConnection();
313 $group_id = $user->getGroup();
314 $org_id = $user->org_id;
316 $sql = "update tt_custom_fields set label = ".$mdb2->quote($name).", type = $type, required = $required".
317 " where id = $id and group_id = $group_id and org_id = $org_id";
318 $affected = $mdb2->exec($sql);
319 return (!is_a($affected, 'PEAR_Error'));
322 // The deleteField deletes a custom field, its options and log entries for group.
323 static function deleteField($field_id) {
325 $mdb2 = getConnection();
327 $group_id = $user->getGroup();
328 $org_id = $user->org_id;
330 // Mark log entries as deleted. TODO: why are we doing this? Research impact.
331 $sql = "update tt_custom_field_log set status = null".
332 " where field_id = $field_id and group_id = $group_id and org_id = $org_id";
333 $affected = $mdb2->exec($sql);
334 if (is_a($affected, 'PEAR_Error'))
337 // Mark field options as deleted.
338 $sql = "update tt_custom_field_options set status = null".
339 " where field_id = $field_id and group_id = $group_id and org_id = $org_id";
340 $affected = $mdb2->exec($sql);
341 if (is_a($affected, 'PEAR_Error'))
344 // Mark custom field as deleted.
345 $sql = "update tt_custom_fields set status = null".
346 " where id = $field_id and group_id = $group_id and org_id = $org_id";
347 $affected = $mdb2->exec($sql);
348 return (!is_a($affected, 'PEAR_Error'));