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

  session_start();
  
  define('ISKUFERTOOLS', true);
  

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



  

  $tool_id = PostOperations::getVarValue('tool');
  $sight_id = PostOperations::getVarValue('sightid');
  $gruppe = PostOperations::getVarValue('gruppe');
  $showbengrp = PostOperations::getVarValue('showbengrp');

  $user_id = SessionOperations::getUserID();
  $dbsight = new DBWorkSightsSettings();
  $writecolumns = new writeColumns();
  if ($user_id != -1) {
    switch (PostOperations::getVarValue('art')) {
      case 'Show_Sort_Sem':

        $newsproseite = PostOperations::getVarValue('newsproseite'); 
        if (PostOperations::isDefined('sortnews')) {
          switch (PostOperations::getVarValue('sortnews')) {
            default:
            case 'erstellauf':  $sortnews = 'erstell_datetime'; $asc = true; break;  
            case 'erstellab':  $sortnews = 'erstell_datetime'; $asc = false; break;  
            case 'bearbeitauf':  $sortnews = 'letzte_bearbeitung_datetime'; $asc = true; break;  
            case 'bearbeitab':  $sortnews = 'letzte_bearbeitung_datetime'; $asc = false;  break;  
          }
        } else {
          $sortnews = 'erstell_datetime';
          $asc = false;
        }

        $objsettings = ToolSettingsAPI::getToolSettings($tool_id);
        $objsettings->newsproseite = ($newsproseite > 0 ? $newsproseite : 5);
        $objsettings->sortnews = $sortnews;
        $objsettings->sortnewsasc = $asc;
        $objsettings->showbengrp = $showbengrp;
        $objsettings->id = $tool_id;

        ToolSettingsAPI::setToolSettings($objsettings);

        $datePart = '';
        $delPart = '';
        
        if (PostOperations::getVarValue('delOldNews', -1) === '1') {
          $date = PostOperations::getVarValue('delOldNewsDays');
          $date = DateTimeOperations::getValidDateString($date);

          if ($date != '') {
            $datePart = '&delOldNewsDays=' . $date;

            $dbw = new DBWorkAktuellesWT();
            $dbw->newSearch();
            $dbw->toobject->erstell_datetime = $date;
            $newsToDel = $dbw->getAktuellesBySearch();
            if (count($newsToDel) > 0) {
              $ids = array();
              foreach ($newsToDel as $news) {
                $ids[] = $news->id;
              }

              $dbw->deleteAktuellesBulk($ids);
              if (count($dbw->getExistingNewsIds($ids)) === 0) {
                $delPart = '&delok=1&delcount=' . count($newsToDel);
              }
            } else {
              $delPart = '&delok=2';
            }
          }
        }

        echo $writecolumns->getLink($tool_id, $sight_id . $datePart . $delPart, $gruppe);
        break;

    }
  }