Skip to content

Commit

Permalink
Issue 26 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
TonnyJe committed Apr 3, 2020
1 parent 8a5c409 commit 74bfe70
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 108 deletions.
33 changes: 16 additions & 17 deletions app/libs/Model/Registration/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeImmutable;
use Nextras\Orm\Entity\Entity;
use Ublaboo\Mailing\IMessageData;

/**
* @property int $id {primary}
Expand All @@ -14,42 +15,40 @@
* @property string $email
* @property string $phone
* @property string $arrival
* @property string $invoice
* @property mixed $invoice
* @property string $companyId
* @property string $vegetarian
* @property mixed $vegetarian
* @property string $skills
* @property string $tshirt
* @property string $presentation
* @property string $note
* @property DateTimeImmutable|null $deletedAt {default null}
*
* @property string $liame {virtual}
*/
class Registration extends Entity
class Registration extends Entity implements IMessageData
{

public const STATUS_REGISTRATION = 'registration';
public const STATUS_WAITINGLIST = 'waitinglist';
public const STATUS_STORNO = 'storno';
public const STATUS_PAYED = 'payed';

public function __construct(int $year, string $name, string $nickname, string $email, string $phone, string $arrival, string $invoice, string $companyId, string $vegetarian, string $skills, string $tshirt, string $presentation, string $note)
public function __construct()
{
parent::__construct();
$this->year = (int) date('Y');
}

$this->year = $year;
$this->name = $name;
$this->nickname = $nickname;
$this->email = $email;
$this->phone = $phone;
$this->arrival = $arrival;
$this->invoice = $invoice;
$this->vegetarian = $vegetarian;
$this->skills = $skills;
$this->tshirt = $tshirt;
$this->presentation = $presentation;
$this->note = $note;
$this->companyId = $companyId;
public function setterLiame(string $value): void
{
$this->email = $value;
}

public function getterLiame(): string
{
return $this->email;
}

public function setInWaitinglist(): void
{
Expand Down
22 changes: 22 additions & 0 deletions app/libs/Model/Registration/RegistrationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,27 @@

class RegistrationMapper extends Mapper
{
/** @var string[] */
private $yesNoValues = [
true => 'yes',
false => 'no',
];

protected function createStorageReflection(): \Nextras\Orm\Mapper\Dbal\StorageReflection\UnderscoredStorageReflection
{
$reflection = parent::createStorageReflection();
$reflection->addMapping('invoice', 'invoice', [$this, 'yesNoEntityMapper'], [$this, 'yesNoStorageMapper']);
$reflection->addMapping('vegetarian', 'vegetarian', [$this, 'yesNoEntityMapper'], [$this, 'yesNoStorageMapper']);
return $reflection;
}

public function yesNoEntityMapper(string $value): bool
{
return (bool) array_search($value, $this->yesNoValues);
}

public function yesNoStorageMapper(bool $value): string
{
return $this->yesNoValues[$value];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\FrontModule\Components;

use App\FrontModule\Mails\RegistrationAdminMail;
use App\FrontModule\Mails\RegistrationData;
use App\FrontModule\Mails\RegistrationMail;
use App\Model\Model;
use App\Model\Registration;
Expand Down Expand Up @@ -64,7 +63,7 @@ public function createComponentRegistrationForm(): Form
->addCondition($form::EQUAL, true)
->toggle('companyid-container');

$company = $form->addText('companyid', 'IČO');
$company = $form->addText('companyId', 'IČO');
$company->addConditionOn($invoice, Form::EQUAL, true)
->setRequired('Vyplňte IČO firmy');

Expand Down Expand Up @@ -104,16 +103,15 @@ public function createComponentRegistrationForm(): Form

$form->addSubmit('actionSend', 'Save');

$form->onValidate[] = function () use ($form): void {
$values = $form->getValues();
$form->setMappedType(Registration::class);

if ($values->email !== '') {
$form->onValidate[] = function (Form $form): void {
if ($form->getComponent('email')->getValue() !== '') {
$form->addError('spam protection activated');
}
};

$form->onSuccess[] = function () use ($form): void {
$values = $form->getValues();
$form->onSuccess[] = function (Form $form, Registration $values): void {
$this->processForm($values);
$this->onSave($this, $this->registration);
};
Expand All @@ -122,28 +120,18 @@ public function createComponentRegistrationForm(): Form
}


private function processForm(ArrayHash $values): void
private function processForm(Registration $participant): void
{
$values->email = $values->liame;

$values['vegetarian'] = $values['vegetarian'] ? 'yes' : 'no';

$values['invoice'] = $values['invoice'] ? 'yes' : 'no';

$participant = new Registration(2020, $values->name, $values->nickname, $values->email, $values->phone, $values->arrival, $values->invoice, $values->companyid, $values->vegetarian, $values->skills, $values->tshirt, $values->presentation, $values->note);

if ($this->fullCamp) {
$participant->setInWaitinglist();
}

$this->model->persistAndFlush($participant);

$registrationData = new RegistrationData(2020, $values['name'], $values['nickname'], $values['email'], $values['phone'], $values['arrival'], $values['invoice'], $values['companyid'], $values['vegetarian'], $values['skills'], $values['tshirt'], $values['presentation'], $values['note']);

$mail = $this->mailFactory->createByType(RegistrationMail::class, $registrationData);
$mail = $this->mailFactory->createByType(RegistrationMail::class, $participant);
$mail->send();

$mailAdmin = $this->mailFactory->createByType(RegistrationAdminMail::class, $registrationData);
$mailAdmin = $this->mailFactory->createByType(RegistrationAdminMail::class, $participant);
$mailAdmin->send();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
{input invoice}
{errors invoice}
<div id="companyid-container">
{label companyid}
{input companyid style=>"width:200px"}
{errors companyid}
{label companyId}
{input companyId style=>"width:200px"}
{errors companyId}
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\FrontModule\Mails;

use App\Model\Registration;
use Nette;
use Ublaboo\Mailing\AbstractMail;
use Ublaboo\Mailing\IComposableMail;
Expand All @@ -13,7 +14,7 @@ final class RegistrationAdminMail extends AbstractMail implements IComposableMai
public function compose(Nette\Mail\Message $message, ?IMessageData $mailData): void
{
// make IComposableMail generic class
assert($mailData instanceof RegistrationData);
assert($mailData instanceof Registration); // Maybe interface???
$message->setFrom($this->mailAddresses['default_sender']);
$message->addReplyTo($mailData->email, $mailData->name);
Expand Down
66 changes: 0 additions & 66 deletions app/modules/FrontModule/Registration/mails/RegistrationData.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\FrontModule\Mails;

use App\Model\Registration;
use Nette;
use Ublaboo\Mailing\AbstractMail;
use Ublaboo\Mailing\IComposableMail;
Expand All @@ -13,7 +14,7 @@ class RegistrationMail extends AbstractMail implements IComposableMail
public function compose(Nette\Mail\Message $message, ?IMessageData $mailData): void
{
// make IComposableMail generic class
assert($mailData instanceof RegistrationData);
assert($mailData instanceof Registration); // Maybe interface???
$message->setFrom($this->mailAddresses['default_sender']);
$message->addReplyTo($this->mailAddresses['default_recipient']);
Expand Down

0 comments on commit 74bfe70

Please sign in to comment.