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

include_once '..' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'constants.php';










$in = new AktuellesByEmail();
echo $in->handleAnfrage();


class AktuellesByEmail
{
  
  public $kennwort;
  
  public $passwort;

  public function __construct()
  {
    $this->kennwort = (isset($_REQUEST['Kennwort'])) ? urldecode($_REQUEST['Kennwort']) : '';
    $this->passwort = (isset($_REQUEST['Passwort'])) ? urldecode($_REQUEST['Passwort']) : '';
  }

  public function handleAnfrage()
  {
    
    $errorCode = ErrorMessageAPI::ERROR_NO_ERROR;
    $errorMsg = '';
    $webid = '';
    $fields = array();
    $undeletedNewsIds = '';

    if ($this->kennwort != '' && $this->passwort != '') {

      
      $filter = FilterFactory::getFilterClass();

      $kennwort = $filter->filterLogin($this->kennwort);
      $passwort = $filter->filterPasswort($this->passwort);

      $dblogin = new DBWorkLogin();

      
      $dbuser_id = $dblogin->LoginOK($kennwort, $passwort);
      if ($dbuser_id instanceof \Kufer\Web\Classes\Identifier\Identifier) {
        $dbnews = new DBWorkAktuellesWT();
        $idsToDelete = PostOperations::getVarValue('del_ids', '');
        if ($idsToDelete != '') {
          $idsToDelete = explode(',', $idsToDelete);
          $dbnews->deleteAktuellesBulk($idsToDelete);
          $nichtGeloeschteIds = $dbnews->getExistingNewsIds($idsToDelete);
          if (count($nichtGeloeschteIds) == 0) {
            $errorCode = ErrorMessageAPI::ERROR_NO_ERROR;
          } else {
            $errorCode = ErrorMessageAPI::ERROR_DEL_NOT_SUCCESSFUL;
            $errorMsg = 'Nicht alle Nachrichten konnten gelöscht werden';
            $undeletedNewsIds = $nichtGeloeschteIds;
          }
        } else {
          $addf = new DateTimeOperations();

          $dbwuser = new DBWorkUser();
          $user = $dbwuser->getUser($dbuser_id);

          $news = $dbnews->getNewObject();

          if ($user->isAdmin()) {
            $username = 'Administrator';
          } else {
            $username = $user->name . ((!empty($user->vorname)) ? ', ' . $user->vorname : '');
          }

          
          $encode = new EncodeCharsetFromTo(CP1252, CodingOperations::getInternalEncoding());
          $titel = $encode->encode(urldecode(PostOperations::getVarValue('Col_titel')));
          $inhalt = nl2br($encode->encode(urldecode(PostOperations::getVarValue('Col_inhalt'))));

          
          if (!($titel == '')) {

            
            
            $news->setValue2Var('titel', $titel);
            $news->setValue2Var('inhalt', $inhalt);

            
            $news->setValue2Var('autor_id', $user->id);

            
            $news->setValue2Var('erstell_datetime', $addf->getDateTimeNow());

            
            $news->setValue2Var('letzte_bearbeitung_datetime', $addf->getDateTimeNow());

            
            $news->setValue2Var('letzter_bearbeiter', $username);

            $bezirk = urldecode(PostOperations::getVarValue('bezirk'));
            $news->setValue2Var('bezirk', $bezirk);

            
            $usergroup = PostOperations::getVarValue(urldecode('benutzergruppe'));
            $news->setValue2Var('benutzergruppe', empty($usergroup) ? 0 : $usergroup);

            $gueltigBis = PostOperations::getVarValue(urldecode('gueltigbis'));
            if ($gueltigBis == '') {
              $news->setValue2Var('gueltigBis', DEFAULTDATE);
            } else {
              $news->setValue2Var('gueltigBis', $gueltigBis);
            }

            
            
            
            $fachbereiche = array(0);

            
            $dnr_privat = PostOperations::getVarValue(urldecode('Col_dnr_privatenachricht'));
            $news->setValue2Var('dnr_privatnachricht', (!empty($dnr_privat) ? $dnr_privat : '0'));

            $id = $dbnews->insertAktuelles($news, $fachbereiche);
            if (!is_numeric($id) || $id < 1) { 
              $errorCode = ErrorMessageAPI::ERROR_GENERIC_ERROR; 
            } else {
              $news = $dbnews->getAktuelles($id);
              
              if ($titel !== $news->titel || $inhalt !== $news->inhalt) {
                $errorCode = ErrorMessageAPI::ERROR_FILTER_PROBLEM; 
                $errorMsg = 'Die News enthält ungültige Zeichen und wurde gefiltert';
                $errorMsg .= ' - die News wurde trotzdem in die Web-Datenbank gespeichert';
                if ($titel !== $news->titel) {
                  $fields['titel'] = array('before' => $titel, 'after' => $news->titel);
                }
                if ($inhalt !== $news->inhalt) {
                  $fields['inhalt'] = array('before' => $inhalt, 'after' => $news->inhalt);
                }
              } else {
                
                $webid = $id;
              }
            }
          } else { 
            $errorCode = ErrorMessageAPI::ERROR_NO_INPUT; 
            $errorMsg = 'Das Feld Titel wurden leer gelassen, ist aber ein Pflichtfeld';
            $fields['titel'] = array('missing' => 'true');
          }
        }
      } else { 
        $errorCode = ErrorMessageAPI::ERROR_INVALID_LOGIN; 
        $errorMsg = 'Falscher Benutzername oder falsches Passwort angegeben';
      }
    } else { 
      $errorCode = ErrorMessageAPI::ERROR_INVALID_LOGIN; 
      $errorMsg = 'Kein Benutzername und/oder Passwort angegeben';
    }

    
    $xml = new ErrorMessageAPI($errorCode, $errorMsg, $webid);
    $xml->setFields($fields);
    $xml->setUndeletedNewsIds($undeletedNewsIds);

    
    return $xml->getXML();
  }
}