1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2021-2025 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Upscheme\Task; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Adds demo records to product tables. |
14
|
|
|
*/ |
15
|
|
|
class DemoAddCmsData extends 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 after() : 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 before() : array |
34
|
|
|
{ |
35
|
|
|
return ['DemoRebuildIndex']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Insert product data. |
41
|
|
|
*/ |
42
|
|
|
public function up() |
43
|
|
|
{ |
44
|
|
|
$context = $this->context(); |
|
|
|
|
45
|
|
|
$value = $context->config()->get( 'setup/default/demo', '' ); |
46
|
|
|
|
47
|
|
|
if( $value === '' ) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->info( 'Processing CMS demo data', 'vv' ); |
53
|
|
|
|
54
|
|
|
$domains = ['media', 'text']; |
55
|
|
|
$manager = \Aimeos\MShop::create( $context, 'cms' ); |
56
|
|
|
|
57
|
|
|
$search = $manager->filter(); |
58
|
|
|
$search->setConditions( $search->compare( '=~', 'cms.label', 'Demo ' ) ); |
59
|
|
|
$pages = $manager->search( $search, $domains ); |
60
|
|
|
|
61
|
|
|
foreach( $pages as $item ) |
62
|
|
|
{ |
63
|
|
|
foreach( $domains as $domain ) { |
64
|
|
|
$item->deleteListItems( $item->getListItems( $domain, null, null, false ), true ); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$manager->delete( $pages ); |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
if( $value === '1' ) { |
72
|
|
|
$this->addDemoData(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Adds the demo data to the database. |
79
|
|
|
* |
80
|
|
|
* @throws \Aimeos\MShop\Exception If the file isn't found |
81
|
|
|
*/ |
82
|
|
|
protected function addDemoData() |
83
|
|
|
{ |
84
|
|
|
$ds = DIRECTORY_SEPARATOR; |
85
|
|
|
$path = __DIR__ . $ds . 'data' . $ds . 'demo-cms.php'; |
86
|
|
|
|
87
|
|
|
if( ( $data = include( $path ) ) == false ) { |
88
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for CMS domain', $path ) ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$data = $this->replaceIds( $data ); |
92
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'cms' ); |
93
|
|
|
|
94
|
|
|
foreach( $data as $entry ) |
95
|
|
|
{ |
96
|
|
|
try |
97
|
|
|
{ |
98
|
|
|
$manager->find( $entry['cms.url'] ); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
101
|
|
|
{ |
102
|
|
|
$item = $manager->create()->fromArray( $entry ); |
103
|
|
|
$this->addRefItems( $item, $entry ); |
104
|
|
|
$manager->save( $item ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Replaces the IDs in the demo data with the actual ones |
112
|
|
|
* |
113
|
|
|
* @param array $data Associative list of CMS demo data |
114
|
|
|
* @return array Modfied CMS demo data |
115
|
|
|
*/ |
116
|
|
|
protected function replaceIds( array $data ) : array |
117
|
|
|
{ |
118
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'catalog' ); |
119
|
|
|
$filter = $manager->filter()->add( 'catalog.code', '=~', 'demo-' ); |
120
|
|
|
|
121
|
|
|
$map = []; |
122
|
|
|
foreach( $manager->search( $filter ) as $id => $item ) { |
123
|
|
|
$map[$item->getCode()] = $id; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach( $data as $pos => $entry ) |
127
|
|
|
{ |
128
|
|
|
foreach( $entry['text'] ?? [] as $idx => $text ) |
129
|
|
|
{ |
130
|
|
|
$content = $text['text.content'] ?? ''; |
131
|
|
|
|
132
|
|
|
foreach( ['2' => 'demo-best', '3' => 'demo-new', '4' => 'demo-deals'] as $id => $code ) |
133
|
|
|
{ |
134
|
|
|
if( $newId = $map[$code] ?? null ) { |
135
|
|
|
$content = str_replace( 'catid=\"' . $id . '\"', 'catid=\"' . $newId . '\"', $content ); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$data[$pos]['text'][$idx]['text.content'] = $content; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $data; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|