Passed
Push — master ( 23d0a5...26f6d6 )
by Aimeos
05:18
created

lib/mshoplib/src/MShop/Media/Manager/Standard.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package MShop
8
 * @subpackage Media
9
 */
10
11
12
namespace Aimeos\MShop\Media\Manager;
13
14
15
/**
16
 * Default media manager implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Media
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
23
	implements \Aimeos\MShop\Media\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	use \Aimeos\MShop\Common\Manager\ListRef\Traits;
26
	use \Aimeos\MShop\Common\Manager\PropertyRef\Traits;
27
28
29
	private $searchConfig = array(
30
		'media.id' => array(
31
			'label' => 'ID',
32
			'code' => 'media.id',
33
			'internalcode' => 'mmed."id"',
34
			'type' => 'integer',
35
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
36
		),
37
		'media.siteid' => array(
38
			'label' => 'Site ID',
39
			'code' => 'media.siteid',
40
			'internalcode' => 'mmed."siteid"',
41
			'type' => 'integer',
42
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
43
			'public' => false,
44
		),
45
		'media.type' => array(
46
			'label' => 'Type',
47
			'code' => 'media.type',
48
			'internalcode' => 'mmed."type"',
49
			'type' => 'string',
50
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
51
		),
52
		'media.label' => array(
53
			'label' => 'Label',
54
			'code' => 'media.label',
55
			'internalcode' => 'mmed."label"',
56
			'type' => 'string',
57
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
58
		),
59
		'media.domain' => array(
60
			'label' => 'Domain',
61
			'code' => 'media.domain',
62
			'internalcode' => 'mmed."domain"',
63
			'type' => 'string',
64
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
65
		),
66
		'media.languageid' => array(
67
			'label' => 'Language code',
68
			'code' => 'media.languageid',
69
			'internalcode' => 'mmed."langid"',
70
			'type' => 'string',
71
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
72
		),
73
		'media.mimetype' => array(
74
			'label' => 'Mime type',
75
			'code' => 'media.mimetype',
76
			'internalcode' => 'mmed."mimetype"',
77
			'type' => 'string',
78
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
79
		),
80
		'media.url' => array(
81
			'label' => 'URL',
82
			'code' => 'media.url',
83
			'internalcode' => 'mmed."link"',
84
			'type' => 'string',
85
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
86
		),
87
		'media.preview' => array(
88
			'label' => 'Preview URLs as JSON encoded string',
89
			'code' => 'media.preview',
90
			'internalcode' => 'mmed."preview"',
91
			'type' => 'string',
92
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
93
		),
94
		'media.status' => array(
95
			'label' => 'Status',
96
			'code' => 'media.status',
97
			'internalcode' => 'mmed."status"',
98
			'type' => 'integer',
99
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
100
		),
101
		'media.ctime' => array(
102
			'code' => 'media.ctime',
103
			'internalcode' => 'mmed."ctime"',
104
			'label' => 'Create date/time',
105
			'type' => 'datetime',
106
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
107
			'public' => false,
108
		),
109
		'media.mtime' => array(
110
			'code' => 'media.mtime',
111
			'internalcode' => 'mmed."mtime"',
112
			'label' => 'Modify date/time',
113
			'type' => 'datetime',
114
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
115
			'public' => false,
116
		),
117
		'media.editor' => array(
118
			'code' => 'media.editor',
119
			'internalcode' => 'mmed."editor"',
120
			'label' => 'Editor',
121
			'type' => 'string',
122
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
123
			'public' => false,
124
		),
125
		'media:has' => array(
126
			'code' => 'media:has()',
127
			'internalcode' => '(
128
				SELECT mmedli_has."id" FROM mshop_media_list AS mmedli_has
129
				WHERE mmed."id" = mmedli_has."parentid" AND :site AND :key LIMIT 1
130
			)',
131
			'label' => 'Media has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
132
			'type' => 'null',
133
			'internaltype' => 'null',
134
			'public' => false,
135
		),
136
		'media:prop' => array(
137
			'code' => 'media:prop()',
138
			'internalcode' => '(
139
				SELECT mmedpr_prop."id" FROM mshop_media_property AS mmedpr_prop
140
				WHERE mmed."id" = mmedpr_prop."parentid" AND :site AND :key LIMIT 1
141
			)',
142
			'label' => 'Media has property item, parameter(<property type>[,<language code>[,<property value>]])',
143
			'type' => 'null',
144
			'internaltype' => 'null',
145
			'public' => false,
146
		),
147
	);
148
149
	private $languageId;
150
151
152
	/**
153
	 * Initializes the object.
154
	 *
155
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
156
	 */
