Skip to content

Commit

Permalink
Support for 6.4, fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed May 11, 2021
1 parent dcbde5c commit f7a6e40
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/store.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: PackPlugin
on:
push:
branches:
- 0.2
- 0.3
tags:
- '*'

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG_de-DE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.3.0
* Kompatiblität zu 6.4

# 0.2.2
* Um die Lesbarkeit zu erhöhen, wurden die Boxen für HTML- und TEXT-Mail erhöht
* In den Mail-Details wird nun auch die Uhrzeit des Versands angezeigt
Expand Down
13 changes: 9 additions & 4 deletions CHANGELOG_en-GB.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
# 0.3.0

* Add compatibility for Shopware 6.4


# 0.2.2
* To improve readability, the boxes for HTML and TEXT mail have been raised
* The time of sending is now also displayed in the mail details
* An error in the display of e-mail addresses has been fixed

# 0.2.1

* Absender Name hinzugefügt
* Standardsortierung zu neueste Einträge geändert
* Sender name added
* Default sorting changed to newest entries

# 0.2.0

* Add compability for Shopware 6.3
* Add compatibility for Shopware 6.3

# 0.1.2

* Add compability for Shopware 6.2
* Add compatibility for Shopware 6.2

# 0.1.1

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frosh/mail-platform-archive",
"version": "0.2.2",
"version": "0.3.0",
"description": "Mail Archive",
"type": "shopware-platform-plugin",
"license": "MIT",
Expand Down Expand Up @@ -40,6 +40,6 @@
}
},
"require": {
"shopware/core": "~6.3"
"shopware/core": "~6.4.0"
}
}
2 changes: 1 addition & 1 deletion src/Content/MailArchive/MailArchiveDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function defineFields(): FieldCollection
(new JsonField('receiver', 'receiver'))->addFlags(new Required())->addFlags(new SearchRanking(SearchRanking::HIGH_SEARCH_RANKING)),
(new StringField('subject', 'subject'))->addFlags(new Required())->addFlags(new SearchRanking(SearchRanking::HIGH_SEARCH_RANKING)),
(new LongTextField('plainText', 'plainText'))->addFlags(new AllowHtml()),
(new LongTextField('htmlText', 'htmlText'))->addFlags(new AllowHtml(), new SearchRanking(SearchRanking::LOW_SEARCH_RAKING)),
(new LongTextField('htmlText', 'htmlText'))->addFlags(new AllowHtml(), new SearchRanking(SearchRanking::LOW_SEARCH_RANKING)),
(new LongTextField('eml', 'eml'))->addFlags(new AllowHtml()),

new FkField('salesChannelId', 'salesChannelId', SalesChannelDefinition::class),
Expand Down
39 changes: 15 additions & 24 deletions src/Controller/Api/MailResendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Frosh\MailArchive\Controller\Api;

use Frosh\MailArchive\Content\MailArchive\MailArchiveEntity;
use Shopware\Core\Content\MailTemplate\Service\MailSender;
use Frosh\MailArchive\Services\MailSender;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
Expand All @@ -13,24 +13,17 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;

