1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2024 |
6
|
|
|
* @package MShop |
7
|
|
|
* @subpackage Common |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MShop\Common\Manager; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provides common methods required by most of the manager classes. |
16
|
|
|
* |
17
|
|
|
* @package MShop |
18
|
|
|
* @subpackage Common |
19
|
|
|
*/ |
20
|
|
|
abstract class Base implements \Aimeos\Macro\Iface |
21
|
|
|
{ |
22
|
|
|
use \Aimeos\Macro\Macroable; |
23
|
|
|
use Sub\Traits; |
24
|
|
|
use Methods; |
25
|
|
|
use Site; |
26
|
|
|
use DB; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
private \Aimeos\MShop\ContextIface $context; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initialization of class. |
34
|
|
|
* |
35
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
36
|
|
|
*/ |
37
|
|
|
public function __construct( \Aimeos\MShop\ContextIface $context ) |
38
|
|
|
{ |
39
|
|
|
$this->context = $context; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Removes old entries from the storage. |
45
|
|
|
* |
46
|
|
|
* @param iterable $siteids List of IDs for sites whose entries should be deleted |
47
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
48
|
|
|
*/ |
49
|
|
|
public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
50
|
|
|
{ |
51
|
|
|
foreach( $this->context()->config()->get( $this->getConfigKey( 'submanagers' ), [] ) as $domain ) { |
52
|
|
|
$this->object()->getSubManager( $domain )->clear( $siteids ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->clearBase( $siteids, $this->getConfigKey( 'delete' ) ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates a new empty item instance |
61
|
|
|
* |
62
|
|
|
* @param array $values Values the item should be initialized with |
63
|
|
|
* @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object |
64
|
|
|
*/ |
65
|
|
|
public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
66
|
|
|
{ |
67
|
|
|
$values['siteid'] = $values['siteid'] ?? $this->context()->locale()->getSiteId(); |
68
|
|
|
return new \Aimeos\MShop\Common\Item\Base( '', $values ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Removes multiple items. |
74
|
|
|
* |
75
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items |
76
|
|
|
* @return \Aimeos\MShop\Attribute\Manager\Iface Manager object for chaining method calls |
77
|
|
|
*/ |
78
|
|
|
public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface |
79
|
|
|
{ |
80
|
|
|
return $this->deleteItemsBase( $itemIds, $this->getConfigKey( 'delete' ) ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a search critera object |
86
|
|
|
* |
87
|
|
|
* @param bool|null $default Add default criteria or NULL for relaxed default criteria |
88
|
|
|
* @param bool $site TRUE for adding site criteria to limit items by the site of related items |
89
|
|
|
* @return \Aimeos\Base\Criteria\Iface New search criteria object |
90
|
|
|
*/ |
91
|
|
|
public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface |
92
|
|
|
{ |
93
|
|
|
return $this->filterBase( $this->getDomain() ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the attributes item specified by its ID. |
99
|
|
|
* |
100
|
|
|
* @param string $id Unique ID of the attribute item in the storage |
101
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
102
|
|
|
* @param bool|null $default Add default criteria or NULL for relaxed default criteria |
103
|
|
|
* @return \Aimeos\MShop\Attribute\Item\Iface Returns the attribute item of the given id |
104
|
|
|
* @throws \Aimeos\MShop\Exception If item couldn't be found |
105
|
|
|
*/ |
106
|
|
|
public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
107
|
|
|
{ |
108
|
|
|
return $this->getItemBase( 'id', $id, $ref, $default ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the available manager types |
114
|
|
|
* |
115
|
|
|
* @param bool $withsub Return also the resource type of sub-managers if true |
116
|
|
|
* @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
117
|
|
|
*/ |
118
|
|
|
public function getResourceType( bool $withsub = true ) : array |
119
|
|
|
{ |
120
|
|
|
return $this->getResourceTypeBase( $this->getDomain(), $this->getConfigKey( 'submanagers' ), [], $withsub ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the attributes that can be used for searching. |
126
|
|
|
* |
127
|
|
|
* @param bool $withsub Return also attributes of sub-managers if true |
128
|
|
|
* @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items |
129
|
|
|
*/ |
130
|
|
|
public function getSearchAttributes( bool $withsub = true ) : array |
131
|
|
|
{ |
132
|
|
|
return array_replace( $this->createAttributes( [ |
133
|
|
|
'id' => [ |
134
|
|
|
'label' => 'ID', |
135
|
|
|
'type' => 'int', |
136
|
|
|
'public' => false, |
137
|
|
|
], |
138
|
|
|
'siteid' => [ |
139
|
|
|
'label' => 'Site ID', |
140
|
|
|
'public' => false, |
141
|
|
|
], |
142
|
|
|
'ctime' => [ |
143
|
|
|
'label' => 'Create date/time', |
144
|
|
|
'type' => 'datetime', |
145
|
|
|
'public' => false, |
146
|
|
|
], |
147
|
|
|
'mtime' => [ |
148
|
|
|
'label' => 'Modification date/time', |
149
|
|
|
'type' => 'datetime', |
150
|
|
|
'public' => false, |
151
|
|
|
], |
152
|
|
|
'editor' => [ |
153
|
|
|
'label' => 'Editor', |
154
|
|
|
'public' => false, |
155
|
|
|
], |
156
|
|
|
] ), $this->getSaveAttributes() ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns a new manager for attribute extensions |
162
|
|
|
* |
163
|
|
|
* @param string $manager Name of the sub manager type in lower case |
164
|
|
|
* @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
165
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc. |
166
|
|
|
*/ |
167
|
|
|
public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
168
|
|
|
{ |
169
|
|
|
return $this->getSubManagerBase( $this->getDomain(), $manager, $name ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Iterates over all matched items and returns the found ones |
175
|
|
|
* |
176
|
|
|
* @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor |
177
|
|
|
* @param string[] $ref List of domains whose items should be fetched too |
178
|
|
|
* @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys |
179
|
|
|
*/ |
180
|
|
|
public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map |
181
|
|
|
{ |
182
|
|
|
if( $cursor->value() === '' ) { |
183
|
|
|
return null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if( ( $first = current( $this->getSearchAttributes() ) ) === false ) { |
187
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'No search configuration available for "%1$s"', get_class( $this ) ) ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$filter = $cursor->filter()->add( $first->getCode(), '>', (int) $cursor->value() )->order( $first->getCode() ); |
191
|
|
|
$items = $this->search( $filter, $ref ); |
192
|
|
|
$cursor->setValue( $items->lastKey() ?: '' ); |
193
|
|
|
|
194
|
|
|
return !$items->isEmpty() ? $items : null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Adds or updates an item object or a list of them. |
200
|
|
|
* |
201
|
|
|
* @param \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved |
202
|
|
|
* @param bool $fetch True if the new ID should be returned in the item |
203
|
|
|
* @return \Aimeos\Map|\Aimeos\MShop\Common\Item\Iface Saved item or items |
204
|
|
|
*/ |
205
|
|
|
public function save( $items, bool $fetch = true ) |
206
|
|
|
{ |
207
|
|
|
foreach( map( $items ) as $item ) |
208
|
|
|
{ |
209
|
|
|
if( method_exists( $this, 'saveItem' ) ) { |
210
|
|
|
$this->saveItem( $item, $fetch ); |
|
|
|
|
211
|
|
|
} else { |
212
|
|
|
$this->saveBase( $item, $fetch ); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return is_array( $items ) ? map( $items ) : $items; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Searches for all items matching the given critera. |
222
|
|
|
* |
223
|
|
|
* @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc. |
224
|
|
|
* @param string[] $ref List of domains to fetch list items and referenced items for |
225
|
|
|
* @param int &$total Number of items that are available in total |
226
|
|
|
* @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys |
227
|
|
|
*/ |
228
|
|
|
public function search( \Aimeos\Base\Criteria\Iface $filter, array $ref = [], int &$total = null ) : \Aimeos\Map |
229
|
|
|
{ |
230
|
|
|
/** mshop/common/manager/search/mysql |
231
|
|
|
* Retrieves the records matched by the given criteria in the database |
232
|
|
|
* |
233
|
|
|
* @see mshop/common/manager/search/ansi |
234
|
|
|
*/ |
235
|
|
|
|
236
|
|
|
/** mshop/common/manager/search/ansi |
237
|
|
|
* Retrieves the records matched by the given criteria in the database |
238
|
|
|
* |
239
|
|
|
* Fetches the records matched by the given criteria from the |
240
|
|
|
* database. The records must be from one of the sites that are |
241
|
|
|
* configured via the context item. If the current site is part of |
242
|
|
|
* a tree of sites, the SELECT statement can retrieve all records |
243
|
|
|
* from the current site and the complete sub-tree of sites. |
244
|
|
|
* |
245
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
246
|
|
|
* their tables must be joined in the SQL context. This is done by |
247
|
|
|
* using the "internaldeps" property from the definition of the ID |
248
|
|
|
* column of the sub-managers. These internal dependencies specify |
249
|
|
|
* the JOIN between the tables and the used columns for joining. The |
250
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
251
|
|
|
* the sub-managers. |
252
|
|
|
* |
253
|
|
|
* To limit the records matched, conditions can be added to the given |
254
|
|
|
* criteria object. It can contain comparisons like column names that |
255
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
256
|
|
|
* operators. The resulting string of SQL conditions replaces the |
257
|
|
|
* ":cond" placeholder before the statement is sent to the database |
258
|
|
|
* server. |
259
|
|
|
* |
260
|
|
|
* If the records that are retrieved should be ordered by one or more |
261
|
|
|
* columns, the generated string of column / sort direction pairs |
262
|
|
|
* replaces the ":order" placeholder. In case no ordering is required, |
263
|
|
|
* the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/" |
264
|
|
|
* markers is removed to speed up retrieving the records. Columns of |
265
|
|
|
* sub-managers can also be used for ordering the result set but then |
266
|
|
|
* no index can be used. |
267
|
|
|
* |
268
|
|
|
* The number of returned records can be limited and can start at any |
269
|
|
|
* number between the begining and the end of the result set. For that |
270
|
|
|
* the ":size" and ":start" placeholders are replaced by the |
271
|
|
|
* corresponding values from the criteria object. The default values |
272
|
|
|
* are 0 for the start and 100 for the size value. |
273
|
|
|
* |
274
|
|
|
* The SQL statement should conform to the ANSI standard to be |
275
|
|
|
* compatible with most relational database systems. This also |
276
|
|
|
* includes using double quotes for table and column names. |
277
|
|
|
* |
278
|
|
|
* @param string SQL statement for searching items |
279
|
|
|
* @since 2023.10 |
280
|
|
|
* @category Developer |
281
|
|
|
* @see mshop/common/manager/insert/ansi |
282
|
|
|
* @see mshop/common/manager/update/ansi |
283
|
|
|
* @see mshop/common/manager/newid/ansi |
284
|
|
|
* @see mshop/common/manager/delete/ansi |
285
|
|
|
* @see mshop/common/manager/count/ansi |
286
|
|
|
*/ |
287
|
|
|
$cfgPathSearch = 'mshop/common/manager/search'; |
288
|
|
|
|
289
|
|
|
/** mshop/common/manager/count/mysql |
290
|
|
|
* Counts the number of records matched by the given criteria in the database |
291
|
|
|
* |
292
|
|
|
* @see mshop/common/manager/count/ansi |
293
|
|
|
*/ |
294
|
|
|
|
295
|
|
|
/** mshop/common/manager/count/ansi |
296
|
|
|
* Counts the number of records matched by the given criteria in the database |
297
|
|
|
* |
298
|
|
|
* Counts all records matched by the given criteria from the |
299
|
|
|
* database. The records must be from one of the sites that are |
300
|
|
|
* configured via the context item. If the current site is part of |
301
|
|
|
* a tree of sites, the statement can count all records from the |
302
|
|
|
* current site and the complete sub-tree of sites. |
303
|
|
|
* |
304
|
|
|
* As the records can normally be limited by criteria from sub-managers, |
305
|
|
|
* their tables must be joined in the SQL context. This is done by |
306
|
|
|
* using the "internaldeps" property from the definition of the ID |
307
|
|
|
* column of the sub-managers. These internal dependencies specify |
308
|
|
|
* the JOIN between the tables and the used columns for joining. The |
309
|
|
|
* ":joins" placeholder is then replaced by the JOIN strings from |
310
|
|
|
* the sub-managers. |
311
|
|
|
* |
312
|
|
|
* To limit the records matched, conditions can be added to the given |
313
|
|
|
* criteria object. It can contain comparisons like column names that |
314
|
|
|
* must match specific values which can be combined by AND, OR or NOT |
315
|
|
|
* operators. The resulting string of SQL conditions replaces the |
316
|
|
|
* ":cond" placeholder before the statement is sent to the database |
317
|
|
|
* server. |
318
|
|
|
* |
319
|
|
|
* Both, the strings for ":joins" and for ":cond" are the same as for |
320
|
|
|
* the "search" SQL statement. |
321
|
|
|
* |
322
|
|
|
* Contrary to the "search" statement, it doesn't return any records |
323
|
|
|
* but instead the number of records that have been found. As counting |
324
|
|
|
* thousands of records can be a long running task, the maximum number |
325
|
|
|
* of counted records is limited for performance reasons. |
326
|
|
|
* |
327
|
|
|
* The SQL statement should conform to the ANSI standard to be |
328
|
|
|
* compatible with most relational database systems. This also |
329
|
|
|
* includes using double quotes for table and column names. |
330
|
|
|
* |
331
|
|
|
* @param string SQL statement for counting items |
332
|
|
|
* @since 2023.10 |
333
|
|
|
* @category Developer |
334
|
|
|
* @see mshop/common/manager/insert/ansi |
335
|
|
|
* @see mshop/common/manager/update/ansi |
336
|
|
|
* @see mshop/common/manager/newid/ansi |
337
|
|
|
* @see mshop/common/manager/delete/ansi |
338
|
|
|
* @see mshop/common/manager/search/ansi |
339
|
|
|
*/ |
340
|
|
|
$cfgPathCount = 'mshop/common/manager/count'; |
341
|
|
|
|
342
|
|
|
$items = []; |
343
|
|
|
$level = $this->getSiteMode(); |
344
|
|
|
$required = [$this->getSearchKey()]; |
345
|
|
|
$conn = $this->context()->db( $this->getResourceName() ); |
346
|
|
|
|
347
|
|
|
$results = $this->searchItemsBase( $conn, $filter, $cfgPathSearch, $cfgPathCount, $required, $total, $level ); |
348
|
|
|
|
349
|
|
|
while( $row = $results->fetch() ) |
350
|
|
|
{ |
351
|
|
|
if( $item = $this->applyFilter( $this->create( $row ) ) ) { |
352
|
|
|
$items[$row['id']] = $item; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return map( $items ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Starts a database transaction on the connection identified by the given name |
362
|
|
|
* |
363
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
364
|
|
|
*/ |
365
|
|
|
public function begin() : \Aimeos\MShop\Common\Manager\Iface |
366
|
|
|
{ |
367
|
|
|
$this->context->db( $this->getResourceName() )->begin(); |
368
|
|
|
return $this; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Commits the running database transaction on the connection identified by the given name |
374
|
|
|
* |
375
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
376
|
|
|
*/ |
377
|
|
|
public function commit() : \Aimeos\MShop\Common\Manager\Iface |
378
|
|
|
{ |
379
|
|
|
$this->context->db( $this->getResourceName() )->commit(); |
380
|
|
|
return $this; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Rolls back the running database transaction on the connection identified by the given name |
386
|
|
|
* |
387
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
388
|
|
|
*/ |
389
|
|
|
public function rollback() : \Aimeos\MShop\Common\Manager\Iface |
390
|
|
|
{ |
391
|
|
|
$this->context->db( $this->getResourceName() )->rollback(); |
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Returns the context object. |
398
|
|
|
* |
399
|
|
|
* @return \Aimeos\MShop\ContextIface Context object |
400
|
|
|
*/ |
401
|
|
|
protected function context() : \Aimeos\MShop\ContextIface |
402
|
|
|
{ |
403
|
|
|
return $this->context; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Returns the site mode constant for inheritance/aggregation |
409
|
|
|
* |
410
|
|
|
* @return int Site mode constant (default: SITE_ALL for inheritance and aggregation) |
411
|
|
|
*/ |
412
|
|
|
protected function getSiteMode() : int |
413
|
|
|
{ |
414
|
|
|
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL; |
415
|
|
|
return $this->context()->config()->get( $this->getConfigKey( 'sitemode' ), $level ); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|