<?php
namespace App\Entity\Abo;
class AboBestellung
{
public const string TYP_PRINT_ABO = 'PRINT_ABO';
public const string TYP_EPAPER_ABO = 'EPAPER_ABO';
public const string TYP_KOMBI_ABO = 'KOMBI_ABO';
public const string TYP_ADD_EPAPER_ABO = 'ADD_EPAPER_ABO';
public const string GLOB_ORDER_SEND_MSG = "Vielen Dank für Ihre Bestellung.\nNach der Bearbeitung erhalten Sie eine entsprechende Rückmeldung per E-Mail.";
private static array $textData = [
self::TYP_PRINT_ABO => ['titel' => 'Print-Abo bestellen', 'order_send_msg' => self::GLOB_ORDER_SEND_MSG],
self::TYP_EPAPER_ABO => ['titel' => 'E-Paper-Abo bestellen', 'order_send_msg' => self::GLOB_ORDER_SEND_MSG],
self::TYP_KOMBI_ABO => ['titel' => 'Standard-Abo bestellen', 'order_send_msg' => self::GLOB_ORDER_SEND_MSG],
self::TYP_ADD_EPAPER_ABO => ['titel' => 'E-Paper zum laufenden Abo hinzufügen', 'order_send_msg' => self::GLOB_ORDER_SEND_MSG],
];
// Formular Felder
public $aboOption;
public $vorname;
public $nachname;
public $firma;
public $strasse;
public $plz;
public $ort;
public $land = 'Schweiz';
public $telefon;
public $email;
public $mitteilung;
public $useRechnungsKontakt;
public $rechnungVorname;
public $rechnungNachname;
public $rechnungFirma;
public $rechnungStrasse;
public $rechnungPlz;
public $rechnungOrt;
public $rechnungLand = 'Schweiz';
public $emailEpaperLogin;
protected function __construct(
private $typeId,
private readonly array $aboOptions,
private $useEpaperLoginFields = false
)
{
$this->aboOption = $this->aboOptions ? $this->aboOptions[0] : []; // die options stammen vom externen content, damit diese variabel bearbeitet werden können
}
public static function newPrintAbo(array $aboOptions): AboBestellung
{
return new self(self::TYP_PRINT_ABO, $aboOptions);
}
public static function newEPaperAbo(array $aboOptions): AboBestellung
{
return new self(self::TYP_EPAPER_ABO, $aboOptions, true);
}
public static function newKombiAbo(array $aboOptions): AboBestellung
{
return new self(self::TYP_KOMBI_ABO, $aboOptions, true);
}
public static function newAddEPaperAbo(array $aboOptions): AboBestellung
{
return new self(self::TYP_ADD_EPAPER_ABO, $aboOptions, true);
}
public function formAboOptions(): array
{
return array_combine($this->aboOptions, $this->aboOptions);
}
/**
* Hilfsfunktion um die gespeicherten Options aus dem Content Textfeld in ein Array umzuwandeln.
*/
public static function transformTextconfigToOptionsArray($optionString): array
{
$options = [];
$data = explode("\n", (string) $optionString);
foreach ($data as $option) {
$option = trim($option);
if (mb_strlen($option) > 0) {
$options[] = $option;
}
}
return $options;
}
public function titel(): string
{
return self::$textData[$this->typeId]['titel'];
}
public function orderTitel(): string
{
return $this->titel();
}
public function orderSendMessage(): string
{
return self::$textData[$this->typeId]['order_send_msg'];
}
public function useEpaperLoginFields(): bool
{
return (bool) $this->useEpaperLoginFields;
}
/**
* Zusammenfassung Bestellung für Mailbody.
*/
public function mailBodyBestellung(): string
{
$body = '<br>
Neue <b>"'.$this->titel().'"</b> Bestellung vom '.date('d.m.Y H:i').' ab www.urnerwochenblatt.ch<br>'
;
if ($this->aboOption) {
$body .= '<br>
<br>
<b>Abo-Option</b><br>'
.$this->aboOption.'<br>'
;
}
$body .= '<br>
<table>
<tr>
<td colspan="2"><br><b>Ihre Angaben/Zahlungsadresse</b><br></td>
</tr>
<tr>
<td>Vor-/Nachname</td><td>'.$this->vorname.' '.$this->nachname.'</td>
</tr>
<tr>
<td>Firma</td><td>'.$this->firma.'</td>
</tr>
<tr>
<td>Strasse</td><td>'.$this->strasse.'</td>
</tr>
<tr>
<td>PLZ/Ort</td><td>'.$this->plz.' '.$this->ort.'</td>
</tr>
<tr>
<td>Land</td><td>'.$this->land.'</td>
</tr>
<tr>
<td>Telefon</td><td>'.$this->telefon.'</td>
</tr>
<tr>
<td>E-Mail</td><td>'.$this->email.'</td>
</tr>
</table>'
;
// Dies ist die Lieferadresse im Formular abweichende Lieferadresse
if ($this->useRechnungsKontakt) {
$body .= '<br>
<table>
<tr>
<td colspan="2"><br><b>Lieferadresse</b><br></td>
</tr>
<tr>
<td>Vor-/Nachname</td><td>'.$this->rechnungVorname.' '.$this->rechnungNachname.'</td>
</tr>
<tr>
<td>Firma</td><td>'.$this->rechnungFirma.'</td>
</tr>
<tr>
<td>Strasse</td><td>'.$this->rechnungStrasse.'</td>
</tr>
<tr>
<td>PLZ/Ort</td><td>'.$this->rechnungPlz.' '.$this->rechnungOrt.'</td>
</tr>
<tr>
<td>Land</td><td>'.$this->rechnungLand.'</td>
</tr>
</table>'
;
}
if ($this->emailEpaperLogin) {
$body .= '<br><br>
<b>E-Paper Login</b><br>'
.$this->emailEpaperLogin.'<br>';
}
if ($this->mitteilung) {
$body .= '<br><br>
<b>Mitteilung</b><br>'
.str_replace("\n", '<br>', strip_tags((string) $this->mitteilung)).'<br>';
}
return $body;
}
public function mailSubjectBestellung(): string
{
return 'Neue Bestellung: '.$this->titel();
}
}