-
Notifications
You must be signed in to change notification settings - Fork 19
/
PluginManager.php
231 lines (203 loc) · 6.73 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?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\Recommend42;
use Eccube\Common\EccubeConfig;
use Eccube\Application;
use Eccube\Entity\Block;
use Eccube\Entity\BlockPosition;
use Eccube\Entity\Layout;
use Eccube\Entity\Master\DeviceType;
use Eccube\Plugin\AbstractPluginManager;
use Eccube\Repository\BlockPositionRepository;
use Eccube\Repository\BlockRepository;
use Eccube\Repository\LayoutRepository;
use Eccube\Repository\Master\DeviceTypeRepository;
use Psr\Container\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Class PluginManager.
*/
class PluginManager extends AbstractPluginManager
{
/**
* @var string コピー元ブロックファイル
*/
private $originBlock;
/**
* @var string ブロック名
*/
private $blockName = 'おすすめ商品';
/**
* @var string ブロックファイル名
*/
private $blockFileName = 'recommend_product_block';
/**
* PluginManager constructor.
*/
public function __construct()
{
// コピー元ブロックファイル
$this->originBlock = __DIR__.'/Resource/template/Block/'.$this->blockFileName.'.twig';
}
/**
* @param null $meta
* @param Application|null $app
* @param ContainerInterface $container
*
* @throws \Exception
*/
public function uninstall(array $meta, ContainerInterface $container)
{
// ブロックの削除
$this->removeDataBlock($container);
$this->removeBlock($container);
}
/**
* @param array|null $meta
* @param ContainerInterface $container
*
* @throws \Exception
*/
public function enable(array $meta = null, ContainerInterface $container)
{
$entityManager = $container->get('doctrine')->getManager();
$this->copyBlock($container);
$Block = $entityManager->getRepository(Block::class)->findOneBy(['file_name' => $this->blockFileName]);
if (is_null($Block)) {
// pagelayoutの作成
$this->createDataBlock($container);
}
}
/**
* @param array|null $meta
* @param ContainerInterface $container
*/
public function disable(array $meta = null, ContainerInterface $container)
{
$this->removeDataBlock($container);
}
/**
* @param array|null $meta
* @param ContainerInterface $container
*/
public function update(array $meta = null, ContainerInterface $container)
{
$this->copyBlock($container);
}
/**
* ブロックを登録.
*
* @param ContainerInterface $container
*
* @throws \Exception
*/
private function createDataBlock(ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
$DeviceType = $em->getRepository(DeviceType::class)->find(DeviceType::DEVICE_TYPE_PC);
try {
/** @var Block $Block */
$Block = $em->getRepository(Block::class)->newBlock($DeviceType);
// Blockの登録
$Block->setName($this->blockName)
->setFileName($this->blockFileName)
->setUseController(false)
->setDeletable(false);
$em->persist($Block);
$em->flush($Block);
// check exists block position
$blockPos = $em->getRepository(BlockPosition::class)->findOneBy(['Block' => $Block]);
if ($blockPos) {
return;
}
// BlockPositionの登録
$blockPos = $em->getRepository(BlockPosition::class)->findOneBy(
['section' => Layout::TARGET_ID_MAIN_BOTTOM, 'layout_id' => Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE],
['block_row' => 'DESC']
);
$BlockPosition = new BlockPosition();
// ブロックの順序を変更
$BlockPosition->setBlockRow(1);
if ($blockPos) {
$blockRow = $blockPos->getBlockRow() + 1;
$BlockPosition->setBlockRow($blockRow);
}
$LayoutDefault = $em->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$BlockPosition->setLayout($LayoutDefault)
->setLayoutId($LayoutDefault->getId())
->setSection(Layout::TARGET_ID_MAIN_BOTTOM)
->setBlock($Block)
->setBlockId($Block->getId());
$em->persist($BlockPosition);
$em->flush($BlockPosition);
} catch (\Exception $e) {
throw $e;
}
}
/**
* ブロックを削除.
*
* @param ContainerInterface $container
*
* @throws \Exception
*/
private function removeDataBlock(ContainerInterface $container)
{
$em = $container->get('doctrine')->getManager();
// Blockの取得(file_nameはアプリケーションの仕組み上必ずユニーク)
/** @var \Eccube\Entity\Block $Block */
$Block = $em->getRepository(Block::class)->findOneBy(['file_name' => $this->blockFileName]);
if (!$Block) {
return;
}
try {
// BlockPositionの削除
$blockPositions = $Block->getBlockPositions();
/** @var \Eccube\Entity\BlockPosition $BlockPosition */
foreach ($blockPositions as $BlockPosition) {
$Block->removeBlockPosition($BlockPosition);
$em->remove($BlockPosition);
}
// Blockの削除
$em->remove($Block);
$em->flush();
} catch (\Exception $e) {
throw $e;
}
}
/**
* Copy block template.
*
* @param ContainerInterface $container
*/
private function copyBlock(ContainerInterface $container)
{
$templateDir = $container->get(EccubeConfig::class)->get('eccube_theme_front_dir');
// ファイルコピー
$file = new Filesystem();
if (!$file->exists($templateDir.'/Block/'.$this->blockFileName.'.twig')) {
// ブロックファイルをコピー
$file->copy($this->originBlock, $templateDir.'/Block/'.$this->blockFileName.'.twig');
}
}
/**
* 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.'/Block/'.$this->blockFileName.'.twig');
}
}