forked from EC-CUBE/coupon-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginManager.php
157 lines (140 loc) · 5.18 KB
/
PluginManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?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 Plugin\Coupon4;
use Eccube\Entity\Layout;
use Eccube\Entity\Page;
use Eccube\Plugin\AbstractPluginManager;
use Eccube\Entity\PageLayout;
use Eccube\Repository\LayoutRepository;
use Eccube\Repository\PageLayoutRepository;
use Eccube\Repository\PageRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Class PluginManager.
*/
class PluginManager extends AbstractPluginManager
{
private $originalDir = __DIR__.'/Resource/template/default/';
private $template1 = 'coupon_shopping_item.twig';
private $template2 = 'coupon_shopping_item_confirm.twig';
private $template3 = 'mypage_history_coupon.twig';
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function enable(array $meta, ContainerInterface $container)
{
$this->copyBlock($container);
$PageLayout = $container->get(PageRepository::class)->findOneBy(['url' => 'plugin_coupon_shopping']);
if (is_null($PageLayout)) {
// pagelayoutの作成
$this->createPageLayout($container);
}
}
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function disable(array $meta, ContainerInterface $container)
{
$this->removeBlock($container);
// pagelayoutの削除
$this->removePageLayout($container);
}
/**
* @param array $meta
* @param ContainerInterface $container
*/
public function update(array $meta, ContainerInterface $container)
{
$PageLayout = $container->get(PageRepository::class)->findOneBy(['url' => 'plugin_coupon_shopping']);
if (is_null($PageLayout)) {
// pagelayoutの作成
$this->createPageLayout($container);
}
}
/**
* @param ContainerInterface $container
*/
private function createPageLayout(ContainerInterface $container)
{
// ページレイアウトにプラグイン使用時の値を代入
/** @var \Eccube\Entity\Page $Page */
$Page = $container->get(PageRepository::class)->newPage();
$Page->setEditType(Page::EDIT_TYPE_DEFAULT);
$Page->setName('商品購入/クーポン利用');
$Page->setUrl('plugin_coupon_shopping');
$Page->setFileName('Coupon4/Resource/template/default/shopping_coupon');
$Page->setMetaRobots('noindex');
// DB登録
$entityManager = $container->get('doctrine')->getManager();
$entityManager->persist($Page);
$entityManager->flush($Page);
$Layout = $container->get(LayoutRepository::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = new PageLayout();
$PageLayout->setPage($Page)
->setPageId($Page->getId())
->setLayout($Layout)
->setLayoutId($Layout->getId())
->setSortNo(0);
$entityManager->persist($PageLayout);
$entityManager->flush($PageLayout);
}
/**
* クーポン用ページレイアウトを削除.
*
* @param ContainerInterface $container
*/
private function removePageLayout(ContainerInterface $container)
{
// ページ情報の削除
$Page = $container->get(PageRepository::class)->findOneBy(['url' => 'plugin_coupon_shopping']);
if ($Page) {
$Layout = $container->get(LayoutRepository::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = $container->get(PageLayoutRepository::class)->findOneBy(['Page' => $Page, 'Layout' => $Layout]);
// Blockの削除
$entityManager = $container->get('doctrine')->getManager();
$entityManager->remove($PageLayout);
$entityManager->remove($Page);
$entityManager->flush();
}
}
/**
* Copy block template.
*
* @param ContainerInterface $container
*/
private function copyBlock(ContainerInterface $container)
{
$templateDir = $container->getParameter('eccube_theme_front_dir');
// ファイルコピー
$file = new Filesystem();
// ブロックファイルをコピー
$file->copy($this->originalDir.$this->template1, $templateDir.'/Coupon4/'.$this->template1);
$file->copy($this->originalDir.$this->template2, $templateDir.'/Coupon4/'.$this->template2);
$file->copy($this->originalDir.$this->template3, $templateDir.'/Coupon4/'.$this->template3);
}
/**
* Remove block template.
*
* @param ContainerInterface $container
*/
private function removeBlock(ContainerInterface $container)
{
$templateDir = $container->getParameter('eccube_theme_front_dir');
$file = new Filesystem();
$file->remove($templateDir.'/Coupon4/'.$this->template1);
$file->remove($templateDir.'/Coupon4/'.$this->template2);
$file->remove($templateDir.'/Coupon4/'.$this->template3);
}
}