forked from EC-CUBE/Recommend-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginManager.php
239 lines (205 loc) · 6.6 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
232
233
234
235
236
237
238
239
<?php
/*
* This file is part of the Recommend Product plugin
*
* Copyright (C) 2016 LOCKON CO.,LTD. All Rights Reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Recommend;
use Eccube\Common\Constant;
use Eccube\Entity\Block;
use Eccube\Entity\BlockPosition;
use Eccube\Entity\Master\DeviceType;
use Eccube\Entity\PageLayout;
use Eccube\Plugin\AbstractPluginManager;
use Eccube\Util\Cache;
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 array $config
* @param \Eccube\Application $app
*/
public function install($config, $app)
{
}
/**
* @param array $config
* @param \Eccube\Application $app
*/
public function uninstall($config, $app)
{
// ブロックの削除
$this->removeDataBlock($app);
if (file_exists($app['config']['block_realdir'].'/'.$this->blockFileName.'.twig')) {
$this->removeBlock($app);
}
$this->migrationSchema($app, __DIR__.'/Resource/doctrine/migration', $config['code'], 0);
}
/**
* @param array $config
* @param \Eccube\Application $app
*/
public function enable($config, $app)
{
$this->copyBlock($app);
// ブロックへ登録
$this->createDataBlock($app);
$this->migrationSchema($app, __DIR__.'/Resource/doctrine/migration', $config['code']);
}
/**
* @param array $config
* @param \Eccube\Application $app
*/
public function disable($config, $app)
{
$this->removeBlock($app);
// ブロックの削除
$this->removeDataBlock($app);
}
/**
* @param array $config
* @param \Eccube\Application $app
*/
public function update($config, $app)
{
$this->copyBlock($app);
$this->migrationSchema($app, __DIR__.'/Resource/doctrine/migration', $config['code']);
}
/**
* ブロックを登録.
*
* @param \Eccube\Application $app
*
* @throws \Exception
*/
private function createDataBlock($app)
{
$em = $app['orm.em'];
try {
$DeviceType = $app['eccube.repository.master.device_type']->find(DeviceType::DEVICE_TYPE_PC);
// check exists block
/** @var Block $Block */
$Block = $app['eccube.repository.block']->findOneBy(array('DeviceType' => $DeviceType, 'file_name' => $this->blockFileName));
if (!$Block) {
/** @var Block $Block */
$Block = $app['eccube.repository.block']->findOrCreate(null, $DeviceType);
// Blockの登録
$Block->setName($this->blockName)
->setFileName($this->blockFileName)
->setDeletableFlg(Constant::DISABLED)
->setLogicFlg(1);
$em->persist($Block);
$em->flush($Block);
}
// check exists block position
$blockPos = $em->getRepository('Eccube\Entity\BlockPosition')->findOneBy(array('block_id' => $Block->getId()));
if ($blockPos) {
return;
}
// BlockPositionの登録
$blockPos = $em->getRepository('Eccube\Entity\BlockPosition')->findOneBy(
array('page_id' => 1, 'target_id' => PageLayout::TARGET_ID_MAIN_BOTTOM),
array('block_row' => 'DESC')
);
$BlockPosition = new BlockPosition();
// ブロックの順序を変更
$BlockPosition->setBlockRow(1);
if ($blockPos) {
$blockRow = $blockPos->getBlockRow() + 1;
$BlockPosition->setBlockRow($blockRow);
}
$PageLayout = $app['eccube.repository.page_layout']->find(1);
$BlockPosition->setPageLayout($PageLayout)
->setPageId($PageLayout->getId())
->setTargetId(PageLayout::TARGET_ID_MAIN_BOTTOM)
->setBlock($Block)
->setBlockId($Block->getId())
->setAnywhere(Constant::ENABLED);
$em->persist($BlockPosition);
$em->flush($BlockPosition);
} catch (\Exception $e) {
throw $e;
}
}
/**
* ブロックを削除.
*
* @param \Eccube\Application $app
*
* @throws \Exception
*/
private function removeDataBlock($app)
{
// Blockの取得(file_nameはアプリケーションの仕組み上必ずユニーク)
/** @var \Eccube\Entity\Block $Block */
$Block = $app['eccube.repository.block']->findOneBy(array('file_name' => $this->blockFileName));
if (!$Block) {
Cache::clear($app, false);
return;
}
$em = $app['orm.em'];
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;
}
Cache::clear($app, false);
}
/**
* Copy block template.
*
* @param $app
*/
private function copyBlock($app)
{
// ファイルコピー
$file = new Filesystem();
// ブロックファイルをコピー
$file->copy($this->originBlock, $app['config']['block_realdir'].'/'.$this->blockFileName.'.twig');
}
/**
* Remove block template.
*
* @param $app
*/
private function removeBlock($app)
{
$file = new Filesystem();
$file->remove($app['config']['block_realdir'].'/'.$this->blockFileName.'.twig');
}
}