Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContactRequest: Add fields in contact form + settings to display them or no, make them required or no #6

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ parameters:
ignoreErrors:
- identifier: missingType.generics
- identifier: missingType.iterableValue
reportUnmatchedIgnoredErrors: false
36 changes: 36 additions & 0 deletions src/Entity/ContactRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class ContactRequest implements ContactRequestInterface

private ?string $message;

private ?string $name;

private ?string $company;

private ?string $phoneNumber;

private ?ChannelInterface $channel;

/**
Expand Down Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/Entity/ContactRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/Factory/ContactRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
79 changes: 79 additions & 0 deletions src/Form/Extension/ContactTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of Monsieur Biz' Contact Request plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* 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 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', 'field_name_displayed');
$isCompanyRequired = $this->isFieldRequired('field_company_required', 'field_company_displayed');
$isPhoneNumberRequired = $this->isFieldRequired('field_phone_number_required', 'field_phone_number_displayed');

$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, string $pathDisplayed): bool
{
return $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $path)
&& $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $pathDisplayed);
}
}
52 changes: 49 additions & 3 deletions src/Form/Type/ContactSettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
]
);
Expand Down
40 changes: 40 additions & 0 deletions src/Migrations/Version20241003154058.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Monsieur Biz' Contact Request plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* 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');
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/doctrine/ContactRequest.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
</id>
<field name="email" column="email"/>
<field name="message" type="text" column="message"/>
<field name="name" type="string" column="name" nullable="true"/>
<field name="company" type="string" column="company" nullable="true"/>
<field name="phoneNumber" type="string" column="phone_number" nullable="true"/>
<field name="createdAt" column="created_at" type="datetime_immutable">
<gedmo:timestampable on="create"/>
</field>
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/monsieurbiz/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div style="text-align: left;">
<div style="color: #1abb9c;">
<strong>{{ 'sylius.email.contact_request.message_from'|trans({}, null, localeCode) }}:</strong>
</div>
<div style="margin-bottom: 20px;">{{ data.email }}</div>

{% if data.name|default('') is not empty %}
<div style="color: #1abb9c;">
<strong>{{ 'monsieurbiz.contact_request.form.name'|trans({}, null, localeCode) }}:</strong>
</div>
<div style="margin-bottom: 20px;">{{ data.name|default('') }}</div>

{% endif %}

{% if data.company|default('') is not empty %}
<div style="color: #1abb9c;">
<strong>{{ 'monsieurbiz.contact_request.form.company'|trans({}, null, localeCode) }}:</strong>
</div>
<div style="margin-bottom: 20px;">{{ data.company|default('') }}</div>
{% endif %}

{% if data.phoneNumber|default('') is not empty %}
<div style="color: #1abb9c;">
<strong>{{ 'monsieurbiz.contact_request.form.phone_number'|trans({}, null, localeCode) }}:</strong>
</div>
<div style="margin-bottom: 20px;">{{ data.phoneNumber|default('') }}</div>
{% endif %}

<div style="color: #1abb9c;">
<strong>{{ 'sylius.email.contact_request.content'|trans({}, null, localeCode) }}:</strong>
</div>
<div>{{ data.message|nl2br }}</div>
</div>
10 changes: 10 additions & 0 deletions src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
11 changes: 11 additions & 0 deletions src/Resources/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

22 changes: 22 additions & 0 deletions src/Resources/views/Admin/ContactRequest/Crud/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@
<p>
{{ resource.email|default('') }}
</p>

{% if resource.name|default('') is not empty %}
<h2>{{ 'monsieurbiz.contact_request.form.name'|trans }}</h2>
<p>
{{ resource.name|default('') }}
</p>
{% endif %}

{% if resource.company|default('') is not empty %}
<h2>{{ 'monsieurbiz.contact_request.form.company'|trans }}</h2>
<p>
{{ resource.company|default('') }}
</p>
{% endif %}

{% if resource.phoneNumber|default('') is not empty %}
<h2>{{ 'monsieurbiz.contact_request.form.phone_number'|trans }}</h2>
<p>
{{ resource.phoneNumber|default('') }}
</p>
{% endif %}

<h2>{{ 'monsieurbiz.contact_request.ui.message'|trans }}</h2>
<div>
{{ resource.message|nl2br|default('')|raw }}
Expand Down
Loading
Loading