<?php
/*
* Plugin Name : DeliveryFreeProduct
*
* Copyright (C) 2016 BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\DeliveryFreeProduct;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Cart;
use Eccube\Entity\CartItem;
use Eccube\Event\EccubeEvents;
use Eccube\Event\TemplateEvent;
use Eccube\Exception\CsvImportException;
use Eccube\Event\EventArgs;
use Eccube\Util\EntityUtil;
use Plugin\DeliveryFreeProduct\Entity\DeliveryFreeConfig;
use Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct;
use Plugin\DeliveryFreeProduct\Repository\ProductRepository;
use Plugin\DeliveryFreeProduct\Service\UtilService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DeliveryFreeProductEvent implements EventSubscriberInterface
{
/** @var ProductRepository */
private $freeProductRepo;
/** @var EntityManagerInterface */
private $em;
/** @var UtilService */
private $deliveryFreeService;
/**
* DeliveryFreeProductEvent constructor.
* @param ProductRepository $freeProductRepo
* @param EntityManagerInterface $em
* @param UtilService $deliveryFreeService
*/
public function __construct(ProductRepository $freeProductRepo, EntityManagerInterface $em, UtilService $deliveryFreeService)
{
$this->freeProductRepo = $freeProductRepo;
$this->em = $em;
$this->deliveryFreeService = $deliveryFreeService;
}
public static function getSubscribedEvents()
{
return [
// admin
EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'hookAdminProductEditComplete',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'hookAdminProductCopyComplete',
EccubeEvents::ADMIN_PRODUCT_CSV_EXPORT => 'hookAdminProductCsvExport',
// front
'Cart/index.twig' => 'onRenderCart',
'csvimportproductext.admin.product.csv.import.product.descriptions' => 'hookAdminProductCsvImportProductDescriptions',
'csvimportproductext.admin.product.csv.import.product.check'=> 'hookAdminProductCsvImportProductCheck',
'csvimportproductext.admin.product.csv.import.product.process' => 'hookAdminProductCsvImportProductProcess',
'deliverycool.delivery_free.condition' => 'hookDeliveryCoolDeliveryFree',
];
}
public function onRenderCart(TemplateEvent $event)
{
$parameters = $event->getParameters();
/** @var Cart[] $Carts */
$Carts = $parameters['Carts'];
$arrDeliveryFree = [];
$originalDeliveryFree = $parameters['is_delivery_free'];
$condition = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
$needAddTemplate = false;
$isCartFree = [];
foreach ($Carts as $key => $Cart) {
// default
$arrDeliveryFree[$Cart->getCartKey()] = $originalDeliveryFree[$Cart->getCartKey()];
if(!$originalDeliveryFree[$Cart->getCartKey()]) {
$count = 0;
$total = 0;
/** @var CartItem $cart_item */
foreach ($Cart->getCartItems() as $cart_item) {
if (!$cart_item->isProduct()) {
continue;
}
$total++;
$plgProducts = $this->freeProductRepo->findBy(['Product' => $cart_item->getProductClass()->getProduct()]);
if($plgProducts)$count++;
}
$deliv_free_flg = false;
if ($condition == DeliveryFreeConfig::MUST_ALL) {
if ($total == $count) {
$deliv_free_flg = true;
$needAddTemplate = true;
}
} else {
if ($count > 0) {
$deliv_free_flg = true;
$needAddTemplate = true;
}
}
$arrDeliveryFree[$Cart->getCartKey()] = $deliv_free_flg;
$isCartFree[$key] = $deliv_free_flg;
}
}
$parameters['is_delivery_free'] = $arrDeliveryFree;
$parameters['is_cart_free'] = $isCartFree;
$event->setParameters($parameters);
if ($needAddTemplate) {
$event->addSnippet("@DeliveryFreeProduct/default/Cart/delivery_free_message.twig");
}
}
public function hookAdminProductEditComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$form = $event->getArgument('form');
$value = $form->get('plg_deliveryfreeproduct')->getData();
$plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
if (EntityUtil::isNotEmpty($plgProduct)) {
$this->em->remove($plgProduct);
$this->em->flush();
}
if (count($value) > 0) {
$plgProduct = new \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct();
$plgProduct->setProduct($Product);
$this->em->persist($plgProduct);
$this->em->flush($plgProduct);
}
}
public function hookAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument('Product');
$CopyProduct = $event->getArgument('CopyProduct');
$plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
if($plgProduct instanceof \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct){
$copyPlgProduct = new \Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct();
$copyPlgProduct->setProduct($CopyProduct);
$this->em->persist($copyPlgProduct);
$this->em->flush($copyPlgProduct);
}
}
public function hookAdminProductCsvExport(EventArgs $event)
{
$ExportCsvRow = $event->getArgument('ExportCsvRow');
if ($ExportCsvRow->isDataNull()) {
$ProductClass = $event->getArgument('ProductClass');
$Product = $ProductClass->getProduct();
$Csv = $event->getArgument('Csv');
$csvEntityName = str_replace('\\\\', '\\', $Csv->getEntityName());
if ($csvEntityName == 'Plugin\DeliveryFreeProduct\Entity\DeliveryFreeProduct') {
$plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
if (!is_null($plgProduct)) {
$value = 1;
} else {
$value = 0;
}
$ExportCsvRow->setData($value);
}
}
}
public function hookAdminProductCsvImportProductDescriptions(EventArgs $event)
{
$header = $event->getArgument('header');
$key = $event->getArgument('key');
if($key == trans('delivery_free_product.product.csv.title')){
$header['description'] = trans('delivery_free_product.admin.product.product_csv.delivery_free_flag_description');
$header['required'] = false;
}
$event->setArgument('header',$header);
}
public function hookAdminProductCsvImportProductCheck(EventArgs $event)
{
$row = $event->getArgument('row');
$data = $event->getArgument('data');
$errors = $event->getArgument('errors');
if(isset($row[trans('delivery_free_product.product.csv.title')])){
if($row[trans('delivery_free_product.product.csv.title')] !== '' && $row[trans('delivery_free_product.product.csv.title')] != 0 && $row[trans('delivery_free_product.product.csv.title')] != 1){
$message = trans('delivery_free_product.product.product_csv.not_correct', [
'%line%' => $data->key() + 1,
'%name%' => trans('delivery_free_product.product.csv.title'),
]);
$errors[] = $message;
}
}
$event->setArgument('errors',$errors);
}
public function hookAdminProductCsvImportProductProcess(EventArgs $event)
{
$row = $event->getArgument('row');
$data = $event->getArgument('data');
$ProductClass = $event->getArgument('ProductClass');
if(isset($row[trans('delivery_free_product.product.csv.title')])){
$Product = $ProductClass->getProduct();
$plgProduct = $this->freeProductRepo->findOneBy(['Product' => $Product]);
if(!is_null($plgProduct)){
$this->em->remove($plgProduct);
$this->em->flush($plgProduct);
}
if($row[trans('delivery_free_product.product.csv.title')] == 1){
$plgProduct = new DeliveryFreeProduct();
$plgProduct->setProduct($Product);
$this->em->persist($plgProduct);
}
}
}
public function hookDeliveryCoolDeliveryFree(EventArgs $event)
{
$isFree = $event->getArgument('isFree');
$Shipping = $event->getArgument('Shipping');
$range = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::RANGE_NAME);
$condition = $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
if($range == DeliveryFreeConfig::BY_SHIPPING){
$count = 0;
$total = 0;
foreach($Shipping->getProductOrderItems() as $orderItem){
$total++;
$plgProducts = $this->freeProductRepo->findBy(['Product' => $orderItem->getProduct()]);
if($plgProducts)$count++;
}
if($condition == DeliveryFreeConfig::MUST_ALL){
if($total == $count)$isFree = true;
}else{
if($count > 0)$isFree = true;
}
}elseif($range == DeliveryFreeConfig::BY_ALL){
$Order = $Shipping->getOrder();
$count = 0;
$total = 0;
/** @var OrderItem $orderDetail */
foreach($Order->getProductOrderItems() as $orderItem){
$total++;
$plgProducts = $this->freeProductRepo->findBy(['Product' => $orderItem->getProduct()]);
if($plgProducts)$count++;
}
if($condition == DeliveryFreeConfig::MUST_ALL){
if($total == $count)$isFree = true;
}else{
if($count > 0)$isFree = true;
}
}
$event->setArgument('isFree',$isFree);
}
}