diff --git a/phpstan.neon b/phpstan.neon index 942036d..e37ca81 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,3 +13,4 @@ parameters: ignoreErrors: - identifier: missingType.generics - identifier: missingType.iterableValue + reportUnmatchedIgnoredErrors: false diff --git a/src/Entity/ContactRequest.php b/src/Entity/ContactRequest.php index 01843f0..6c81069 100644 --- a/src/Entity/ContactRequest.php +++ b/src/Entity/ContactRequest.php @@ -27,6 +27,12 @@ class ContactRequest implements ContactRequestInterface private ?string $message; + private ?string $name; + + private ?string $company; + + private ?string $phoneNumber; + private ?ChannelInterface $channel; /** @@ -93,4 +99,34 @@ public function setUpdatedAt(?DateTimeInterface $updatedAt): void { $this->updatedAt = $updatedAt; } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(?string $name): void + { + $this->name = $name; + } + + public function getCompany(): ?string + { + return $this->company; + } + + public function setCompany(?string $company): void + { + $this->company = $company; + } + + public function getPhoneNumber(): ?string + { + return $this->phoneNumber; + } + + public function setPhoneNumber(?string $phoneNumber): void + { + $this->phoneNumber = $phoneNumber; + } } diff --git a/src/Entity/ContactRequestInterface.php b/src/Entity/ContactRequestInterface.php index d2881d8..095818d 100644 --- a/src/Entity/ContactRequestInterface.php +++ b/src/Entity/ContactRequestInterface.php @@ -41,4 +41,16 @@ public function setCreatedAt(?DateTimeInterface $createdAt): void; public function getUpdatedAt(): ?DateTimeInterface; public function setUpdatedAt(?DateTimeInterface $updatedAt): void; + + public function getName(): ?string; + + public function setName(?string $name): void; + + public function getCompany(): ?string; + + public function setCompany(?string $company): void; + + public function getPhoneNumber(): ?string; + + public function setPhoneNumber(?string $phoneNumber): void; } diff --git a/src/Factory/ContactRequestFactory.php b/src/Factory/ContactRequestFactory.php index e683554..b38d48f 100644 --- a/src/Factory/ContactRequestFactory.php +++ b/src/Factory/ContactRequestFactory.php @@ -34,6 +34,9 @@ public function createNewFromChannelAndData(ChannelInterface $channel, array $da $contactRequest = $this->createNew(); $contactRequest->setEmail($data['email']); $contactRequest->setMessage($data['message']); + $contactRequest->setName($data['name'] ?? null); + $contactRequest->setCompany($data['company'] ?? null); + $contactRequest->setPhoneNumber($data['phoneNumber'] ?? null); $contactRequest->setChannel($channel); return $contactRequest; diff --git a/src/Form/Extension/ContactTypeExtension.php b/src/Form/Extension/ContactTypeExtension.php new file mode 100644 index 0000000..221a3b7 --- /dev/null +++ b/src/Form/Extension/ContactTypeExtension.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusContactRequestPlugin\Form\Extension; + +use MonsieurBiz\SyliusSettingsPlugin\Provider\SettingsProviderInterface; +use Sylius\Bundle\CoreBundle\Form\Type\ContactType; +use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\Extension\Core\Type\TelType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Validator\Constraints as Assert; + +final class ContactTypeExtension extends AbstractTypeExtension +{ + public function __construct( + private readonly SettingsProviderInterface $settingProvider, + ) { + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + parent::buildForm($builder, $options); + + $defaultConstraints = [ + new Assert\Length(['max' => 255]), + ]; + + $requiredConstraints = [ + new Assert\NotBlank(), + new Assert\Length(['max' => 255]), + ]; + + $isNameRequired = $this->isFieldRequired('field_name_required'); + $isCompanyRequired = $this->isFieldRequired('field_company_required'); + $isPhoneNumberRequired = $this->isFieldRequired('field_phone_number_required'); + + $builder + ->add('name', TextType::class, [ + 'label' => 'monsieurbiz.contact_request.form.name', + 'required' => $isNameRequired, + 'constraints' => $isNameRequired ? $requiredConstraints : $defaultConstraints, + ]) + ->add('company', TextType::class, [ + 'label' => 'monsieurbiz.contact_request.form.company', + 'required' => $isCompanyRequired, + 'constraints' => $isCompanyRequired ? $requiredConstraints : $defaultConstraints, + ]) + ->add('phoneNumber', TelType::class, [ + 'label' => 'monsieurbiz.contact_request.form.phone_number', + 'required' => $isPhoneNumberRequired, + 'constraints' => $isPhoneNumberRequired ? $requiredConstraints : $defaultConstraints, + ]) + ; + } + + public static function getExtendedTypes(): iterable + { + return [ + ContactType::class, + ]; + } + + private function isFieldRequired(string $path): bool + { + return $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $path); + } +} diff --git a/src/Form/Type/ContactSettingsType.php b/src/Form/Type/ContactSettingsType.php index 2595bb9..c96ec6b 100644 --- a/src/Form/Type/ContactSettingsType.php +++ b/src/Form/Type/ContactSettingsType.php @@ -24,6 +24,7 @@ final class ContactSettingsType extends AbstractSettingsType implements Settings { /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function buildForm(FormBuilderInterface $builder, array $options): void { @@ -47,10 +48,55 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ); $this->addWithDefaultCheckbox( $builder, - 'meta_title', - TextType::class, + 'field_name_displayed', + CheckboxType::class, + [ + 'label' => 'monsieurbiz.contact_request.settings.field_name_displayed', + 'required' => false, + ] + ); + $this->addWithDefaultCheckbox( + $builder, + 'field_name_required', + CheckboxType::class, + [ + 'label' => 'monsieurbiz.contact_request.settings.field_name_required', + 'required' => false, + ] + ); + $this->addWithDefaultCheckbox( + $builder, + 'field_company_displayed', + CheckboxType::class, + [ + 'label' => 'monsieurbiz.contact_request.settings.field_company_displayed', + 'required' => false, + ] + ); + $this->addWithDefaultCheckbox( + $builder, + 'field_company_required', + CheckboxType::class, + [ + 'label' => 'monsieurbiz.contact_request.settings.field_company_required', + 'required' => false, + ] + ); + $this->addWithDefaultCheckbox( + $builder, + 'field_phone_number_displayed', + CheckboxType::class, + [ + 'label' => 'monsieurbiz.contact_request.settings.field_phone_number_displayed', + 'required' => false, + ] + ); + $this->addWithDefaultCheckbox( + $builder, + 'field_phone_number_required', + CheckboxType::class, [ - 'label' => 'monsieurbiz.contact_request.ui.meta_title', + 'label' => 'monsieurbiz.contact_request.settings.field_phone_number_required', 'required' => false, ] ); diff --git a/src/Migrations/Version20241003154058.php b/src/Migrations/Version20241003154058.php new file mode 100644 index 0000000..b0cb803 --- /dev/null +++ b/src/Migrations/Version20241003154058.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusContactRequestPlugin\Migrations; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +/** + * Auto-generated Migration: Please modify to your needs! + */ +final class Version20241003154058 extends AbstractMigration +{ + public function getDescription(): string + { + return ''; + } + + public function up(Schema $schema): void + { + // this up() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE monsieurbiz_contact_request ADD name VARCHAR(255) DEFAULT NULL, ADD company VARCHAR(255) DEFAULT NULL, ADD phone_number VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE monsieurbiz_contact_request DROP name, DROP company, DROP phone_number'); + } +} diff --git a/src/Resources/config/doctrine/ContactRequest.orm.xml b/src/Resources/config/doctrine/ContactRequest.orm.xml index 6600006..2f9dfe3 100644 --- a/src/Resources/config/doctrine/ContactRequest.orm.xml +++ b/src/Resources/config/doctrine/ContactRequest.orm.xml @@ -11,6 +11,9 @@ + + + diff --git a/src/Resources/config/monsieurbiz/settings.yaml b/src/Resources/config/monsieurbiz/settings.yaml index 84a5197..d1f981e 100644 --- a/src/Resources/config/monsieurbiz/settings.yaml +++ b/src/Resources/config/monsieurbiz/settings.yaml @@ -8,3 +8,10 @@ monsieurbiz_sylius_settings: use_locales: true classes: form: MonsieurBiz\SyliusContactRequestPlugin\Form\Type\ContactSettingsType + default_values: + field_name_displayed: true + field_name_required: true + field_company_displayed: true + field_company_required: false + field_phone_number_displayed: true + field_phone_number_required: false diff --git a/src/Resources/templates/bundles/SyliusCoreBundle/Email/Blocks/ContactRequest/_content.html.twig b/src/Resources/templates/bundles/SyliusCoreBundle/Email/Blocks/ContactRequest/_content.html.twig new file mode 100644 index 0000000..9294dc2 --- /dev/null +++ b/src/Resources/templates/bundles/SyliusCoreBundle/Email/Blocks/ContactRequest/_content.html.twig @@ -0,0 +1,25 @@ +
+
+ {{ 'sylius.email.contact_request.message_from'|trans({}, null, localeCode) }}: +
+
{{ data.email }}
+ +
+ {{ 'monsieurbiz.contact_request.form.name'|trans({}, null, localeCode) }}: +
+
{{ data.name|default('') }}
+
+ {{ 'monsieurbiz.contact_request.form.company'|trans({}, null, localeCode) }}: +
+
{{ data.company|default('') }}
+ +
+ {{ 'monsieurbiz.contact_request.form.phone_number'|trans({}, null, localeCode) }}: +
+
{{ data.phoneNumber|default('') }}
+ +
+ {{ 'sylius.email.contact_request.content'|trans({}, null, localeCode) }}: +
+
{{ data.message|nl2br }}
+
diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 02f0956..d7d3a67 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -11,6 +11,16 @@ monsieurbiz: meta_description: "Meta Description" meta_keywords: "Meta Keyword" hide_sylius_default_content: "Hide Sylius default content" + form: + name: 'Name' + company: 'Company' + phone_number: 'Phone number' settings: plugin_name: 'Contact page' description: 'Configure the contact page' + field_name_displayed: 'Display the name field' + field_name_required: 'The name field is required' + field_company_displayed: 'Display the company field' + field_company_required: 'The company field is required' + field_phone_number_displayed: 'Display the phone field' + field_phone_number_required: 'The phone field is required' diff --git a/src/Resources/translations/messages.fr.yaml b/src/Resources/translations/messages.fr.yaml index 30235b8..2ef367f 100644 --- a/src/Resources/translations/messages.fr.yaml +++ b/src/Resources/translations/messages.fr.yaml @@ -11,6 +11,17 @@ monsieurbiz: meta_description: "Meta Description" meta_keywords: "Meta Keyword" hide_sylius_default_content: "Masquer le contenu par défaut de Sylius" + form: + name: 'Nom' + company: 'Entreprise' + phone_number: 'Numéro de téléphone' settings: plugin_name: 'Page de contact' description: 'Configurer la page de contact' + field_name_displayed: 'Afficher le champ nom' + field_name_required: 'Le champ nom est requis' + field_company_displayed: 'Afficher le champ entreprise' + field_company_required: 'Le champ entreprise est requis' + field_phone_number_displayed: 'Afficher le champ téléphone' + field_phone_number_required: 'Le champ téléphone est requis' + diff --git a/src/Resources/views/Admin/ContactRequest/Crud/show.html.twig b/src/Resources/views/Admin/ContactRequest/Crud/show.html.twig index bc8930c..9b9ad49 100644 --- a/src/Resources/views/Admin/ContactRequest/Crud/show.html.twig +++ b/src/Resources/views/Admin/ContactRequest/Crud/show.html.twig @@ -33,6 +33,22 @@

{{ resource.email|default('') }}

+

{{ 'monsieurbiz.contact_request.form.name'|trans }}

+

+ {{ resource.name|default('') }} +

+

{{ 'monsieurbiz.contact_request.form.company'|trans }}

+

+ {{ resource.company|default('') }} +

+

{{ 'monsieurbiz.contact_request.form.phone_number'|trans }}

+

+ {{ resource.phone_number|default('') }} +

+

{{ 'monsieurbiz.contact_request.ui.email'|trans }}

+

+ {{ resource.email|default('') }} +

{{ 'monsieurbiz.contact_request.ui.message'|trans }}

{{ resource.message|nl2br|default('')|raw }} diff --git a/src/Resources/views/Shop/ContactRequest/request.html.twig b/src/Resources/views/Shop/ContactRequest/request.html.twig index 8a4d96e..5414a96 100644 --- a/src/Resources/views/Shop/ContactRequest/request.html.twig +++ b/src/Resources/views/Shop/ContactRequest/request.html.twig @@ -47,6 +47,17 @@
{{ form_start(form, {'action': path('sylius_shop_contact_request'), 'attr': {'class': 'ui large loadable form', 'novalidate': 'novalidate'}}) }} {{ form_row(form.email, sylius_test_form_attribute('contact-email')) }} + + {% if setting('monsieurbiz_contact_request.contact', 'field_name_displayed')|default(false) %} + {{ form_row(form.name) }} + {% endif %} + {% if setting('monsieurbiz_contact_request.contact', 'field_company_displayed')|default(false) %} + {{ form_row(form.company) }} + {% endif %} + {% if setting('monsieurbiz_contact_request.contact', 'field_phone_number_displayed')|default(false) %} + {{ form_row(form.phoneNumber) }} + {% endif %} + {{ form_row(form.message, sylius_test_form_attribute('contact-message')) }} {{ sylius_template_event('sylius.shop.contact.request.form', {'form': form}) }}