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






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

    $this->select = 's.id, s.tool_id, s.description, '
      . $this->clob('s.header') . ', '
      . $this->clob('s.body') . ', '
      . $this->clob('s.footer') . ', '
      . $this->clob('s.nodata') . ', '
      . $this->clob('s.nocolumns');
  }

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

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

  

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->toolid = $this->getDatamodulValue('tool_id');
    $tmp->description = $this->getDatamodulValue('description');
    $tmp->header = $this->getDatamodulValue('header');
    $tmp->body = $this->getDatamodulValue('body');
    $tmp->footer = $this->getDatamodulValue('footer');
    $tmp->nodata = $this->getDatamodulValue('nodata');
    $tmp->nocolumn = $this->getDatamodulValue('nocolumns');

    return $tmp;
  }

  
  public function getSights($pTool_Id)
  {
    $tool_id = $this->dboperation->getInt4Sql($pTool_Id);
    if (!is_numeric($tool_id)) {
      return array();
    }

    $this->dboperation->clear();
    $this->dboperation->query("SELECT $this->select FROM web_sights s WHERE s.tool_id=$tool_id order by s.id");

    return $this->getAllRows();
  }

  
  public function getSight($pSight_Id, $pClearCache = false)
  {
    $sight_id = $this->dboperation->getInt4Sql($pSight_Id);
    if (!is_numeric($sight_id)) {
      return $this->getNewObject();
    }

    static $cache = array();

    if (isset($cache[$sight_id]) && !$pClearCache) {
      return $cache[$sight_id];
    }
    
    $this->dboperation->clear();
    $this->dboperation->query("SELECT $this->select FROM web_sights s WHERE s.id = $sight_id");
    $cache[$sight_id] = $this->getFirstRow();

    return $cache[$sight_id];
  }

  
  public function setSight($pSight)
  {
    $this->dboperation->clear();
    $sql = 'UPDATE web_sights SET '
      . 'header = \'' . str_replace("'", "\\'", $pSight->header) . '\', '
      . 'body = \'' . str_replace("'", "\\'", $pSight->body) . '\', '
      . 'footer = \'' . str_replace("'", "\\'", $pSight->footer) . '\', '
      . 'nocolumns = \'' . str_replace("'", "\\'", $pSight->nocolumn) . '\', '
      . 'nodata = \'' . str_replace("'", "\\'", $pSight->nodata) . '\' '
      . ' WHERE id = ' . $pSight->id;
    $this->dboperation->query($sql);
  }

  public function getToolIds()
  {
    $this->dboperation->clear();
    $this->dboperation->query('SELECT DISTINCT tool_id FROM web_sights');

    return $this->getAllRowsSingleColumn('tool_id');
  }
}