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

 if (!defined('BASEDIR')) {
   include_once_ex('classes/constants.php');
 }


class DataMenuWunschzettelUebersicht extends DataMenuMain
{
  
  public $dbworkWunschzettel;

  private $anzahlGesamt;

  private $anzahlProSeite;
  private $pageId;
  private $start_kurs;

  public function __construct($urlParam)
  {
    parent::__construct($urlParam);

    
    $this->dbworkWunschzettel = new DBWorkWunschzettel();
    $datamenu = new WlAnmeldung();

    
    $likeValue = SecurityOperations::filterSQL(GetOperations::getVarValue('like', '0'));
    $wishId = SecurityOperations::filterSQL(GetOperations::getVarValue('wish', ''));
    $paramEmail = SecurityOperations::filterSQL(PostOperations::getVarValue('wishedit_email', ''));

    $allPosts = PostOperations::getAll();
    $cleanPosts = $datamenu->getCleanPostVariables($allPosts);

    $cleanPosts['nachrichten_id'] = $cleanPosts['id'];
    unset($cleanPosts['id']);

    if ($wishId == '') {
      $wishId = $cleanPosts['nachrichten_id'];
    }
    
    $actualSessionWishes = array();
    $actualSessionWishes = $this->dbworkWunschzettel->getAllLikeSessionWishes();

    
    $alleEmailsObject = $this->dbworkWunschzettel->getAllEmails($wishId);


    
    if (count($this->dbworkWunschzettel->getAllLikesToSessionId(SessionOperations::getVarValueIntern('user_session_id'))) == 0) {
      SessionOperations::setVarValueIntern('hasAlreadyLiked', false);
    }

    
    $alleEmails = array();
    foreach ($alleEmailsObject as $eMail) {
      if (isset($eMail->email)) {
        array_push($alleEmails, $eMail->email);
      }
    }

    
    if (SessionOperations::getUserID() != '-1') {
      
      $userId = SessionOperations::getUserID();
    } elseif (SessionOperations::getSessionID() != '') {
      
      $userId = SessionOperations::getSessionID();
    } else {
      
      $userId = '0';
    }

    
    SessionOperations::setVarValueIntern('user_session_id', $userId);

    
    $userIdFromSession = SessionOperations::getVarValueIntern('user_id');

    
    $userSessionId = SessionOperations::getVarValueIntern('user_session_id');

    if (!isset($userSessionId)) {
      
      SessionOperations::setVarValueIntern('user_id', $this->dbworkWunschzettel->getUserIdFromSession($userSessionId));
    }

    
    if ($likeValue == '1') {
      if ($paramEmail == '') {
        
        SessionOperations::setVarValueIntern('like_uebersicht_warnungen', 'Fehler: Bitte tragen Sie eine E-Mail Adresse ein');
      } elseif (ValidierungBasis::getInstance()->isValidVar('email', $paramEmail) === 'email') {
        
        SessionOperations::setVarValueIntern('like_uebersicht_warnungen', 'Fehler: Keine valide E-Mail');
      } elseif (in_array($paramEmail, $alleEmails) && !SessionOperations::getVarValueIntern('likes_email_owner')) {
        
        SessionOperations::setVarValueIntern('like_uebersicht_warnungen', 'Fehler: Diese E-Mail Adresse wurde schon mal eingetragen');
      } elseif (SessionOperations::getVarValueIntern('like_abgesendet') == $wishId) {
        SessionOperations::setVarValueIntern('like_uebersicht_warnungen', '');
      } else {

            
        if (!in_array($wishId, $actualSessionWishes)) {
          $actualSessionWishes[] = $wishId;
        }

        SessionOperations::setVarValueIntern('likes_wishes', $actualSessionWishes);

        
        SessionOperations::setVarValueIntern('likes_email_owner', true);

        
        $like_user_email = $paramEmail;
        $like_user_vorname = PostOperations::getVarValue('wishedit_vorname', '');
        $like_user_nachname = PostOperations::getVarValue('wishedit_name', '');
        $like_user_tel = PostOperations::getVarValue('wishedit_tel', '');

        
        if (SessionOperations::getVarValueIntern('like_user_email') == '') {
          SessionOperations::setVarValueIntern('like_user_email', $like_user_email);
        }
        if (SessionOperations::getVarValueIntern('like_user_vorname') == '') {
          SessionOperations::setVarValueIntern('like_user_vorname', $like_user_vorname);
        }
        if (SessionOperations::getVarValueIntern('like_user_name') == '') {
          SessionOperations::setVarValueIntern('like_user_name', $like_user_nachname);
        }
        if (SessionOperations::getVarValueIntern('like_user_tel') == '') {
          SessionOperations::setVarValueIntern('like_user_tel', $like_user_tel);
        }

        $cleanPosts['session_id'] = $userSessionId;

        if (SessionOperations::getVarValueIntern('hasAlreadyLiked')) {
          $this->dbworkWunschzettel->insertLikeToWish($cleanPosts, true, $userIdFromSession);
        } else {
          
          SessionOperations::setVarValueIntern('hasAlreadyLiked', true);
          $this->dbworkWunschzettel->insertLikeToWish($cleanPosts, false);
        }

        SessionOperations::setVarValueIntern('like_uebersicht_warnungen', '');
        SessionOperations::setVarValueIntern('like_abgesendet', $wishId);
      }
    } elseif ($likeValue == '0') {
      $newSessionWishes = array();
      foreach ($actualSessionWishes as $sessionWishes) {
        if ($sessionWishes != $wishId) {
          array_push($newSessionWishes, $sessionWishes);
        }
      }

      
      $this->dbworkWunschzettel->deleteLikeToWish($wishId, $userSessionId);
      SessionOperations::setVarValueIntern('likes_wishes', $newSessionWishes);
      
      SessionOperations::setVarValueIntern('like_uebersicht_warnungen', '');

      SessionOperations::setVarValueIntern('like_abgesendet', '');
    }


    
    $this->settings = ToolSettingsAPI::getToolSettings(4);
    $this->anzahlProSeite = $this->settings->wishDefaultAnzahlProSeite;

    
    $this->anzahlGesamt = count($this->dbworkWunschzettel->getAllWishes());
    $this->pageId = GetOperations::getVarValue('pageId', 0);

    $seitenary = SessionOperations::getVarValueIntern('wb_suche_seitenindary');
    if ($seitenary == '') {
      $seitenary = array();
    }

    
    if ($this->pageId == 1 || !isset($this->pageId)) {
      $this->start_kurs = $seitenary['anf'][0]->value;
    } else {
      $this->start_kurs = $seitenary['end'][$this->pageId - 2]->value;
    }
    $this->blaetternAry = $this->getBlaettern();
    $this->exportadapter = new ExpWunschzettel();
  }

