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 team.
 
  37   var $options = array(); // Array of options for a dropdown custom field.
 
  40   function CustomFields($team_id) {
 
  41         $mdb2 = getConnection();
 
  44         $sql = "select id, type, label, required from tt_custom_fields where team_id = $team_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         // Check if the option exists.
 
 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'))
 
 122     if ($val = $res->fetchRow()) $id = $val['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'))
 
 134   // updateOption updates option name.
 
 135   static function updateOption($id, $option_name) {
 
 137     $mdb2 = getConnection();
 
 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'));
 
 144   // delete Option deletes an option and all custom field log entries that used it.
 
 145   static function deleteOption($id) {
 
 147         $mdb2 = getConnection();
 
 149         $field_id = CustomFields::getFieldIdForOption($id);
 
 151         // First make sure that the field is ours.
 
 152         $sql = "select team_id from tt_custom_fields where id = $field_id";
 
 153         $res = $mdb2->query($sql);
 
 154         if (is_a($res, 'PEAR_Error'))
 
 156         $val = $res->fetchRow();
 
 157         if ($user->team_id != $val['team_id'])
 
 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'))
 
 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'));
 
 172   // getOptions returns an array of options for a custom field.
 
 173   static function getOptions($field_id) {
 
 175         $mdb2 = getConnection();
 
 178         // First make sure that the field is ours.
 
 179         $sql = "select team_id from tt_custom_fields where id = $field_id";
 
 180         $res = $mdb2->query($sql);
 
 181         if (is_a($res, 'PEAR_Error'))
 
 183         $val = $res->fetchRow();
 
 184         if ($user->team_id != $val['team_id'])
 
 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'];
 
 199   // getOptiondName returns an option name for a custom field.
 
 200   static function getOptionName($id) {
 
 202         $mdb2 = getConnection();
 
 204         $field_id = CustomFields::getFieldIdForOption($id);
 
 206         // First make sure that the field is ours.
 
 207         $sql = "select team_id from tt_custom_fields where id = $field_id";
 
 208         $res = $mdb2->query($sql);
 
 209         if (is_a($res, 'PEAR_Error'))
 
 211         $val = $res->fetchRow();
 
 212         if ($user->team_id != $val['team_id'])
 
 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'];
 
 226   // getFields returns an array of custom fields for team.
 
 227   static function getFields() {
 
 229         $mdb2 = getConnection();    
 
 232         $sql = "select id, type, label from tt_custom_fields where team_id = $user->team_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']);
 
 243   // getField returns a custom field.
 
 244   static function getField($id) {
 
 246         $mdb2 = getConnection();    
 
 248         $sql = "select label, type, required from tt_custom_fields where id = $id and team_id = $user->team_id";
 
 249     $res = $mdb2->query($sql);
 
 250     if (!is_a($res, 'PEAR_Error')) {
 
 251       $val = $res->fetchRow();
 
 259   // getFieldIdForOption returns field id from an associated option id.
 
 260   static function getFieldIdForOption($option_id) {
 
 261         $mdb2 = getConnection();    
 
 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'];
 
 273   // The insertField inserts a custom field for team.
 
 274   static function insertField($field_name, $field_type, $required) {
 
 278         $mdb2 = getConnection();
 
 280     $sql = "insert into tt_custom_fields (team_id, type, label, required, status) values($user->team_id, $field_type, ".$mdb2->quote($field_name).", $required, 1)";
 
 281     $affected = $mdb2->exec($sql);
 
 282     return (!is_a($affected, 'PEAR_Error'));
 
 285   // The updateField updates custom field for team.
 
 286   static function updateField($id, $name, $type, $required) {
 
 290         $mdb2 = getConnection();
 
 292     $sql = "update tt_custom_fields set label = ".$mdb2->quote($name).", type = $type, required = $required where id = $id and team_id = $user->team_id";
 
 293     $affected = $mdb2->exec($sql);
 
 294     return (!is_a($affected, 'PEAR_Error'));
 
 297   // The deleteField deletes a custom field, its options and log entries for team.
 
 298   static function deleteField($field_id) {
 
 300         // Our overall intention is to keep the code simple and manageable.
 
 301         // If a users wishes to delete a field, we will delete all its options and log entries.
 
 302         // Otherwise we have to do conditional queries depending on field status (this complicates things).
 
 305         $mdb2 = getConnection();
 
 307         // First make sure that the field is ours so that we can safely delete it.
 
 308         $sql = "select team_id from tt_custom_fields where id = $field_id";
 
 309         $res = $mdb2->query($sql);
 
 310         if (is_a($res, 'PEAR_Error'))
 
 312         $val = $res->fetchRow();
 
 313         if ($user->team_id != $val['team_id'])
 
 316         // Mark log entries as deleted.
 
 317         $sql = "update tt_custom_field_log set status = NULL where field_id = $field_id";
 
 318     $affected = $mdb2->exec($sql);
 
 319     if (is_a($affected, 'PEAR_Error'))
 
 322         // Delete field options.
 
 323         $sql = "delete from tt_custom_field_options where field_id = $field_id";
 
 324     $affected = $mdb2->exec($sql);
 
 325     if (is_a($affected, 'PEAR_Error'))
 
 329         $sql = "delete from tt_custom_fields where id = $field_id and team_id = $user->team_id";
 
 330     $affected = $mdb2->exec($sql);
 
 331     return (!is_a($affected, 'PEAR_Error'));