-
Notifications
You must be signed in to change notification settings - Fork 39
/
Bootstrap.php
419 lines (362 loc) · 12.8 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?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 Doctrine\DBAL\Connection;
use Enlight_Components_Db_Adapter_Pdo_Mysql as PDOConnection;
use Shopware\SwagMigration\Components\Migration\PasswordEncoder\Md5Reversed;
use Shopware\SwagMigration\Components\Migration\PasswordEncoder\Sha512;
use Shopware\SwagMigration\Subscriber\Resources;
/**
* Shopware SwagMigration Plugin - Bootstrap
*/
class Shopware_Plugins_Backend_SwagMigration_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
/**
* @var PDOConnection
*/
private $db;
/**
* @var Connection
*/
private $connection;
/**
* Install method of the plugin. Register the migration controller, create the backend menu item and creates the
* plugin database table.
*
* @return array
*/
public function install()
{
$this->checkVersion('5.5.0');
$this->subscribeEvents();
$parent = $this->Menu()->findOneBy(['label' => 'Inhalte']);
$this->createMenuItem(
[
'label' => 'Shop-Migration',
'class' => 'sprite-database-import',
'active' => 0,
'parent' => $parent,
'position' => 0,
'controller' => 'SwagMigration',
'action' => 'Index',
]
);
$sql = '
CREATE TABLE IF NOT EXISTS `s_plugin_migrations` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeID` INT(11) UNSIGNED NOT NULL,
`sourceID` VARCHAR(255) NOT NULL,
`targetID` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `typeID` (`typeID`,`sourceID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;';
$this->db->query($sql);
$this->createForm();
return ['success' => true, 'invalidateCache' => ['backend']];
}
/**
* @return array
*/
public function enable()
{
$this->connection->update('s_core_menu', ['active' => 1], ['name' => 'Shop-Migration']);
return ['success' => true, 'invalidateCache' => ['frontend', 'backend']];
}
/**
* @return array
*/
public function disable()
{
$this->connection->update('s_core_menu', ['active' => 0], ['name' => 'Shop-Migration']);
return ['success' => true, 'invalidateCache' => ['frontend', 'backend']];
}
/**
* @param string $version
*
* @throws RuntimeException
*/
public function checkVersion($version)
{
if (!$this->assertMinimumVersion($version)) {
throw new RuntimeException('This plugin requires Shopware ' . $version . ' or a later version');
}
}
/**
* Update the plugin to the current version
*
* @param string $version
*
* @return bool
*/
public function update($version)
{
$this->checkVersion('5.5.0');
$this->subscribeEvents();
// Create form
$this->createForm();
// Clean up the migration table in order to not have duplicate entries
$sql = '
-- Remove non existing article references
DELETE m FROM `s_plugin_migrations` m
LEFT JOIN s_articles_details ad
ON ad.id = m.targetID
WHERE ad.id IS NULL AND typeID = 1;
-- Remove non existing category references
DELETE m FROM `s_plugin_migrations` m
LEFT JOIN s_categories c
ON c.id = m.targetID
WHERE c.id IS NULL AND typeID IN (2,99);
-- Remove non-existing customer references
DELETE m FROM `s_plugin_migrations` m
LEFT JOIN s_user u
ON u.id = m.targetID
WHERE u.id IS NULL AND typeID = 3;
-- Remove non-existing order references
DELETE m FROM `s_plugin_migrations` m
LEFT JOIN s_order o
ON o.id = m.targetID
WHERE o.id IS NULL AND typeID = 4;
-- Replace the old index
ALTER TABLE `s_plugin_migrations` DROP INDEX `typeID` ,
ADD UNIQUE `typeID` ( `typeID` , `sourceID` );';
try {
$this->db->query($sql);
} catch (\Exception $e) {
// The above statement is just a cleanup statement, so errors should not cancel the whole update process
}
// Make sure that s_order_number is valid
$sql = "
INSERT IGNORE INTO `s_order_number` (`number`, `name`, `desc`) VALUES
(30004, 'user', 'Kunden'),
(30002, 'invoice', 'Bestellungen'),
(30000, 'doc_1', 'Lieferscheine'),
(30000, 'doc_2', 'Gutschriften'),
(30000, 'doc_0', 'Rechnungen'),
(20001, 'articleordernumber', 'Artikelbestellnummer '),
(20000, 'sSERVICE1', 'Service - 1'),
(20000, 'sSERVICE2', 'Service - 2'),
(210, 'blogordernumber', 'Blog - ID');";
$this->db->query($sql);
// Fix snippet
$oldSnippet = "Die Produkt-Nummer '%s' ist ungültig. Eine gültige Nummer darf:<br>
* höchstens 40 Zeichen lang sein<br>
* keine anderen Zeichen als : 'a-zA-Z0-9-_. ' und SPACE beinhalten<br>
<br>
Sie können den Import dennoch erzwingen. Beachten Sie: <br>
* Dabei werden zu lange Produkt-Nummern abgeschnitten. Dies kann zu 'Duplicate Key'-Fehlern führen<br>
* Artikel mit ungültigen Nummern werden Sie später nur ändern und speichern können, wenn Sie dabei die Nummer anpassen<br>
";
$newSnippet = "Die Produkt-Nummer '%s' ist ungültig. Eine gültige Nummer darf:<br>
* höchstens 30 Zeichen lang sein<br>
* keine anderen Zeichen als : 'a-zA-Z0-9-_.'<br>
<br>
Sie können den Import dennoch erzwingen. Beachten Sie: <br>
* Dabei werden zu lange Produkt-Nummern abgeschnitten. Dies kann zu 'Duplicate Key'-Fehlern führen<br>
* Artikel mit ungültigen Nummern werden Sie später nur ändern und speichern können, wenn Sie dabei die Nummer anpassen<br>
";
$sql = 'UPDATE s_core_snippets SET `value` = ? WHERE `name` = ? AND `value` = ?';
$this->db->query($sql, [$newSnippet, 'numberNotValid', $oldSnippet]);
$oldSnippet = 'Magento 1.8.1.0 bis 1.9.3.4';
$newSnippet = 'Magento 1.8.1.0 bis 1.9.4.5';
$sql = 'UPDATE s_core_snippets SET `value` = ? WHERE `name` = ? AND `value` = ?';
$this->db->query($sql, [$newSnippet, 'profile-magento', $oldSnippet]);
return true;
}
/**
* Subscribe the needed events
*/
public function subscribeEvents()
{
$this->subscribeEvent('Enlight_Controller_Front_DispatchLoopStartup', 'onStartDispatch');
$this->subscribeEvent(
'Enlight_Controller_Dispatcher_ControllerPath_Backend_SwagMigration',
'onGetControllerPath'
);
$this->subscribeEvent('Shopware_Components_Password_Manager_AddEncoder', 'onAddPasswordEncoder');
$this->subscribeEvent('Enlight_Controller_Action_PostDispatch', 'onPostDispatch', 110);
}
/**
* add migration services
*/
public function onStartDispatch()
{
$container = Shopware()->Container();
$subscribers = [new Resources($container)];
foreach ($subscribers as $subscriber) {
$this->get('events')->addSubscriber($subscriber);
}
}
/**
* register namespace
* initialise database connection
*/
public function afterInit()
{
/** @var Enlight_Loader $loader */
$loader = $this->get('loader');
$this->connection = $this->get('dbal_connection');
$loader->registerNamespace(
'Shopware\SwagMigration',
$this->Path()
);
$this->db = $this->get('db');
}
/**
* Create the config form for the plugin
*/
public function createForm()
{
$form = $this->Form();
$form->setElement(
'boolean',
'debugMigration',
[
'description' => 'Soll eine Debug-Ausgabe geschrieben werden? Achtung! Kann die Geschwindigkeit des Imports negativ beeinflussen.',
'label' => 'Debug-Ausgabe',
'value' => false,
]
);
$translation = [
'en_GB' => [
'debugMigration' => [
'label' => 'Debug output',
'description' => 'Should a debug output be written? Attention! Could reduce the import speed.',
],
],
];
$this->addFormTranslations($translation);
$form->setElement(
'boolean',
'stopOnException',
[
'description' => 'Das Migration Tool versucht nicht korrekte Nummern automatisch zu korrigieren, wenn man es so anwählt im Tool. Mit der Option können Sie entscheiden, ob bei einem Fehler die Migration abgebrochen werden soll. Das alte Verhalten war bei solch einem Fehler abzubrechen.',
'label' => 'Bei Fehler abbrechen',
'value' => false,
]
);
$translation = [
'en_GB' => [
'stopOnException' => [
'label' => 'abort on error',
'description' => 'The migration tool automatically tries to correct incorrect numbers, like i. e. ordernumbers, if you choose it in the tool itself. With this option, you can decide whether it should stop on errors or not. The legacy behaviour was to stop the migration on such errors.',
],
],
];
$this->addFormTranslations($translation);
$form->setElement(
'text',
'defaultIdPlaceholder',
[
'description' => 'Der Standard-Platzhalter, der im Fehlerfall eingesetzt werden soll beim Nummer korrigieren.',
'label' => 'Standard Platzhalter Nummernkorrektur',
'value' => 'n / a',
]
);
$translation = [
'en_GB' => [
'defaultIdPlaceholder' => [
'label' => 'default placeholder for number fix',
'description' => 'The default placeholder, that should be placed in at number fixing plugin feature.',
],
],
];
$this->addFormTranslations($translation);
}
/**
* Callback function to register our password encoders
*
* @return array
*/
public function onAddPasswordEncoder(Enlight_Event_EventArgs $args)
{
$hashes = $args->getReturn();
$hashes[] = new Md5Reversed();
$hashes[] = new Sha512();
return $hashes;
}
/**
* Register template dir on time
*/
public function onPostDispatch()
{
$this->registerMyTemplateDir();
}
/**
* Uninstall method of the plugin. The plugin database table will be dropped.
*
* @return bool
*/
public function uninstall()
{
$sql = 'DROP TABLE IF EXISTS `s_plugin_migrations`;';
$this->db->query($sql);
return parent::uninstall();
}
/**
* Backend controller path event. Returns the path of the backend migration controller.
*
* @return string
*/
public function onGetControllerPath()
{
$this->registerMyTemplateDir();
return $this->Path() . 'Controllers/Backend/SwagMigration.php';
}
/**
* Returns the meta information about the plugin
* as an array.
* Keep in mind that the plugin description located
* in the info.txt.
*
* @return array
*/
public function getInfo()
{
return [
'version' => $this->getVersion(),
'label' => $this->getLabel(),
'author' => 'shopware AG',
'description' => \file_get_contents($this->Path() . 'info.txt'),
'support' => 'http://forum.shopware.com/',
'link' => 'http://forum.shopware.com/',
'copyright' => 'shopware AG',
];
}
/**
* Returns the version of the plugin as a string
*
* @throws RuntimeException
*
* @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.');
}
/**
* Returns the well-formatted name of the plugin
* as a sting
*
* @return string
*/
public function getLabel()
{
return 'Shopware Migration';
}
/**
* Convenience function to register template and snippet dirs
*/
protected function registerMyTemplateDir()
{
$this->get('snippets')->addConfigDir($this->Path() . 'Snippets/');
$this->get('template')->addTemplateDir($this->Path() . 'Views/');
}
}