157
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
158
	{
159
		parent::__construct( $context );
160
		$this->setResourceName( 'db-media' );
161
162
		$self = $this;
163
		$locale = $context->getLocale();
164
		$this->languageId = $locale->getLanguageId();
165
166
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
167
		$level = $context->getConfig()->get( 'mshop/media/manager/sitemode', $level );
168
169
		$siteIds = [$locale->getSiteId()];
170
171
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_PATH ) {
172
			$siteIds = array_merge( $siteIds, $locale->getSitePath() );
173
		}
174
175
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) {
176
			$siteIds = array_merge( $siteIds, $locale->getSiteSubTree() );
177
		}
178
179
180
		$this->searchConfig['media:has']['function'] = function( &$source, array $params ) use ( $self, $siteIds ) {
181
182
			foreach( $params as $key => $param ) {
183
				$params[$key] = trim( $param, '\'' );
184
			}
185
186
			$source = str_replace( ':site', $self->toExpression( 'mmedli_has."siteid"', $siteIds ), $source );
187
			$str = $self->toExpression( 'mmedli_has."key"', join( '|', $params ), isset( $params[2] ) ? '==' : '=~' );
188
			$source = str_replace( ':key', $str, $source );
189
190
			return $params;
191
		};
192
193
194
		$this->searchConfig['media:prop']['function'] = function( &$source, array $params ) use ( $self, $siteIds ) {
195
196
			foreach( $params as $key => $param ) {
197
				$params[$key] = trim( $param, '\'' );
198
			}
199
200
			$params[2] = ( isset( $params[2] ) ? md5( $params[2] ) : null );
201
			$source = str_replace( ':site', $self->toExpression( 'mmedpr_prop."siteid"', $siteIds ), $source );
202
			$str = $self->toExpression( 'mmedpr_prop."key"', join( '|', $params ), isset( $params[2] ) ? '==' : '=~' );
203
			$source = str_replace( ':key', $str, $source );
204
205
			return $params;
206
		};
207
	}
208
209
210
	/**
211
	 * Removes old entries from the storage.
212
	 *
213
	 * @param string[] $siteids List of IDs for sites whose entries should be deleted
214
	 * @return \Aimeos\MShop\Media\Manager\Iface Manager object for chaining method calls
215
	 */
216
	public function cleanup( array $siteids )
217
	{
218
		$path = 'mshop/media/manager/submanagers';
219
		$default = ['lists', 'property', 'type'];
220
221
		foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) {
222
			$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
223
		}
224
225
		return $this->cleanupBase( $siteids, 'mshop/media/manager/standard/delete' );
226
	}
227
228
229
	/**
230
	 * Creates a new empty item instance
231
	 *
232
	 * @param array $values Values the item should be initialized with
233
	 * @return \Aimeos\MShop\Media\Item\Iface New media item object
234
	 */
235
	public function createItem( array $values = [] )
236
	{
237
		$values['media.siteid'] = $this->getContext()->getLocale()->getSiteId();
238
		return $this->createItemBase( $values );
239
	}
240
241
242
	/**
243
	 * Returns the available manager types
244
	 *
245
	 * @param boolean $withsub Return also the resource type of sub-managers if true
246
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
247
	 */
248
	public function getResourceType( $withsub = true )
249
	{
250
		$path = 'mshop/media/manager/submanagers';
251
		$default = ['lists', 'property'];
252
253
		return $this->getResourceTypeBase( 'media', $path, $default, $withsub );
254
	}
255
256
257
	/**
258
	 * Returns the attributes that can be used for searching.
259
	 *
260
	 * @param boolean $withsub Return also attributes of sub-managers if true
261
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items
262
	 */
263
	public function getSearchAttributes( $withsub = true )