  public function _getClickPath($pName = 'Wunschzettel')
  {
    $tmp = $pName;
    
    DataMenuMain::_getClickPath();
    if ($this->toolsettings->clickWunschzettelText != '') {
      $tmp = $this->toolsettings->clickWunschzettelText;
    }

    $this->clickpfad .= "<span class='clickadrbold'> " . CleanOperations::cleanStringForHtml($tmp) . "</span>\n";
    
    return $this->clickpfad;
  }

  public function getsight($sightid = 390)
  {
    parent::getsight($sightid);
  }

  public function getdataarraybody()
  {
    $this->dataarraybody = $this->dbworkWunschzettel->getWishesByLimit($this->anzahlProSeite, true, $this->start_kurs);
  }

  public function getBlaettern()
  {
    $countBefore = max(($this->pageId - 1) * $this->anzahlProSeite, 0);
    $anzahlDatenSaetzeVorDieserSeite = min(($this->pageId + 1) * $this->anzahlProSeite, $this->anzahlGesamt);
    $countBehind = $this->anzahlGesamt - $anzahlDatenSaetzeVorDieserSeite;

    $blaetternapi = new SeitenblaetternAPI($this->anzahlProSeite, $countBefore, $countBehind, $this->anzahlGesamt);

    $retAry = $blaetternapi->getBlaetternArray();
    $retAry[4] = $this->getBackwBlaetternLink();
    $retAry[5] = $this->getForwBlaetternLink();

    return $retAry;
  }

  protected function getBackwBlaetternLink()
  {
    if ($this->pageId > 0) {
      $urlBackw = new URLParameterWunschzettel();
      $urlBackw->kathaupt = 252;
      $urlBackw->pageId = $this->pageId - 1;
      $urlBackw->implodeSettings();

      return INDEXSCRIPT . '?urlparameter=' . $urlBackw->stream;
    }
  }

  protected function getForwBlaetternLink()
  {
    $urlForw = new URLParameterWunschzettel();
    $urlForw->kathaupt = 252;
    if ($this->pageId == 1 || $this->pageId == 0) {
      $urlForw->pageId = '2';
    } else {
      if ($this->pageId < ceil($this->anzahlGesamt / $this->anzahlProSeite)) {
        $urlForw->pageId = $this->pageId + 1;
      }
    }
    $urlForw->implodeSettings();

    return INDEXSCRIPT . '?urlparameter=' . $urlForw->stream;
  }
}