-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOssImportProductStream.php
More file actions
executable file
·180 lines (157 loc) · 5.22 KB
/
OssImportProductStream.php
File metadata and controls
executable file
·180 lines (157 loc) · 5.22 KB
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
<?php
namespace OssImportProductStream;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\CustomModels\ImportExport\Profile;
/**
* Class OssImportProductStream
* @package OssImportProductStream
* @author Odessite <alexey.palamar@odessite.com.ua>
*/
class OssImportProductStream extends Plugin
{
const PROFILE_NAME = 'oss_product_stream';
const ADAPTER_TYPE = 'OssProductStream';
/**
* @return array
*/
public static function getSubscribedEvents()
{
return array(
'Enlight_Controller_Front_StartDispatch' => 'onEnlightControllerFrontStartDispatch',
);
}
/**
* This method can be overridden
*
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext)
{
parent::install($installContext);
$this->installProfile()
->updateSchema();
}
/**
* This method can be overridden
*
* @param UninstallContext $context
*/
public function uninstall(UninstallContext $context)
{
parent::uninstall($context);
$this->uninstallProfile()
->dropSchema();
}
/**
* This method can be overridden
*
* @param ActivateContext $context
*/
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
}
/**
* This method can be overridden
*
* @param DeactivateContext $context
*/
public function deactivate(DeactivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
}
public function onEnlightControllerFrontStartDispatch()
{
$this->container->get('loader')->registerNamespace('OssImportProductStream\Components', $this->getPath() . '/Components/');
}
/**
* Creates import/export profile
* TODO: profile will be unexisted if someone delete SwagImportExport and then install again, so in general probably would be better to extend profile repo and check is profile already exit
*
* @throws \Exception
* @return OssImportProductStream
*/
public function installProfile()
{
// Required to call import/export for profile repo will be available
$this->container->get('swag_import_export.profile_service');
$repository = $this->container->get('models')->getRepository(Profile::class);
/** @var \Shopware\CustomModels\ImportExport\Repository $repository */
$profile = $repository->findOneBy([
'name' => self::PROFILE_NAME,
]);
if (!($profile instanceof Profile)) {
$profile = new Profile();
$profile->setName(self::PROFILE_NAME);
$profile->setDescription(
self::PROFILE_NAME.'_description'
);
$profile->setType('OssProductStream');
$profile->setDefault(1);
$treePath = $this->getPath() . '/Components/SwagImportExport/Profiles/oss_product_stream.json';
if (file_exists($treePath)) {
$tree = file_get_contents($treePath);
$tree = preg_replace('/\\s+/', '', $tree);
$profile->setTree($tree);
}
$this->container->get('models')->persist($profile);
}
return $this;
}
/**
* Remove import/export profile
*
* @throws \Exception
* @return OssImportProductStream
*/
public function uninstallProfile()
{
$this->container->get('swag_import_export.profile_service');
$repository = $this->container->get('models')->getRepository(Profile::class);
/** @var \Shopware\CustomModels\ImportExport\Repository $repository */
$profile = $repository->findOneBy([
'name' => self::PROFILE_NAME,
]);
if ($profile instanceof Profile) {
$this->container->get('models')->remove($profile);
}
return $this;
}
/**
* add attribute for mark imported profile stream
*
* @return OssImportProductStream
*/
private function updateSchema()
{
/**@var \Shopware\Bundle\AttributeBundle\Service\CrudService $service **/
$service = $this->container->get('shopware_attribute.crud_service');
$service->update('s_product_streams_attributes', 'imported', 'boolean');
Shopware()->Models()->generateAttributeModels([
's_product_streams_attributes'
]);
return $this;
}
/**
* remove attribute
*
* @return OssImportProductStream
*/
private function dropSchema()
{
/**@var \Shopware\Bundle\AttributeBundle\Service\CrudService $service **/
$service = $this->container->get('shopware_attribute.crud_service');
try {
$service->delete('s_product_streams_attributes', 'imported');
} catch (\Exception $e) {
}
Shopware()->Models()->generateAttributeModels([
's_product_streams_attributes'
]);
return $this;
}
}