-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPluginManager.php
188 lines (173 loc) · 5.63 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
<?php
/**
* Author: lqdung1992@gmail.com
* Date: 1/29/2019
* Time: 2:52 PM
*/
namespace Plugin\GHNDelivery;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Delivery;
use Eccube\Entity\DeliveryFee;
use Eccube\Entity\Layout;
use Eccube\Entity\Master\SaleType;
use Eccube\Entity\Page;
use Eccube\Entity\PageLayout;
use Eccube\Entity\Payment;
use Eccube\Entity\PaymentOption;
use Eccube\Plugin\AbstractPluginManager;
use Eccube\Repository\Master\PrefRepository;
use Plugin\GHNDelivery\Entity\GHNConfig;
use Plugin\GHNDelivery\Entity\GHNDelivery;
use Plugin\GHNDelivery\Repository\GHNConfigRepository;
use Plugin\GHNDelivery\Repository\GHNDeliveryRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class PluginManager
* @package Plugin\GHNDelivery
*/
class PluginManager extends AbstractPluginManager
{
/**
* Install the plugin.
*
* @param array $meta
* @param ContainerInterface $container
*/
public function install(array $meta, ContainerInterface $container)
{
/** @var EntityManagerInterface $em */
$em = $container->get('doctrine.orm.entity_manager');
$this->loadFixturesGHNPref($em);
$this->setupGHNDelivery($container, $em);
$this->setupPageLayout($em);
// flush all
$em->flush();
}
/**
* Update the plugin.
*
* @param array $meta
* @param ContainerInterface $container
*/
public function update(array $meta, ContainerInterface $container)
{
}
/**
* Enable the plugin.
*
* @param array $meta
* @param ContainerInterface $container
*/
public function enable(array $meta, ContainerInterface $container)
{
}
/**
* Disable the plugin.
*
* @param array $meta
* @param ContainerInterface $container
*/
public function disable(array $meta, ContainerInterface $container)
{
}
/**
* Uninstall the plugin.
*
* @param array $meta
* @param ContainerInterface $container
*/
public function uninstall(array $meta, ContainerInterface $container)
{
/** @var EntityManagerInterface $em */
$em = $container->get('doctrine.orm.entity_manager');
$configRepo = $container->get(GHNDeliveryRepository::class);
/** @var GHNDelivery[] $config */
$config = $configRepo->findAll();
foreach ($config as $GHNDelivery) {
foreach ($GHNDelivery->getDelivery()->getDeliveryFees() as $deliveryFee) {
$em->remove($deliveryFee);
}
$delivery = $GHNDelivery->getDelivery();
$delivery->setVisible(false);
$delivery->setName("Giao hàng nhanh - Đã xóa");
$em->remove($GHNDelivery);
$em->persist($delivery);
}
$em->flush();
}
/**
* @param $em
*/
private function loadFixturesGHNPref($em): void
{
// setup giao hang nhanh province
$loader = new \Eccube\Doctrine\Common\CsvDataFixtures\Loader();
$loader->loadFromDirectory(__DIR__ . '/Resource/doctrine/import_csv/');
$executor = new \Eccube\Doctrine\Common\CsvDataFixtures\Executor\DbalExecutor($em);
$fixtures = $loader->getFixtures();
$executor->execute($fixtures);
}
/**
* @param ContainerInterface $container
* @param $em
*/
private function setupGHNDelivery(ContainerInterface $container, EntityManagerInterface $em): void
{
// setup GHN delivery
$saleType = $em->getRepository(SaleType::class)->find(SaleType::SALE_TYPE_NORMAL);
$paymentMethods = $em->getRepository(Payment::class)->findAll();
$delivery = new Delivery();
$delivery->setName('Giao hàng nhanh')
->setSaleType($saleType)
->setServiceName('GHN')
->setVisible(true)
->setConfirmUrl("https://giaohangnhanh.vn");
$em->persist($delivery);
$em->flush($delivery);
foreach ($paymentMethods as $paymentMethod) {
$method = new PaymentOption();
$method->setDelivery($delivery)
->setDeliveryId($delivery->getId())
->setPayment($paymentMethod)
->setPaymentId($paymentMethod->getId());
$em->persist($method);
}
// save delivery id
$configDelivery = new GHNDelivery();
$configDelivery->setDelivery($delivery);
$em->persist($configDelivery);
// setup delivery fee - all zero
$prefRepo = $container->get(PrefRepository::class);
$allPrefs = $prefRepo->findAll();
foreach ($allPrefs as $pref) {
$GHNFee = new DeliveryFee();
$GHNFee->setPref($pref)
->setDelivery($delivery)
->setFee(0);
$em->persist($GHNFee);
}
}
/**
* @param $em
*/
private function setupPageLayout(EntityManagerInterface $em): void
{
// add to layout
$page = new Page();
$page->setName("Giao hàng nhanh - Tính phí")
->setUrl('ghn_delivery_shopping')
->setFileName("@GHNDelivery/front/Shopping/delivery.twig")
->setEditType(Page::EDIT_TYPE_DEFAULT);
$em->persist($page);
$em->flush($page);
$layout = $em->getRepository(Layout::class)->find(Layout::DEFAULT_LAYOUT_UNDERLAYER_PAGE);
$pageLayout = new PageLayout();
$pageLayout->setPage($page)
->setPageId($page->getId())
->setLayout($layout)
->setLayoutId($layout->getId())
->setSortNo(1);
$page->addPageLayout($pageLayout);
$em->persist($pageLayout);
}
}