diff --git a/Action/PersistToDatabaseAction.php b/Action/PersistToDatabaseAction.php index 5b45a17a..9d937f0f 100644 --- a/Action/PersistToDatabaseAction.php +++ b/Action/PersistToDatabaseAction.php @@ -32,6 +32,14 @@ class PersistToDatabaseAction implements ActionInterface */ protected $repository; + /** + * PersistToDatabaseAction constructor. + * + * @param FieldHandlerRegistry $fieldHandlerRegistry + * @param EzInfoCollectionRepository $infoCollectionRepository + * @param EzInfoCollectionAttributeRepository $infoCollectionAttributeRepository + * @param Repository $repository + */ public function __construct( FieldHandlerRegistry $fieldHandlerRegistry, EzInfoCollectionRepository $infoCollectionRepository, @@ -84,8 +92,6 @@ public function act(InformationCollected $event) $ezInfoAttribute->setDataFloat($value->getDataFloat()); $ezInfoAttribute->setDataText($value->getDataText()); - dump($ezInfoAttribute); - $this->infoCollectionAttributeRepository->save($ezInfoAttribute); } } diff --git a/Action/SendEmailAction.php b/Action/SendEmailAction.php index fa07a5f0..f8bdff1a 100644 --- a/Action/SendEmailAction.php +++ b/Action/SendEmailAction.php @@ -2,15 +2,84 @@ namespace Netgen\Bundle\InformationCollectionBundle\Action; +use eZ\Publish\API\Repository\ContentService; use Netgen\Bundle\InformationCollectionBundle\Event\InformationCollected; +use Netgen\Bundle\InformationCollectionBundle\Factory\EmailDataFactory; +use Netgen\Bundle\InformationCollectionBundle\FieldHandler\Legacy\Registry\FieldHandlerRegistry; +use Swift_Mailer; +use Swift_Message; +use Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine; class SendEmailAction implements ActionInterface { + /** + * @var FieldHandlerRegistry + */ + protected $fieldHandlerRegistry; + + /** + * @var Swift_Mailer + */ + protected $mailer; + + /** + * @var DelegatingEngine + */ + protected $template; + + /** + * @var ContentService + */ + protected $contentService; + + /** + * @var EmailDataFactory + */ + protected $emailDataFactory; + + public function __construct( + FieldHandlerRegistry $fieldHandlerRegistry, + EmailDataFactory $emailDataFactory, + Swift_Mailer $mailer, + DelegatingEngine $template, + ContentService $contentService + ) + { + $this->fieldHandlerRegistry = $fieldHandlerRegistry; + $this->mailer = $mailer; + $this->template = $template; + $this->contentService = $contentService; + $this->emailDataFactory = $emailDataFactory; + } + /** * @inheritDoc */ public function act(InformationCollected $event) { - dump('Email acted'); + dump($event->getInformationCollectionStruct()->getCollectedFields()); + $location = $event->getLocation(); + $contentType = $event->getContentType(); + $content = $this->contentService->loadContent($location->contentId); + + $emailData = $this->emailDataFactory->build($content); + + $message = Swift_Message::newInstance(); + $message->setSubject($emailData->getSubject()); + $message->setTo($emailData->getRecipient()); + $message->setFrom($emailData->getSender()); + + $message->setBody( + $this->template->render( $emailData->getTemplate(), + [ + 'content' => $content, + 'contentType' => $contentType, + 'params' => $event->getInformationCollectionStruct()->getCollectedFields(), + ] + ), + 'text/html' + ); + + return $this->mailer->send( $message ); } } \ No newline at end of file diff --git a/Controller/FullViewController.php b/Controller/InformationCollectionController.php similarity index 79% rename from Controller/FullViewController.php rename to Controller/InformationCollectionController.php index c30bb17e..0eead17b 100644 --- a/Controller/FullViewController.php +++ b/Controller/InformationCollectionController.php @@ -10,8 +10,10 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\HttpFoundation\Request; -class FullViewController +class InformationCollectionController { + const VIEW_TYPE = 'full'; + /** * @var FormBuilder */ @@ -28,18 +30,28 @@ class FullViewController * @param FormBuilder $builder * @param EventDispatcherInterface $dispatcher */ - public function __construct(FormBuilder $builder, EventDispatcherInterface $dispatcher) + public function __construct( + FormBuilder $builder, + EventDispatcherInterface $dispatcher + ) { $this->builder = $builder; $this->dispatcher = $dispatcher; } + public function displayAndHandleInformationCollector(ContentView $view, Request $request) { + $useAjax = false; $isValid = false; + if (self::VIEW_TYPE !== $view->getViewType()) { + $useAjax = true; + } + $location = $view->getLocation(); /** @var FormBuilderInterface $formBuilder */ - $form = $this->builder->createFormForLocation($location); + $form = $this->builder->createFormForLocation($location, $useAjax) + ->getForm(); $form->handleRequest($request); diff --git a/Event/InformationCollected.php b/Event/InformationCollected.php index fac95aee..62f1a0df 100644 --- a/Event/InformationCollected.php +++ b/Event/InformationCollected.php @@ -6,6 +6,7 @@ use eZ\Publish\API\Repository\Values\Content\Location; use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct; +use Netgen\EzPlatformSiteApi\API\Values\Content; use Symfony\Component\EventDispatcher\Event; class InformationCollected extends Event @@ -15,14 +16,21 @@ class InformationCollected extends Event */ protected $data; + /** + * @var Content + */ + protected $additionalContent; + /** * InformationCollected constructor. * * @param DataWrapper $data + * @param Content $additionalContent */ - public function __construct(DataWrapper $data) + public function __construct(DataWrapper $data, Content $additionalContent = null) { $this->data = $data; + $this->additionalContent = $additionalContent; } /** @@ -54,4 +62,14 @@ public function getLocation() { return $this->data->target; } + + /** + * Returns additional content + * + * @return Content + */ + public function getAdditionalContent() + { + return $this->additionalContent; + } } \ No newline at end of file diff --git a/Factory/EmailDataFactory.php b/Factory/EmailDataFactory.php new file mode 100644 index 00000000..6b35a0db --- /dev/null +++ b/Factory/EmailDataFactory.php @@ -0,0 +1,70 @@ +configResolver = $configResolver; + $this->translationHelper = $translationHelper; + $this->fieldHelper = $fieldHelper; + } + + public function build(Content $content) + { + return new EmailData( + $this->resolve($content, 'recipient', 'email'), + $this->resolve($content, 'sender', 'email'), + $this->resolve($content, 'subject'), + $this->resolveTemplate() + ); + } + + protected function resolve(Content $content, $field, $property = 'text') + { + if ( + array_key_exists($field, $content->fields) && + !$this->fieldHelper->isFieldEmpty($content, $field) + ) { + + $fieldValue = $this->translationHelper->getTranslatedField($content, $field); + + return $fieldValue->value->$property; + } else { + + return $this->configResolver->getParameter('information_collection.email.' . $field, 'netgen'); + + } + } + + protected function resolveTemplate() + { + return $this->configResolver->getParameter('information_collection.email.template', 'netgen'); + } +} \ No newline at end of file diff --git a/FieldHandler/Legacy/EmailAddressValueHandler.php b/FieldHandler/Legacy/EmailAddressValueHandler.php index b19606f5..2a270146 100644 --- a/FieldHandler/Legacy/EmailAddressValueHandler.php +++ b/FieldHandler/Legacy/EmailAddressValueHandler.php @@ -30,4 +30,11 @@ public function getValue(Value $value, FieldDefinition $fieldDefinition) ); } + /** + * @inheritDoc + */ + public function toString(Value $value, FieldDefinition $fieldDefinition) + { + return ''; + } } \ No newline at end of file diff --git a/FieldHandler/Legacy/EnhancedSelectionValueHandler.php b/FieldHandler/Legacy/EnhancedSelectionValueHandler.php index 95ff546c..dfdac0ad 100644 --- a/FieldHandler/Legacy/EnhancedSelectionValueHandler.php +++ b/FieldHandler/Legacy/EnhancedSelectionValueHandler.php @@ -29,4 +29,12 @@ public function getValue(Value $value, FieldDefinition $fieldDefinition) (string)$value ); } + + /** + * @inheritDoc + */ + public function toString(Value $value, FieldDefinition $fieldDefinition) + { + return ''; + } } \ No newline at end of file diff --git a/FieldHandler/Legacy/LegacyFieldHandlerInterface.php b/FieldHandler/Legacy/LegacyFieldHandlerInterface.php index b8951cfa..64ab3421 100644 --- a/FieldHandler/Legacy/LegacyFieldHandlerInterface.php +++ b/FieldHandler/Legacy/LegacyFieldHandlerInterface.php @@ -26,4 +26,14 @@ public function supports(Value $value); * @return LegacyHandledFieldValue */ public function getValue(Value $value, FieldDefinition $fieldDefinition); + + /** + * Transforms field value object to string + * + * @param Value $value + * @param FieldDefinition $fieldDefinition + * + * @return string + */ + public function toString(Value $value, FieldDefinition $fieldDefinition); } \ No newline at end of file diff --git a/FieldHandler/Legacy/Registry/FieldHandlerRegistry.php b/FieldHandler/Legacy/Registry/FieldHandlerRegistry.php index c373620e..b4475887 100644 --- a/FieldHandler/Legacy/Registry/FieldHandlerRegistry.php +++ b/FieldHandler/Legacy/Registry/FieldHandlerRegistry.php @@ -45,7 +45,6 @@ public function addHandler(LegacyFieldHandlerInterface $handler) */ public function handleField(Value $value, FieldDefinition $fieldDefinition) { - dump($this->fieldHandlers); foreach ($this->fieldHandlers as $fieldHandler) { if ($fieldHandler->supports($value)) { @@ -55,4 +54,24 @@ public function handleField(Value $value, FieldDefinition $fieldDefinition) throw new \RuntimeException('LegacyFieldHandler for field not found in FieldHandlerRegistry'); } + + /** + * @param Value $value + * @param FieldDefinition $fieldDefinition + * + * @return LegacyHandledFieldValue + * + * @throws \RuntimeException + */ + public function toStringHandle(Value $value, FieldDefinition $fieldDefinition) + { + foreach ($this->fieldHandlers as $fieldHandler) { + + if ($fieldHandler->supports($value)) { + return $fieldHandler->toString($value, $fieldDefinition); + } + } + + throw new \RuntimeException('LegacyFieldHandler for field not found in FieldHandlerRegistry'); + } } \ No newline at end of file diff --git a/FieldHandler/Legacy/TextLineValueHandler.php b/FieldHandler/Legacy/TextLineValueHandler.php index cddedd39..b071fddc 100644 --- a/FieldHandler/Legacy/TextLineValueHandler.php +++ b/FieldHandler/Legacy/TextLineValueHandler.php @@ -30,4 +30,11 @@ public function getValue(Value $value, FieldDefinition $fieldDefinition) ); } + /** + * @inheritDoc + */ + public function toString(Value $value, FieldDefinition $fieldDefinition) + { + return ''; + } } \ No newline at end of file diff --git a/Form/Builder/FormBuilder.php b/Form/Builder/FormBuilder.php index 14379ab8..dd51e740 100644 --- a/Form/Builder/FormBuilder.php +++ b/Form/Builder/FormBuilder.php @@ -2,10 +2,11 @@ namespace Netgen\Bundle\InformationCollectionBundle\Form\Builder; -use eZ\Publish\API\Repository\ContentTypeService; +use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormFactoryInterface; -use Symfony\Component\Form\FormInterface; +use Symfony\Component\Routing\RouterInterface; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\API\Repository\ContentTypeService; use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct; @@ -28,28 +29,41 @@ class FormBuilder */ protected $contentTypeService; + /** + * @var RouterInterface + */ + protected $router; + /** * FormBuilder constructor. * * @param FormFactoryInterface $formFactory * @param ContentTypeService $contentTypeService - * @param boolean $useCsrf + * @param RouterInterface $router + * @param bool $useCsrf */ - public function __construct(FormFactoryInterface $formFactory, ContentTypeService $contentTypeService, $useCsrf) + public function __construct( + FormFactoryInterface $formFactory, + ContentTypeService $contentTypeService, + RouterInterface $router, + $useCsrf + ) { $this->formFactory = $formFactory; $this->useCsrf = $useCsrf; $this->contentTypeService = $contentTypeService; + $this->router = $router; } /** * Creates Information collection Form object for given Location object * * @param Location $location + * @param bool $useAjax * - * @return FormInterface + * @return FormBuilderInterface */ - public function createFormForLocation(Location $location) + public function createFormForLocation(Location $location, $useAjax = false) { $contentInfo = $location->contentInfo; @@ -66,6 +80,10 @@ public function createFormForLocation(Location $location) ] ); - return $formBuilder->getForm(); + if ($useAjax) { + $formBuilder->setAction($this->router->generate('netgen_information_collection_handle_ajax', ['location' => $location->id])); + } + + return $formBuilder; } } \ No newline at end of file diff --git a/Resources/config/parameters.yml b/Resources/config/parameters.yml index 23ab4418..12d6c0cc 100644 --- a/Resources/config/parameters.yml +++ b/Resources/config/parameters.yml @@ -1,2 +1,6 @@ parameters: - netgen_information_collection.form.use_csrf: true \ No newline at end of file + netgen.default.information_collection.form.use_csrf: true + netgen.default.information_collection.email.recipient: 'mario.b@netgen.hr' + netgen.default.information_collection.email.subject: 'Test' + netgen.default.information_collection.email.sender: 'mario.b@netgen.hr' + netgen.default.information_collection.email.template: 'NetgenInformationCollectionBundle:email:email.html.twig' diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 880b944e..20999634 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -1,3 +1,5 @@ -netgen_information_collection_homepage: - path: /hello/{name} - defaults: { _controller: NetgenInformationCollectionBundle:Default:index } +netgen_information_collection_handle_ajax: + path: /information/collection/handle/{location} + defaults: { _controller: netgen_information_collection.controller:handleInformationCollectorViaAjax } + methods: + - POST diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 79fd27fc..69b76ecd 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -2,7 +2,7 @@ imports: - { resource: handlers/legacy.yml } parameters: - netgen_information_collection.controller.full.class: Netgen\Bundle\InformationCollectionBundle\Controller\FullViewController + netgen_information_collection.controller.class: Netgen\Bundle\InformationCollectionBundle\Controller\InformationCollectionController netgen_information_collection.form.builder.class: Netgen\Bundle\InformationCollectionBundle\Form\Builder\FormBuilder # Doctrine parameteters for information collection @@ -19,9 +19,11 @@ parameters: netgen_information_collection.listener.class: Netgen\Bundle\InformationCollectionBundle\Listener\InformationCollectedListener netgen_information_collection.field_handler.legacy.registry.class: Netgen\Bundle\InformationCollectionBundle\FieldHandler\Legacy\Registry\FieldHandlerRegistry + + netgen_information_collection.factory.email_data.class: Netgen\Bundle\InformationCollectionBundle\Factory\EmailDataFactory services: - netgen_information_collection.controller.full: - class: '%netgen_information_collection.controller.full.class%' + netgen_information_collection.controller: + class: '%netgen_information_collection.controller.class%' arguments: - '@netgen_information_collection.form.builder' - '@event_dispatcher' @@ -31,7 +33,8 @@ services: arguments: - '@form.factory' - '@ezpublish.api.service.content_type' - - '%netgen_information_collection.form.use_csrf%' + - '@router' + - '%netgen.default.information_collection.form.use_csrf%' # Info collection repositories netgen_information_collection.repository.ez_info_collection: @@ -60,6 +63,12 @@ services: netgen_information_collection.action.email: class: '%netgen_information_collection.action.email.class%' + arguments: + - '@netgen_information_collection.field_handler.legacy.registry' + - '@netgen_information_collection.factory.email_data' + - '@mailer' + - '@templating' + - '@ezpublish.api.service.content' tags: - { name: netgen_information_collection.action } @@ -71,4 +80,11 @@ services: - { name: kernel.event_subscriber } netgen_information_collection.field_handler.legacy.registry: - class: '%netgen_information_collection.field_handler.legacy.registry.class%' \ No newline at end of file + class: '%netgen_information_collection.field_handler.legacy.registry.class%' + + netgen_information_collection.factory.email_data: + class: '%netgen_information_collection.factory.email_data.class%' + arguments: + - '@ezpublish.config.resolver' + - '@ezpublish.translation_helper' + - '@ezpublish.field_helper' diff --git a/Resources/views/Default/index.html.twig b/Resources/views/Default/index.html.twig deleted file mode 100644 index 4ce626e9..00000000 --- a/Resources/views/Default/index.html.twig +++ /dev/null @@ -1 +0,0 @@ -Hello {{ name }}! diff --git a/Resources/views/email/email.html.twig b/Resources/views/email/email.html.twig new file mode 100644 index 00000000..cdf69c10 --- /dev/null +++ b/Resources/views/email/email.html.twig @@ -0,0 +1,3 @@ +{% for field_identifier, value in params %} +
{{ ez_field_name(content, field_identifier) }}: -> {{ value }}
+{% endfor %} \ No newline at end of file diff --git a/Value/EmailData.php b/Value/EmailData.php new file mode 100644 index 00000000..80a83f96 --- /dev/null +++ b/Value/EmailData.php @@ -0,0 +1,62 @@ +recipient = $recipient; + $this->subject = $subject; + $this->template = $template; + $this->sender = $sender; + } + + public function getRecipient() + { + return $this->recipient; + } + + public function getSubject() + { + return $this->subject; + } + + public function getSender() + { + return $this->sender; + } + + public function getTemplate() + { + return $this->template; + } +} \ No newline at end of file