264
	{
265
		/** mshop/media/manager/submanagers
266
		 * List of manager names that can be instantiated by the media manager
267
		 *
268
		 * Managers provide a generic interface to the underlying storage.
269
		 * Each manager has or can have sub-managers caring about particular
270
		 * aspects. Each of these sub-managers can be instantiated by its
271
		 * parent manager using the getSubManager() method.
272
		 *
273
		 * The search keys from sub-managers can be normally used in the
274
		 * manager as well. It allows you to search for items of the manager
275
		 * using the search keys of the sub-managers to further limit the
276
		 * retrieved list of items.
277
		 *
278
		 * @param array List of sub-manager names
279
		 * @since 2014.03
280
		 * @category Developer
281
		 */
282
		$path = 'mshop/media/manager/submanagers';
283
284
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
285
	}
286
287
288
	/**
289
	 * Removes multiple items specified by ids in the array.
290
	 *
291
	 * @param string[] $ids List of IDs
292
	 * @return \Aimeos\MShop\Media\Manager\Iface Manager object for chaining method calls
293
	 */
294
	public function deleteItems( array $ids )
295
	{
296
		/** mshop/media/manager/standard/delete/mysql
297
		 * Deletes the items matched by the given IDs from the database
298
		 *
299
		 * @see mshop/media/manager/standard/delete/ansi
300
		 */
301
302
		/** mshop/media/manager/standard/delete/ansi
303
		 * Deletes the items matched by the given IDs from the database
304
		 *
305
		 * Removes the records specified by the given IDs from the media database.
306
		 * The records must be from the site that is configured via the
307
		 * context item.
308
		 *
309
		 * The ":cond" placeholder is replaced by the name of the ID column and
310
		 * the given ID or list of IDs while the site ID is bound to the question
311
		 * mark.
312
		 *
313
		 * The SQL statement should conform to the ANSI standard to be
314
		 * compatible with most relational database systems. This also
315
		 * includes using double quotes for table and column names.
316
		 *
317
		 * @param string SQL statement for deleting items
318
		 * @since 2014.03
319
		 * @category Developer
320
		 * @see mshop/media/manager/standard/insert/ansi
321
		 * @see mshop/media/manager/standard/update/ansi
322
		 * @see mshop/media/manager/standard/newid/ansi
323
		 * @see mshop/media/manager/standard/search/ansi
324
		 * @see mshop/media/manager/standard/count/ansi
325
		 */
326
		$path = 'mshop/media/manager/standard/delete';
327
328
		return $this->deleteItemsBase( $ids, $path );
329
	}
330
331
332
	/**
333
	 * Returns an item for the given ID.
334
	 *
335
	 * @param string $id ID of the item that should be retrieved
336
	 * @param string[] $ref List of domains to fetch list items and referenced items for
337
	 * @param boolean $default Add default criteria
338
	 * @return \Aimeos\MShop\Media\Item\Iface Returns the media item of the given id
339
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
340
	 */
341
	public function getItem( $id, array $ref = [], $default = false )
342
	{
343
		return $this->getItemBase( 'media.id', $id, $ref, $default );
344
	}
345
346
347
	/**
348
	 * Adds a new item to the storage or updates an existing one.
349
	 *
350
	 * @param \Aimeos\MShop\Media\Item\Iface $item New item that should be saved to the storage
351
	 * @param boolean $fetch True if the new ID should be returned in the item
352
	 * @return \Aimeos\MShop\Media\Item\Iface $item Updated item including the generated ID
353
	 */
