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 __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     // 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 group_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->group_id != $val['group_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 group_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->group_id != $val['group_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   // getOptionName 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 group_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->group_id != $val['group_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 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']);
 
 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 group_id = $user->group_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) {
 
 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'));
 
 282   // The updateField updates custom field for team.
 
 283   static function updateField($id, $name, $type, $required) {
 
 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'));
 
 291   // The deleteField deletes a custom field, its options and log entries for team.
 
 292   static function deleteField($field_id) {
 
 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).
 
 299     $mdb2 = getConnection();
 
 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'))
 
 306     $val = $res->fetchRow();
 
 307     if ($user->group_id != $val['group_id'])
 
 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'))
 
 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'))
 
 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'));