<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

class DBWorkWarenkorb
{ 
  
  public $ary;
  public $data; 
  public $sessionvar = '';
  public $stringop;
  public $datetimeop;
  public $mathop;
  
  public $delimiter_entries = '@';

  
  public $delimiter_menge = '|';

  
  public $addiereMenge = true;

  public $dbtyp;

  
  public function __construct()
  {
    $this->dbtyp = 1;
    $this->stringop = new StringOperations();
    $this->datetimeop = new DateTimeOperations();
    $this->mathop = new MathOperations();
    $this->initiate();
  }

  public function cleardata()
  {
  }

  public function initiate()
  {
  }

  
  public function explodeEntries()
  {
    $sessionVal = SessionOperations::getVarValueWk($this->sessionvar);
    if (strlen($sessionVal) === 0) {
      $this->ary = array();
    } else {
      $this->ary = explode($this->delimiter_entries, $sessionVal);
    }
  }

  
  public function implodeEntries()
  {
    SessionOperations::setVarValueWk($this->sessionvar, implode($this->delimiter_entries, $this->ary));
  }

  
  public function explodeEntry($exEntry)
  {
    
    $this->cleardata();
    $tmp = explode($this->delimiter_menge, $exEntry);
    $this->setDataToDefaultIfNull();
    $this->data->id = $tmp[0];
    if (isset($tmp[1])) {
      $this->data->menge = $tmp[1];
    }
  }

  private function setDataToDefaultIfNull()
  {
    if (is_null($this->data)) {
      $this->data = new Ware();
    }
  }

  
  public function implodeEntry()
  {
    return implode($this->delimiter_menge, array($this->data->id, $this->data->menge));
  }

  
  public function getEntries()
  {
    
    
    $this->explodeEntries();
    if ((SessionOperations::getVarValueWk($this->sessionvar) != '')) {
      $tmp = array();
      for ($i = 0;$i < count($this->ary);$i++) {
        
        $this->explodeEntry($this->ary[$i]);
        $tmp[$i] = $this->data;
      }

      return $tmp;
    }

    return array();
  }

  
  public function EntryCount()
  {
    if (SessionOperations::getVarValueWk($this->sessionvar) == '') {
      return 0;
    } else {
      return count($this->getEntries());
    }
  }

  
  public function getEntriesIDSerialized()
  {
    $tmpary = array();
    $this->explodeEntries();
    for ($i = 0;$i < count($this->ary);$i++) {
      $tmp = explode($this->delimiter_menge, $this->ary[$i]);
      $tmpary[] = $tmp[0];

      

    }

    return $tmpary;
  }

  
  public function getEntry($id)
  {
    
    $tmp = $this->getEntryIndex($id);
    if ($tmp > -1) {
      
      $this->explodeEntries();
      
      $this->explodeEntry($this->ary[$tmp]);

      return $this->data;
    }
  }

  
  public function getEntryIndex($id)
  {
    
    
    $this->explodeEntries();
    for ($i = 0;$i < count($this->ary);$i++) {
      
      $this->explodeEntry($this->ary[$i]);
      if ($this->data->id == $id) {
        return $i;
      }
    }

    return -1;
  }

  
  public function addEntry($id, $menge = 1)
  {
    
    $tmp = $this->getEntryIndex($id);
    
    
    $this->explodeEntries();
    
    if ($tmp > -1) {
      
      $this->explodeEntry($this->ary[$tmp]);
      if ($this->addiereMenge) {
        $this->data->menge += $menge;
      } else {
        $this->data->menge = $menge;
      }
      
      $this->ary[$tmp] = $this->implodeEntry();
    }
    
    else {
      
      $this->cleardata();
      $this->data->id = $id;
      $this->data->menge = $menge;
      
      if (SessionOperations::getVarValueWk($this->sessionvar) == '') {
        $this->ary[0] = $this->implodeEntry();
      } else {
        array_push($this->ary, $this->implodeEntry());
      }
    }
    
    
    $this->implodeEntries();
  }

  
  public function delEntry($id)
  {
    
    $tmp = $this->getEntryIndex($id);
    
    
    $this->explodeEntries();
    if ($tmp > -1) {
      unset($this->ary[$tmp]);
    }
    
    $this->implodeEntries();
  }

  
  public function clear()
  {
    SessionOperations::setVarValueWk($this->sessionvar, null);
  }

  public static function getDefaultInstance($pNewInstance = false)
  {
    static $instance = array();
    if (!array_key_exists(static::class, $instance)) {
      $instance[static::class] = new static($pNewInstance);
    }

    return $instance[static::class];
  }
}