-
Notifications
You must be signed in to change notification settings - Fork 5
/
Bootstrap.php
163 lines (148 loc) · 4.71 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
<?php
/*
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
/**
* Shopware SwagVariantFilter Plugin
*
*/
class Shopware_Plugins_Frontend_SwagVariantFilter_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
/**
* Installs the plugin
*
* Creates and subscribe the events and hooks
* Creates the Backend Form
*
* @return bool
*/
public function install()
{
$this->createForm();
$this->createTranslations();
$this->subscribeEvent('Enlight_Controller_Action_PreDispatch_Frontend', 'onStartDispatch');
return true;
}
/**
* registers the namespace + services
*/
public function afterInit()
{
$this->Application()->Loader()->registerNamespace('Shopware\SwagVariantFilter', $this->Path());
}
/**
* Init Services & Subscribers
*
* @param Enlight_Controller_ActionEventArgs $args
*/
public function onStartDispatch(Enlight_Controller_ActionEventArgs $args)
{
if (!$this->assertMinimumVersion('5')) {
$this->initializeLegacy($args);
return;
}
$this->Application()->Events()->addSubscriber(new Shopware\SwagVariantFilter\Subscriber\Filter());
$this->Application()->Events()->addSubscriber(new Shopware\SwagVariantFilter\Subscriber\ServiceContainer(
Shopware()->Plugins()->Frontend()->SwagVariantFilter()->Config(),
Shopware()->Front()->Request()
));
}
/**
* Initialize legacy SW4 handlers
*
* @param $args
*/
private function initializeLegacy(Enlight_Controller_ActionEventArgs $args)
{
$requestHelper = new \Shopware\SwagVariantFilter\Components\LegacyFilter\RequestHelper($args->getRequest());
$this->Application()->Events()->addSubscriber(new \Shopware\SwagVariantFilter\Subscriber\LegacyServiceContainer($requestHelper));
$this->Application()->Events()->addSubscriber(new Shopware\SwagVariantFilter\Subscriber\Legacy($requestHelper));
}
/**
* Return Plugin-Version
*
* @return String
* @throws Exception
*/
public function getVersion()
{
$info = json_decode(file_get_contents(__DIR__ . '/plugin.json'), true);
if ($info) {
return $info['currentVersion'];
} else {
throw new Exception('The plugin has an invalid version file.');
}
}
/**
* Get (nice) name for plugin manager list
* @return string
*/
public function getLabel()
{
return 'Varianten Filter';
}
/**
* @param string $version
* @return bool
*/
public function update($version)
{
$this->subscribeEvent('Enlight_Controller_Action_PreDispatch_Frontend', 'onStartDispatch');
return true;
}
/**
* Creates the backend config form
*
*/
protected function createForm()
{
$form = $this->Form();
$form->setElement('text', 'categoryids', array(
'label' => 'Aktiviert in diesen Kategorien (Komma separiert)',
'value' => "",
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
));
$form->setElement('number', 'mininstock', array(
'label' => 'Artikel im Filterergebnis verbergen, falls Lagerbestand kleiner',
'description' => '',
'value' => 1,
'required' => true,
'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
));
}
/**
* Creates Translation
*/
public function createTranslations()
{
$form = $this->Form();
$translations = array(
'en_GB' => array(
'categoryids' => 'Enabled in these categories (comma separated)',
)
);
$shopRepository = Shopware()->Models()->getRepository('\Shopware\Models\Shop\Locale');
foreach ($translations as $locale => $snippets) {
$localeModel = $shopRepository->findOneBy(array(
'locale' => $locale
));
foreach ($snippets as $element => $snippet) {
if ($localeModel === null) {
continue;
}
$elementModel = $form->getElement($element);
if ($elementModel === null) {
continue;
}
$translationModel = new \Shopware\Models\Config\ElementTranslation();
$translationModel->setLabel($snippet);
$translationModel->setLocale($localeModel);
$elementModel->addTranslation($translationModel);
}
}
}
}