Skip to content

Commit

Permalink
Added email handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Blazek committed Nov 15, 2016
1 parent 87ec150 commit 8453e78
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 25 deletions.
10 changes: 8 additions & 2 deletions Action/PersistToDatabaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -84,8 +92,6 @@ public function act(InformationCollected $event)
$ezInfoAttribute->setDataFloat($value->getDataFloat());
$ezInfoAttribute->setDataText($value->getDataText());

dump($ezInfoAttribute);

$this->infoCollectionAttributeRepository->save($ezInfoAttribute);
}
}
Expand Down
71 changes: 70 additions & 1 deletion Action/SendEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;

class FullViewController
class InformationCollectionController
{
const VIEW_TYPE = 'full';

/**
* @var FormBuilder
*/
Expand All @@ -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);

Expand Down
20 changes: 19 additions & 1 deletion Event/InformationCollected.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -54,4 +62,14 @@ public function getLocation()
{
return $this->data->target;
}

/**
* Returns additional content
*
* @return Content
*/
public function getAdditionalContent()
{
return $this->additionalContent;
}
}
70 changes: 70 additions & 0 deletions Factory/EmailDataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Netgen\Bundle\InformationCollectionBundle\Factory;

use eZ\Publish\Core\Helper\FieldHelper;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\API\Repository\Values\Content\Content;
use Netgen\Bundle\InformationCollectionBundle\Value\EmailData;

class EmailDataFactory
{
/**
* @var ConfigResolverInterface
*/
protected $configResolver;

/**
* @var TranslationHelper
*/
protected $translationHelper;

/**
* @var FieldHelper
*/
protected $fieldHelper;

public function __construct(
ConfigResolverInterface $configResolver,
TranslationHelper $translationHelper,
FieldHelper $fieldHelper
)
{
$this->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');
}
}
7 changes: 7 additions & 0 deletions FieldHandler/Legacy/EmailAddressValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ public function getValue(Value $value, FieldDefinition $fieldDefinition)
);
}

/**
* @inheritDoc
*/
public function toString(Value $value, FieldDefinition $fieldDefinition)
{
return '';
}
}
8 changes: 8 additions & 0 deletions FieldHandler/Legacy/EnhancedSelectionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public function getValue(Value $value, FieldDefinition $fieldDefinition)
(string)$value
);
}

/**
* @inheritDoc
*/
public function toString(Value $value, FieldDefinition $fieldDefinition)
{
return '';
}
}
10 changes: 10 additions & 0 deletions FieldHandler/Legacy/LegacyFieldHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 20 additions & 1 deletion FieldHandler/Legacy/Registry/FieldHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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');
}
}
7 changes: 7 additions & 0 deletions FieldHandler/Legacy/TextLineValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ public function getValue(Value $value, FieldDefinition $fieldDefinition)
);
}

/**
* @inheritDoc
*/
public function toString(Value $value, FieldDefinition $fieldDefinition)
{
return '';
}
}
Loading

0 comments on commit 8453e78

Please sign in to comment.