namespace Lc\ShopBundle\Controller\Backend; | namespace Lc\ShopBundle\Controller\Backend; | ||||
use App\Repository\UserRepository; | use App\Repository\UserRepository; | ||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use FOS\UserBundle\Model\UserManagerInterface; | |||||
use Lc\ShopBundle\Context\NewsInterface; | use Lc\ShopBundle\Context\NewsInterface; | ||||
use Lc\ShopBundle\Context\UserInterface; | use Lc\ShopBundle\Context\UserInterface; | ||||
use Lc\ShopBundle\Services\UtilsManager; | |||||
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport; | use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport; | ||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||||
use Symfony\Component\Security\Core\Security; | |||||
use Symfony\Contracts\Translation\TranslatorInterface; | |||||
class NewsController extends AdminController | class NewsController extends AdminController | ||||
{ | { | ||||
public function sendAction() | public function sendAction() | ||||
{ | { | ||||
$idNews = $this->request->get('id') ; | $idNews = $this->request->get('id') ; | ||||
array_push($messages, $message); | array_push($messages, $message); | ||||
} | } | ||||
$result = $this->mailjetTransport->bulkSend($messages); | |||||
if($countUsers > 0) { | if($countUsers > 0) { | ||||
$result = $this->mailjetTransport->bulkSend($messages); | |||||
$this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.'); | $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.'); | ||||
$news->setIsSent(true) ; | $news->setIsSent(true) ; |
parent::updateEntity($user); | 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 ; | |||||
} | |||||
} | } |
*/ | */ | ||||
protected $creditHistories; | protected $creditHistories; | ||||
/** | |||||
* @ORM\Column(type="boolean") | |||||
*/ | |||||
protected $active; | |||||
public function __construct() | public function __construct() | ||||
{ | { | ||||
$this->creditHistories = new ArrayCollection(); | $this->creditHistories = new ArrayCollection(); | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getActive(): ?bool | |||||
{ | |||||
return $this->active; | |||||
} | |||||
public function setActive(bool $active): self | |||||
{ | |||||
$this->active = $active; | |||||
return $this; | |||||
} | |||||
} | } |
return $this->createQueryBuilder('e') | return $this->createQueryBuilder('e') | ||||
->where(':newsletter MEMBER OF e.newsletters') | ->where(':newsletter MEMBER OF e.newsletters') | ||||
->setParameter('newsletter', $newsletter->getId()) | ->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() | ->getQuery() | ||||
->getResult(); | ->getResult(); | ||||
} | } |
public function createUserMerchant(UserInterface $user, MerchantInterface $merchant = null) | public function createUserMerchant(UserInterface $user, MerchantInterface $merchant = null) | ||||
{ | { | ||||
$merchant = $this->getMerchant($merchant) ; | $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 ; | return $userMerchant ; | ||||
} | } |
namespace Lc\ShopBundle\Services ; | 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 | 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 ; | |||||
} | |||||
} | } |