@@ -3,14 +3,18 @@ | |||
namespace Lc\ShopBundle\Controller\Backend; | |||
use App\Repository\UserRepository; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use FOS\UserBundle\Model\UserManagerInterface; | |||
use Lc\ShopBundle\Context\NewsInterface; | |||
use Lc\ShopBundle\Context\UserInterface; | |||
use Lc\ShopBundle\Services\UtilsManager; | |||
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport; | |||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |||
use Symfony\Component\Security\Core\Security; | |||
use Symfony\Contracts\Translation\TranslatorInterface; | |||
class NewsController extends AdminController | |||
{ | |||
public function sendAction() | |||
{ | |||
$idNews = $this->request->get('id') ; | |||
@@ -36,9 +40,9 @@ class NewsController extends AdminController | |||
array_push($messages, $message); | |||
} | |||
$result = $this->mailjetTransport->bulkSend($messages); | |||
if($countUsers > 0) { | |||
$result = $this->mailjetTransport->bulkSend($messages); | |||
$this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.'); | |||
$news->setIsSent(true) ; |
@@ -28,5 +28,30 @@ class UserController extends AdminController | |||
parent::updateEntity($user); | |||
} | |||
public function removeEntity($user) | |||
{ | |||
// désactivation UserMerchant | |||
if($user->getUserMerchants()) { | |||
$merchant = $this->merchantUtils->getMerchantCurrent() ; | |||
foreach($user->getUserMerchants() as $userMerchant) { | |||
if($userMerchant->getMerchant() == $merchant) { | |||
$userMerchant->setActive(false) ; | |||
$this->em->persist($userMerchant); | |||
$this->em->flush() ; | |||
$this->addFlash('success', 'Utilisateur supprimé du hub'); | |||
} | |||
} | |||
} | |||
} | |||
protected function createUserListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null) | |||
{ | |||
$queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter) ; | |||
$queryBuilder->innerJoin('entity.userMerchants', 'um') ; | |||
$queryBuilder->andWhere('um.merchant = :merchant AND um.active = 1') ; | |||
$queryBuilder->setParameter('merchant', $this->merchantUtils->getMerchantCurrent()) ; | |||
return $queryBuilder ; | |||
} | |||
} |
@@ -42,6 +42,11 @@ abstract class UserMerchant implements FilterMerchantInterface | |||
*/ | |||
protected $creditHistories; | |||
/** | |||
* @ORM\Column(type="boolean") | |||
*/ | |||
protected $active; | |||
public function __construct() | |||
{ | |||
$this->creditHistories = new ArrayCollection(); | |||
@@ -130,4 +135,16 @@ abstract class UserMerchant implements FilterMerchantInterface | |||
return $this; | |||
} | |||
public function getActive(): ?bool | |||
{ | |||
return $this->active; | |||
} | |||
public function setActive(bool $active): self | |||
{ | |||
$this->active = $active; | |||
return $this; | |||
} | |||
} |
@@ -23,6 +23,10 @@ class UserRepository extends BaseRepository implements DefaultRepositoryInterfac | |||
return $this->createQueryBuilder('e') | |||
->where(':newsletter MEMBER OF e.newsletters') | |||
->setParameter('newsletter', $newsletter->getId()) | |||
->andWhere('e.enabled = 1') | |||
->innerJoin('e.userMerchants', 'um') | |||
->andWhere('um.merchant = :merchant AND um.active = 1') | |||
->setParameter('merchant', $newsletter->getMerchant()) | |||
->getQuery() | |||
->getResult(); | |||
} |
@@ -44,16 +44,13 @@ class CreditUtils | |||
public function createUserMerchant(UserInterface $user, MerchantInterface $merchant = null) | |||
{ | |||
$merchant = $this->getMerchant($merchant) ; | |||
$classUserMerchant = $this->em->getClassMetadata(UserMerchantInterface::class)->getName(); | |||
$userMerchant = new $classUserMerchant ; | |||
$userMerchant->setUser($user) ; | |||
$userMerchant->setMerchant($merchant) ; | |||
$userMerchant->setCredit(0) ; | |||
$userMerchant->setCreditActive(true) ; | |||
$this->em->persist($userMerchant) ; | |||
$this->em->flush() ; | |||
$userMerchant = $this->merchantUtils->initUserMerchant([ | |||
'user' => $user, | |||
'merchant' => $merchant, | |||
'credit' => 0, | |||
'credit_active' => true, | |||
]) ; | |||
return $userMerchant ; | |||
} |
@@ -2,7 +2,61 @@ | |||
namespace Lc\ShopBundle\Services ; | |||
use App\Entity\UserMerchant; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\ShopBundle\Context\UserMerchantInterface; | |||
use Symfony\Component\Security\Core\Security; | |||
class MerchantUtils | |||
{ | |||
protected $security ; | |||
protected $em ; | |||
public function __construct(Security $security, EntityManagerInterface $em) | |||
{ | |||
$this->security = $security ; | |||
$this->em = $em ; | |||
} | |||
public function initUserMerchant($params = []) | |||
{ | |||
$classUserMerchant = $this->em->getClassMetadata(UserMerchantInterface::class)->getName() ; | |||
$userMerchantRepository = $this->em->getRepository($classUserMerchant) ; | |||
$user = isset($params['user']) ? $params['user'] : null ; | |||
$merchant = isset($params['merchant']) ? $params['merchant'] : null ; | |||
$active = isset($params['active']) ? $params['active'] : true ; | |||
$flush = isset($params['flush']) ? $params['flush'] : true ; | |||
$userMerchant = $userMerchantRepository->findOneBy([ | |||
'user' => $user, | |||
'merchant' => $merchant | |||
]) ; | |||
if(!$userMerchant) { | |||
$userMerchant = new $classUserMerchant ; | |||
$userMerchant->setUser($user) ; | |||
$userMerchant->setMerchant($merchant) ; | |||
} | |||
$userMerchant->setActive($active) ; | |||
if(isset($params['credit_active'])) { | |||
$userMerchant->setCreditActive($params['credit_active']) ; | |||
} | |||
if(isset($params['credit'])) { | |||
$userMerchant->setCredit($params['credit']) ; | |||
} | |||
$this->em->persist($userMerchant); | |||
if($flush) { | |||
$this->em->flush() ; | |||
} | |||
return $userMerchant ; | |||
} | |||
} |