|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
|
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2024 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
namespace Aimeos\Upscheme\Task; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Adds demo records to catalog tables. |
|
15
|
|
|
*/ |
|
16
|
|
|
class DemoAddCatalogData extends MShopAddDataAbstract |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Returns the list of task names which this task depends on. |
|
20
|
|
|
* |
|
21
|
|
|
* @return string[] List of task names |
|
22
|
|
|
*/ |
|
23
|
|
|
public function after() : array |
|
24
|
|
|
{ |
|
25
|
|
|
return ['Catalog', 'Media', 'Product', 'Text', 'MShopSetLocale']; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Returns the list of task names which depends on this task. |
|
31
|
|
|
* |
|
32
|
|
|
* @return string[] List of task names |
|
33
|
|
|
*/ |
|
34
|
|
|
public function before() : array |
|
35
|
|
|
{ |
|
36
|
|
|
return ['DemoRebuildIndex']; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Insert catalog nodes and relations. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function up() |
|
44
|
|
|
{ |
|
45
|
|
|
$context = $this->context(); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if( ( $value = $context->config()->get( 'setup/default/demo', '' ) ) === '' ) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$this->info( 'Processing catalog demo data', 'vv' ); |
|
53
|
|
|
|
|
54
|
|
|
$item = $this->removeItems(); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
if( $value === '1' ) { |
|
58
|
|
|
$this->addDemoData(); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Adds the demo data to the database. |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \RuntimeException If the file isn't found |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function addDemoData() |
|
69
|
|
|
{ |
|
70
|
|
|
$ds = DIRECTORY_SEPARATOR; |
|
71
|
|
|
$path = __DIR__ . $ds . 'data' . $ds . 'demo-catalog.php'; |
|
72
|
|
|
|
|
73
|
|
|
if( ( $data = include( $path ) ) == false ) { |
|
74
|
|
|
throw new \RuntimeException( sprintf( 'No file "%1$s" found for catalog domain', $path ) ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$context = $this->context(); |
|
78
|
|
|
$manager = \Aimeos\MShop::create( $context, 'catalog' ); |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
$item = $manager->getTree( null, ['media', 'text'], \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ); |
|
82
|
|
|
} catch( \Exception $e ) { |
|
83
|
|
|
$item = $manager->insert( $manager->create()->fromArray( $data ) ); |
|
84
|
|
|
} |
|
85
|
|
|
var_dump($item->getId(), $item->getLabel()); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$this->addRefItems( $item, $data ); |
|
88
|
|
|
|
|
89
|
|
|
$manager->save( $item ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
protected function addCategories( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $data ) : \Aimeos\MShop\Common\Item\ListsRef\Iface |
|
94
|
|
|
{ |
|
95
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'catalog' ); |
|
96
|
|
|
|
|
97
|
|
|
foreach( $data as $entry ) |
|
98
|
|
|
{ |
|
99
|
|
|
$catItem = $manager->create()->fromArray( $entry ); |
|
100
|
|
|
$catItem = $manager->insert( $catItem, $item->getId() ); |
|
101
|
|
|
|
|
102
|
|
|
$this->addRefItems( $catItem, $entry ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $item; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Deletes the demo catalog items |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function removeItems() : ?\Aimeos\MShop\Catalog\Item\Iface |
|
113
|
|
|
{ |
|
114
|
|
|
$context = $this->context(); |
|
115
|
|
|
$domains = ['media', 'text']; |
|
116
|
|
|
$manager = \Aimeos\MShop::create( $context, 'catalog' ); |
|
117
|
|
|
|
|
118
|
|
|
try |
|
119
|
|
|
{ |
|
120
|
|
|
// Don't delete the catalog node because users are likely use it for production |
|
121
|
|
|
$item = $manager->getTree( null, $domains, \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ); |
|
122
|
|
|
$this->removeRefItems( map( $item ), $domains ); |
|
123
|
|
|
$manager->save( $item ); |
|
124
|
|
|
} |
|
125
|
|
|
catch( \Exception $e ) { ; } // If no root node was already inserted into the database |
|
126
|
|
|
|
|
127
|
|
|
$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-')->add( 'catalog.level', '==', 1 )->slice( 0, 0x7fffffff ); |
|
128
|
|
|
$items = $manager->search( $filter, $domains ); |
|
129
|
|
|
|
|
130
|
|
|
$this->removeRefItems( $items, $domains ); |
|
131
|
|
|
|
|
132
|
|
|
$manager->delete( $items ); |
|
133
|
|
|
|
|
134
|
|
|
return $item ?? null; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|