Completed
Push — master ( 7f402b...a6068b )
by Aimeos
10:08
created

Standard::getPropertyItems()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 20
rs 9.4285
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-2017
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\ListRef\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Media\Manager\Iface
24
{
25
	use \Aimeos\MShop\Common\Manager\PropertyRef\Traits;
26
27
28
	private $searchConfig = array(
29
		'media.id' => array(
30
			'label' => 'ID',
31
			'code' => 'media.id',
32
			'internalcode' => 'mmed."id"',
33
			'type' => 'integer',
34
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
35
		),
36
		'media.siteid' => array(
37
			'label' => 'Site ID',
38
			'code' => 'media.siteid',
39
			'internalcode' => 'mmed."siteid"',
40
			'type' => 'integer',
41
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
42
			'public' => false,
43
		),
44
		'media.typeid' => array(
45
			'label' => 'Type ID',
46
			'code' => 'media.typeid',
47
			'internalcode' => 'mmed."typeid"',
48
			'type' => 'integer',
49
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
50
			'public' => false,
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
	);
126
127
	private $languageId;
128
129
130
	/**
131
	 * Initializes the object.
132
	 *
133
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
134
	 */
135
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
136
	{
137
		parent::__construct( $context );
138
		$this->setResourceName( 'db-media' );
139
140
		$this->languageId = $context->getLocale()->getLanguageId();
141
	}
142
143
144
	/**
145
	 * Removes old entries from the storage.
146
	 *
147
	 * @param array $siteids List of IDs for sites whose entries should be deleted
148
	 */
149
	public function cleanup( array $siteids )
150
	{
151
		$path = 'mshop/media/manager/submanagers';
152
		foreach( $this->getContext()->getConfig()->get( $path, array( 'type', 'lists' ) ) as $domain ) {
153
			$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
154
		}
155
156
		$this->cleanupBase( $siteids, 'mshop/media/manager/standard/delete' );
157
	}
158
159
160
	/**
161
	 * Returns the available manager types
162
	 *
163
	 * @param boolean $withsub Return also the resource type of sub-managers if true
164
	 * @return array Type of the manager and submanagers, subtypes are separated by slashes
165
	 */
166
	public function getResourceType( $withsub = true )
167
	{
168
		$path = 'mshop/media/manager/submanagers';
169
170
		return $this->getResourceTypeBase( 'media', $path, array( 'type', 'lists' ), $withsub );
171
	}
172
173
174
	/**
175
	 * Returns the attributes that can be used for searching.
176
	 *
177
	 * @param boolean $withsub Return also attributes of sub-managers if true
178
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
179
	 */
180
	public function getSearchAttributes( $withsub = true )
181
	{
182
		/** mshop/media/manager/submanagers
183
		 * List of manager names that can be instantiated by the media manager
184
		 *
185
		 * Managers provide a generic interface to the underlying storage.
186
		 * Each manager has or can have sub-managers caring about particular
187
		 * aspects. Each of these sub-managers can be instantiated by its
188
		 * parent manager using the getSubManager() method.
189
		 *
190
		 * The search keys from sub-managers can be normally used in the
191
		 * manager as well. It allows you to search for items of the manager
192
		 * using the search keys of the sub-managers to further limit the
193
		 * retrieved list of items.
194
		 *
195
		 * @param array List of sub-manager names
196
		 * @since 2014.03
197
		 * @category Developer
198
		 */
199
		$path = 'mshop/media/manager/submanagers';
200
201
		return $this->getSearchAttributesBase( $this->searchConfig, $path, array( 'type', 'lists' ), $withsub );
202
	}
203
204
205
	/**
206
	 * Creates a new media object.
207
	 *
208
	 * @return \Aimeos\MShop\Media\Item\Iface New media object
209
	 */
210
	public function createItem()
211
	{
212
		$values = array( 'media.siteid' => $this->getContext()->getLocale()->getSiteId() );
213
		return $this->createItemBase( $values );
214
	}
215
216
217
	/**
218
	 * Removes multiple items specified by ids in the array.
219
	 *
220
	 * @param array $ids List of IDs
221
	 */
222
	public function deleteItems( array $ids )
223
	{
224
		/** mshop/media/manager/standard/delete/mysql
225
		 * Deletes the items matched by the given IDs from the database
226
		 *
227
		 * @see mshop/media/manager/standard/delete/ansi
228
		 */
229
230
		/** mshop/media/manager/standard/delete/ansi
231
		 * Deletes the items matched by the given IDs from the database
232
		 *
233
		 * Removes the records specified by the given IDs from the media database.
234
		 * The records must be from the site that is configured via the
235
		 * context item.
236
		 *
237
		 * The ":cond" placeholder is replaced by the name of the ID column and
238
		 * the given ID or list of IDs while the site ID is bound to the question
239
		 * mark.
240
		 *
241
		 * The SQL statement should conform to the ANSI standard to be
242
		 * compatible with most relational database systems. This also
243
		 * includes using double quotes for table and column names.
244
		 *
245
		 * @param string SQL statement for deleting items
246
		 * @since 2014.03
247
		 * @category Developer
248
		 * @see mshop/media/manager/standard/insert/ansi
249
		 * @see mshop/media/manager/standard/update/ansi
250
		 * @see mshop/media/manager/standard/newid/ansi
251
		 * @see mshop/media/manager/standard/search/ansi
252
		 * @see mshop/media/manager/standard/count/ansi
253
		 */
254
		$path = 'mshop/media/manager/standard/delete';
255
		$this->deleteItemsBase( $ids, $path );
256
	}
257
258
259
	/**
260
	 * Returns an item for the given ID.
261
	 *
262
	 * @param integer $id ID of the item that should be retrieved
263
	 * @param string[] $ref List of domains to fetch list items and referenced items for
264
	 * @param boolean $default Add default criteria
265
	 * @return \Aimeos\MShop\Media\Item\Iface Returns the media item of the given id
266
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
267
	 */
268
	public function getItem( $id, array $ref = [], $default = false )
269
	{
270
		return $this->getItemBase( 'media.id', $id, $ref, $default );
271
	}
272
273
274
	/**
275
	 * Adds a new item to the storage or updates an existing one.
276
	 *
277
	 * @param \Aimeos\MShop\Media\Item\Iface $item New item that should be saved to the storage
278
	 * @param boolean $fetch True if the new ID should be returned in the item
279
	 * @return \Aimeos\MShop\Common\Item\Iface $item Updated item including the generated ID
0 ignored issues
show
Documentation introduced by
Should the return type not be \Aimeos\MShop\Common\Item\ListRef\Iface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
280
	 */
281
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
282
	{
283
		$iface = '\\Aimeos\\MShop\\Media\\Item\\Iface';
284
		if( !( $item instanceof $iface ) ) {
285
			throw new \Aimeos\MShop\Media\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) );
286
		}
287
288
		if( !$item->isModified() )
289
		{
290
			$item = $this->savePropertyItems( $item, 'media' );
0 ignored issues
show
Documentation introduced by
$item is of type object<Aimeos\MShop\Common\Item\Iface>, but the function expects a object<Aimeos\MShop\Comm...Item\PropertyRef\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
291
			return $this->saveRefItems( $item, 'media' );
0 ignored issues
show
Documentation introduced by
$item is of type object<Aimeos\MShop\Comm...Item\PropertyRef\Iface>, but the function expects a object<Aimeos\MShop\Common\Item\ListRef\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
292
		}
293
294
		$context = $this->getContext();
295
296
		$dbm = $context->getDatabaseManager();
297
		$dbname = $this->getResourceName();
298
		$conn = $dbm->acquire( $dbname );
299
300
		try
301
		{
302
			$id = $item->getId();
303
			$date = date( 'Y-m-d H:i:s' );
304
305
			if( $id === null )
306
			{
307
				/** mshop/media/manager/standard/insert/mysql
308
				 * Inserts a new media record into the database table
309
				 *
310
				 * @see mshop/media/manager/standard/insert/ansi
311
				 */
312
313
				/** mshop/media/manager/standard/insert/ansi
314
				 * Inserts a new media record into the database table
315
				 *
316
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
317
				 * the database and the newly created ID retrieved afterwards
318
				 * using the "newid" SQL statement.
319
				 *
320
				 * The SQL statement must be a string suitable for being used as
321
				 * prepared statement. It must include question marks for binding
322
				 * the values from the media item to the statement before they are
323
				 * sent to the database server. The number of question marks must
324
				 * be the same as the number of columns listed in the INSERT
325
				 * statement. The order of the columns must correspond to the
326
				 * order in the saveItems() method, so the correct values are
327
				 * bound to the columns.
328
				 *
329
				 * The SQL statement should conform to the ANSI standard to be
330
				 * compatible with most relational database systems. This also
331
				 * includes using double quotes for table and column names.
332
				 *
333
				 * @param string SQL statement for inserting records
334
				 * @since 2014.03
335
				 * @category Developer
336
				 * @see mshop/media/manager/standard/update/ansi
337
				 * @see mshop/media/manager/standard/newid/ansi
338
				 * @see mshop/media/manager/standard/delete/ansi
339
				 * @see mshop/media/manager/standard/search/ansi
340
				 * @see mshop/media/manager/standard/count/ansi
341
				 */
342
				$path = 'mshop/media/manager/standard/insert';
343
			}
344
			else
345
			{
346
				/** mshop/media/manager/standard/update/mysql
347
				 * Updates an existing media record in the database
348
				 *
349
				 * @see mshop/media/manager/standard/update/ansi
350
				 */
351
352
				/** mshop/media/manager/standard/update/ansi
353
				 * Updates an existing media record in the database
354
				 *
355
				 * Items which already have an ID (i.e. the ID is not NULL) will
356
				 * be updated in the database.
357
				 *
358
				 * The SQL statement must be a string suitable for being used as
359
				 * prepared statement. It must include question marks for binding
360
				 * the values from the media item to the statement before they are
361
				 * sent to the database server. The order of the columns must
362
				 * correspond to the order in the saveItems() method, so the
363
				 * correct values are bound to the columns.
364
				 *
365
				 * The SQL statement should conform to the ANSI standard to be
366
				 * compatible with most relational database systems. This also
367
				 * includes using double quotes for table and column names.
368
				 *
369
				 * @param string SQL statement for updating records
370
				 * @since 2014.03
371
				 * @category Developer
372
				 * @see mshop/media/manager/standard/insert/ansi
373
				 * @see mshop/media/manager/standard/newid/ansi
374
				 * @see mshop/media/manager/standard/delete/ansi
375
				 * @see mshop/media/manager/standard/search/ansi
376
				 * @see mshop/media/manager/standard/count/ansi
377
				 */
378
				$path = 'mshop/media/manager/standard/update';
379
			}
380
381
			$stmt = $this->getCachedStatement( $conn, $path );
382
383
			$stmt->bind( 1, $item->getLanguageId() );
384
			$stmt->bind( 2, $item->getTypeId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
385
			$stmt->bind( 3, $item->getLabel() );
386
			$stmt->bind( 4, $item->getMimeType() );
387
			$stmt->bind( 5, $item->getUrl() );
388
			$stmt->bind( 6, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
389
			$stmt->bind( 7, $item->getDomain() );
390
			$stmt->bind( 8, $item->getPreview() );
391
			$stmt->bind( 9, $date ); // mtime
392
			$stmt->bind( 10, $context->getEditor() );
393
			$stmt->bind( 11, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
394
395
			if( $id !== null ) {
396
				$stmt->bind( 12, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
397
				$item->setId( $id ); //is not modified anymore
398
			} else {
399
				$stmt->bind( 12, $date ); // ctime
400
			}
401
402
			$stmt->execute()->finish();
403
404
			if( $id === null && $fetch === true )
405
			{
406
				/** mshop/media/manager/standard/newid/mysql
407
				 * Retrieves the ID generated by the database when inserting a new record
408
				 *
409
				 * @see mshop/media/manager/standard/newid/ansi
410
				 */
411
412
				/** mshop/media/manager/standard/newid/ansi
413
				 * Retrieves the ID generated by the database when inserting a new record
414
				 *
415
				 * As soon as a new record is inserted into the database table,
416
				 * the database server generates a new and unique identifier for
417
				 * that record. This ID can be used for retrieving, updating and
418
				 * deleting that specific record from the table again.
419
				 *
420
				 * For MySQL:
421
				 *  SELECT LAST_INSERT_ID()
422
				 * For PostgreSQL:
423
				 *  SELECT currval('seq_mmed_id')
424
				 * For SQL Server:
425
				 *  SELECT SCOPE_IDENTITY()
426
				 * For Oracle:
427
				 *  SELECT "seq_mmed_id".CURRVAL FROM DUAL
428
				 *
429
				 * There's no way to retrive the new ID by a SQL statements that
430
				 * fits for most database servers as they implement their own
431
				 * specific way.
432
				 *
433
				 * @param string SQL statement for retrieving the last inserted record ID
434
				 * @since 2014.03
435
				 * @category Developer
436
				 * @see mshop/media/manager/standard/insert/ansi
437
				 * @see mshop/media/manager/standard/update/ansi
438
				 * @see mshop/media/manager/standard/delete/ansi
439
				 * @see mshop/media/manager/standard/search/ansi
440
				 * @see mshop/media/manager/standard/count/ansi
441
				 */
442
				$path = 'mshop/media/manager/standard/newid';
443
				$item->setId( $this->newId( $conn, $path ) );
444
			}
445
446
			$dbm->release( $conn, $dbname );
447
		}
448
		catch( \Exception $e )
449
		{
450
			$dbm->release( $conn, $dbname );
451
			throw $e;
452
		}
453
454
		$item = $this->savePropertyItems( $item, 'media' );
0 ignored issues
show
Documentation introduced by
$item is of type object<Aimeos\MShop\Common\Item\Iface>, but the function expects a object<Aimeos\MShop\Comm...Item\PropertyRef\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
455
		return $this->saveRefItems( $item, 'media' );
0 ignored issues
show
Documentation introduced by
$item is of type object<Aimeos\MShop\Comm...Item\PropertyRef\Iface>, but the function expects a object<Aimeos\MShop\Common\Item\ListRef\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
456
	}
457
458
459
	/**
460
	 * Returns the item objects matched by the given search criteria.
461
	 *
462
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
463
	 * @param string[] $ref List of domains to fetch list items and referenced items for
464
	 * @param integer|null &$total Number of items that are available in total
465
	 * @return array List of items implementing \Aimeos\MShop\Media\Item\Iface
466
	 */
467
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
468
	{
469
		$map = $typeIds = [];
470
		$context = $this->getContext();
471
472
		$dbm = $context->getDatabaseManager();
473
		$dbname = $this->getResourceName();
474
		$conn = $dbm->acquire( $dbname );
475
476
		try
477
		{
478
			$required = array( 'media' );
479
480
			/** mshop/media/manager/sitemode
481
			 * Mode how items from levels below or above in the site tree are handled
482
			 *
483
			 * By default, only items from the current site are fetched from the
484
			 * storage. If the ai-sites extension is installed, you can create a
485
			 * tree of sites. Then, this setting allows you to define for the
486
			 * whole media domain if items from parent sites are inherited,
487
			 * sites from child sites are aggregated or both.
488
			 *
489
			 * Available constants for the site mode are:
490
			 * * 0 = only items from the current site
491
			 * * 1 = inherit items from parent sites
492
			 * * 2 = aggregate items from child sites
493
			 * * 3 = inherit and aggregate items at the same time
494
			 *
495
			 * You also need to set the mode in the locale manager
496
			 * (mshop/locale/manager/standard/sitelevel) to one of the constants.
497
			 * If you set it to the same value, it will work as described but you
498
			 * can also use different modes. For example, if inheritance and
499
			 * aggregation is configured the locale manager but only inheritance
500
			 * in the domain manager because aggregating items makes no sense in
501
			 * this domain, then items wil be only inherited. Thus, you have full
502
			 * control over inheritance and aggregation in each domain.
503
			 *
504
			 * @param integer Constant from Aimeos\MShop\Locale\Manager\Base class
505
			 * @category Developer
506
			 * @since 2018.01
507
			 * @see mshop/locale/manager/standard/sitelevel
508
			 */
509
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
510
			$level = $context->getConfig()->get( 'mshop/media/manager/sitemode', $level );
511
512
			/** mshop/media/manager/standard/search/mysql
513
			 * Retrieves the records matched by the given criteria in the database
514
			 *
515
			 * @see mshop/media/manager/standard/search/ansi
516
			 */
517
518
			/** mshop/media/manager/standard/search/ansi
519
			 * Retrieves the records matched by the given criteria in the database
520
			 *
521
			 * Fetches the records matched by the given criteria from the media
522
			 * database. The records must be from one of the sites that are
523
			 * configured via the context item. If the current site is part of
524
			 * a tree of sites, the SELECT statement can retrieve all records
525
			 * from the current site and the complete sub-tree of sites.
526
			 *
527
			 * As the records can normally be limited by criteria from sub-managers,
528
			 * their tables must be joined in the SQL context. This is done by
529
			 * using the "internaldeps" property from the definition of the ID
530
			 * column of the sub-managers. These internal dependencies specify
531
			 * the JOIN between the tables and the used columns for joining. The
532
			 * ":joins" placeholder is then replaced by the JOIN strings from
533
			 * the sub-managers.
534
			 *
535
			 * To limit the records matched, conditions can be added to the given
536
			 * criteria object. It can contain comparisons like column names that
537
			 * must match specific values which can be combined by AND, OR or NOT
538
			 * operators. The resulting string of SQL conditions replaces the
539
			 * ":cond" placeholder before the statement is sent to the database
540
			 * server.
541
			 *
542
			 * If the records that are retrieved should be ordered by one or more
543
			 * columns, the generated string of column / sort direction pairs
544
			 * replaces the ":order" placeholder. In case no ordering is required,
545
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
546
			 * markers is removed to speed up retrieving the records. Columns of
547
			 * sub-managers can also be used for ordering the result set but then
548
			 * no index can be used.
549
			 *
550
			 * The number of returned records can be limited and can start at any
551
			 * number between the begining and the end of the result set. For that
552
			 * the ":size" and ":start" placeholders are replaced by the
553
			 * corresponding values from the criteria object. The default values
554
			 * are 0 for the start and 100 for the size value.
555
			 *
556
			 * The SQL statement should conform to the ANSI standard to be
557
			 * compatible with most relational database systems. This also
558
			 * includes using double quotes for table and column names.
559
			 *
560
			 * @param string SQL statement for searching items
561
			 * @since 2014.03
562
			 * @category Developer
563
			 * @see mshop/media/manager/standard/insert/ansi
564
			 * @see mshop/media/manager/standard/update/ansi
565
			 * @see mshop/media/manager/standard/newid/ansi
566
			 * @see mshop/media/manager/standard/delete/ansi
567
			 * @see mshop/media/manager/standard/count/ansi
568
			 */
569
			$cfgPathSearch = 'mshop/media/manager/standard/search';
570
571
			/** mshop/media/manager/standard/count/mysql
572
			 * Counts the number of records matched by the given criteria in the database
573
			 *
574
			 * @see mshop/media/manager/standard/count/ansi
575
			 */
576
577
			/** mshop/media/manager/standard/count/ansi
578
			 * Counts the number of records matched by the given criteria in the database
579
			 *
580
			 * Counts all records matched by the given criteria from the media
581
			 * database. The records must be from one of the sites that are
582
			 * configured via the context item. If the current site is part of
583
			 * a tree of sites, the statement can count all records from the
584
			 * current site and the complete sub-tree of sites.
585
			 *
586
			 * As the records can normally be limited by criteria from sub-managers,
587
			 * their tables must be joined in the SQL context. This is done by
588
			 * using the "internaldeps" property from the definition of the ID
589
			 * column of the sub-managers. These internal dependencies specify
590
			 * the JOIN between the tables and the used columns for joining. The
591
			 * ":joins" placeholder is then replaced by the JOIN strings from
592
			 * the sub-managers.
593
			 *
594
			 * To limit the records matched, conditions can be added to the given
595
			 * criteria object. It can contain comparisons like column names that
596
			 * must match specific values which can be combined by AND, OR or NOT
597
			 * operators. The resulting string of SQL conditions replaces the
598
			 * ":cond" placeholder before the statement is sent to the database
599
			 * server.
600
			 *
601
			 * Both, the strings for ":joins" and for ":cond" are the same as for
602
			 * the "search" SQL statement.
603
			 *
604
			 * Contrary to the "search" statement, it doesn't return any records
605
			 * but instead the number of records that have been found. As counting
606
			 * thousands of records can be a long running task, the maximum number
607
			 * of counted records is limited for performance reasons.
608
			 *
609
			 * The SQL statement should conform to the ANSI standard to be
610
			 * compatible with most relational database systems. This also
611
			 * includes using double quotes for table and column names.
612
			 *
613
			 * @param string SQL statement for counting items
614
			 * @since 2014.03
615
			 * @category Developer
616
			 * @see mshop/media/manager/standard/insert/ansi
617
			 * @see mshop/media/manager/standard/update/ansi
618
			 * @see mshop/media/manager/standard/newid/ansi
619
			 * @see mshop/media/manager/standard/delete/ansi
620
			 * @see mshop/media/manager/standard/search/ansi
621
			 */
622
			$cfgPathCount = 'mshop/media/manager/standard/count';
623
624
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
625
626
			while( ( $row = $results->fetch() ) !== false )
627
			{
628
				$map[$row['media.id']] = $row;
629
				$typeIds[$row['media.typeid']] = null;
630
			}
631
632
			$dbm->release( $conn, $dbname );
633
		}
634
		catch( \Exception $e )
635
		{
636
			$dbm->release( $conn, $dbname );
637
			throw $e;
638
		}
639
640
		if( !empty( $typeIds ) )
641
		{
642
			$typeManager = $this->getObject()->getSubManager( 'type' );
643
			$typeSearch = $typeManager->createSearch();
644
			$typeSearch->setConditions( $typeSearch->compare( '==', 'media.type.id', array_keys( $typeIds ) ) );
645
			$typeSearch->setSlice( 0, $search->getSliceSize() );
646
			$typeItems = $typeManager->searchItems( $typeSearch );
647
648
			foreach( $map as $id => $row )
649
			{
650
				if( isset( $typeItems[$row['media.typeid']] ) )
651
				{
652
					$map[$id]['media.type'] = $typeItems[$row['media.typeid']]->getCode();
653
					$map[$id]['media.typename'] = $typeItems[$row['media.typeid']]->getName();
654
				}
655
			}
656
		}
657
658
		$propItems = $this->getPropertyItems( array_keys( $map ), 'media' );
659
660
		return $this->buildItems( $map, null, 'media', $propItems );
661
	}
662
663
664
	/**
665
	 * creates a search object and sets base criteria
666
	 *
667
	 * @param boolean $default
668
	 * @return \Aimeos\MW\Criteria\Iface
669
	 */
670
	public function createSearch( $default = false )
671
	{
672
		if( $default === true )
673
		{
674
			$object = $this->createSearchBase( 'media' );
675
			$langid = $this->getContext()->getLocale()->getLanguageId();
676
677
			if( $langid !== null )
678
			{
679
				$temp = array(
680
					$object->compare( '==', 'media.languageid', $langid ),
681
					$object->compare( '==', 'media.languageid', null ),
682
				);
683
684
				$expr = array(
685
					$object->getConditions(),
686
					$object->combine( '||', $temp ),
687
				);
688
689
				$object->setConditions( $object->combine( '&&', $expr ) );
690
			}
691
692
			return $object;
693
		}
694
695
		return parent::createSearch();
696
	}
697
698
699
	/**
700
	 * Returns a new manager for product extensions
701
	 *
702
	 * @param string $manager Name of the sub manager type in lower case
703
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
704
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc.
705
	 */
706
	public function getSubManager( $manager, $name = null )
707
	{
708
		return $this->getSubManagerBase( 'media', $manager, $name );
709
	}
710
711
712
	/**
713
	 * Creates a new media item instance.
714
	 *
715
	 * @param array $values Associative list of key/value pairs
716
	 * @param array $listItems List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface
717
	 * @param array $refItems List of items reference to this item
718
	 * @param array $propItems List of media property items implementing \Aimeos\MShop\Common\Item\Property\Iface
719
	 * @return \Aimeos\MShop\Media\Item\Iface New product item
720
	 */
721
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [], array $propItems = [] )
722
	{
723
		$values['languageid'] = $this->languageId;
724
725
		return new \Aimeos\MShop\Media\Item\Standard( $values, $listItems, $refItems, $propItems );
726
	}
727
}
728