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

  
  if (!defined('ISKUFERTOOLS')) {
    define('ISKUFERTOOLS', true);
  }

  if (!session_id()) {
    session_start();
  }

  if (!defined('BASEDIR')) {
    include_once '..' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'constants.php';
  }

  CodingOperations::encodeEingaben();
  CodingOperations::setObStartIfInternalAndExternalEncodingAreDiffrent();
  CodingOperations::setEncodingResponseHeader();

  if (!SessionOperations::isAdmin()) {
    Menu::forbidden();
    die();
  }

  SecurityOperations::PurgeInput(true);

  class writeColumns
  {
    public $dbsight;

    public function __construct()
    {
      SecurityOperations::PurgeInput(1);
      $this->dbsight = new DBWorkSightsSettings();
    }

    public function getLink($ptoolid, $psightid, $pgroup = '', $panchor = '', $peditcolumn = '')
    {
      $ptoolid = CleanOperations::cleanInt($ptoolid);
      $psightid = CleanOperations::cleanInt($psightid);
      $pgroup = CleanOperations::cleanStringForJs($pgroup);
      $panchor = CleanOperations::cleanStringForJs($panchor);
      $peditcolumn = CleanOperations::cleanStringForJs($peditcolumn);

      if (SessionOperations::getVarValueIntern('fastheader', false) == true) {
        $str = 'hleinstell.php?tool=' . $ptoolid . '&sightid=' . $psightid;
        if ($pgroup != '') {
          $str .= '&gruppe=' . $pgroup;
        }
        if ($peditcolumn != '') {
          $str .= '&editid=' . $peditcolumn;
        }
        if ($panchor != '') {
          $str .= '#anchor' . $panchor;
        }
        header('location: ' . $str);
      } else {
        $str = '<script>document.location.href="hleinstell.php?tool=' . CleanOperations::cleanInt($ptoolid) . '&sightid=' . CleanOperations::cleanInt($psightid) . '&gruppe=' . CleanOperations::cleanStringForJs($pgroup);
        if ($peditcolumn != '') {
          $str .= '&editid=' . CleanOperations::cleanStringForJs($peditcolumn);
        }
        if ($panchor != '') {
          $str .= '#anchor' . CleanOperations::cleanStringForJs($panchor);
        }
        $str .= '"</script>';

        return $str;
      }
    }

    public function addColumn($psight_field_id, $ptitle, $pwidth, $pfontsize, $pbold, $px = 0)
    {
      if ($psight_field_id != '' && $pfontsize != 0 && $pwidth != 0 && isset($ptitle)) {
        $objcolumn = new Column();
        $objcolumn->sight_field_id = $psight_field_id;
        $objcolumn->title = $ptitle;
        $objcolumn->width = $pwidth;
        $objcolumn->fontsize = $pfontsize;
        if ($pbold == '') {
          $pbold = 0;
        }
        $objcolumn->bold = $pbold;
        if ($px == '') {
          $px = 0;
        }
        $objcolumn->x = $px;
        $this->dbsight->addColumn($objcolumn);
      }
    }

    public function deleteColumn($pcolumnid)
    {
      $this->dbsight->delColumn($pcolumnid);
    }

    public function updateColumn($pid, $psight_field_id, $ptitle, $pwidth, $pfontsize, $pbold, $px = 0)
    {
      $objcolumn = new Column();
      $objcolumn->id = $pid;
      $objcolumn->sight_field_id = $psight_field_id;
      $objcolumn->title = $ptitle;
      $objcolumn->width = $pwidth;
      $objcolumn->fontsize = $pfontsize;
      if ($pbold == '') {
        $pbold = 0;
      }
      $objcolumn->bold = $pbold;
      if ($px == '') {
        $px = 0;
      }
      $objcolumn->x = $px;
      $this->dbsight->updateColumn($objcolumn);
    }

    public function moveColumnUp($psight_id, $pcolumn)
    {
      $this->dbsight->MoveColumnUp($psight_id, $pcolumn);
    }

    public function moveColumnDown($psight_id, $pcolumn)
    {
      $this->dbsight->MoveColumnDown($psight_id, $pcolumn);
    }

    
    public function writeDefaultSettingsKurse($tool_id, $sight_id, $objsettings)
    {
      $kurseproseite = PostOperations::getVarValue('kurseproseite'); 
      switch (PostOperations::getVarValue('kurs_ausgef', 'rot')) {
        default:
        case 'rot':    $farbe = '#FF0000'; break;
        case 'braun':  $farbe = '#663300'; break;
        case 'lila':   $farbe = '#CC00CC'; break;
        case 'blau':   $farbe = '#0000CC'; break;
        case 'gelb':   $farbe = '#FFFF00'; break;
        case 'blind':  $farbe = '#blind';  break;
      }
      $arrayResult = $this->getSortierungsSettings();
      $sortkurs = $arrayResult[0];
      $asc = $arrayResult[1];
      $sortkurs2 = $arrayResult[2];
      $asc2 = $arrayResult[3];
      
      $objsettings->kurseproseite = $kurseproseite;
      $objsettings->sortkurse = $sortkurs;
      $objsettings->sortkurseasc = $asc;
      $objsettings->sortkurse2 = $sortkurs2;
      $objsettings->sortkurseasc2 = $asc2;
      $objsettings->id = $tool_id;
      $objsettings = $this->setSemester($objsettings);
      $objsettings->colorkurseausf = $farbe;
      ToolSettingsAPI::setToolSettings($objsettings);

      
      echo $this->getLink($tool_id, $sight_id, PostOperations::getVarValue('gruppe'));
    }

    
    public function setSemester($pObjsettings)
    {
      $pObjsettings->useAbweichendeSemester = PostOperations::isDefined('useAbweichendeSemester');

      $pObjsettings->semlist = $this->getSemlistFromRequest();
      
      $preFilterKursSettings = $this->getPreFilterKursSettingsFromRequest();
      $pObjsettings->kursFilterVorbelegen = $preFilterKursSettings[0];
      $pObjsettings->knrVon = $preFilterKursSettings[2];
      $pObjsettings->knrBis = $preFilterKursSettings[3];
      $pObjsettings->keineAusgefallenenKurse = PostOperations::isDefined('keineAusgefallenenKurse');
      
      if ($pObjsettings->id != \ToolsAPI::TOOL_ANMELDUNGBUCHEN_MA) {
        $pObjsettings->kursFilterKursbeginnVorbelegen = $preFilterKursSettings[1];
        $pObjsettings->kursBegVon = $preFilterKursSettings[4];
        $pObjsettings->kursBegBis = $preFilterKursSettings[5];
        $pObjsettings->kursBegMonatVon = $preFilterKursSettings[6];
        $pObjsettings->kursBegMonatBis = $preFilterKursSettings[7];
      }

      return $pObjsettings;
    }

    
    public function getSemlistFromRequest()
    {
      $semapi = new SemesterHlAPI();
      $semester = $semapi->getSemester();

      return implode(',', $this->getMultiSelectFromRequest(count($semester), 'semlist'));
    }

    
    protected function getMultiSelectFromRequest($pDataCount, $pVarName)
    {
      
      return RequestOperations::getVarValue($pVarName . '-item', array());
    }

    
    public function getStatusFromRequest($pStatusart = 'tnstatus')
    {
      if ($pStatusart == 'ktstatus') {
        $statusAry = array_keys(ConstantOperations::getKurstagStatus4EinschraenkungAufKTStatus());
      } else {
        $dbhlstatKZ = new DBWorkHLStatusKZ();
        $statusAry = $dbhlstatKZ->getAll();
      }
      $statusAry = writeColumns::getMultiSelectFromRequest(count($statusAry), $pStatusart);

      return implode('', $statusAry);
    }

    
    public function getPreFilterKursSettingsFromRequest()
    {
      $preFilterKursSettings[] = PostOperations::getVarValue('kursfilter_vorbelegen');
      $preFilterKursSettings[] = PostOperations::getVarValue('kursFilterKursbeginnVorbelegen');
      $preFilterKursSettings[] = PostOperations::getVarValue('knr_von');
      $preFilterKursSettings[] = PostOperations::getVarValue('knr_bis');
      $preFilterKursSettings[] = PostOperations::getVarValue('kurs_begVon');
      $preFilterKursSettings[] = PostOperations::getVarValue('kurs_begBis');
      $preFilterKursSettings[] = PostOperations::getVarValue('kurs_begMonatVon');
      $preFilterKursSettings[] = PostOperations::getVarValue('kurs_begMonatBis');

      return $preFilterKursSettings;
    }

    
    public function getSortierungsSettings($variante2 = false)
    {
      $sortkurs = $asc = $sortkurs2 = $asc2 = '';
      for ($i = 0; $i < 2; $i++) {
        if ($i == 0) {
          $kriterium = PostOperations::getVarValue('sortkurse', 'kursauf');
        } else { 
          $kriterium = PostOperations::getVarValue('sortkurse2', 'kursauf');
        }
        switch ($kriterium) {
          default:
          case 'kursauf':  $sortkurstmp = ($variante2 == false ? 'nr' : 'knr'); $asctmp = true; break;  
          case 'kursab':  $sortkurstmp = ($variante2 == false ? 'nr' : 'knr'); $asctmp = false; break;  
          case 'beginnauf':  $sortkurstmp = 'beginn_dat'; $asctmp = true; break;  
          case 'beginnab':  $sortkurstmp = 'beginn_dat'; $asctmp = false;  break;  
          case 'ortauf':  $sortkurstmp = ($variante2 == false ? 'ort' : 'k.ort'); $asctmp = true; break;  
          case 'ortab':  $sortkurstmp = ($variante2 == false ? 'ort' : 'k.ort'); $asctmp = false; break;  
          case 'aussenstelleauf':  $sortkurstmp = 'aussenst'; $asctmp = true; break;  
          case 'aussenstelleab':  $sortkurstmp = 'aussenst'; $asctmp = false; break;  
          case 'beginnzeitauf':  $sortkurstmp = 'beginn'; $asctmp = true; break;  
          case 'beginnzeitab':  $sortkurstmp = 'beginn'; $asctmp = false; break;  
          case 'anmeldedatumauf':  $sortkurstmp = 'b.anm_datum'; $asctmp = true; break;  
          case 'anmeldedatumab':  $sortkurstmp = 'b.anm_datum'; $asctmp = false; break;  
        }
        if ($i == 0) {
          $sortkurs = $sortkurstmp;
          $asc = $asctmp;
        } else { 
          $sortkurs2 = $sortkurstmp;
          $asc2 = $asctmp;
        }
      }

      return array($sortkurs, $asc, $sortkurs2, $asc2);
    }

    
    public function getTnStatusFromPOST($pToolId, $pArt = '')
    {
      $Fa = '';
      if ($pArt == 'fa') {
        $Fa = 'Fa';
      }

      
      $tnStatus = '';
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusVorbehaltsstufe1' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusVorbehaltsstufe2' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusAngemeldet' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusAnmeldungFirma' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusInteresse' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusPlatzreservierung' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusStorno' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusTeilgenommen' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusUmmeldung' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusWarteliste' . $Fa);
      $tnStatus .= PostOperations::getVarValue('anwFilterStatusAbsage' . $Fa);
      
      return $tnStatus;
    }

    
    public function setSortierung($pObjSettings, $pSortPerson, $pPrefix = '')
    {
      $prefix = $pPrefix;
      if ($prefix != '') {
        if (StringOperations::strpos($pPrefix, '.') === false) {
          $prefix = $pPrefix . '.';
        }
      }

      switch (PostOperations::getVarValue($pSortPerson, 'nameauf')) {
        default:
        case 'nameauf':  $sort = $prefix . 'name'; $asc = true; break;
        case 'nameab':  $sort = $prefix . 'name'; $asc = false; break;
        case 'nrauf':  $sort = $prefix . 'nr'; $asc = true; break;
        case 'nrab':  $sort = $prefix . 'nr'; $asc = false; break;
        case 'anmeldedatumauf':  $sort = 'b.anm_datum'; $asc = true; break;
        case 'anmeldedatumab':  $sort = 'b.anm_datum'; $asc = false; break;
      }

      $pObjSettings->{$pSortPerson} = $sort;
      $pObjSettings->{$pSortPerson . 'asc'} = $asc;

      return $pObjSettings;
    }
  }

  
  class FilterInpStr
  {
    
    public $allowedInput = '';

    
    public function setAllowedInput()
    {
    }

    
    public function eingabeFiltern($inputStr)
    {
      $restrictStr = '';
      
      $inpStrBig = StringOperations::strtoupper($inputStr);
      
      for ($i = 0;$i < StringOperations::strlen($inpStrBig);$i++) {
        
        
        if (StringOperations::strpos($this->allowedInput, StringOperations::substr($inpStrBig, $i, 1)) !== false) {
          
          if (StringOperations::strpos($restrictStr, StringOperations::substr($inpStrBig, $i, 1)) === false) {
            $restrictStr .= StringOperations::substr($inpStrBig, $i, 1);
          }
        }
      }

      return $restrictStr;
    }
  } 

  
  class FilterInpKtStat extends FilterInpStr
  {
    
    public function setAllowedInput()
    {
      $this->allowedInput = '-XAZVNEPIKD';
    }
  }

  
  class FilterInpTnStat extends FilterInpStr
  {
    
    public function setAllowedInput()
    {
      
      
      
      $dbhlstatKZ = new DBWorkHLStatusKZ();

      $anmstatary = $dbhlstatKZ->getAll();
      $this->allowedInput = '';
      foreach ($anmstatary as $anmstat) {
        if (!empty($anmstat->kennz)) {
          $this->allowedInput .= $anmstat->kennz;
        } else {
          $this->allowedInput .= '-';
        }
      }
    }
  }

  
  class WriteStat
  {
    
    public $sortvarstr;    
    public $sortname;      
    public $kenn;          
    public $colorstat;     
    public $status;        
    public $sortasc;       
    public $sortobjvar;    

    
    public function __construct($objsettings)
    {
    }

    
    public function getFilterobject()
    {
      return null;
    }

    
    public function setObjsettingVar($pObjsettings)
    {
    }

    
    public function writeSettings($objsettings, $pTool_id, $pSight_id)
    {
      
      switch (PostOperations::getVarValue($this->sortvarstr, 'nrauf')) {
          
          default:
          case 'nrauf':  $sortvar = $this->sortname; $asc = true; break;
          case 'nrab':  $sortvar = $this->sortname; $asc = false; break;
          case 'nameauf':  $sortvar = 't.name'; $asc = true; break;
          case 'nameab':  $sortvar = 't.name'; $asc = false; break;
          case 'anmeldedatumauf':  $sortvar = 'b.anm_datum'; $asc = true; break;
          case 'anmeldedatumab':  $sortvar = 'b.anm_datum'; $asc = false; break;
        }

      
      $writeColumns = new writeColumns();
      $statusArt = PostOperations::getVarValue('statusart');
      
      $statusOld = $this->status;
      $status = $writeColumns->getStatusFromRequest($statusArt);
      
      $this->status = $status;

      
      
      
      
      

      
      
      $oldStatusFarbAry = array();
      for ($i = 0;$i < StringOperations::strlen($statusOld);$i++) {
        $oldStatFarbElem = new stdClass();
        $oldStatFarbElem->status = $statusOld[$i];
        
        $farbeFromPost = PostOperations::getVarValue($this->kenn . 'status_anzeige' . ($i + 1), 'schwarz');
        switch ($farbeFromPost) {
          case 'grün':
            $oldStatFarbElem->farbe = '#33A567';
            break;
          case 'rot':
            $oldStatFarbElem->farbe = '#FF0000';
            break;
          case 'braun':
            $oldStatFarbElem->farbe = '#663300';
            break;
          case 'lila':
            $oldStatFarbElem->farbe = '#CC00CC';
            break;
          case 'blau':
            $oldStatFarbElem->farbe = '#0000CC';
            break;
          case 'gelb':
            $oldStatFarbElem->farbe = '#FFFF00';
            break;
          case 'schwarz':
            $oldStatFarbElem->farbe = '#000000';
            break;
          case 'frei':
            
            $oldStatFarbElem->farbe = '#000000';
            if (PostOperations::getVarValue($this->kenn . 'status_frei' . ($i + 1)) != '') {
              $oldStatFarbElem->farbe = PostOperations::getVarValue($this->kenn . 'status_frei' . ($i + 1));
            }
            break;
        } 

        $oldStatusFarbAry[] = $oldStatFarbElem;
      }
      
      $newStatFarbAry = array();
      for ($i = 0;$i < StringOperations::strlen($this->status);$i++) {  
        
        $aktStatFlag = StringOperations::substr($this->status, $i, 1); 
        $statkennz = $aktStatFlag . '=';
        

        
        if (($pos = StringOperations::strpos($statusOld, $aktStatFlag)) !== false) {
          
          $oldStatFarbElem = $oldStatusFarbAry[$pos];
          $farbe = $oldStatFarbElem->farbe;
          $newStatFarbAry[] = $statkennz . $farbe;
        
        }
        
        
        else {
          $farbe = '#000000'; 
          $newStatFarbAry[] = $statkennz . $farbe;
          
        }
      } 

      
      
      $this->colorstat = implode(':', $newStatFarbAry);
      

      
      
      $statusFilter = $this->getFilterobject();
      $statusFilter->setAllowedInput();
      

      $this->sortobjvar = $sortvar; 
      $this->sortasc = $asc;  
      
      $this->setObjsettingVar($objsettings);
      $objsettings->id = $pTool_id;
      
      
      ToolSettingsAPI::setToolSettings($objsettings);
      
      echo $writeColumns->getLink($pTool_id, $pSight_id, PostOperations::getVarValue('gruppe'));
      
    }

    
  } 

  
  class WriteStatKt extends WriteStat
  {
    
    public $suffix = '';

    
    public function getFilterobject()
    {
      return new FilterInpKtStat();
    }

    
    public function __construct($pObjsettings, $pSuffix = '')
    {
      $this->suffix = $pSuffix;
      $this->sortvarstr = 'sortkt';
      $this->sortname = 'kt.datum';
      $this->kenn = 'kt';
      $settingAry = get_object_vars($pObjsettings);
      $this->colorstat = $settingAry['colorktstatus' . $pSuffix];
      $this->status = $settingAry['ktstatus' . $pSuffix];
      $this->sortasc = $settingAry['sortktasc' . $pSuffix];
      $this->sortobjvar = $settingAry['sortkt' . $pSuffix];
    }

    
    public function setObjsettingVar($pObjsettings)
    {
      $var = 'colorktstatus' . $this->suffix;
      $pObjsettings->{$var} = $this->colorstat;
      $var = 'sortktasc' . $this->suffix;
      $pObjsettings->{$var} = $this->sortasc;
      $var = 'ktstatus' . $this->suffix;
      $pObjsettings->{$var} = $this->status;
      $var = 'sortkt' . $this->suffix;
      $pObjsettings->{$var} = $this->sortobjvar;
    }
  }

  
  class WriteStatTn extends WriteStat
  {
    
    public function getFilterobject()
    {
      return new FilterInpTnStat();
    }

    
    public function __construct($pObjsettings)
    {
      $this->sortvarstr = 'sorttn';
      $this->sortname = 't.name';
      $this->kenn = 'tn';
      $this->colorstat = $pObjsettings->colortnstatus;
      $this->status = $pObjsettings->tnstatus;
      $this->sortasc = $pObjsettings->sorttnasc;
      $this->sortobjvar = $pObjsettings->sorttn;
    }

    
    public function setObjsettingVar($pObjsettings)
    {
      $pObjsettings->colortnstatus = $this->colorstat;
      $pObjsettings->sorttnasc = $this->sortasc;
      $pObjsettings->tnstatus = $this->status;
      $pObjsettings->sorttn = $this->sortobjvar;
    }
  }