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

class DBWorkWebKeys extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'wk.id, ' . $this->dboperation->dbparser->cast2Text('public_key', 'wk') . ', '
        . $this->dboperation->dbparser->cast2Text('modulus', 'wk') . ', wk.gueltigvon, wk.gueltigbis';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->public_key = $this->getDatamodulValue('public_key');
    $tmp->modulus = $this->getDatamodulValue('modulus');
    $tmp->gueltigvon = $this->getDatamodulValue('gueltigvon');
    $tmp->gueltigbis = $this->getDatamodulValue('gueltigbis');

    return $tmp;
  }

  
  public function getCurrentWebKey()
  {
    $sql = 'SELECT ' . $this->select . ' FROM web_keys wk ';
    $sql .= 'WHERE ' . $this->datetimeop->getFormateDate4ServerNow()
      . ' >= wk.gueltigvon AND wk.gueltigbis >= ' . $this->datetimeop->getFormateDate4ServerNow()
      . ' ORDER BY id desc';
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    if ($this->dboperation->rowcount() > 0) {
      return $this->getFirstRow();
    } else {
      return false;
    }
  }
}