Browse Source

Gestion de la newsletter

reduction
Guillaume 4 years ago
parent
commit
5db04c86fe
7 changed files with 124 additions and 4 deletions
  1. +8
    -0
      ShopBundle/Context/NewsInterface.php
  2. +10
    -1
      ShopBundle/Controller/Admin/AdminController.php
  3. +51
    -0
      ShopBundle/Controller/Admin/NewsController.php
  4. +5
    -3
      ShopBundle/EventSubscriber/EasyAdminSubscriber.php
  5. +31
    -0
      ShopBundle/Model/News.php
  6. +17
    -0
      ShopBundle/Model/User.php
  7. +2
    -0
      ShopBundle/Resources/views/backend/news/action_send.html.twig

+ 8
- 0
ShopBundle/Context/NewsInterface.php View File

@@ -0,0 +1,8 @@
<?php

namespace Lc\ShopBundle\Context ;

interface NewsInterface
{

}

+ 10
- 1
ShopBundle/Controller/Admin/AdminController.php View File

@@ -3,6 +3,7 @@
namespace Lc\ShopBundle\Controller\Admin;

use App\Entity\MerchantConfig;
use App\Repository\UserRepository;
use Lc\ShopBundle\Context\AddressInterface;
use Lc\ShopBundle\Repository\MerchantConfigRepository;
use Doctrine\ORM\EntityManagerInterface;
@@ -19,7 +20,9 @@ use Lc\ShopBundle\Form\AbstractEditPositionType;
use Lc\ShopBundle\Form\ChoiceProductCategoryType;
use Lc\ShopBundle\Form\PriceType;
use Lc\ShopBundle\Form\Widget\PriceWidgetType;
use Lc\ShopBundle\Services\GlobalParam;
use Lc\ShopBundle\Services\Utils;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormEvent;
@@ -32,17 +35,23 @@ class AdminController extends EasyAdminController
protected $userManager;
protected $em ;
protected $utils ;
protected $mailjetTransport ;
protected $globalParam ;

public function __construct(
Security $security,
UserManagerInterface $userManager,
EntityManagerInterface $em,
Utils $utils)
Utils $utils,
MailjetTransport $mailjetTransport,
GlobalParam $globalParam)
{
$this->security = $security;
$this->userManager = $userManager;
$this->em = $em ;
$this->utils = $utils ;
$this->mailjetTransport = $mailjetTransport ;
$this->globalParam = $globalParam ;
}

public function showAction()

+ 51
- 0
ShopBundle/Controller/Admin/NewsController.php View File

@@ -0,0 +1,51 @@
<?php

namespace Lc\ShopBundle\Controller\Admin;

use App\Repository\UserRepository;
use Lc\ShopBundle\Context\NewsInterface;
use Lc\ShopBundle\Context\UserInterface;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class NewsController extends AdminController
{

public function sendAction()
{
$idNews = $this->request->get('id') ;
$users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findBy([
'isSubscribedNewsletter' => true
]) ;
$countUsers = count($users) ;
$messages = [];
$news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
if($news) {
foreach ($users as $user) {
$message = new \Swift_Message('[Place du Local] '.$news->getTitle());
$message->addTo($user->getEmail())
->addFrom('no-reply@opendistrib.net', 'Place du Local')
->setBody($this->renderView('mail/news.html.twig', [
'message' => $news->getDescription(),
'image' => $news->getImage(),
]), 'text/html');
array_push($messages, $message);
}

$result = $this->mailjetTransport->bulkSend($messages);

if($countUsers > 0) {
$this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');
}
else {
$this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
}
}
else {
throw new NotFoundHttpException('Actualité introuvable') ;
}

return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
}

}

+ 5
- 3
ShopBundle/EventSubscriber/EasyAdminSubscriber.php View File

@@ -39,10 +39,12 @@ class EasyAdminSubscriber implements EventSubscriberInterface
$paginator->nbResultsOffline = $entityRepo->count(array('status'=>0,'merchant'=>$currentMerchant));
$paginator->nbResultsDeleted = $entityRepo->count(array('status'=>-1,'merchant'=>$currentMerchant));
}else{

$paginator->nbResultsOnline = $entityRepo->count(array('status'=>1));
/*$paginator->nbResultsOnline = $entityRepo->count(array('status'=>1));
$paginator->nbResultsOffline = $entityRepo->count(array('status'=>0));
$paginator->nbResultsDeleted = $entityRepo->count(array('status'=>-1));
$paginator->nbResultsDeleted = $entityRepo->count(array('status'=>-1));*/
$paginator->nbResultsOnline = $entityRepo->count([]);
$paginator->nbResultsOffline = $entityRepo->count([]);
$paginator->nbResultsDeleted = $entityRepo->count([]);
}
}


+ 31
- 0
ShopBundle/Model/News.php View File

@@ -0,0 +1,31 @@
<?php

namespace Lc\ShopBundle\Model;

use Doctrine\ORM\Mapping as ORM;
use Lc\ShopBundle\Model\AbstractDocumentEntity;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ORM\MappedSuperclass()
*/
abstract class News extends AbstractDocumentEntity
{
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
protected $date;

public function getDate(): ?\DateTimeInterface
{
return $this->date;
}

public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;

return $this;
}
}

+ 17
- 0
ShopBundle/Model/User.php View File

@@ -49,6 +49,11 @@ abstract class User extends UserModelFOS
*/
protected $carts;

/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $isSubscribedNewsletter;

public function __construct()
{
parent::__construct();
@@ -217,4 +222,16 @@ abstract class User extends UserModelFOS

return $this;
}

public function getIsSubscribedNewsletter(): ?bool
{
return $this->isSubscribedNewsletter;
}

public function setIsSubscribedNewsletter(?bool $isSubscribedNewsletter): self
{
$this->isSubscribedNewsletter = $isSubscribedNewsletter;

return $this;
}
}

+ 2
- 0
ShopBundle/Resources/views/backend/news/action_send.html.twig View File

@@ -0,0 +1,2 @@

<a class="btn btn-primary" href="{{ path('easyadmin', {entity: 'News', action: 'send', id: item.id}) }}"><i class="fa fa-paper-plane"></i> Envoyer</a>

Loading…
Cancel
Save