class MailResendController extends AbstractController
{
/**
* @var EntityRepositoryInterface
*/
private $mailArchiveRepository;
private EntityRepositoryInterface $mailArchiveRepository;

/**
* @var MailSender
*/
private $mailSender;
private MailSender $mailSender;

/**
* @var RequestStack
*/
private $requestStack;
private RequestStack $requestStack;

public function __construct(EntityRepositoryInterface $mailArchiveRepository, MailSender $mailSender, RequestStack $requestStack)
{
Expand All @@ -41,7 +34,7 @@ public function __construct(EntityRepositoryInterface $mailArchiveRepository, Ma

/**
* @RouteScope(scopes={"api"})
* @Route(path="/api/v{version}/_action/frosh-mail-archive/resend-mail")
* @Route(path="/api/_action/frosh-mail-archive/resend-mail")
*/
public function resend(Request $request)
{
Expand All @@ -60,22 +53,20 @@ public function resend(Request $request)

$this->requestStack->getMasterRequest()->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID, $mailArchive->getSalesChannelId());

$message = new \Swift_Message();
$message = new Email();

foreach ($mailArchive->getReceiver() as $mail => $name) {
$message->addTo($mail, $name);
$message->addTo(new Address($mail, $name));
}

$message->setFrom($mailArchive->getSender());
$message->setSubject($mailArchive->getSubject());

if (!empty($mailArchive->getPlainText())) {
$message->addPart($mailArchive->getPlainText(), 'text/plain');
foreach ($mailArchive->getSender() as $mail => $name) {
$message->from(new Address($mail, $name));
}

if (!empty($mailArchive->getHtmlText())) {
$message->addPart($mailArchive->getHtmlText(), 'text/html');
}
$message->subject($mailArchive->getSubject());

$message->html($mailArchive->getHtmlText());
$message->text($mailArchive->getPlainText());

$this->mailSender->send($message);

Expand Down
2 changes: 1 addition & 1 deletion src/Migration/Migration1575569953createTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getCreationTimestamp(): int

public function update(Connection $connection): void
{
$connection->executeQuery('CREATE TABLE `frosh_mail_archive` (
$connection->executeStatement('CREATE TABLE `frosh_mail_archive` (
`id` BINARY(16) NOT NULL,
`sender` VARCHAR(255) NOT NULL,
`receiver` JSON NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions src/Migration/Migration1598204175SenderToJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function getCreationTimestamp(): int

public function update(Connection $connection): void
{
$connection->executeUpdate('UPDATE frosh_mail_archive SET sender = JSON_OBJECT(sender, \'\')');
$connection->executeUpdate('ALTER TABLE `frosh_mail_archive`
$connection->executeStatement('UPDATE frosh_mail_archive SET sender = JSON_OBJECT(sender, \'\')');
$connection->executeStatement('ALTER TABLE `frosh_mail_archive`
CHANGE `sender` `sender` json NOT NULL AFTER `id`;');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Shopware.Module.register('frosh-mail-archive', {
name: 'frosh-mail-archive.title',
title: 'frosh-mail-archive.title',
description: '',
color: '#62ff80',
color: '#243758',
icon: 'default-communication-envelope',

routes: {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<tag name="shopware.entity.definition" entity="frosh_mail_archive"/>
</service>

<service id="Frosh\MailArchive\Services\MailSender" public="true" decorates="Shopware\Core\Content\MailTemplate\Service\MailSender">
<service id="Frosh\MailArchive\Services\MailSender" public="true" decorates="Shopware\Core\Content\Mail\Service\MailSender">
<argument type="service" id="Frosh\MailArchive\Services\MailSender.inner"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="frosh_mail_archive.repository"/>
Expand Down
85 changes: 42 additions & 43 deletions src/Services/MailSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,58 @@

namespace Frosh\MailArchive\Services;

use Shopware\Core\Content\MailTemplate\Service\MailSender as ShopwareMailSender;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Content\Mail\Service\AbstractMailSender;
use Shopware\Core\Content\Mail\Service\AbstractMailService;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

class MailSender extends ShopwareMailSender
class MailSender extends AbstractMailSender
{
/**
* @var MailSender
*/
private $mailSender;
private AbstractMailSender $mailSender;

/**
* @var RequestStack
*/
private $requestStack;
private RequestStack $requestStack;

/**
* @var EntityRepositoryInterface
*/
private $mailArchiveRepository;
private EntityRepositoryInterface $mailArchiveRepository;

/**
* @var EntityRepositoryInterface
*/
private $customerRepository;
private EntityRepositoryInterface $customerRepository;

public function __construct(
ShopwareMailSender $mailSender,
AbstractMailSender $mailSender,
RequestStack $requestStack,
EntityRepositoryInterface $mailArchiveRepository,
EntityRepositoryInterface $customerRepository
)
{
) {
$this->mailSender = $mailSender;
$this->requestStack = $requestStack;
$this->mailArchiveRepository = $mailArchiveRepository;
$this->customerRepository = $customerRepository;
}

public function send(\Swift_Message $message): void
public function send(Email $email, ?Envelope $envelope = null): void
{
$this->saveMail($message);
$this->saveMail($email);

$this->mailSender->send($message);
$this->mailSender->send($email, $envelope);
}

private function saveMail(\Swift_Message $message): void
private function saveMail(Email $message): void
{
$plain = null;
$html = null;

foreach ($message->getChildren() as $child) {
if ($child->getBodyContentType() === 'text/html') {
$html = $child->getBody();
continue;
}
if ($child->getBodyContentType() === 'text/plain') {
$plain = $child->getBody();
}
}

$this->mailArchiveRepository->create([
[
'id' => Uuid::randomHex(),
'sender' => $message->getFrom(),
'receiver' => $message->getTo(),
'sender' => [$message->getFrom()[0]->getAddress() => $message->getFrom()[0]->getName()],
'receiver' => $this->convertAddress($message->getTo()),
'subject' => $message->getSubject(),
'plainText' => nl2br($plain),
'htmlText' => $html,
'plainText' => nl2br($message->getTextBody()),
'htmlText' => $message->getHtmlBody(),
'eml' => $message->toString(),
'salesChannelId' => $this->getCurrentSalesChannelId(),
'customerId' => $this->getCustomerIdByMail(array_keys($message->getTo()))
Expand All @@ -91,11 +70,31 @@ private function getCurrentSalesChannelId(): ?string
return $this->requestStack->getMasterRequest()->attributes->get('sw-sales-channel-id');
}

private function getCustomerIdByMail(array $mails)
private function getCustomerIdByMail(array $mails): ?string
{
$criteria = new Criteria();

$criteria->addFilter(new EqualsAnyFilter('email', $mails));
return $this->customerRepository->searchIds($criteria, Context::createDefaultContext())->firstId();
}

public function getDecorated(): AbstractMailSender
{
return $this->mailSender;
}

/**
* @param Address[] $addresses
* @return array
*/
private function convertAddress(array $addresses): array
{
$list = [];

foreach ($addresses as $address) {
$list[$address->getAddress()] = $address->getName();
}

return $list;
}
}

0 comments on commit f7a6e40

Please sign in to comment.