<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Utils\PersonenErweiternStrategies;


abstract class ErweiterePersonStrategy
{
  private $dataAry;
  private $toolId;
  private $knr;
  private $referer;
  private $refererLabel;

  
  abstract protected function getPersonObjFromData($pDataObj);

  abstract protected function getPersonUrlPart($pDataObj);

  abstract protected function setPersonObj($pPersonObj, $pDataObj);

  abstract protected function getActionPart();

  
  public function fillAll($pDataAry, $pToolId, $pKnr, $pReferer = '', $pRefererLabel = '')
  {
    
    $this->dataAry = $pDataAry;
    $this->toolId = $pToolId;
    $this->knr = $pKnr;
    $this->referer = $pReferer;
    $this->refererLabel = $pRefererLabel;

    return $this->fillEmailFormularData4AllPersons();
    
  }

  
  protected function fillEmailFormularData4AllPersons()
  {
    return array_map(function($data) {
      return $this->fillEmailFormularData($data);
    }, $this->dataAry);
  }

  
  protected function fillEmailFormularData($pDataObj)
  {
    
    $person = $this->getPersonObjFromData($pDataObj);
    
    $person->email_formular = '';
    $person->emailgrafik_formular = '';


    
    
    if (!empty($person->email) && !empty($person->email->wert)) {
      if (\EmailOperations::isValidEmail($person->email)) {
        $mailadress = $person->email->wert;
        $link = 'index.php?tool_id=' . $this->toolId . '&knr=' . $this->knr . $this->getPersonUrlPart($pDataObj) . $this->getActionPart() . $this->getRefererUrlPart();
        $person->email_formular = '<a class="emailform" href="' . $link . '">' . $mailadress . '</a>' . "\n";
        $person->emailgrafik_formular = '<a class="emailform" href="' . $link . '">'
          . '<i class="fa fa-envelope-open-o"></i></a>' . "\n";
      }
      
      else { 
        
        $mailadress = $person->email->wert;
        $person->email_formular = '<a class="invalidMailLink" href ="#/" title="Keine gültige E-Mail Adresse">'
          . $mailadress . '</a>' . "\n";
        $person->emailgrafik_formular = '<a class="invalidMailLink" href ="#/" title="Keine gültige E-Mail Adresse">'
          . '<i class="fa fa-envelope-o"></i></a>' . "\n";
      }
    }

    
    $this->setPersonObj($person, $pDataObj);

    return $pDataObj;
  }

  

  protected function getRefererUrlPart()
  {
    if (trim($this->referer) != '') {
      return '&referer=' . urlencode($this->referer) . '&refererLabel=' . urlencode(\StringOperations::htmlentities($this->refererLabel));
    }

    return '';
  }
}