<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class DBWorkWebLoginProtocol extends DBWork
{
  
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = ' wlp.id, wlp.user_id, wlp.modul, wlp.vorgang, wlp.zeitpunkt, wlp.login ';
  }

  
  public function getNewObject()
  {
    return new WebLoginProtocol();
  }

  public function getTableName(): string
  {
    return 'web_login_protocol';
  }

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $columns = array('id', 'user_id', 'modul', 'vorgang', 'zeitpunkt', 'login');
    foreach ($columns as $column) {
      $tmp->{$column} = $this->getDatamodulValue($column);
    }

    return $tmp;
  }

  
  public function getWebLoginProtocol($pId)
  {
    $builder = $this->getSelectQueryBuilder();
    $builder->select($this->select)
            ->from('web_login_protocol', 'wlp')
            ->where('wlp.id = :id:')
            ->paramInt('id', $pId);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getFirstRow();
  }

  public function getAllQuery()
  {
    return $this->getSelectQueryBuilder()
                    ->select($this->select)
                    ->from('web_login_protocol', 'wlp')
                    ->orderByDesc('zeitpunkt')
                    ->orderByDesc('id')
                    ->paramRaw('table', 'wlp');
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\WebLoginProtocolSQLFilter $filter): \SelectQueryBuilder
  {
    $cond = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($cond);
  }

  
  public function findByFilter(\Kufer\Web\Classes\Dbfilter\WebLoginProtocolSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function countByFilter(\Kufer\Web\Classes\Dbfilter\WebLoginProtocolSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) as anzahl');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());

    $this->dboperation->selectrow(0);

    return (int)$this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }

  
  public function getAll()
  {
    $this->dboperation->clear();
    $this->dboperation->query($this->getAllQuery()->getSQL());

    return $this->getAllRows();
  }

  
  public function insertWebLoginProtocol($pWebLoginProtocol)
  {
    
    if (empty($pWebLoginProtocol->login)) {
      return $pWebLoginProtocol->id;
    }
    $pWebLoginProtocol->id = $this->insertRecord('web_login_protocol', 'id', 'user_id', false);
    $orig = $this->getWebLoginProtocol($pWebLoginProtocol->id);
    array_push($this->updateAlways, 'modul', 'vorgang');
    $this->updateObject($pWebLoginProtocol, $orig, 'web_login_protocol', 'id', 'A', true);

    return $pWebLoginProtocol->id;
  }

  public function getDateTimesOfFirstAndLast()
  {
    static $cache = null;
    if (is_null($cache)) {
      $builder = $this->getAllQuery()
                      ->select('wlp.zeitpunkt');

      
      
      $builder->clearOrderBy()
              ->orderByAsc('wlp.zeitpunkt');

      $this->dboperation->clear();
      $this->dboperation->query($builder->getSQL());

      if ($this->dboperation->rowcount() > 0) {
        $searchFromBeginning = true;
        $firstDateTime = $this->getFirstDateNotEmpty($searchFromBeginning);
        $lastDateTime = $this->getFirstDateNotEmpty(!$searchFromBeginning);

        $cache = array($firstDateTime, $lastDateTime);
      } else {
        $cache = false;
      }
    }

    return $cache;
  }

  
  private function getFirstDateNotEmpty($pSearchFromBeginning)
  {
    $rowCount = $this->dboperation->rowcount();
    for ($i = 0; $i < $rowCount; $i++) {
      $selectRow = $pSearchFromBeginning ? $i : $rowCount - ($i + 1);
      $this->dboperation->selectrow($selectRow);
      $date = $this->getDatamodulValue('zeitpunkt');

      if ($date != '') {
        return $date;
      }
    }

    return false;
  }
}