Passed
Push — master ( 0dde8b...fa860a )
by Aimeos
04:21
created

Standard::__construct()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 23
nc 4
nop 1
dl 0
loc 43
rs 8.4444
c 0
b 0
f 0
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 URL',
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 mmedli_has."domain" = $1 :type :refid
130
				LIMIT 1
131
			)',
132
			'label' => 'Media has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
133
			'type' => 'null',
134
			'internaltype' => 'null',
135
			'public' => false,
136
		),
137
		'media:prop' => array(
138
			'code' => 'media:prop()',
139
			'internalcode' => '(
140
				SELECT mmedpr_prop."id" FROM mshop_media_property AS mmedpr_prop
141
				WHERE mmed."id" = mmedpr_prop."parentid" AND :site AND mmedpr_prop."type" = $1 :langid :value
142
				LIMIT 1
143
			)',
144
			'label' => 'Media has property item, parameter(<property type>[,<language code>[,<property value>]])',
145
			'type' => 'null',
146
			'internaltype' => 'null',
147
			'public' => false,
148
		),
149
	);
150
151
	private $languageId;
152
153
154
	/**
155
	 * Initializes the object.
156
	 *
157
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
158
	 */
159
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
160
	{
161
		parent::__construct( $context );
162
		$this->setResourceName( 'db-media' );
163
164
		$locale = $context->getLocale();
165
		$this->languageId = $locale->getLanguageId();
166
167
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
168
		$level = $context->getConfig()->get( 'mshop/media/manager/sitemode', $level );
169
170
		$siteIds = [$locale->getSiteId()];
171
172
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_PATH ) {
173
			$siteIds = array_merge( $siteIds, $locale->getSitePath() );
174
		}
175
176
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) {
177
			$siteIds = array_merge( $siteIds, $locale->getSiteSubTree() );
178
		}
179
180
		$this->replaceSiteMarker( $this->searchConfig['media:has'], 'mmedli_has."siteid"', $siteIds, ':site' );
181
		$this->replaceSiteMarker( $this->searchConfig['media:prop'], 'mmedpr_prop."siteid"', $siteIds, ':site' );
182
183
184
		$this->searchConfig['media:has']['function'] = function( &$source, array $params ) {
185
186
			$source = str_replace( ':type', isset( $params[1] ) ? 'AND mmedli_has."type" = $2' : '', $source );
187
			$source = str_replace( ':refid', isset( $params[2] ) ? 'AND mmedli_has."refid" = $3' : '', $source );
188
189
			return $params;
190
		};
191
192
193
		$this->searchConfig['media:prop']['function'] = function( &$source, array $params ) {
194
195
			$lang = 'AND mmedpr_prop."langid"';
196
			$lang = isset( $params[1] ) ? ( $params[1] !== 'null' ? $lang . ' = $2' : $lang . ' IS NULL' ) : '';
197
198
			$source = str_replace( ':langid', $lang, $source );
199
			$source = str_replace( ':value', isset( $params[2] ) ? 'AND mmedpr_prop."value" = $3' : '', $source );
200
201
			return $params;
202
		};
203
	}
204
205
206
	/**
207
	 * Removes old entries from the storage.
208
	 *
209
	 * @param string[] $siteids List of IDs for sites whose entries should be deleted
210
	 * @return \Aimeos\MShop\Media\Manager\Iface Manager object for chaining method calls
211
	 */
212
	public function cleanup( array $siteids )
213
	{
214
		$path = 'mshop/media/manager/submanagers';
215
		$default = ['lists', 'property', 'type'];
216
217
		foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) {
218
			$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
219
		}
220
221
		return $this->cleanupBase( $siteids, 'mshop/media/manager/standard/delete' );
222
	}
223
224
225
	/**
226
	 * Creates a new empty item instance
227
	 *
228
	 * @param array $values Values the item should be initialized with
229
	 * @return \Aimeos\MShop\Media\Item\Iface New media item object
230
	 */
231
	public function createItem( array $values = [] )
232
	{
233
		$values['media.siteid'] = $this->getContext()->getLocale()->getSiteId();
234
		return $this->createItemBase( $values );
235
	}
