Skip to content

Commit

Permalink
Allow to resend the GiftCard email via admin.yaml
Browse files Browse the repository at this point in the history
Fixes Setono#17
  • Loading branch information
Roshyo committed Aug 6, 2020
1 parent b4fb535 commit 0c8e4aa
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 7 deletions.
92 changes: 92 additions & 0 deletions src/Controller/Action/ResendGiftCardEmailAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

use const FILTER_SANITIZE_URL;
use function filter_var;
use Setono\SyliusGiftCardPlugin\EmailManager\GiftCardEmailManagerInterface;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class ResendGiftCardEmailAction
{
/** @var GiftCardEmailManagerInterface */
private $giftCardEmailManager;

/** @var GiftCardRepositoryInterface */
private $giftCardRepository;

/** @var FlashBagInterface */
private $flashBag;

/** @var UrlGeneratorInterface */
private $router;

public function __construct(
GiftCardEmailManagerInterface $giftCardEmailManager,
GiftCardRepositoryInterface $giftCardRepository,
FlashBagInterface $flashBag,
UrlGeneratorInterface $router
) {
$this->giftCardEmailManager = $giftCardEmailManager;
$this->giftCardRepository = $giftCardRepository;
$this->flashBag = $flashBag;
$this->router = $router;
}

public function __invoke(Request $request, int $id): Response
{
$giftCard = $this->giftCardRepository->find($id);
if (!$giftCard instanceof GiftCardInterface) {
$this->flashBag->add('error', [
'message' => 'setono_sylius_gift_card.gift_card.not_found',
'parameters' => ['%id%' => $id],
]);

return new RedirectResponse($this->getRedirectRoute($request));
}

if ($giftCard->getOrder() instanceof OrderInterface) {
$this->giftCardEmailManager->sendEmailWithGiftCardsFromOrder($giftCard->getOrder(), [$giftCard]);
$this->flashBag->add('success', [
'message' => 'setono_sylius_gift_card.gift_card.resent',
'parameters' => ['%id%' => $id],
]);
} elseif ($giftCard->getCustomer() instanceof CustomerInterface) {
$this->giftCardEmailManager->sendEmailToCustomerWithGiftCard($giftCard->getCustomer(), $giftCard);
$this->flashBag->add('success', [
'message' => 'setono_sylius_gift_card.gift_card.resent',
'parameters' => ['%id%' => $id],
]);
} else {
$this->flashBag->add('error', [
'message' => 'setono_sylius_gift_card.gift_card.impossible_to_resend_email',
'parameters' => ['%id%' => $id],
]);
}

return new RedirectResponse($this->getRedirectRoute($request));
}

private function getRedirectRoute(Request $request): string
{
if ($request->headers->has('referer')) {
$filtered = filter_var($request->headers->get('referer'), FILTER_SANITIZE_URL);

if (false === $filtered) {
return $this->router->generate('gift_card_admin_gift_card_index');
}
}

return $this->router->generate('gift_card_admin_gift_card_index');
}
}
5 changes: 5 additions & 0 deletions src/Model/GiftCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,9 @@ public function getChannelCode(): ?string

return $channel->getCode();
}

public function hasAssociatedOrderOrCustomer(): bool
{
return $this->getCustomer() instanceof CustomerInterface || $this->getOrder() instanceof OrderInterface;
}
}
2 changes: 2 additions & 0 deletions src/Model/GiftCardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ public function getCustomerIdentification(): ?array;
public function getOrderIdentification(): ?array;

public function getChannelCode(): ?string;

public function hasAssociatedOrderOrCustomer(): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ sylius_grid:
delete_conditional: "@SetonoSyliusGiftCardPlugin/Admin/GiftCard/Grid/Action/delete_conditional.html.twig"
gift_card_balance: "@SetonoSyliusGiftCardPlugin/Admin/GiftCard/Grid/Action/gift_card_balance.html.twig"
list_conditional: "@SetonoSyliusGiftCardPlugin/Admin/GiftCard/Grid/Action/list_conditional.html.twig"
resend_gift_card_email: '@SetonoSyliusGiftCardPlugin/Admin/GiftCard/Grid/Action/resend_email.html.twig'
grids:
setono_sylius_gift_card_admin_gift_card:
driver:
Expand Down Expand Up @@ -113,6 +114,15 @@ sylius_grid:
route: setono_sylius_gift_card_admin_gift_card_pdf
parameters:
id: resource.id
resend_email:
type: resend_gift_card_email
label: setono_sylius_gift_card.ui.resend_email
options:
visible: resource.hasAssociatedOrderOrCustomer
link:
route: setono_sylius_gift_card_admin_gift_card_resend_email
parameters:
id: resource.id
delete:
type: delete_conditional
options:
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/config/routes/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ setono_sylius_gift_card_admin_gift_card_pdf:
_sylius:
section: admin

setono_sylius_gift_card_admin_gift_card_resend_email:
path: /gift-cards/{id}/resend-email
requirements:
id: \d+
methods: [ GET ]
defaults:
_controller: setono_sylius_gift_card.controller.action.resend_email
_sylius:
section: admin

