Skip to content

Commit

Permalink
Merge pull request #3 from anonymze/feature/more-fields
Browse files Browse the repository at this point in the history
add: fields select, radio, checkboxes
  • Loading branch information
roadster31 authored Mar 29, 2024
2 parents 4ca48f0 + 3f800e5 commit f885a05
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.11</version>
<version>1.0.12</version>
<authors>
<author>
<name>Thomas Da Silva Mendonca</name>
Expand Down
29 changes: 22 additions & 7 deletions Controller/Front/OpenApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OpenApi\Controller\Front\BaseFrontOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use OpenApi\Annotations as OA;
Expand Down Expand Up @@ -68,7 +69,7 @@ class OpenApiController extends BaseFrontOpenApiController
*/
public function getForms(
Request $request,
ModelFactory $modelFactory
ModelFactory $modelFactory,
) {
$locale = $this->findLocale($request);

Expand All @@ -82,12 +83,26 @@ public function getForms(
$formQuery->filterByCode($code);
}

return OpenApiService::jsonResponse(array_map(
function (CustomContact $form) use ($modelFactory, $locale) {
return $modelFactory->buildModel('CustomContact', $form, $locale);
},
iterator_to_array($formQuery->find())
));
$arr = [];

/**
* @var CustomContact $form
*/
foreach ($formQuery->find() as $form) {
/** @var \CustomContact\Model\Api\CustomContact $objet */
$objet = $modelFactory->buildModel('CustomContact', $form, $locale);

$arr[] = [
"id" => $objet->getId(),
"code" => $objet->getCode(),
"title" => $objet->getTitle(),
"email" => $objet->getEmail(),
"fieldConfiguration" => $objet->getFieldConfiguration(),
"returnUrl" => $objet->getReturnUrl(),
];
}

return OpenApiService::jsonResponse($arr);
}

protected function findLocale(Request $request)
Expand Down
76 changes: 56 additions & 20 deletions EventListener/CustomContactEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,69 @@ public function sendCustomerContact(CustomContactEvent $event)

$destinationEmails = array_map('trim', explode(',', $customContact->getEmail()));

$fields = json_decode($event->getCustomContact()->getFieldConfiguration(), true, 512, JSON_THROW_ON_ERROR);

// check if the custom contact form has a select contact field
$hasSelectListContact = false;
$labelSelectListContact = '';
$selectedContact = false;

// we verify the select contact exists
foreach ($fields as $field) {
if ($field['type'] === 'select-contact') {
$hasSelectListContact = true;
$labelSelectListContact = $field['label'];
break;
}
}

// and we verify the user selected a value in this select
if ($hasSelectListContact !== false) {
foreach ($event->getFields() as $key => $field) {
if ($labelSelectListContact === $key && filter_var($field, FILTER_VALIDATE_EMAIL)) {
$selectedContact = $field;
break;
}
}
}

if ($hasSelectListContact !== false && $selectedContact !== false) {
$this->sendEmail($selectedContact, $customContact->getTitle(), $event);
return;
}

if (empty($destinationEmails)) {
$destinationEmails = [ConfigQuery::getStoreEmail()];
}

foreach ($destinationEmails as $emailAddress) {
$email = $this->mailer->createEmailMessage(
CustomContact::MAIL_CUSTOM_CONTACT,
[ConfigQuery::getStoreEmail() => $event->getCustomContact()->getTitle()],
[$emailAddress => ConfigQuery::getStoreName()],
[
'form_title' => $customContact->getTitle(),
'store_name' => ConfigQuery::getStoreName(),
'fields' => $event->getFields()
]
);

foreach ($event->getFileFields() as $fileField) {
/** @var UploadedFile $file */
foreach ($fileField as $file) {
$email->attach($file->getContent(), $file->getClientOriginalName());
}
}
$this->sendEmail($emailAddress, $customContact->getTitle(), $event);
}
}

// Override subject
$email->subject($customContact->getTitle());

