-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathPluginManager.php
165 lines (148 loc) · 5.65 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
158
159
160
161
162
163
164
165
<?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\Coupon42;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\Layout;
use Eccube\Entity\Page;
use Eccube\Entity\PageLayout;
use Eccube\Plugin\AbstractPluginManager;
use Eccube\Repository\LayoutRepository;
use Eccube\Repository\PageLayoutRepository;
use Eccube\Repository\PageRepository;
use Psr\Container\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);
/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get('doctrine')->getManager();
$PageLayout = $entityManager->getRepository(Page::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)
{
/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get('doctrine')->getManager();
$PageLayout = $entityManager->getRepository(Page::class)->findOneBy(['url' => 'plugin_coupon_shopping']);
if (is_null($PageLayout)) {
// pagelayoutの作成
$this->createPageLayout($container);
}
}
/**
* @param ContainerInterface $container
*/
private function createPageLayout(ContainerInterface $container)
{
// ページレイアウトにプラグイン使用時の値を代入
/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get('doctrine')->getManager();
/** @var \Eccube\Entity\Page $Page */
$Page = $entityManager->getRepository(Page::class)->newPage();
$Page->setEditType(Page::EDIT_TYPE_DEFAULT);
$Page->setName('商品購入/クーポン利用');
$Page->setUrl('plugin_coupon_shopping');
$Page->setFileName('Coupon42/Resource/template/default/shopping_coupon');
$Page->setMetaRobots('noindex');
// DB登録
$entityManager->persist($Page);
$entityManager->flush($Page);
$Layout = $entityManager->getRepository(Layout::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)
{
// ページ情報の削除
/** @var EntityManagerInterface $entityManager */
$entityManager = $container->get('doctrine')->getManager();
$Page = $entityManager->getRepository(Page::class)->findOneBy(['url' => 'plugin_coupon_shopping']);
if ($Page) {
$Layout = $entityManager->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$PageLayout = $entityManager->getRepository(PageLayout::class)->findOneBy(['Page' => $Page, 'Layout' => $Layout]);
// Blockの削除
$entityManager->remove($PageLayout);
$entityManager->remove($Page);
$entityManager->flush();
}
}
/**
* Copy block template.
*
* @param ContainerInterface $container
*/
private function copyBlock(ContainerInterface $container)
{
$templateDir = $container->get(EccubeConfig::class)->get('eccube_theme_front_dir');
// ファイルコピー
$file = new Filesystem();
// ブロックファイルをコピー
$file->copy($this->originalDir.$this->template1, $templateDir.'/Coupon42/'.$this->template1);
$file->copy($this->originalDir.$this->template2, $templateDir.'/Coupon42/'.$this->template2);
$file->copy($this->originalDir.$this->template3, $templateDir.'/Coupon42/'.$this->template3);
}
/**
* Remove block template.
*
* @param ContainerInterface $container
*/
private function removeBlock(ContainerInterface $container)
{
$templateDir = $container->get(EccubeConfig::class)->get('eccube_theme_front_dir');
$file = new Filesystem();
$file->remove($templateDir.'/Coupon42/'.$this->template1);
$file->remove($templateDir.'/Coupon42/'.$this->template2);
$file->remove($templateDir.'/Coupon42/'.$this->template3);
}
}