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


class DBWorkBereichsRechte extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);

    $this->select = 'rb.id, rb.benid, rb.woher, rb.feld, rb.inhalt, rb.recht';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $obj = $this->getNewObject();

    $obj->id = $this->getDatamodulValue('id');
    $obj->benid = $this->getDatamodulValue('BenID');
    $obj->woher = $this->getDatamodulValue('Woher');
    $obj->feld = $this->getDatamodulValue('Feld');
    $obj->inhalt = $this->getDatamodulValue('Inhalt');
    $obj->recht = $this->getDatamodulValue('Recht');

    return $obj;
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from('rechtber', 'rb');
  }

  
  public function getRechte4UserQuery(\Kufer\Web\Classes\Identifier\Identifier $pUserId, string $pTyp): \SelectQueryBuilder
  {
    return $this->getAllQuery()
      ->innerJoin('web_users wu', 'rb.benid = wu.ben AND :userId:')
      ->and('rb.woher = :typ:')
      ->paramId('userId', $pUserId, 'wu.id', 'wu.guid')
      ->paramString('typ', $pTyp);
  }

  
  public function getRechte4User(\Kufer\Web\Classes\Identifier\Identifier $pUserId, string $pTyp): array
  {
    $query = $this->getRechte4UserQuery($pUserId, $pTyp);

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

    return $this->getAllRows();
  }
}