-
Notifications
You must be signed in to change notification settings - Fork 12
/
Bootstrap.php
288 lines (255 loc) · 7.85 KB
/
Bootstrap.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* (c) shopware AG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Shopware\Components\Model\ModelManager;
use Shopware\CustomModels\CustomSort\ProductSort;
use Shopware\Models\Config\Element;
use Shopware\Models\Config\Form;
use Shopware\SwagCustomSort\Components\Listing;
use Shopware\SwagCustomSort\Components\Sorting;
use Shopware\SwagCustomSort\Subscriber\Backend;
use Shopware\SwagCustomSort\Subscriber\ControllerPath;
use Shopware\SwagCustomSort\Subscriber\Frontend;
use Shopware\SwagCustomSort\Subscriber\Resource;
use Shopware\SwagCustomSort\Subscriber\Sort;
use Shopware\SwagCustomSort\Subscriber\StoreFrontBundle;
/**
* Class Shopware_Plugins_Frontend_SwagCustomSort_Bootstrap
*/
class Shopware_Plugins_Frontend_SwagCustomSort_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
/**
* Returns the plugin label which is displayed in the plugin information and
* in the Plugin Manager.
*
* @return string
*/
public function getLabel()
{
return 'Individuelle Sortierung';
}
/**
* Returns the version of the plugin as a string
*
* @throws Exception
*
* @return string
*/
public function getVersion()
{
$info = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'plugin.json'), true);
if ($info) {
return $info['currentVersion'];
}
throw new RuntimeException('The plugin has an invalid version file.');
}
/**
* Install Plugin / Add Events
*
* @throws Exception
*
* @return bool
*/
public function install()
{
if (!$this->assertMinimumVersion('5.0.0')) {
throw new RuntimeException('This plugin requires Shopware 5.0.0 or a later version');
}
$this->subscribeEvents();
$this->createDatabase();
$this->createAttributes();
$this->createMenu();
$this->createForm($this->Form());
return true;
}
/**
* @param string $version
*
* @return array
*/
public function update($version)
{
if (version_compare($version, '2.0.0', '<=')) {
$sql = 'RENAME TABLE `s_articles_sort` TO `s_products_sort`;';
Shopware()->Db()->query($sql);
$sql = 'ALTER TABLE `s_products_sort` CHANGE `articleId` `productId` INT(11) NOT NULL;';
Shopware()->Db()->query($sql);
}
return ['success' => true, 'invalidateCache' => $this->getInvalidateCacheArray()];
}
/**
* Standard plugin enable method
*
* @return array
*/
public function enable()
{
$sql = "UPDATE s_core_menu SET active = 1 WHERE controller = 'CustomSort';";
Shopware()->Db()->query($sql);
return ['success' => true, 'invalidateCache' => $this->getInvalidateCacheArray()];
}
/**
* Standard plugin disable method
*
* @return array
*/
public function disable()
{
$sql = "UPDATE s_core_menu SET active = 0 WHERE controller = 'CustomSort';";
Shopware()->Db()->query($sql);
return ['success' => true, 'invalidateCache' => $this->getInvalidateCacheArray()];
}
/**
* Main entry point for the bonus system: Registers various subscribers to hook into shopware
*/
public function onStartDispatch()
{
/** @var Enlight_Event_EventManager $eventManager */
$eventManager = $this->get('events');
$container = $this->get('service_container');
$resourceSubscriber = new Resource($this->get('models'), $this->get('config'));
$eventManager->addSubscriber($resourceSubscriber);
/** @var Sorting $sortingComponent */
$sortingComponent = $container->get('swagcustomsort.sorting_component');
/** @var Listing $listingComponent */
$listingComponent = $this->get('swagcustomsort.listing_component');
$subscribers = [
new Resource($this->get('models'), $this->get('config')),
new ControllerPath($this->Path(), $this->get('template')),
new Frontend($this),
new Backend($this, $this->get('models')),
new Sort($this->get('models'), $sortingComponent, $listingComponent),
new StoreFrontBundle($container, $sortingComponent),
];
foreach ($subscribers as $subscriber) {
$eventManager->addSubscriber($subscriber);
}
}
/**
* Method to always register the custom models and the namespace for the auto-loading
*/
public function afterInit()
{
$this->registerCustomModels();
$this->Application()->Loader()->registerNamespace('Shopware\SwagCustomSort', $this->Path());
}
/**
* Registers all necessary events.
*/
private function subscribeEvents()
{
$this->subscribeEvent('Enlight_Controller_Front_StartDispatch', 'onStartDispatch');
}
/**
* Creates the backend menu item.
*/
private function createMenu()
{
$parent = $this->Menu()->findOneBy(['label' => 'Artikel']);
$this->createMenuItem(
[
'label' => 'Custom sort',
'controller' => 'CustomSort',
'action' => 'Index',
'active' => 0,
'class' => 'sprite-blue-document-text-image',
'parent' => $parent,
'position' => 6,
]
);
}
/**
* Creates the plugin database tables over the doctrine schema tool.
*/
private function createDatabase()
{
/** @var ModelManager $em */
$em = $this->get('models');
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = [
$em->getClassMetadata(ProductSort::class),
];
try {
$tool->createSchema($classes);
} catch (\Doctrine\ORM\Tools\ToolsException $e) {
}
}
/**
* creates necessary attributes for categories
*/
private function createAttributes()
{
/** @var ModelManager $em */
$em = $this->get('models');
$em->addAttribute(
's_categories_attributes',
'swag',
'link',
'int(11)'
);
$em->addAttribute(
's_categories_attributes',
'swag',
'show_by_default',
'int(1)',
true,
0
);
$em->addAttribute(
's_categories_attributes',
'swag',
'deleted_position',
'int(11)'
);
$em->addAttribute(
's_categories_attributes',
'swag',
'base_sort',
'int(11)'
);
$em->generateAttributeModels(['s_categories_attributes']);
}
/**
* @param Form $form
*/
private function createForm(Form $form)
{
$form->setElement(
'text',
'swagCustomSortName',
[
'label' => 'Name',
'value' => 'Individuelle Sortierung',
'description' => 'Die neue Sortierung ist unter diesem Namen im Frontend sichtbar.',
'required' => true,
'scope' => Element::SCOPE_SHOP,
]
);
$this->addFormTranslations(
[
'en_GB' => [
'swagCustomSortName' => [
'label' => 'Name',
'description' => 'The new sort will be visible in the frontend under this name.',
'value' => 'Custom Sorting',
],
],
]
);
}
/**
* @return array
*/
private function getInvalidateCacheArray()
{
return [
'backend',
'proxy',
'config',
];
}
}