236
237
238
	/**
239
	 * Returns the available manager types
240
	 *
241
	 * @param boolean $withsub Return also the resource type of sub-managers if true
242
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
243
	 */
244
	public function getResourceType( $withsub = true )
245
	{
246
		$path = 'mshop/media/manager/submanagers';
247
		$default = ['lists', 'property'];
248
249
		return $this->getResourceTypeBase( 'media', $path, $default, $withsub );
250
	}
251
252
253
	/**
254
	 * Returns the attributes that can be used for searching.
255
	 *
256
	 * @param boolean $withsub Return also attributes of sub-managers if true
257
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items
258
	 */
259
	public function getSearchAttributes( $withsub = true )
260
	{
261
		/** mshop/media/manager/submanagers
262
		 * List of manager names that can be instantiated by the media manager
263
		 *
264
		 * Managers provide a generic interface to the underlying storage.
265
		 * Each manager has or can have sub-managers caring about particular
266
		 * aspects. Each of these sub-managers can be instantiated by its
267
		 * parent manager using the getSubManager() method.
268
		 *
269
		 * The search keys from sub-managers can be normally used in the
270
		 * manager as well. It allows you to search for items of the manager
271
		 * using the search keys of the sub-managers to further limit the
272
		 * retrieved list of items.
273
		 *
274
		 * @param array List of sub-manager names
275
		 * @since 2014.03
276
		 * @category Developer
277
		 */
278
		$path = 'mshop/media/manager/submanagers';
279
280
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
281
	}
282
283
284
	/**
285
	 * Removes multiple items specified by ids in the array.
286
	 *
287
	 * @param string[] $ids List of IDs
288
	 * @return \Aimeos\MShop\Media\Manager\Iface Manager object for chaining method calls
289
	 */
290
	public function deleteItems( array $ids )
291
	{
292
		/** mshop/media/manager/standard/delete/mysql
293
		 * Deletes the items matched by the given IDs from the database
294
		 *
295
		 * @see mshop/media/manager/standard/delete/ansi
296
		 */
297
298
		/** mshop/media/manager/standard/delete/ansi
299
		 * Deletes the items matched by the given IDs from the database
300
		 *
301
		 * Removes the records specified by the given IDs from the media database.
302
		 * The records must be from the site that is configured via the
303
		 * context item.
304
		 *
305
		 * The ":cond" placeholder is replaced by the name of the ID column and
306
		 * the given ID or list of IDs while the site ID is bound to the question
307
		 * mark.
308
		 *
309
		 * The SQL statement should conform to the ANSI standard to be
310
		 * compatible with most relational database systems. This also
311
		 * includes using double quotes for table and column names.
312
		 *
313
		 * @param string SQL statement for deleting items
314
		 * @since 2014.03
315
		 * @category Developer
316
		 * @see mshop/media/manager/standard/insert/ansi
317
		 * @see mshop/media/manager/standard/update/ansi
318
		 * @see mshop/media/manager/standard/newid/ansi
319
		 * @see mshop/media/manager/standard/search/ansi
320
		 * @see mshop/media/manager/standard/count/ansi
321
		 */
322
		$path = 'mshop/media/manager/standard/delete';
323
324
		return $this->deleteItemsBase( $ids, $path );
325
	}
326
327
328
	/**
329
	 * Returns an item for the given ID.
330
	 *
331
	 * @param string $id ID of the item that should be retrieved
332
	 * @param string[] $ref List of domains to fetch list items and referenced items for
333
	 * @param boolean $default Add default criteria
334
	 * @return \Aimeos\MShop\Media\Item\Iface Returns the media item of the given id
335
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
336
	 */
337
	public function getItem( $id, array $ref = [], $default = false )
338
	{
339
		return $this->getItemBase( 'media.id', $id, $ref, $default );
340
	}
341
342
343
	/**
344
	 * Adds a new item to the storage or updates an existing one.
345
	 *
346
	 * @param \Aimeos\MShop\Media\Item\Iface $item New item that should be saved to the storage
347
	 * @param boolean $fetch True if the new ID should be returned in the item
348
	 * @return \Aimeos\MShop\Media\Item\Iface $item Updated item including the generated ID
349
	 */
