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

namespace Kufer\KuferTools\Basis\Markieren;

class Markieren
{
  
  private $toolId;

  
  private $idsToMark;

  
  private $idsToUnmark;

  
  private $markerAction;

  
  private $sessionVarName;

  
  private $selectionSessionVarname;

  
  public function __construct($pToolId, $pIdsToMark, $pIdsToUnmark, $pMarkerAction, $pSessionVarName, $pSelectionSessionVarName = 'selnrstr_')
  {
    $this->toolId = $pToolId;
    $this->idsToMark = $pIdsToMark;
    $this->idsToUnmark = $pIdsToUnmark;
    $this->markerAction = $pMarkerAction;
    $this->sessionVarName = $pSessionVarName;
    $this->selectionSessionVarname = $pSelectionSessionVarName;
  }

  
  public static function getMarkierenFromRequest($pToolId, $pSessionVarName = 'totaldatastr_', $pSelectionSessionVarName = 'selnrstr_')
  {
    
    
    
    $idsToMark = \PostOperations::getVarValue('idsToMark', \GetOperations::getVarValue('idsToMark', ''));
    $idsToUnmark = \PostOperations::getVarValue('idsToUnmark', \GetOperations::getVarValue('idsToUnmark', ''));
    $markerAction = \PostOperations::getVarValue('markerAction', \GetOperations::getVarValue('markerAction'));

    return new Markieren($pToolId, $idsToMark, $idsToUnmark, $markerAction, $pSessionVarName, $pSelectionSessionVarName);
  }

  public function setMarkedIdsToSession()
  {
    $toolsApi = new \ToolsAPI();
    $toolsApi->updateAndGetSelectedIdsbyParams(
      $this->markerAction,
      $this->idsToMark,
      $this->idsToUnmark,
      $this->toolId,
      $this->sessionVarName,
      $this->selectionSessionVarname
    );
  }

  public function clearMarkedIdsInSession()
  {
    $toolsApi = new \ToolsAPI();
    $toolsApi->updateAndGetSelectedIdsbyParams('loescheMarkierungen', '', '', $this->toolId, $this->sessionVarName, $this->selectionSessionVarname);
  }

  
  public function getMarkedIdsFromSession()
  {
    return \SessionOperations::getVarValueWTIntern($this->selectionSessionVarname . $this->toolId);
  }

  public function getMarkedIdsFromAsArray()
  {
    $markedIds = $this->getMarkedIdsFromSession();
    if ($markedIds === '') {
      return array();
    }

    return explode(':', $markedIds);
  }

  public function updateAndGetIdsFromSessionAsArray()
  {
    $this->setMarkedIdsToSession();

    return $this->getMarkedIdsFromAsArray();
  }

  public function setTotalDataStr(string $str): void
  {
    \SessionOperations::setVarValueWTIntern($this->sessionVarName . $this->toolId, $str);
  }

  public function setMarkedIdsStr(string $str): void
  {
    \SessionOperations::setVarValueWTIntern($this->selectionSessionVarname . $this->toolId, $str);
  }

  public function setTotalDataArray(array $ary): void
  {
    $this->setTotalDataStr(implode(':', array_unique($ary)));
  }

  public function setMarkedIdsArray(array $ary): void
  {
    $this->setMarkedIdsStr(implode(':', array_unique($ary)));
  }
}