$this->mailer->send($email);
protected function sendEmail(string $email, string $title, CustomContactEvent $event) {
$emailService = $this->mailer->createEmailMessage(
CustomContact::MAIL_CUSTOM_CONTACT,
[ConfigQuery::getStoreEmail() => $event->getCustomContact()->getTitle()],
[$email => ConfigQuery::getStoreName()],
[
'form_title' => $title,
'store_name' => ConfigQuery::getStoreName(),
'fields' => $event->getFields()
]
);

foreach ($event->getFileFields() as $fileField) {
/** @var UploadedFile $file */
foreach ($fileField as $file) {
$email->attach($file->getContent(), $file->getClientOriginalName());
}
}

// Override subject
$emailService->subject($title);

$this->mailer->send($emailService);
}
}
9 changes: 9 additions & 0 deletions I18n/backOffice/default/fr_FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
'Delete this custom contact form' => 'Supprimer ce formulaire',
'Field type' => 'Type de champ',
'File upload' => 'Envoi de fichier',
'Field select' => 'Champ liste',
'Field date' => 'Champ date',
'Field choice' => 'Champ choix',
'Form configuration' => 'Configuration formulaire',
'Form field' => 'Champ',
'Form fields configuration' => 'Configuration des champs du formulaire',
"Field list" => "Champ liste",
"Field list contacts" => "Champ liste à contacts",
"Checkboxes" => "Cases à cocher",
"Radios" => "Boutons radios",
'Home' => 'Accueil',
'Id' => 'Id',
'Is field required' => 'Champ requis',
Expand All @@ -22,4 +29,6 @@
'Simple field' => 'Champ texte simple',
'Title' => 'Titre',
'Update this custom contact form' => 'Mettre à jour ce formulaire',
"Multiple selection" => "Sélection multiple",
"Options" => "Options",
);
2 changes: 2 additions & 0 deletions I18n/en_US.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

return array(
"Multiple selection" => "Multiple selection",
"Options" => "Options",
);
2 changes: 2 additions & 0 deletions I18n/fr_FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
'Success message' => 'Message de succès',
'Title' => 'Titre',
'Update ' => 'Mettre à jour',
"Multiple selection" => "Sélection multiple",
"Options" => "Options",
);
91 changes: 83 additions & 8 deletions templates/backOffice/default/custom_contact/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#editor_holder .well {
background-color: transparent;
border: none;
padding: 0;
box-shadow: none;
}

Expand Down Expand Up @@ -106,8 +105,6 @@

const element = document.getElementById('editor_holder');

console.log(JSONEditor.defaults.language);

const editor = new JSONEditor(
document.getElementById('editor_holder'),
{
Expand Down Expand Up @@ -148,16 +145,94 @@
},
"type": {
"type": "string",
"enum": ["text","textarea", "file"],
"enum": ["text","textarea", "file", "date", "select", 'select-contact', "checkbox", "radio"],
"options": {
"enum_titles" : [
"{intl l="Simple field" d='customcontact.bo.default' js=1}",
"{intl l="Long text field" d='customcontact.bo.default' js=1}",
"{intl l="File upload" d='customcontact.bo.default' js=1}"
"{intl l="File upload" d='customcontact.bo.default' js=1}",
"{intl l="Field date" d='customcontact.bo.default' js=1}",
"{intl l="Field list" d='customcontact.bo.default' js=1}",
"{intl l="Field list contacts" d='customcontact.bo.default' js=1}",
"{intl l="Checkboxes" d='customcontact.bo.default' js=1}",
"{intl l="Radios" d='customcontact.bo.default' js=1}",
]
},
"title": "{intl l="Field type" d='customcontact.bo.default' js=1}"
},
"Liste email contact": {
"title": "{intl l="Email" d='customcontact.bo.default' js=1}",
"type": "array",
"options": {
"dependencies": {
"type": "select-contact"
}
},
"items": {
"properties": {
"email": {
"type": "string",
"title": "Email"
},
"label": {
"type": "string",
"title": "Label"
},
}
},
},
"Liste selection multiple": {
"title": "{intl l="Multiple selection" d='customcontact.bo.default' js=1}",
"type": "boolean",
"options": {
"dependencies": {
"type": "select"
}
},
"examples": [
false,
true
]
},
"Options select": {
"title": "{intl l="Options" d='customcontact.bo.default' js=1}",
"type": "array",
"options": {
"dependencies": {
"type": "select"
}
},
"items": {
"type": "string",
"title": "Option"
},
},
"Labels cases a cocher": {
"title": "{intl l="Options" d='customcontact.bo.default' js=1}",
"type": "array",
"items": {
"type": "string",
"title": "Label"
},
"options": {
"dependencies": {
"type": "checkbox"
}
},
},
"Labels boutons radio": {
"title": "{intl l="Options" d='customcontact.bo.default' js=1}",
"type": "array",
"items": {
"type": "string",
"title": "Label"
},
"options": {
"dependencies": {
"type": "radio"
}
},
},
"placeholder": {
"type": "string",
"title": "{intl l="Placeholder" d='customcontact.bo.default' js=1}"
Expand All @@ -169,9 +244,9 @@
);

{literal}
editor.on('change', () => {
document.querySelector('#json_field').value = JSON.stringify(editor.getValue());
});
editor.on('change', () => {
document.querySelector('#json_field').value = JSON.stringify(editor.getValue());
});
{/literal}
</script>
{/block}

0 comments on commit f885a05

Please sign in to comment.