From 63eb2604b1b0075ed861fdfba820f37e352f6660 Mon Sep 17 00:00:00 2001 From: TonnyJe Date: Fri, 3 Apr 2020 23:45:46 +0200 Subject: [PATCH] Issue 26 solved --- app/libs/Model/Registration/Registration.php | 33 +++++----- .../Model/Registration/RegistrationMapper.php | 25 +++++++ .../RegistrationForm/RegistrationForm.php | 28 +++----- .../RegistrationForm/registrationForm.latte | 6 +- .../mails/RegistrationAdminMail.php | 3 +- .../Registration/mails/RegistrationData.php | 66 ------------------- .../Registration/mails/RegistrationMail.php | 3 +- 7 files changed, 56 insertions(+), 108 deletions(-) delete mode 100644 app/modules/FrontModule/Registration/mails/RegistrationData.php diff --git a/app/libs/Model/Registration/Registration.php b/app/libs/Model/Registration/Registration.php index e726b14..4304513 100644 --- a/app/libs/Model/Registration/Registration.php +++ b/app/libs/Model/Registration/Registration.php @@ -4,6 +4,7 @@ use DateTimeImmutable; use Nextras\Orm\Entity\Entity; +use Ublaboo\Mailing\IMessageData; /** * @property int $id {primary} @@ -14,16 +15,18 @@ * @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'; @@ -31,25 +34,21 @@ class Registration extends Entity 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 { diff --git a/app/libs/Model/Registration/RegistrationMapper.php b/app/libs/Model/Registration/RegistrationMapper.php index 4e21972..290dced 100644 --- a/app/libs/Model/Registration/RegistrationMapper.php +++ b/app/libs/Model/Registration/RegistrationMapper.php @@ -3,8 +3,33 @@ namespace App\Model; use Nextras\Orm\Mapper\Mapper; +use Nextras\Orm\Mapper\Dbal\StorageReflection\UnderscoredStorageReflection; class RegistrationMapper extends Mapper { + /** @var string[] */ + private $yesNoValues = [ + true => 'yes', + false => 'no', + ]; + + protected function createStorageReflection(): 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]; + } + } diff --git a/app/modules/FrontModule/Registration/components/RegistrationForm/RegistrationForm.php b/app/modules/FrontModule/Registration/components/RegistrationForm/RegistrationForm.php index adf601d..04d16d0 100644 --- a/app/modules/FrontModule/Registration/components/RegistrationForm/RegistrationForm.php +++ b/app/modules/FrontModule/Registration/components/RegistrationForm/RegistrationForm.php @@ -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; @@ -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'); @@ -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); }; @@ -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(); } diff --git a/app/modules/FrontModule/Registration/components/RegistrationForm/registrationForm.latte b/app/modules/FrontModule/Registration/components/RegistrationForm/registrationForm.latte index 6626203..0e091a8 100644 --- a/app/modules/FrontModule/Registration/components/RegistrationForm/registrationForm.latte +++ b/app/modules/FrontModule/Registration/components/RegistrationForm/registrationForm.latte @@ -37,9 +37,9 @@ {input invoice} {errors invoice}
- {label companyid} - {input companyid style=>"width:200px"} - {errors companyid} + {label companyId} + {input companyId style=>"width:200px"} + {errors companyId}
diff --git a/app/modules/FrontModule/Registration/mails/RegistrationAdminMail.php b/app/modules/FrontModule/Registration/mails/RegistrationAdminMail.php index 69d1b00..a842d51 100644 --- a/app/modules/FrontModule/Registration/mails/RegistrationAdminMail.php +++ b/app/modules/FrontModule/Registration/mails/RegistrationAdminMail.php @@ -2,6 +2,7 @@ namespace App\FrontModule\Mails; +use App\Model\Registration; use Nette; use Ublaboo\Mailing\AbstractMail; use Ublaboo\Mailing\IComposableMail; @@ -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); diff --git a/app/modules/FrontModule/Registration/mails/RegistrationData.php b/app/modules/FrontModule/Registration/mails/RegistrationData.php deleted file mode 100644 index 0ff431c..0000000 --- a/app/modules/FrontModule/Registration/mails/RegistrationData.php +++ /dev/null @@ -1,66 +0,0 @@ -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; - } - -} diff --git a/app/modules/FrontModule/Registration/mails/RegistrationMail.php b/app/modules/FrontModule/Registration/mails/RegistrationMail.php index df5b629..5a02ebb 100644 --- a/app/modules/FrontModule/Registration/mails/RegistrationMail.php +++ b/app/modules/FrontModule/Registration/mails/RegistrationMail.php @@ -2,6 +2,7 @@ namespace App\FrontModule\Mails; +use App\Model\Registration; use Nette; use Ublaboo\Mailing\AbstractMail; use Ublaboo\Mailing\IComposableMail; @@ -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']);