<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Controller\Admin\School;
use Eccube\Controller\AbstractController;
use Eccube\Entity\Page;
use Eccube\Entity\PageLayout;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Customize\Form\Type\Admin\ShopGuideEditType;
use Eccube\Repository\Master\DeviceTypeRepository;
use Eccube\Repository\PageLayoutRepository;
use Eccube\Repository\PageRepository;
use Eccube\Util\CacheUtil;
use Eccube\Util\StringUtil;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
use Customize\Repository\SchoolRepository;
class ShopGuideController extends AbstractController
{
/**
* @var PageRepository
*/
protected $pageRepository;
/**
* @var PageLayoutRepository
*/
protected $pageLayoutRepository;
/**
* @var DeviceTypeRepository
*/
protected $deviceTypeRepository;
/**
* @var SchoolRepository
*/
protected $schoolRepository;
/**
* PageController constructor.
*
* @param PageRepository $pageRepository
* @param DeviceTypeRepository $deviceTypeRepository
*/
public function __construct(
PageRepository $pageRepository,
PageLayoutRepository $pageLayoutRepository,
DeviceTypeRepository $deviceTypeRepository,
SchoolRepository $schoolRepository
) {
$this->pageRepository = $pageRepository;
$this->pageLayoutRepository = $pageLayoutRepository;
$this->deviceTypeRepository = $deviceTypeRepository;
$this->schoolRepository = $schoolRepository;
}
/**
* @Route("/%eccube_admin_route%/school/shop_guide/{id}/edit/{page_id}", requirements={"id" = "\d+"}, requirements={"page_id" = "\d+"},name="admin_school_shop_guide_edit", methods={"GET", "POST"})
* @Template("@admin/School/shop_guide.twig")
*/
public function edit(Request $request, $id = null, Environment $twig, CacheUtil $cacheUtil, $page_id = null)
{
$Page = $this->pageRepository->newPage();
$fs = new Filesystem();
if($page_id == '4'){
$guide_file_name = 'Block/School/delivery_guide'.$id;
$org_file_name = 'Block/delivery_guide';
}
else{
$guide_file_name = 'Help/School/guide'.$id;
$org_file_name = 'Help/guide';
}
$templatePath = $this->getParameter('eccube_theme_front_dir');
$guideFilePath = $templatePath.'/'.$guide_file_name.'.twig';
if (!$fs->exists($guideFilePath)) {
$fs->copy($templatePath.'/'.$org_file_name.'.twig', $guideFilePath);
}
$arr_section_file_name = ['payment_method', 'product_delivery', 'incorrect_size', '', 'order_flow'];
if($page_id != '4')
{
$org_file_name1 = 'Help/guide_'.$arr_section_file_name[$page_id - 1];
$guide_file_name1 = 'Help/School/guide_'.$arr_section_file_name[$page_id - 1].$id;
$guideFilePath1 = $templatePath.'/'.$guide_file_name1.'.twig';
if (!$fs->exists($guideFilePath1)) {
$fs->copy($templatePath.'/'.$org_file_name1.'.twig', $guideFilePath1);
}
$guide_file_name = $guide_file_name1;
}
$guideFilePath = $templatePath.'/'.$guide_file_name.'.twig';
$Page->setFileName($guide_file_name);
$isUserDataPage = false;
$builder = $this->formFactory
->createBuilder(ShopGuideEditType::class, $Page);
$event = new EventArgs(
[
'builder' => $builder,
'Page' => $Page,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::ADMIN_SCHOOL_SHOP_GUIDE_EDIT_INITIALIZE);
$form = $builder->getForm();
// 更新時
$fileName = null;
$source = $twig->getLoader()
->getSourceContext($guide_file_name.'.twig')
->getCode();
$form->get('tpl_data')->setData($source);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$Page = $form->getData();
$pageData = $form->get('tpl_data')->getData();
$pageData = StringUtil::convertLineFeed($pageData);
$fs->dumpFile($guideFilePath, $pageData);
$event = new EventArgs(
[
'form' => $form,
'Page' => $Page,
'templatePath' => $templatePath,
'filePath' => $guideFilePath,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::ADMIN_SCHOOL_SHOP_GUIDE_EDIT_COMPLETE);
$this->addSuccess('admin.common.save_complete', 'admin');
// キャッシュの削除
$cacheUtil->clearTwigCache();
$cacheUtil->clearDoctrineCache();
return $this->redirectToRoute('admin_school_shop_guide_edit', ['id' => $id, 'page_id' => $page_id]);
}
$url = '';
$projectDir = $this->getParameter('kernel.project_dir');
$templatePath = str_replace($projectDir.'/', '', $templatePath);
return [
'form' => $form->createView(),
'page_id' => $Page->getId(),
'is_user_data_page' => $isUserDataPage,
'is_confirm_page' => $Page->getEditType() == Page::EDIT_TYPE_DEFAULT_CONFIRM,
'template_path' => $templatePath,
'url' => $url,
'school_id' => $id,
'page_id' => $page_id,
'School' => $this->schoolRepository->find($id)
];
}
/**
* @Route("/%eccube_admin_route%/school/shop_guide/{id}/restore/{page_id}", requirements={"id" = "\d+"}, requirements={"page_id" = "\d+"},name="admin_school_shop_guide_restore", methods={"GET"})
*
*/
public function restore(Request $request, $id = null, $page_id = null, Environment $twig, RouterInterface $router, CacheUtil $cacheUtil)
{
$Page = $this->pageRepository->newPage();
$fs = new Filesystem();
$arr_section_file_name = ['payment_method', 'product_delivery', 'incorrect_size', '', 'order_flow'];
if($page_id == '4'){
$guide_file_name = 'Block/School/delivery_guide'.$id;
$org_file_name = 'Block/delivery_guide';
}
else{
$guide_file_name = 'Help/School/guide_'.$arr_section_file_name[$page_id - 1].$id;
$org_file_name = 'Help/guide_'.$arr_section_file_name[$page_id - 1];
}
$templatePath = $this->getParameter('eccube_theme_front_dir');
$guideFilePath = $templatePath.'/'.$guide_file_name.'.twig';
if ($fs->exists($guideFilePath)) {
$fs->remove($guideFilePath);
$fs->copy($templatePath.'/'.$org_file_name.'.twig', $guideFilePath);
}
$event = new EventArgs(
[
'Page' => $Page,
'templatePath' => $templatePath,
'filePath' => $guideFilePath,
],
$request
);
$this->eventDispatcher->dispatch($event, EccubeEvents::ADMIN_SCHOOL_SHOP_GUIDE_RESTORE_COMPLETE);
$this->addSuccess('admin.common.save_complete', 'admin');
// キャッシュの削除
$cacheUtil->clearTwigCache();
$cacheUtil->clearDoctrineCache();
return $this->redirectToRoute('admin_school_shop_guide_edit', ['id' => $id, 'page_id' => $page_id]);
}
}