-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPort1Typo3Connector.php
242 lines (209 loc) · 8.36 KB
/
Port1Typo3Connector.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
240
241
242
<?php
namespace Port1Typo3Connector;
use Port1Typo3Connector\Components\ApiArticlesOrderNumberDecorator;
use Port1Typo3Connector\Components\ApiTokenDecorator;
use Port1Typo3Connector\Components\ApiUrlDecorator\ApiArticlesUrlDecorator;
use Port1Typo3Connector\Components\ApiUrlDecorator\ApiCategoriesUrlDecorator;
use Port1Typo3Connector\Components\ApiUrlDecorator\ApiVariantsUrlDecorator;
use Port1Typo3Connector\Service\Notification\Command;
use Port1Typo3Connector\Service\Notification\Typo3NotificationService;
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;
use Shopware\Bundle\StoreFrontBundle\Struct\Media;
use Shopware\Components\Model\ModelEntity;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Models\Article\Article;
use Shopware\Models\Category\Category;
use Shopware\Models\Shop\Shop;
/**
* Class Port1Typo3Connector
*
* @package Port1Typo3Connector
*/
class Port1Typo3Connector extends Plugin
{
/**
* @var array
*/
static protected $apiEndpoints = [
0 => 'Articles',
1 => 'Caches',
2 => 'Categories',
3 => 'Customers',
4 => 'CustomerGroups',
5 => 'Media',
6 => 'Orders',
7 => 'PropertyGroups',
8 => 'Shops',
9 => 'Translations',
10 => 'Variants',
11 => 'Version',
];
/**
* @param \Shopware\Components\Plugin\Context\InstallContext $context
*/
public function install(InstallContext $context)
{
$this->addTypo3ApiUrlAttribute();
}
public static function getSubscribedEvents()
{
$result = [
// we don`t need to extendExtJS for now, because it is hard to place the attribute field within another form
// https://developers.shopware.com/developers-guide/attribute-system/#move-attribute-fields-into-anoth
// 'Enlight_Controller_Action_PostDispatchSecure_Backend_UserManager' => 'extendExtJS'
Article::class . '::postPersist' => 'createEntity',
Article::class . '::postUpdate' => 'updateEntity',
Article::class . '::preRemove' => 'deleteEntity',
Category::class . '::postPersist' => 'createEntity',
Category::class . '::preUpdate ' => 'preUpdateEntity',
Category::class . '::postUpdate' => 'updateEntity',
Category::class . '::preRemove' => 'deleteEntity',
Media::class . '::postPersist' => 'createEntity',
Media::class . '::postUpdate' => 'updateEntity',
Media::class . '::preRemove' => 'deleteEntity',
Shop::class . '::postPersist' => 'createEntity',
Shop::class . '::postUpdate' => 'updateEntity',
Shop::class . '::preRemove' => 'deleteEntity'
];
/**
* subscribe to init event for each endpoint controller of REST-API
*/
foreach (self::$apiEndpoints as $apiEndpoint) {
/**
* subscribe init api to all endpoints
*/
$result['Enlight_Controller_Action_Init_Api_' . ucfirst($apiEndpoint)][] = 'onInitApiAddToken';
if (class_exists('Port1Typo3Connector\\Components\\ApiUrlDecorator\\Api' . ucfirst($apiEndpoint) . 'UrlDecorator')) {
$result['Enlight_Controller_Action_PostDispatchSecure_Api_' . ucfirst($apiEndpoint)][] = [
'onApi' . ucfirst($apiEndpoint) . 'AddUrl'
];
}
if (class_exists('Port1Typo3Connector\\Components\\Api' . ucfirst($apiEndpoint) . 'OrderNumberDecorator')) {
$result['Enlight_Controller_Action_PostDispatchSecure_Api_' . ucfirst($apiEndpoint)][] = [
'onApi' . ucfirst($apiEndpoint) . 'AddOrderNumber'
];
}
}
return $result;
}
/**
* @param \Enlight_Event_EventArgs $arguments
*/
public function createEntity(\Enlight_Event_EventArgs $arguments)
{
/** @var ModelEntity|Article|Category|Media|Shop $entity */
$entity = $arguments->get('entity');
/** @var Typo3NotificationService $notificationService */
$notificationService = $this->container->get('port1_typo3_connector.typo3_notification_service');
$notificationService->notify(
Command::COMMAND_CREATE,
$entity
);
}
/**
* @param \Enlight_Event_EventArgs $arguments
*/
public function updateEntity(\Enlight_Event_EventArgs $arguments)
{
/** @var ModelEntity|Article|Category|Media|Shop $entity */
$entity = $arguments->get('entity');
/** @var Typo3NotificationService $notificationService */
$notificationService = $this->container->get('port1_typo3_connector.typo3_notification_service');
$notificationService->notify(
Command::COMMAND_UPDATE,
$entity
);
}
/**
* @param \Enlight_Event_EventArgs $arguments
*/
public function deleteEntity(\Enlight_Event_EventArgs $arguments)
{
/** @var ModelEntity|Article|Category|Media|Shop $entity */
$entity = $arguments->get('entity');
/** @var Typo3NotificationService $notificationService */
$notificationService = $this->container->get('port1_typo3_connector.typo3_notification_service');
$notificationService->notify(
Command::COMMAND_DELETE,
$entity
);
}
/**
* @param \Enlight_Event_EventArgs $args
*/
public function onInitApiAddToken(\Enlight_Event_EventArgs $args)
{
$apiTokenDecorator = new ApiTokenDecorator($args->get('subject'));
$apiTokenDecorator->addApiToken();
}
/**
* @param \Enlight_Event_EventArgs $args
* @throws \Exception
*/
public function onApiArticlesAddUrl(\Enlight_Event_EventArgs $args)
{
$apiUrlDecorator = new ApiArticlesUrlDecorator($args->get('subject'), $this->container);
$apiUrlDecorator->addUrl();
}
/**
* @param \Enlight_Event_EventArgs $args
* @throws \Exception
*/
public function onApiCategoriesAddUrl(\Enlight_Event_EventArgs $args)
{
$apiUrlDecorator = new ApiCategoriesUrlDecorator($args->get('subject'), $this->container);
$apiUrlDecorator->addUrl();
}
/**
* @param \Enlight_Event_EventArgs $args
* @throws \Exception
*/
public function onApiVariantsAddUrl(\Enlight_Event_EventArgs $args)
{
$apiUrlDecorator = new ApiVariantsUrlDecorator($args->get('subject'), $this->container);
$apiUrlDecorator->addUrl();
}
/**
* @param \Enlight_Event_EventArgs $args
*/
public function onApiArticlesAddOrderNumber(\Enlight_Event_EventArgs $args)
{
$apiOrderNumberDecorator = new ApiArticlesOrderNumberDecorator($args->get('subject'), $this->container);
$apiOrderNumberDecorator->addOrderNumber();
}
/**
* add TYPO3 API-URL attribute to s_core_auth_attributes
* @throws \Exception
*/
protected function addTypo3ApiUrlAttribute()
{
/** @var \Shopware\Bundle\AttributeBundle\Service\CrudService $service */
$service = $this->container->get('shopware_attribute.crud_service');
$service->update('s_core_auth_attributes', 'typo3_api_url', TypeMapping::TYPE_STRING, [
'label' => 'TYPO3 API-URL',
'supportText' => 'Enter the TYPO3 API-URL here to push notifications about changes of articles / categories to this endpoint.',
'helpText' => 'Enter the TYPO3 API-URL here',
//user has the opportunity to translate the attribute field for each shop
'translatable' => true,
//attribute will be displayed in the backend module
'displayInBackend' => true,
//numeric position for the backend view, sorted ascending
'position' => 100,
//user can modify the attribute in the free text field module
'custom' => false,
]);
return true;
}
/**
* @param \Enlight_Event_EventArgs $arguments
* @throws \Exception
*/
public function extendExtJS(\Enlight_Event_EventArgs $arguments)
{
/** @var \Enlight_View_Default $view */
$view = $arguments->get('subject')->View();
$view->addTemplateDir($this->getPath() . '/Views/');
$view->extendsTemplate('backend/port1_typo3_connector/view/user/create.js');
}
}