|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2021 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
namespace Aimeos\MW\Setup\Task; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Adds demo records to product tables. |
|
14
|
|
|
*/ |
|
15
|
|
|
class DemoAddCmsData extends \Aimeos\MW\Setup\Task\MShopAddDataAbstract |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Returns the list of task names which this task depends on. |
|
19
|
|
|
* |
|
20
|
|
|
* @return string[] List of task names |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getPreDependencies() : array |
|
23
|
|
|
{ |
|
24
|
|
|
return ['MShopAddTypeDataCms', 'DemoAddTypeData']; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the list of task names which depends on this task. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string[] List of task names |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getPostDependencies() : array |
|
34
|
|
|
{ |
|
35
|
|
|
return ['DemoRebuildIndex']; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Insert product data. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function migrate() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->msg( 'Processing CMS demo data', 0 ); |
|
45
|
|
|
|
|
46
|
|
|
$context = $this->getContext(); |
|
47
|
|
|
$value = $context->getConfig()->get( 'setup/default/demo', '' ); |
|
48
|
|
|
|
|
49
|
|
|
if( $value === '' ) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->status( 'OK' ); |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$domains = ['media', 'text']; |
|
57
|
|
|
$manager = \Aimeos\MShop::create( $context, 'cms' ); |
|
58
|
|
|
|
|
59
|
|
|
$search = $manager->filter(); |
|
60
|
|
|
$search->setConditions( $search->compare( '=~', 'cms.label', 'Demo ' ) ); |
|
61
|
|
|
$pages = $manager->search( $search, $domains ); |
|
62
|
|
|
|
|
63
|
|
|
foreach( $domains as $domain ) |
|
64
|
|
|
{ |
|
65
|
|
|
$rmIds = map(); |
|
66
|
|
|
|
|
67
|
|
|
foreach( $pages as $item ) { |
|
68
|
|
|
$rmIds = $rmIds->merge( $item->getRefItems( $domain, null, null, false )->keys() ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
\Aimeos\MShop::create( $context, $domain )->delete( $rmIds->toArray() ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$manager->delete( $pages->toArray() ); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if( $value === '1' ) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->addDemoData(); |
|
80
|
|
|
$this->status( 'added' ); |
|
81
|
|
|
} |
|
82
|
|
|
else |
|
83
|
|
|
{ |
|
84
|
|
|
$this->status( 'removed' ); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Adds the demo data to the database. |
|
91
|
|
|
* |
|
92
|
|
|
* @throws \Aimeos\MShop\Exception If the file isn't found |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function addDemoData() |
|
95
|
|
|
{ |
|
96
|
|
|
$ds = DIRECTORY_SEPARATOR; |
|
97
|
|
|
$path = __DIR__ . $ds . 'data' . $ds . 'demo-cms.php'; |
|
98
|
|
|
|
|
99
|
|
|
if( ( $data = include( $path ) ) == false ) { |
|
100
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for CMS domain', $path ) ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$data = $this->replaceIds( $data ); |
|
104
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'cms' ); |
|
105
|
|
|
|
|
106
|
|
|
foreach( $data as $entry ) |
|
107
|
|
|
{ |
|
108
|
|
|
try |
|
109
|
|
|
{ |
|
110
|
|
|
$manager->find( $entry['cms.url'] ); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
113
|
|
|
{ |
|
114
|
|
|
$item = $manager->create()->fromArray( $entry ); |
|
115
|
|
|
$this->addRefItems( $item, $entry ); |
|
116
|
|
|
$manager->save( $item ); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Adds the referenced items from the given entry data. |
|
124
|
|
|
* |
|
125
|
|
|
* @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with list items |
|
126
|
|
|
* @param array $entry Associative list of data with stock, attribute, media, price, text and product sections |
|
127
|
|
|
* @return \Aimeos\MShop\Common\Item\ListsRef\Iface $item Updated item |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function addRefItems( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array $entry ) |
|
130
|
|
|
{ |
|
131
|
|
|
$context = $this->getContext(); |
|
132
|
|
|
$domain = $item->getResourceType(); |
|
133
|
|
|
$listManager = \Aimeos\MShop::create( $context, $domain . '/lists' ); |
|
134
|
|
|
|
|
135
|
|
|
foreach( ['media', 'text'] as $refDomain ) |
|
136
|
|
|
{ |
|
137
|
|
|
if( isset( $entry[$refDomain] ) ) |
|
138
|
|
|
{ |
|
139
|
|
|
$manager = \Aimeos\MShop::create( $context, $refDomain ); |
|
140
|
|
|
|
|
141
|
|
|
foreach( $entry[$refDomain] as $data ) |
|
142
|
|
|
{ |
|
143
|
|
|
$listItem = $listManager->create()->fromArray( $data ); |
|
144
|
|
|
$refItem = $manager->create()->fromArray( $data ); |
|
145
|
|
|
|
|
146
|
|
|
$item->addListItem( $refDomain, $listItem, $refItem ); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $item; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Replaces the IDs in the demo data with the actual ones |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $data Associative list of CMS demo data |
|
159
|
|
|
* @return array Modfied CMS demo data |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function replaceIds( array $data ) : array |
|
162
|
|
|
{ |
|
163
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
|
164
|
|
|
$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-' ); |
|
165
|
|
|
|
|
166
|
|
|
$map = []; |
|
167
|
|
|
foreach( $manager->search( $filter ) as $id => $item ) { |
|
168
|
|
|
$map[$item->getCode()] = $id; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
foreach( $data as $pos => $entry ) |
|
172
|
|
|
{ |
|
173
|
|
|
foreach( $entry['text'] ?? [] as $idx => $text ) |
|
174
|
|
|
{ |
|
175
|
|
|
$content = $text['text.content'] ?? ''; |
|
176
|
|
|
|
|
177
|
|
|
foreach( ['2' => 'demo-best', '3' => 'demo-new', '4' => 'demo-deals'] as $id => $code ) |
|
178
|
|
|
{ |
|
179
|
|
|
if( $newId = $map[$code] ?? null ) { |
|
180
|
|
|
$content = str_replace( 'catid=\"' . $id . '\"', 'catid=\"' . $newId . '\"', $content ); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$data[$pos]['text'][$idx]['text.content'] = $content; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $data; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|