<?php
   class saveValues
   {
      var $db         = "";

      var $table      = "";
      var $id         = 0;
      var $user       = "";
      var $link       = "";

      var $primary    = "id";

      var $values_new = array();
      var $values_old = array();
      var $misc       = array();

      var $structure  = array("id");
      var $cat_values = array();
      var $defaults   = array();
      var $where      = 1;

      var $format     = array();

      function decategorize ($values,$level=0,$known=array())
      {
         foreach ($values as $key => $array)
         {
            $known[$this->structure[$level]] = $key;

            if ($level < count($this->structure)-1)
            {
               $this->decategorize($array,$level+1,$known);
            }
             else
            {
               $temp_array = array_merge($known,$array,$this->defaults);

               $where = "";
               foreach ($known as $k => $v)
               {
                  $where .= " AND {$k} = '{$v}'";
               }
               $sql = "SELECT * FROM {$this->table} WHERE {$this->where} {$where}";
               $erg = $this->db->sql_exec($sql);

               if (1 == $this->db->sql_num_rows($erg))
               {
                  $t_values_old = $this->db->sql_fetch_assoc($erg);

                  $t_id = $t_values_old[$this->primary];
                  $this->values_old[$t_id] = $this->remove_null($t_values_old);
                  $this->values_new[$t_id] = $this->format($temp_array);
               }
                else
               {
                  $this->cat_values[] = $this->format($temp_array);
               }
            }
         }
      }

      function remove_null ($t_array)
      {
         foreach ($t_array as $key => $value)
         {
            if (!$value) $t_array[$key] = '';
         }
         return $t_array;
      }

      function load ($val)
      {
         $this->values_new = $val;
         $this->values_new['benutzer_id'] = $this->user;
         $this->values_new['timestamp'] = time();

         if (0 == $this->id)
         {
            $sql = "INSERT INTO {$this->table} ({$this->primary}) VALUES ('')";
            $erg = $this->db->sql_exec($sql);

            $this->id = $this->db->sql_insert_id();
         }

         $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = '{$this->id}'";
         $erg = $this->db->sql_exec($sql);

         $this->values_old = $this->db->sql_fetch_assoc($erg);
      }

      function perform ($format=1)
      {
         if (1 == $format)
         {
            $this->format();
         }

         if (0 < count($this->values_old))
         {
            $this->comparison();
            $this->storage();
         }

         if (0 < count($this->cat_values))
         {
            $this->insert();
         }
    //     $this->linkup();
      }

      function format ($values=0)
      {
         $return_values = 1;

         if (0 == $values)
         {
            $values = $this->values_new;
            $return_values = 0;
         }

         foreach ($this->format as $t_field => $t_format)
         {
            switch($t_format)
            {
               case 'date':
                  $values[$t_field] = date_format($values[$t_field]);
                  break;
               case 'datetime':
                  $t_date = date_format($values[$t_field.'_d']);
                  $t_hour = $values[$t_field.'_h'];
                  $t_min  = $values[$t_field.'_m'];

                  $t_datetime = $t_date . " " . $t_hour . ":" . $t_min . ":00";
                  $values[$t_field] = $t_datetime;
                  break;
               case 'time':
                  $t_hour = $values[$t_field.'_h'];
                  $t_min  = $values[$t_field.'_m'];

                  $t_time = $t_hour . ":" . $t_min . ":00";
                  $values[$t_field] = $t_time;
                  break;
               case 'timetodecimal':
                  $values[$t_field] = timetodecimal($values[$t_field]);
                  break;
               case 'filename':
                  $values[$t_field] = str_replace("\\","\\\\",$values[$t_field]);
                  break;
               case 'text':
                  // z.Zt. keine Änderung der Variablen
                  break;
               case 'float':
                  $values[$t_field] = str_replace(",",".",$values[$t_field]);
                  break;
            }
         }
         if (1 == $return_values)
         {
            return ($values);
         }
          else
         {
            $this->values_new = $values;
         }
      }

      function comparison ()
      {
         foreach ($this->values_new as $t_name => $t_value)
         {
            if (is_array($t_value))
            {
               foreach ($t_value as $tsub_name => $tsub_value)
               {
                  if (isset($this->values_old[$t_name][$tsub_name]))
                  {
                     if ($this->values_old[$t_name][$tsub_name] == $tsub_value)
                     {
                        unset($this->values_new[$t_name][$tsub_name]);
                     }
                  }
                   else
                  {
                     unset($this->values_new[$t_name][$tsub_name]);
                     $this->misc[$t_name][$tsub_name] = $tsub_value;
                  }
               }
            }
             else
            {
               if (isset($this->values_old[$t_name]))
               {
                  if ($t_value == $this->values_old[$t_name])
                  {
                     unset($this->values_new[$t_name]);
                  }
               }
                else
               {
                  unset($this->values_new[$t_name]);
                  $this->misc[$t_name] = $t_value;
               }
            }
         }
      }

      function backup ()
      {
         $sql = "INSERT INTO `{$this->db->dbs_backup}`.`{$this->table}` SELECT * FROM `{$this->db->dbs_database}`.`{$this->table}` WHERE {$this->primary} = '{$this->id}'";
         // $erg = $this->db->sql_exec($sql);
      }

      function storage ()
      {
         $this->backup();

         foreach ($this->values_new as $t_name => $t_value)
         {
            if (is_array($t_value))
            {
               $t_id = $t_name;
               foreach ($t_value as $tsub_name => $tsub_value)
               {
                  $sql = "UPDATE {$this->table} SET {$tsub_name} = '{$tsub_value}' WHERE {$this->primary} = '{$t_id}'";
                  $erg = $this->db->sql_exec($sql);
               }
            }
             else
            {
               $sql = "UPDATE {$this->table} SET {$t_name} = '{$t_value}' WHERE {$this->primary} = '{$this->id}'";
               $erg = $this->db->sql_exec($sql);
            }
         }
      }

      function insert ()
      {
         foreach ($this->cat_values as $values)
         {
            $replace_columns = implode(",",array_keys($values));
            $replace_values  = "'" . implode("','",$values) . "'";

            $sql = "REPLACE {$this->table} ({$replace_columns}) VALUES ({$replace_values})";
            $erg = $this->db->sql_exec($sql);
         }
      }

      function linkup ()
      {
         if ($this->link == "")
         {
            $this->link = DEFAULT_LINK;
         }
         echo "<script>
                   function link (url)
                   {
                      window.location.href = url;
                   }
                   window.setTimeout(\"link('{$this->link}')\"," . TIMEOUT . ");
               </script>";
      }
   }
?>