350
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
351
	{
352
		self::checkClass( \Aimeos\MShop\Media\Item\Iface::class, $item );
353
354
		if( !$item->isModified() )
355
		{
356
			$item = $this->savePropertyItems( $item, 'media', $fetch );
357
			return $this->saveListItems( $item, 'media', $fetch );
358
		}
359
360
		$context = $this->getContext();
361
362
		$dbm = $context->getDatabaseManager();
363
		$dbname = $this->getResourceName();
364
		$conn = $dbm->acquire( $dbname );
365
366
		try
367
		{
368
			$id = $item->getId();
369
			$date = date( 'Y-m-d H:i:s' );
370
371
			if( $id === null )
372
			{
373
				/** mshop/media/manager/standard/insert/mysql
374
				 * Inserts a new media record into the database table
375
				 *
376
				 * @see mshop/media/manager/standard/insert/ansi
377
				 */
378
379
				/** mshop/media/manager/standard/insert/ansi
380
				 * Inserts a new media record into the database table
381
				 *
382
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
383
				 * the database and the newly created ID retrieved afterwards
384
				 * using the "newid" SQL statement.
385
				 *
386
				 * The SQL statement must be a string suitable for being used as
387
				 * prepared statement. It must include question marks for binding
388
				 * the values from the media item to the statement before they are
389
				 * sent to the database server. The number of question marks must
390
				 * be the same as the number of columns listed in the INSERT
391
				 * statement. The order of the columns must correspond to the
392
				 * order in the saveItems() method, so the correct values are
393
				 * bound to the columns.
394
				 *
395
				 * The SQL statement should conform to the ANSI standard to be
396
				 * compatible with most relational database systems. This also
397
				 * includes using double quotes for table and column names.
398
				 *
399
				 * @param string SQL statement for inserting records
400
				 * @since 2014.03
401
				 * @category Developer
402
				 * @see mshop/media/manager/standard/update/ansi
403
				 * @see mshop/media/manager/standard/newid/ansi
404
				 * @see mshop/media/manager/standard/delete/ansi
405
				 * @see mshop/media/manager/standard/search/ansi
406
				 * @see mshop/media/manager/standard/count/ansi
407
				 */
408
				$path = 'mshop/media/manager/standard/insert';
409
			}
410
			else
411
			{
412
				/** mshop/media/manager/standard/update/mysql
413
				 * Updates an existing media record in the database
414
				 *
415
				 * @see mshop/media/manager/standard/update/ansi
416
				 */
417
418
				/** mshop/media/manager/standard/update/ansi
419
				 * Updates an existing media record in the database
420
				 *
421
				 * Items which already have an ID (i.e. the ID is not NULL) will
422
				 * be updated in the database.
423
				 *
424
				 * The SQL statement must be a string suitable for being used as
425
				 * prepared statement. It must include question marks for binding
426
				 * the values from the media item to the statement before they are
427
				 * sent to the database server. The order of the columns must
428
				 * correspond to the order in the saveItems() method, so the
429
				 * correct values are bound to the columns.
430
				 *
431
				 * The SQL statement should conform to the ANSI standard to be
432
				 * compatible with most relational database systems. This also
433
				 * includes using double quotes for table and column names.
434
				 *
435
				 * @param string SQL statement for updating records
436
				 * @since 2014.03
437
				 * @category Developer
438
				 * @see mshop/media/manager/standard/insert/ansi
439
				 * @see mshop/media/manager/standard/newid/ansi
440
				 * @see mshop/media/manager/standard/delete/ansi
441
				 * @see mshop/media/manager/standard/search/ansi
442
				 * @see mshop/media/manager/standard/count/ansi
443
				 */
444
				$path = 'mshop/media/manager/standard/update';
445
			}
446
447
			$stmt = $this->getCachedStatement( $conn, $path );
448
449
			$stmt->bind( 1, $item->getLanguageId() );
450
			$stmt->bind( 2, $item->getType() );
451
			$stmt->bind( 3, $item->getLabel() );
452
			$stmt->bind( 4, $item->getMimeType() );
453
			$stmt->bind( 5, $item->getUrl() );
454
			$stmt->bind( 6, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
455
			$stmt->bind( 7, $item->getDomain() );
456
			$stmt->bind( 8, $item->getPreview() );
457
			$stmt->bind( 9, $date ); // mtime
458
			$stmt->bind( 10, $context->getEditor() );
459
			$stmt->bind( 11, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
460
461
			if( $id !== null ) {
462
				$stmt->bind( 12, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
463
				$item->setId( $id ); //is not modified anymore
464
			} else {
465
				$stmt->bind( 12, $date ); // ctime
466
			}
467
468
			$stmt->execute()->finish();
469
470
			if( $id === null )
471
			{
472
				/** mshop/media/manager/standard/newid/mysql
473
				 * Retrieves the ID generated by the database when inserting a new record
474
				 *
475
				 * @see mshop/media/manager/standard/newid/ansi
476
				 */
477
478
				/** mshop/media/manager/standard/newid/ansi
479
				 * Retrieves the ID generated by the database when inserting a new record
480
				 *
481
				 * As soon as a new record is inserted into the database table,
482
				 * the database server generates a new and unique identifier for
483
				 * that record. This ID can be used for retrieving, updating and
484
				 * deleting that specific record from the table again.
485
				 *
486
				 * For MySQL:
487
				 *  SELECT LAST_INSERT_ID()
488
				 * For PostgreSQL:
489
				 *  SELECT currval('seq_mmed_id')
490
				 * For SQL Server:
491
				 *  SELECT SCOPE_IDENTITY()
492
				 * For Oracle:
493
				 *  SELECT "seq_mmed_id".CURRVAL FROM DUAL
494
				 *
495
				 * There's no way to retrive the new ID by a SQL statements that
496
				 * fits for most database servers as they implement their own
497
				 * specific way.
498
				 *
499
				 * @param string SQL statement for retrieving the last inserted record ID
500
				 * @since 2014.03
501
				 * @category Developer
502
				 * @see mshop/media/manager/standard/insert/ansi
503
				 * @see mshop/media/manager/standard/update/ansi
504
				 * @see mshop/media/manager/standard/delete/ansi
505
				 * @see mshop/media/manager/standard/search/ansi
506
				 * @see mshop/media/manager/standard/count/ansi
507
				 */
508
				$path = 'mshop/media/manager/standard/newid';
509
				$item->setId( $this->newId( $conn, $path ) );
510
			}
511
512
			$dbm->release( $conn, $dbname );
513
		}
514
		catch( \Exception $e )
515
		{
516
			$dbm->release( $conn, $dbname );
517
			throw $e;
518
		}
519
520
		$item = $this->savePropertyItems( $item, 'media', $fetch );
521
		return $this->saveListItems( $item, 'media', $fetch );
522
	}
523
524
525
	/**
526
	 * Returns the item objects matched by the given search criteria.
527
	 *
528
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
529
	 * @param string[] $ref List of domains to fetch list items and referenced items for
530
	 * @param integer|null &$total Number of items that are available in total
531
	 * @return \Aimeos\MShop\Media\Item\Iface[] List of media items
532
	 */
533
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
534
	{
535
		$map = [];
536
		$context = $this->getContext();
537
538
		$dbm = $context->getDatabaseManager();
539
		$dbname = $this->getResourceName();
540
		$conn = $dbm->acquire( $dbname );
541
542
		try
543
		{
544
			$required = array( 'media' );
545
546
			/** mshop/media/manager/sitemode
547
			 * Mode how items from levels below or above in the site tree are handled
548
			 *
549
			 * By default, only items from the current site are fetched from the
550
			 * storage. If the ai-sites extension is installed, you can create a
551
			 * tree of sites. Then, this setting allows you to define for the
552
			 * whole media domain if items from parent sites are inherited,
553
			 * sites from child sites are aggregated or both.
554
			 *
555
			 * Available constants for the site mode are:
556
			 * * 0 = only items from the current site
557
			 * * 1 = inherit items from parent sites
558
			 * * 2 = aggregate items from child sites
559
			 * * 3 = inherit and aggregate items at the same time
560
			 *
561
			 * You also need to set the mode in the locale manager
562
			 * (mshop/locale/manager/standard/sitelevel) to one of the constants.
563
			 * If you set it to the same value, it will work as described but you
564
			 * can also use different modes. For example, if inheritance and
565
			 * aggregation is configured the locale manager but only inheritance
566
			 * in the domain manager because aggregating items makes no sense in
567
			 * this domain, then items wil be only inherited. Thus, you have full
568
			 * control over inheritance and aggregation in each domain.
569
			 *
570
			 * @param integer Constant from Aimeos\MShop\Locale\Manager\Base class
571
			 * @category Developer
572
			 * @since 2018.01
573
			 * @see mshop/locale/manager/standard/sitelevel
574
			 */
575
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
576
			$level = $context->getConfig()->get( 'mshop/media/manager/sitemode', $level );
577
578
			/** mshop/media/manager/standard/search/mysql
579
			 * Retrieves the records matched by the given criteria in the database
580
			 *
581
			 * @see mshop/media/manager/standard/search/ansi
582
			 */
583
584
			/** mshop/media/manager/standard/search/ansi
585
			 * Retrieves the records matched by the given criteria in the database
586
			 *
587
			 * Fetches the records matched by the given criteria from the media
588
			 * database. The records must be from one of the sites that are
589
			 * configured via the context item. If the current site is part of
590
			 * a tree of sites, the SELECT statement can retrieve all records
591
			 * from the current site and the complete sub-tree of sites.
592
			 *
593
			 * As the records can normally be limited by criteria from sub-managers,
594
			 * their tables must be joined in the SQL context. This is done by
595
			 * using the "internaldeps" property from the definition of the ID
596
			 * column of the sub-managers. These internal dependencies specify
597
			 * the JOIN between the tables and the used columns for joining. The
598
			 * ":joins" placeholder is then replaced by the JOIN strings from
599
			 * the sub-managers.
600
			 *
601
			 * To limit the records matched, conditions can be added to the given
602
			 * criteria object. It can contain comparisons like column names that
603
			 * must match specific values which can be combined by AND, OR or NOT
604
			 * operators. The resulting string of SQL conditions replaces the
605
			 * ":cond" placeholder before the statement is sent to the database
606
			 * server.
607
			 *
608
			 * If the records that are retrieved should be ordered by one or more
609
			 * columns, the generated string of column / sort direction pairs
610
			 * replaces the ":order" placeholder. In case no ordering is required,
611
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
612
			 * markers is removed to speed up retrieving the records. Columns of
613
			 * sub-managers can also be used for ordering the result set but then
614
			 * no index can be used.
615
			 *
616
			 * The number of returned records can be limited and can start at any
617
			 * number between the begining and the end of the result set. For that
618
			 * the ":size" and ":start" placeholders are replaced by the
619
			 * corresponding values from the criteria object. The default values
620
			 * are 0 for the start and 100 for the size value.
621
			 *
622
			 * The SQL statement should conform to the ANSI standard to be
623
			 * compatible with most relational database systems. This also
624
			 * includes using double quotes for table and column names.
625
			 *
626
			 * @param string SQL statement for searching items
627
			 * @since 2014.03
628
			 * @category Developer
629
			 * @see mshop/media/manager/standard/insert/ansi
630
			 * @see mshop/media/manager/standard/update/ansi
631
			 * @see mshop/media/manager/standard/newid/ansi
632
			 * @see mshop/media/manager/standard/delete/ansi
633
			 * @see mshop/media/manager/standard/count/ansi
634
			 */
635
			$cfgPathSearch = 'mshop/media/manager/standard/search';
636
637
			/** mshop/media/manager/standard/count/mysql
638
			 * Counts the number of records matched by the given criteria in the database
639
			 *
640
			 * @see mshop/media/manager/standard/count/ansi
641
			 */
642
643
			/** mshop/media/manager/standard/count/ansi
644
			 * Counts the number of records matched by the given criteria in the database
645
			 *
646
			 * Counts all records matched by the given criteria from the media
647
			 * database. The records must be from one of the sites that are
648
			 * configured via the context item. If the current site is part of
649
			 * a tree of sites, the statement can count all records from the
650
			 * current site and the complete sub-tree of sites.
651
			 *
652
			 * As the records can normally be limited by criteria from sub-managers,
653
			 * their tables must be joined in the SQL context. This is done by
654
			 * using the "internaldeps" property from the definition of the ID
655
			 * column of the sub-managers. These internal dependencies specify
656
			 * the JOIN between the tables and the used columns for joining. The
657
			 * ":joins" placeholder is then replaced by the JOIN strings from
658
			 * the sub-managers.
659
			 *
660
			 * To limit the records matched, conditions can be added to the given
661
			 * criteria object. It can contain comparisons like column names that
662
			 * must match specific values which can be combined by AND, OR or NOT
663
			 * operators. The resulting string of SQL conditions replaces the
664
			 * ":cond" placeholder before the statement is sent to the database
665
			 * server.
666
			 *
667
			 * Both, the strings for ":joins" and for ":cond" are the same as for
668
			 * the "search" SQL statement.
669
			 *
670
			 * Contrary to the "search" statement, it doesn't return any records
671
			 * but instead the number of records that have been found. As counting
672
			 * thousands of records can be a long running task, the maximum number
673
			 * of counted records is limited for performance reasons.
674
			 *
675
			 * The SQL statement should conform to the ANSI standard to be
676
			 * compatible with most relational database systems. This also
677
			 * includes using double quotes for table and column names.
678
			 *
679
			 * @param string SQL statement for counting items
680
			 * @since 2014.03
681
			 * @category Developer
682
			 * @see mshop/media/manager/standard/insert/ansi
683
			 * @see mshop/media/manager/standard/update/ansi
684
			 * @see mshop/media/manager/standard/newid/ansi
685
			 * @see mshop/media/manager/standard/delete/ansi
686
			 * @see mshop/media/manager/standard/search/ansi
687
			 */
688
			$cfgPathCount = 'mshop/media/manager/standard/count';
689
690
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
691
692
			while( ( $row = $results->fetch() ) !== false ) {
693
				$map[$row['media.id']] = $row;
694
			}
695
696
			$dbm->release( $conn, $dbname );
697
		}
698
		catch( \Exception $e )
699
		{
700
			$dbm->release( $conn, $dbname );
701
			throw $e;
702
		}
703
704
		$propItems = $this->getPropertyItems( array_keys( $map ), 'media' );
705
706
		return $this->buildItems( $map, null, 'media', $propItems );
707
	}
708
709
710
	/**
711
	 * Creates a search critera object
712
	 *
713
	 * @param boolean $default Add default criteria (optional)
714
	 * @return \Aimeos\MW\Criteria\Iface New search criteria object
715
	 */
716
	public function createSearch( $default = false )
717
	{
718
		if( $default === true )
719
		{
720
			$object = $this->createSearchBase( 'media' );
721
			$langid = $this->getContext()->getLocale()->getLanguageId();
722
723
			if( $langid !== null )
724
			{
725
				$temp = array(
726
					$object->compare( '==', 'media.languageid', $langid ),
727
					$object->compare( '==', 'media.languageid', null ),
728
				);
729
730
				$expr = array(
731
					$object->getConditions(),
732
					$object->combine( '||', $temp ),
733
				);
734
735
				$object->setConditions( $object->combine( '&&', $expr ) );
736
			}
737
738
			return $object;
739
		}
740
741
		return parent::createSearch();
742
	}
743
744
745
	/**
746
	 * Returns a new manager for media extensions
747
	 *
748
	 * @param string $manager Name of the sub manager type in lower case
749
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
750
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc.
751
	 */
752
	public function getSubManager( $manager, $name = null )
753
	{
754
		return $this->getSubManagerBase( 'media', $manager, $name );
755
	}
756
757
758
	/**
759
	 * Creates a new media item instance.
760
	 *
761
	 * @param array $values Associative list of key/value pairs
762
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
763
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of items referenced
764
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
765
	 * @return \Aimeos\MShop\Media\Item\Iface New media item
766
	 */
767
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [], array $propItems = [] )
768
	{
769
		$values['languageid'] = $this->languageId;
770
771
		return new \Aimeos\MShop\Media\Item\Standard( $values, $listItems, $refItems, $propItems );
772
	}
773
}
774