354
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
355
	{
356
		self::checkClass( \Aimeos\MShop\Media\Item\Iface::class, $item );
357
358
		if( !$item->isModified() )
359
		{
360
			$item = $this->savePropertyItems( $item, 'media', $fetch );
361
			return $this->saveListItems( $item, 'media', $fetch );
362
		}
363
364
		$context = $this->getContext();
365
366
		$dbm = $context->getDatabaseManager();
367
		$dbname = $this->getResourceName();
368
		$conn = $dbm->acquire( $dbname );
369
370
		try
371
		{
372
			$id = $item->getId();
373
			$date = date( 'Y-m-d H:i:s' );
374
			$columns = $this->getObject()->getSaveAttributes();
1 ignored issue
show
The method getSaveAttributes() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean getSearchAttributes()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

374
			$columns = $this->getObject()->/** @scrutinizer ignore-call */ getSaveAttributes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
375
376
			if( $id === null )
377
			{
378
				/** mshop/media/manager/standard/insert/mysql
379
				 * Inserts a new media record into the database table
380
				 *
381
				 * @see mshop/media/manager/standard/insert/ansi
382
				 */
383
384
				/** mshop/media/manager/standard/insert/ansi
385
				 * Inserts a new media record into the database table
386
				 *
387
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
388
				 * the database and the newly created ID retrieved afterwards
389
				 * using the "newid" SQL statement.
390
				 *
391
				 * The SQL statement must be a string suitable for being used as
392
				 * prepared statement. It must include question marks for binding
393
				 * the values from the media item to the statement before they are
394
				 * sent to the database server. The number of question marks must
395
				 * be the same as the number of columns listed in the INSERT
396
				 * statement. The order of the columns must correspond to the
397
				 * order in the saveItems() method, so the correct values are
398
				 * bound to the columns.
399
				 *
400
				 * The SQL statement should conform to the ANSI standard to be
401
				 * compatible with most relational database systems. This also
402
				 * includes using double quotes for table and column names.
403
				 *
404
				 * @param string SQL statement for inserting records
405
				 * @since 2014.03
406
				 * @category Developer
407
				 * @see mshop/media/manager/standard/update/ansi
408
				 * @see mshop/media/manager/standard/newid/ansi
409
				 * @see mshop/media/manager/standard/delete/ansi
410
				 * @see mshop/media/manager/standard/search/ansi
411
				 * @see mshop/media/manager/standard/count/ansi
412
				 */
413
				$path = 'mshop/media/manager/standard/insert';
414
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
415
			}
416
			else
417
			{
418
				/** mshop/media/manager/standard/update/mysql
419
				 * Updates an existing media record in the database
420
				 *
421
				 * @see mshop/media/manager/standard/update/ansi
422
				 */
423
424
				/** mshop/media/manager/standard/update/ansi
425
				 * Updates an existing media record in the database
426
				 *
427
				 * Items which already have an ID (i.e. the ID is not NULL) will
428
				 * be updated in the database.
429
				 *
430
				 * The SQL statement must be a string suitable for being used as
431
				 * prepared statement. It must include question marks for binding
432
				 * the values from the media item to the statement before they are
433
				 * sent to the database server. The order of the columns must
434
				 * correspond to the order in the saveItems() method, so the
435
				 * correct values are bound to the columns.
436
				 *
437
				 * The SQL statement should conform to the ANSI standard to be
438
				 * compatible with most relational database systems. This also
439
				 * includes using double quotes for table and column names.
440
				 *
441
				 * @param string SQL statement for updating records
442
				 * @since 2014.03
443
				 * @category Developer
444
				 * @see mshop/media/manager/standard/insert/ansi
445
				 * @see mshop/media/manager/standard/newid/ansi
446
				 * @see mshop/media/manager/standard/delete/ansi
447
				 * @see mshop/media/manager/standard/search/ansi
448
				 * @see mshop/media/manager/standard/count/ansi
449
				 */
450
				$path = 'mshop/media/manager/standard/update';
451
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
452
			}
453
454
			$idx = 1;
455
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
456
457
			foreach( $columns as $name => $entry ) {
458
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
459
			}
460
461
			$stmt->bind( $idx++, $item->getLanguageId() );
462
			$stmt->bind( $idx++, $item->getType() );
463
			$stmt->bind( $idx++, $item->getLabel() );
464
			$stmt->bind( $idx++, $item->getMimeType() );
465
			$stmt->bind( $idx++, $item->getUrl() );
466
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
467
			$stmt->bind( $idx++, $item->getDomain() );
468
			$stmt->bind( $idx++, json_encode( $item->getPreviews(), JSON_FORCE_OBJECT ) );
469
			$stmt->bind( $idx++, $date ); // mtime
470
			$stmt->bind( $idx++, $context->getEditor() );
471
			$stmt->bind( $idx++, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
472
473
			if( $id !== null ) {
474
				$stmt->bind( $idx++, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
475
				$item->setId( $id ); //is not modified anymore
476
			} else {
477
				$stmt->bind( $idx++, $date ); // ctime
478
			}
479
480
			$stmt->execute()->finish();
481
482
			if( $id === null )
483
			{
484
				/** mshop/media/manager/standard/newid/mysql
485
				 * Retrieves the ID generated by the database when inserting a new record
486
				 *
487
				 * @see mshop/media/manager/standard/newid/ansi
488
				 */
489
490
				/** mshop/media/manager/standard/newid/ansi
491
				 * Retrieves the ID generated by the database when inserting a new record
492
				 *
493
				 * As soon as a new record is inserted into the database table,
494
				 * the database server generates a new and unique identifier for
495
				 * that record. This ID can be used for retrieving, updating and
496
				 * deleting that specific record from the table again.
497
				 *
498
				 * For MySQL:
499
				 *  SELECT LAST_INSERT_ID()
500
				 * For PostgreSQL:
501
				 *  SELECT currval('seq_mmed_id')
502
				 * For SQL Server:
503
				 *  SELECT SCOPE_IDENTITY()
504
				 * For Oracle:
505
				 *  SELECT "seq_mmed_id".CURRVAL FROM DUAL
506
				 *
507
				 * There's no way to retrive the new ID by a SQL statements that
508
				 * fits for most database servers as they implement their own
509
				 * specific way.
510
				 *
511
				 * @param string SQL statement for retrieving the last inserted record ID
512
				 * @since 2014.03
513
				 * @category Developer
514
				 * @see mshop/media/manager/standard/insert/ansi
515
				 * @see mshop/media/manager/standard/update/ansi
516
				 * @see mshop/media/manager/standard/delete/ansi
517
				 * @see mshop/media/manager/standard/search/ansi
518
				 * @see mshop/media/manager/standard/count/ansi
519
				 */
520
				$path = 'mshop/media/manager/standard/newid';
521
				$item->setId( $this->newId( $conn, $path ) );
522
			}
523
524
			$dbm->release( $conn, $dbname );
525
		}
526
		catch( \Exception $e )
527
		{
528
			$dbm->release( $conn, $dbname );
529
			throw $e;
530
		}
531
532
		$item = $this->savePropertyItems( $item, 'media', $fetch );
533
		return $this->saveListItems( $item, 'media', $fetch );
534
	}
535
536
537
	/**
538
	 * Returns the item objects matched by the given search criteria.
539
	 *
540
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
541
	 * @param string[] $ref List of domains to fetch list items and referenced items for
542
	 * @param integer|null &$total Number of items that are available in total
543
	 * @return \Aimeos\MShop\Media\Item\Iface[] List of media items
544
	 */
545
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
546
	{
547
		$map = [];
548
		$context = $this->getContext();
549
550
		$dbm = $context->getDatabaseManager();
551
		$dbname = $this->getResourceName();
552
		$conn = $dbm->acquire( $dbname );
553
554
		try
555
		{
556
			$required = array( 'media' );
557
558
			/** mshop/media/manager/sitemode
559
			 * Mode how items from levels below or above in the site tree are handled
560
			 *
561
			 * By default, only items from the current site are fetched from the
562
			 * storage. If the ai-sites extension is installed, you can create a
563
			 * tree of sites. Then, this setting allows you to define for the
564
			 * whole media domain if items from parent sites are inherited,
565
			 * sites from child sites are aggregated or both.
566
			 *
567
			 * Available constants for the site mode are:
568
			 * * 0 = only items from the current site
569
			 * * 1 = inherit items from parent sites
570
			 * * 2 = aggregate items from child sites
571
			 * * 3 = inherit and aggregate items at the same time
572
			 *
573
			 * You also need to set the mode in the locale manager
574
			 * (mshop/locale/manager/standard/sitelevel) to one of the constants.
575
			 * If you set it to the same value, it will work as described but you
576
			 * can also use different modes. For example, if inheritance and
577
			 * aggregation is configured the locale manager but only inheritance
578
			 * in the domain manager because aggregating items makes no sense in
579
			 * this domain, then items wil be only inherited. Thus, you have full
580
			 * control over inheritance and aggregation in each domain.
581
			 *
582
			 * @param integer Constant from Aimeos\MShop\Locale\Manager\Base class
583
			 * @category Developer
584
			 * @since 2018.01
585
			 * @see mshop/locale/manager/standard/sitelevel
586
			 */
587
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
588
			$level = $context->getConfig()->get( 'mshop/media/manager/sitemode', $level );
589
590
			/** mshop/media/manager/standard/search/mysql
591
			 * Retrieves the records matched by the given criteria in the database
592
			 *
593
			 * @see mshop/media/manager/standard/search/ansi
594
			 */
595
596
			/** mshop/media/manager/standard/search/ansi
597
			 * Retrieves the records matched by the given criteria in the database
598
			 *
599
			 * Fetches the records matched by the given criteria from the media
600
			 * database. The records must be from one of the sites that are
601
			 * configured via the context item. If the current site is part of
602
			 * a tree of sites, the SELECT statement can retrieve all records
603
			 * from the current site and the complete sub-tree of sites.
604
			 *
605
			 * As the records can normally be limited by criteria from sub-managers,
606
			 * their tables must be joined in the SQL context. This is done by
607
			 * using the "internaldeps" property from the definition of the ID
608
			 * column of the sub-managers. These internal dependencies specify
609
			 * the JOIN between the tables and the used columns for joining. The
610
			 * ":joins" placeholder is then replaced by the JOIN strings from
611
			 * the sub-managers.
612
			 *
613
			 * To limit the records matched, conditions can be added to the given
614
			 * criteria object. It can contain comparisons like column names that
615
			 * must match specific values which can be combined by AND, OR or NOT
616
			 * operators. The resulting string of SQL conditions replaces the
617
			 * ":cond" placeholder before the statement is sent to the database
618
			 * server.
619
			 *
620
			 * If the records that are retrieved should be ordered by one or more
621
			 * columns, the generated string of column / sort direction pairs
622
			 * replaces the ":order" placeholder. In case no ordering is required,
623
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
624
			 * markers is removed to speed up retrieving the records. Columns of
625
			 * sub-managers can also be used for ordering the result set but then
626
			 * no index can be used.
627
			 *
628
			 * The number of returned records can be limited and can start at any
629
			 * number between the begining and the end of the result set. For that
630
			 * the ":size" and ":start" placeholders are replaced by the
631
			 * corresponding values from the criteria object. The default values
632
			 * are 0 for the start and 100 for the size value.
633
			 *
634
			 * The SQL statement should conform to the ANSI standard to be
635
			 * compatible with most relational database systems. This also
636
			 * includes using double quotes for table and column names.
637
			 *
638
			 * @param string SQL statement for searching items
639
			 * @since 2014.03
640
			 * @category Developer
641
			 * @see mshop/media/manager/standard/insert/ansi
642
			 * @see mshop/media/manager/standard/update/ansi
643
			 * @see mshop/media/manager/standard/newid/ansi
644
			 * @see mshop/media/manager/standard/delete/ansi
645
			 * @see mshop/media/manager/standard/count/ansi
646
			 */
647
			$cfgPathSearch = 'mshop/media/manager/standard/search';
648
649
			/** mshop/media/manager/standard/count/mysql
650
			 * Counts the number of records matched by the given criteria in the database
651
			 *
652
			 * @see mshop/media/manager/standard/count/ansi
653
			 */
654
655
			/** mshop/media/manager/standard/count/ansi
656
			 * Counts the number of records matched by the given criteria in the database
657
			 *
658
			 * Counts all records matched by the given criteria from the media
659
			 * database. The records must be from one of the sites that are
660
			 * configured via the context item. If the current site is part of
661
			 * a tree of sites, the statement can count all records from the
662
			 * current site and the complete sub-tree of sites.
663
			 *
664
			 * As the records can normally be limited by criteria from sub-managers,
665
			 * their tables must be joined in the SQL context. This is done by
666
			 * using the "internaldeps" property from the definition of the ID
667
			 * column of the sub-managers. These internal dependencies specify
668
			 * the JOIN between the tables and the used columns for joining. The
669
			 * ":joins" placeholder is then replaced by the JOIN strings from
670
			 * the sub-managers.
671
			 *
672
			 * To limit the records matched, conditions can be added to the given
673
			 * criteria object. It can contain comparisons like column names that
674
			 * must match specific values which can be combined by AND, OR or NOT
675
			 * operators. The resulting string of SQL conditions replaces the
676
			 * ":cond" placeholder before the statement is sent to the database
677
			 * server.
678
			 *
679
			 * Both, the strings for ":joins" and for ":cond" are the same as for
680
			 * the "search" SQL statement.
681
			 *
682
			 * Contrary to the "search" statement, it doesn't return any records
683
			 * but instead the number of records that have been found. As counting
684
			 * thousands of records can be a long running task, the maximum number
685
			 * of counted records is limited for performance reasons.
686
			 *
687
			 * The SQL statement should conform to the ANSI standard to be
688
			 * compatible with most relational database systems. This also
689
			 * includes using double quotes for table and column names.
690
			 *
691
			 * @param string SQL statement for counting items
692
			 * @since 2014.03
693
			 * @category Developer
694
			 * @see mshop/media/manager/standard/insert/ansi
695
			 * @see mshop/media/manager/standard/update/ansi
696
			 * @see mshop/media/manager/standard/newid/ansi
697
			 * @see mshop/media/manager/standard/delete/ansi
698
			 * @see mshop/media/manager/standard/search/ansi
699
			 */
700
			$cfgPathCount = 'mshop/media/manager/standard/count';
701
702
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
703
704
			while( ( $row = $results->fetch() ) !== false )
705
			{
706
				if( ( $row['media.previews'] = json_decode( $config = $row['media.previews'], true ) ) === null )
707
				{
708
					$msg = sprintf( 'Invalid JSON as result of search for ID "%2$s" in "%1$s": %3$s', 'mshop_media.config', $row['media.id'], $config );
709
					$this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::WARN );
710
				}
711
				$map[(string) $row['media.id']] = $row;
712
			}
713
714
			$dbm->release( $conn, $dbname );
715
		}
716
		catch( \Exception $e )
717
		{
718
			$dbm->release( $conn, $dbname );
719
			throw $e;
720
		}
721
722
		$propItems = $this->getPropertyItems( array_keys( $map ), 'media' );
723
724
		return $this->buildItems( $map, null, 'media', $propItems );
725
	}
726
727
728
	/**
729
	 * Creates a search critera object
730
	 *
731
	 * @param boolean $default Add default criteria (optional)
732
	 * @return \Aimeos\MW\Criteria\Iface New search criteria object
733
	 */
734
	public function createSearch( $default = false )
735
	{
736
		if( $default === true )
737
		{
738
			$object = $this->createSearchBase( 'media' );
739
			$langid = $this->getContext()->getLocale()->getLanguageId();
740
741
			if( $langid !== null )
742
			{
743
				$temp = array(
744
					$object->compare( '==', 'media.languageid', $langid ),
745
					$object->compare( '==', 'media.languageid', null ),
746
				);
747
748
				$expr = array(
749
					$object->getConditions(),
750
					$object->combine( '||', $temp ),
751
				);
752
753
				$object->setConditions( $object->combine( '&&', $expr ) );
754
			}
755
756
			return $object;
757
		}
758
759
		return parent::createSearch();
760
	}
761
762
763
	/**
764
	 * Returns a new manager for media extensions
765
	 *
766
	 * @param string $manager Name of the sub manager type in lower case
767
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
768
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc.
769
	 */
770
	public function getSubManager( $manager, $name = null )
771
	{
772
		return $this->getSubManagerBase( 'media', $manager, $name );
773
	}
774
775
776
	/**
777
	 * Creates a new media item instance.
778
	 *
779
	 * @param array $values Associative list of key/value pairs
780
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
781
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of items referenced
782
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
783
	 * @return \Aimeos\MShop\Media\Item\Iface New media item
784
	 */
785
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [], array $propItems = [] )
786
	{
787
		$values['.languageid'] = $this->languageId;
788
789
		return new \Aimeos\MShop\Media\Item\Standard( $values, $listItems, $refItems, $propItems );
790
	}
791
}
792