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



namespace Kufer\KuferTools\Ausgaben\SammelEMail\EmailTasks;

class EmailSenderException extends \Exception {}

abstract class EmailSender {

  protected $commands = array();
  protected $emailSendObj;
  
  protected $uploadFiles;
  protected $kursId;

  abstract public function isUploadFault(); 
  
  
  abstract public function getAttachStrategy($pToolId = '', $pisTnListAnhang = '');
  
   
  abstract public function deleteAnhangFiles();
  
  
  abstract protected function getUserBez();
  
  

  public function __construct($pKursId, $pUploadFiles) {
    $this->kursId = $pKursId;
    $this->uploadFiles = $pUploadFiles;
    $this->emailSendObj = new EmailSendObj();
  }
 
  
  public function addCommands($pCommands) {
    $this->commands[] = $pCommands;
  } 
  
  public function getCommands() {
    return $this->commands;
  }
    
    
  public function sendMail2User($pKontakte, $pUser, $pToolId, $pToolSectionId, $pKurs , $pEmailContent, $pAttachStrategy = Null)
  {  
    
    $this->emailSendObj->setKontakte($pKontakte);
    $this->emailSendObj->setUser($pUser); 
    $this->emailSendObj->setToolId($pToolId);
    $this->emailSendObj->setToolSectionId($pToolSectionId);
    $this->emailSendObj->setKurs($pKurs);
    $this->emailSendObj->setEmailContent($pEmailContent);
    $this->emailSendObj->setAttachStrategy($pAttachStrategy);
    
    
    
    $absender = $this->getAbsenderDatamodul($pUser);
    $this->emailSendObj->setAbsender($absender); 
    
    
    
    $bezeichner = $this->getUserBez(); 
    $this->emailSendObj->setBezeichner($bezeichner);
    
    
    $versandStatus =  new \stdClass;
    $versandStatus->einrichtung = array();
    $versandStatus->person = array();
    $versandStatus->empfaenger = array();
    $versandStatus->mitarbeiterPlanend = array();
    $versandStatus->mitarbeiterVerwaltend = array();
    
    
    
    foreach ($this->commands as $command) {
      foreach ($command as $commandTo=>$command) {
        
        $versandAry = $command->execute($this->emailSendObj);
        $empfaenger = &$versandStatus->$commandTo;
        $empfaenger['versendet'] = $versandAry['versendet'];
        $empfaenger['nichtVersendet'] = $versandAry['nichtVersendet'];
      }
    }
    
    return $versandStatus;
  }
  
   
   
  public function getEmpfaengerIdsInvalidEmail($pKontakte) {
    $valid = \ValidierungBasis::getInstance();
    
    $userIdsInvalidEmail = array();
    foreach ($pKontakte as $kontakt) {
      $email = $kontakt->getVarValueEx('email', 'kommunikation');
      
      
      if ($valid->isValidVar('email', $email) !== true) {
        
        $userIdsInvalidEmail[] = $kontakt->id;
      } 
    }
    return $userIdsInvalidEmail;
  } 
  
  
  
  protected function getAbsenderDatamodul($pUser)
  {
    
    if ($pUser->ben >= 0) {
      $dbwBasysUser = new \DBWorkBASysUser();
      return $dbwBasysUser->getBASysUser($pUser->ben);
    }
    else if ($pUser->dnr >= 0) {
      $dbwDoz = new \DBWorkDozenten();
      return $dbwDoz->getDozent($pUser->dnr);
    }
    else if ($pUser->anr >= 0) {
      $dbwAdr = new \DBWorkAdresse();
      return $dbwAdr->getAdresse($pUser->anr);
    }
    else if ($pUser->tnr >= 0) {
      $dbwAdr = new \DBWorkTeilnehmer();
      return $dbwAdr->getTeilnehm($pUser->tnr);
    }
    return '';
  } 
  
  
  
  
} 


class EmailSendObj {
   public $kontakte;
   public $user; 
   public $toolId; 
   public $toolSectionId;
   public $kurs;
   public $emailContent;
   public $attachStrategy = Null;
   public $person;
   
  
  public function setKontakte($pKontakte) {
   $this->kontakte = $pKontakte;
  }
  public function setUser($pUser) {
   $this->user = $pUser;
  }
  public function setToolId($pToolId) {
   $this->toolId = $pToolId;
  }
  public function setToolSectionId($pToolSectionId) {
   $this->toolSectionId = $pToolSectionId;
  }
  public function setKurs($pKurs) {
    $this->kurs = $pKurs;
   }
  public function setEmailContent($pEmailContent) {
    $this->emailContent = $pEmailContent;
   }
  public function setAttachStrategy($pAttachStrategy) {
    $this->attachStrategy = $pAttachStrategy;
   }

  public function setBezeichner($pBezeichner) {
   $this->bezeichner = $pBezeichner;
  }
 
  public function setAbsender($pAbsender) {
   $this->person = $pAbsender;
  }
  
  
}