setono_sylius_gift_card_admin_gift_card_configuration:
resource: |
alias: setono_sylius_gift_card.gift_card_configuration
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
<argument type="service" id="router"/>
</service>

<service id="setono_sylius_gift_card.controller.action.resend_email"
class="Setono\SyliusGiftCardPlugin\Controller\Action\ResendGiftCardEmailAction">
<argument type="service" id="setono_sylius_gift_card.email_manager.gift_card_order"/>
<argument type="service" id="setono_sylius_gift_card.repository.gift_card"/>
<argument type="service" id="session.flash_bag"/>
<argument type="service" id="router"/>
</service>

</services>

</container>
3 changes: 3 additions & 0 deletions src/Resources/translations/flashes.en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
setono_sylius_gift_card:
gift_card:
delete_error: The gift card code can't be removed.
impossible_to_resend_email: 'Impossible to re send email for gift card with id "%id%"'
not_found: 'Impossible to find gift card with id "%id%"'
read_error: Impossible to view this gift card.
resent: The email has successfully been resent
gift_card_added: Gift card was added to your order
gift_card_removed: Gift card removed from your order
3 changes: 3 additions & 0 deletions src/Resources/translations/flashes.fr.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
setono_sylius_gift_card:
gift_card:
delete_error: Le chèque-cadeau n'a pas pu être supprimé.
impossible_to_resend_email: 'Impossible d''envoyer l''email pour le chèque-cadeau "%id%"'
not_found: 'Impossible de trouver le chèque-cadeau "%id%"'
read_error: Impossible the visualiser le chèque-cadeau.
resent: L'email a bien été renvoyé
gift_card_added: Le chèque-cadeau a été ajouté à votre commande
gift_card_removed: Le chèque-cadeau a été supprimé de votre commande
7 changes: 4 additions & 3 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ setono_sylius_gift_card:
ui:
active: Active
add_gift_card_to_order: Add gift card
covered_by_gift_cards: Covered by gift cards
amount: Amount
applied_gift_cards: Applied gift cards
average_amount: Average amount
bought_at_order: Bought at order
covered_by_gift_cards: Covered by gift cards
count: Count
create_for_channel: Create for %channel%
download_pdf: PDF
Expand All @@ -31,17 +33,16 @@ setono_sylius_gift_card:
gift_card_code_is_invalid: Gift card code is invalid
gift_cards: Gift cards
gift_card_search: Gift card search
applied_gift_cards: Applied gift cards
gift_cards_adjustment_total: Gift cards total
initial_amount: Initial amount
bought_at_order: Bought at order
list_orders: List orders
manage_gift_cards: Manage gift cards
manage_gift_card_configurations: Manage gift card configurations
new_gift_card: New gift card
new_gift_card_configuration: New gift card configuration
manually_created: Manually created
no_gift_card: No gift card applied
resend_email: Resend email
total: Total
your_gift_card_will_only_be_charged_if_you_complete_your_order: Your gift card will only be charged if you complete your order
email:
Expand Down
11 changes: 7 additions & 4 deletions src/Resources/translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ setono_sylius_gift_card:
ui:
active: Actif
add_gift_card_to_order: Ajouter le chèque-cadeau
covered_by_gift_cards: Montant pris en charge
amount: Montant
applied_gift_cards: Chèques-cadeaux utilisés
average_amount: Montant moyen
bought_at_order: Commande d'origine
covered_by_gift_cards: Montant pris en charge
count: Nombre
create_for_channel: Créer pour %channel%
download_pdf: PDF
Expand All @@ -31,22 +33,23 @@ setono_sylius_gift_card:
gift_card_code_is_invalid: Numéro de chèque-cadeau invalide
gift_cards: Chèques-cadeaux
gift_card_search: Chercher un chèque cadeau
applied_gift_cards: Chèques-cadeaux utilisés
gift_cards_adjustment_total: Montant des chèques-cadeaux
initial_amount: Montant initial
bought_at_order: Commande d'origine
list_orders: Lister les commandes
manage_gift_cards: Gérer les chèques-cadeaux
manage_gift_card_configurations: Gérer les configurations de chèques-cadeaux
new_gift_card: Nouveau chèque-cadeau
new_gift_card_configuration: Nouvelle configuration de chèque-cadeau
manually_created: Créé manuellement
no_gift_card: Pas de chèque-cadeau utilisé
resend_email: Renvoyer l'email
total: Total
your_gift_card_will_only_be_charged_if_you_complete_your_order: Votre chèque-cadeau ne sera débité que si vous complétez votre commande
email:
your_gift_card: Votre chèque cadeau
balance: Solde
new_gift_card: Nouveau chèque-cadeau
new_gift_card_intro: Un nouveau chèque-cadeau a été créé pour vous
your_gift_card: Votre chèque cadeau
your_gift_cards_you_bought_in_the_order: Vos chèques-cadeaux achetés avec cette commande

# Missing from Sylius for some reason
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %}


{% set visible = options.visible is defined ? options.visible : true %}

{% if visible %}
{% set path = options.link.url|default(path(options.link.route, options.link.parameters)) %}

{{ buttons.default(path, action.label, null, 'mail', 'purple') }}
{% endif %}

0 comments on commit 0c8e4aa

Please sign in to comment.