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







class DBWorkTAN extends DBWork
{ 
  function getNewObject()
  { return new TAN(); }

  
  function getActualRow()
  { $tmp = $this->getNewObject();
    $tmp->id      = $this->getDatamodulValue('id');
    $tmp->userid  = $this->getDatamodulValue('user_id');
    $tmp->tan     = $this->getDatamodulValue('tan');
    $tmp->used    = $this->getDatamodulValue('used');
    $tmp->usedate = $this->getDatamodulValue('usedate');
    
    return $tmp;
  }

  
  function getTAN($pTan_Id)
  { 
  	$tan_id = $this->dboperation->getInt4Sql($pTan_Id);
  	$this->dboperation->clear();
    $this->dboperation->query("SELECT wt.* FROM web_tans wt WHERE wt.id=".$tan_id);
    return $this->getFirstRow();
  }
  
  
  function getTANbyTAN($pTan)
  { 
  	$tan = $this->dboperation->getString4Sql($pTan);
  	$this->dboperation->clear();
    $this->dboperation->query("SELECT wt.* FROM web_tans wt WHERE wt.tan='".$tan."'");
    return $this->getFirstRow();
  }
  
  
  function getTANsbyUser($pUser_Id)
  { 
  	$user_id = $this->dboperation->getInt4Sql($pUser_Id);
  	$this->dboperation->clear();
    $this->dboperation->query("SELECT wt.* \n
                               FROM web_tans wt INNER JOIN web_users wu ON (wt.user_id=wu.id AND wu.id=".$user_id.")");
    return $this->getAllRows();
  }
  
  
  
  function checkTAN($pUser_Id,$pTan)  
  { 
  	$result = false;
  	$user_id = $this->dboperation->getInt4Sql($pUser_Id);
  	$tan = $this->dboperation->getString4Sql($pTan);
    $this->dboperation->clear();
    $this->dboperation->query("SELECT wt.* \n
                               FROM web_tans wt INNER JOIN web_users wu ON (wt.user_id=wu.id AND wu.id=".$user_id.")\n
                               WHERE wt.tan='".StringOperations::strtoupper($tan)."'");
    if ($this->dboperation->rowcount()>0)
    { $this->dboperation->selectrow(0);
      if ($this->getDatamodulValue('used')<1) $result = true;
    }
    return $result;
  }
  
  
  function setUsedTAN($user_id,$tan)
  { $edittan = $this->getTANbyTAN($tan);
    $edittan->used = 1;
    $edittan->usedate = $this->datetimeop->getDateNow();
    $orgtan = $this->getTANbyTAN($tan);
    $this->updateObject($edittan,$orgtan,"web_tans",$user_id,"id");
    $this->setActionIDs();
    return $edittan->id;
  }
}
?>