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






class DBWorkAssignedPrivileges extends DBWork
{
  
  public function getNewObject()
  {
    return new AssignedPrivileg();
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->tool_id = $this->getDatamodulValue('tool_id');
    $tmp->text = $this->getDatamodulValue('description');
    $tmp->access = $this->getDatamodulValue('accessnumber');

    return $tmp;
  }

  public function getXtraAssignedPrivileges($pTool_Id, $pUser_Id)
  {
    $tool_id = $this->dboperation->getInt4Sql($pTool_Id);
    $user_id = $this->dboperation->getInt4Sql($pUser_Id);
    $this->dboperation->clear();
    $this->dboperation->query("SELECT p.tool_id,p.description, ptu.accessnumber, ptu.id\n
                               FROM web_privileges_t_u ptu \n
                                    INNER JOIN web_tools_users tu ON (tu.id=ptu.tool_user_id) \n
                                    INNER JOIN web_privileges p ON (ptu.privileg_id=p.id) \n
                               WHERE tu.tool_id=" . $tool_id . ' AND tu.user_id=' . $user_id . "\n
                               ORDER BY p.keynumber");

    return $this->getAllRows();
  }

  public function getXtraAssignedPrivilege($pPrivileg_Id)
  {
    $privileg_id = $this->dboperation->getInt4Sql($pPrivileg_Id);
    $this->dboperation->clear();
    $this->dboperation->query("SELECT p.tool_id,p.description, ptu.accessnumber, ptu.id\n
                               FROM web_privileges_t_u ptu \n
                                    INNER JOIN web_tools_users tu ON (tu.id=ptu.tool_user_id) \n
                                    INNER JOIN web_privileges p ON (ptu.privileg_id=p.id) \n
                               WHERE ptu.id=" . $privileg_id . "\n
                               ORDER BY p.keynumber");

    return $this->getFirstRow();
  }

  public function addXtraAssignedPrivileg($pPrivileg_Id, $tool_id, $user_id, $pAccess)
  {
    $privileg_id = $this->dboperation->getInt4Sql($pPrivileg_Id);
    $access = $this->dboperation->getInt4Sql($pAccess);
    $tmp = new DBWorkTools($this->dbtyp, true);
    $tool_user_id = $tmp->getToolUserID($user_id, $tool_id);
    $this->dboperation->clear();
    $this->dboperation->execute("INSERT INTO web_privileges_t_u (privileg_id,tool_user_id,accessnumber)\n
                               VALUES(" . $privileg_id . ',' . $tool_user_id . ',' . $access . ') ');
  }

  public function editXtraAssignedPrivileg($pPrivileg_Id, $pAccess)
  {
    $privileg_id = $this->dboperation->getInt4Sql($pPrivileg_Id);
    $access = $this->dboperation->getInt4Sql($pAccess);
    $this->dboperation->clear();
    $this->dboperation->execute("UPDATE web_privileges_t_u \n
                               SET accessnumber=" . $access . "\n
                               WHERE id=" . $privileg_id);
  }

  public function delXtraAssignedPrivileg($pPrivileg_Id)
  {
    $privileg_id = $this->dboperation->getInt4Sql($pPrivileg_Id);
    $this->dboperation->clear();
    $this->dboperation->execute("DELETE FROM web_privileges_t_u \n
                               WHERE id=" . $privileg_id);
  }

  
  public function getTemplateAssignedPrivileges($template_id)
  { 
  }

  public function getTemplateAssignedPrivilege($pPrivileg_Id)
  {
    $privileg_id = $this->dboperation->getInt4Sql($pPrivileg_Id);
    $this->dboperation->clear();
    $this->dboperation->query("SELECT p.tool_id,p.description, ptu.accessnumber, ptu.id\n
                               FROM web_privileges_t_u ptu \n
                                    INNER JOIN web_tools_users tu ON (tu.id=ptu.tool_user_id) \n
                                    INNER JOIN web_privileges p ON (ptu.privileg_id=p.id) \n
                               WHERE ptu.id=" . $privileg_id . "\n
                               ORDER BY p.keynumber");

    return $this->getFirstRow();
  }
}