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


class WbLoginDbloptin extends Datamodul
{
  public $token;
  public $expires;
  public $web_wblogin_id;

  public function __construct()
  {
    parent::__construct();
    unset($this->id);
  }

  
  public static function createNewObject()
  {
    $instance = new self();
    $expireDate = DateTimeOperations::getTimestampNow();
    $expireDate += ((int)ToolSettingsAPI::getToolSettings(0)->stammhoererOptinExpires) * 60 * 60;
    $instance->setValue2Var('expires', $expireDate);

    $tmp = \SecurityOperations::generateSalt();
    $instance->setValue2Var('token', md5(time() . $tmp)); 

    return $instance;
  }

  public function getTyp($pFieldName)
  {
    switch ($pFieldName) {
      default:
        return parent::getTyp($pFieldName);
      case 'expires':
      case 'web_wblogin_id':
        return \DatamodulVarType::TYPE_INT;
    }
  }

  public function isExpired()
  {
    return ($this->expires < DateTimeOperations::getTimestampNow());
  }

  public function getWbLogin()
  {
    $dbwWbLogins = new DBWorkWBLogins();

    return $dbwWbLogins->getById($this->web_wblogin_id);
  }
}