Total Complexity | 58 |
Total Lines | 860 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Standard |
||
22 | extends \Aimeos\MShop\Locale\Manager\Base |
||
23 | implements \Aimeos\MShop\Locale\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface |
||
24 | { |
||
25 | private $searchConfig = array( |
||
26 | 'locale.id' => array( |
||
27 | 'code' => 'locale.id', |
||
28 | 'internalcode' => 'mloc."id"', |
||
29 | 'label' => 'ID', |
||
30 | 'type' => 'integer', |
||
31 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
||
32 | 'public' => false, |
||
33 | ), |
||
34 | 'locale.siteid' => array( |
||
35 | 'code' => 'locale.siteid', |
||
36 | 'internalcode' => 'mloc."siteid"', |
||
37 | 'label' => 'Site ID', |
||
38 | 'type' => 'string', |
||
39 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
||
40 | 'public' => false, |
||
41 | ), |
||
42 | 'locale.languageid' => array( |
||
43 | 'code' => 'locale.languageid', |
||
44 | 'internalcode' => 'mloc."langid"', |
||
45 | 'label' => 'Language ID', |
||
46 | 'type' => 'string', |
||
47 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
||
48 | ), |
||
49 | 'locale.currencyid' => array( |
||
50 | 'code' => 'locale.currencyid', |
||
51 | 'internalcode' => 'mloc."currencyid"', |
||
52 | 'label' => 'Currency ID', |
||
53 | 'type' => 'string', |
||
54 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
||
55 | ), |
||
56 | 'locale.status' => array( |
||
57 | 'code' => 'locale.status', |
||
58 | 'internalcode' => 'mloc."status"', |
||
59 | 'label' => 'Status', |
||
60 | 'type' => 'integer', |
||
61 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
||
62 | ), |
||
63 | 'locale.position' => array( |
||
64 | 'code' => 'locale.position', |
||
65 | 'internalcode' => 'mloc."pos"', |
||
66 | 'label' => 'Position', |
||
67 | 'type' => 'integer', |
||
68 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
||
69 | ), |
||
70 | 'locale.ctime' => array( |
||
71 | 'code' => 'locale.ctime', |
||
72 | 'internalcode' => 'mloc."ctime"', |
||
73 | 'label' => 'Create date/time', |
||
74 | 'type' => 'datetime', |
||
75 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
||
76 | 'public' => false, |
||
77 | ), |
||
78 | 'locale.mtime' => array( |
||
79 | 'code' => 'locale.mtime', |
||
80 | 'internalcode' => 'mloc."mtime"', |
||
81 | 'label' => 'Modify date/time', |
||
82 | 'type' => 'datetime', |
||
83 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
||
84 | 'public' => false, |
||
85 | ), |
||
86 | 'locale.editor' => array( |
||
87 | 'code' => 'locale.editor', |
||
88 | 'internalcode' => 'mloc."editor"', |
||
89 | 'label' => 'Editor', |
||
90 | 'type' => 'string', |
||
91 | 'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
||
92 | 'public' => false, |
||
93 | ), |
||
94 | ); |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Initializes the object. |
||
99 | * |
||
100 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
101 | */ |
||
102 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
106 | } |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Returns the locale item for the given site code, language code and currency code. |
||
111 | * |
||
112 | * @param string $site Site code |
||
113 | * @param string $lang Language code (optional) |
||
114 | * @param string $currency Currency code (optional) |
||
115 | * @param bool $active Flag to get only active items (optional) |
||
116 | * @param int|null $level Constant from abstract class which site ID levels should be available (optional), |
||
117 | * based on config or value for SITE_PATH if null |
||
118 | * @param bool $bare Allow locale items with sites only |
||
119 | * @return \Aimeos\MShop\Locale\Item\Iface Locale item for the given parameters |
||
120 | * @throws \Aimeos\MShop\Locale\Exception If no locale item is found |
||
121 | */ |
||
122 | public function bootstrap( string $site, string $lang = '', string $currency = '', bool $active = true, int $level = null, |
||
123 | bool $bare = false ) : \Aimeos\MShop\Locale\Item\Iface |
||
124 | { |
||
125 | $siteItem = $this->object()->getSubManager( 'site' )->find( $site ); |
||
126 | |||
127 | // allow enabled sites and sites under review |
||
128 | if( $active && $siteItem->getStatus() < 1 && $siteItem->getStatus() !== -1 ) { |
||
129 | throw new \Aimeos\MShop\Locale\Exception( 'Site not found' ); |
||
130 | } |
||
131 | |||
132 | $siteId = $siteItem->getSiteId(); |
||
133 | $sites = [Base::SITE_ONE => $siteId]; |
||
134 | |||
135 | return $this->bootstrapBase( $site, $lang, $currency, $active, $siteItem, $siteId, $sites, $bare ); |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Removes old entries from the storage. |
||
141 | * |
||
142 | * @param iterable $siteids List of IDs for sites whose entries should be deleted |
||
143 | * @return \Aimeos\MShop\Locale\Manager\Iface Manager object for chaining method calls |
||
144 | */ |
||
145 | public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface |
||
146 | { |
||
147 | return $this->clearBase( $siteids, 'mshop/locale/manager/delete' ); |
||
148 | } |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Creates a new empty item instance |
||
153 | * |
||
154 | * @param array $values Values the item should be initialized with |
||
155 | * @return \Aimeos\MShop\Locale\Item\Iface New locale item object |
||
156 | */ |
||
157 | public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
||
167 | } |
||
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Creates a filter object. |
||
173 | * |
||
174 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
175 | * @param bool $site TRUE for adding site criteria to limit items by the site of related items |
||
176 | * @return \Aimeos\Base\Criteria\Iface Returns the filter object |
||
177 | */ |
||
178 | public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface |
||
179 | { |
||
180 | return $this->filterBase( 'locale', $default ); |
||
181 | } |
||
182 | |||
183 | |||
184 | /** |
||
185 | * Returns the item specified by its ID. |
||
186 | * |
||
187 | * @param string $id Unique ID of the locale item |
||
188 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
189 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
190 | * @return \Aimeos\MShop\Locale\Item\Iface Returns the locale item of the given id |
||
191 | * @throws \Aimeos\MShop\Exception If item couldn't be found |
||
192 | */ |
||
193 | public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface |
||
194 | { |
||
195 | return $this->getItemBase( 'locale.id', $id, $ref, $default ); |
||
196 | } |
||
197 | |||
198 | |||
199 | /** |
||
200 | * Searches for all items matching the given critera. |
||
201 | * |
||
202 | * @param \Aimeos\Base\Criteria\Iface $search Criteria object with conditions, sortations, etc. |
||
203 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
204 | * @param int &$total Number of items that are available in total |
||
205 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Locale\Item\Iface with ids as keys |
||
206 | */ |
||
207 | public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map |
||
208 | { |
||
209 | $items = []; |
||
210 | $level = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH; |
||
211 | $search = (clone $search)->add( $this->siteCondition( 'locale.siteid', $level ) ); |
||
212 | |||
213 | foreach( $this->searchEntries( $search, $ref, $total ) as $row ) |
||
214 | { |
||
215 | if( $item = $this->applyFilter( $this->createItemBase( $row ) ) ) { |
||
216 | $items[$row['locale.id']] = $item; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | return map( $items ); |
||
221 | } |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Removes multiple items. |
||
226 | * |
||
227 | * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items |
||
228 | * @return \Aimeos\MShop\Locale\Manager\Iface Manager object for chaining method calls |
||
229 | */ |
||
230 | public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface |
||
265 | } |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Adds or updates an item object. |
||
270 | * |
||
271 | * @param \Aimeos\MShop\Locale\Item\Iface $item Item object whose data should be saved |
||
272 | * @param bool $fetch True if the new ID should be returned in the item |
||
273 | * @return \Aimeos\MShop\Locale\Item\Iface $item Updated item including the generated ID |
||
274 | */ |
||
275 | public function saveItem( \Aimeos\MShop\Locale\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Locale\Item\Iface |
||
276 | { |
||
277 | if( !$item->isModified() ) { |
||
278 | return $item; |
||
279 | } |
||
280 | |||
281 | $context = $this->context(); |
||
282 | $conn = $context->db( $this->getResourceName() ); |
||
283 | |||
284 | $id = $item->getId(); |
||
285 | $date = date( 'Y-m-d H:i:s' ); |
||
286 | $columns = $this->object()->getSaveAttributes(); |
||
287 | |||
288 | if( $id === null ) |
||
289 | { |
||
290 | /** mshop/locale/manager/insert/mysql |
||
291 | * Inserts a new locale record into the database table |
||
292 | * |
||
293 | * @see mshop/locale/manager/insert/ansi |
||
294 | */ |
||
295 | |||
296 | /** mshop/locale/manager/insert/ansi |
||
297 | * Inserts a new locale record into the database table |
||
298 | * |
||
299 | * Items with no ID yet (i.e. the ID is NULL) will be created in |
||
300 | * the database and the newly created ID retrieved afterwards |
||
301 | * using the "newid" SQL statement. |
||
302 | * |
||
303 | * The SQL statement must be a string suitable for being used as |
||
304 | * prepared statement. It must include question marks for binding |
||
305 | * the values from the locale item to the statement before they are |
||
306 | * sent to the database server. The number of question marks must |
||
307 | * be the same as the number of columns listed in the INSERT |
||
308 | * statement. The order of the columns must correspond to the |
||
309 | * order in the save() method, so the correct values are |
||
310 | * bound to the columns. |
||
311 | * |
||
312 | * The SQL statement should conform to the ANSI standard to be |
||
313 | * compatible with most relational database systems. This also |
||
314 | * includes using double quotes for table and column names. |
||
315 | * |
||
316 | * @param string SQL statement for inserting records |
||
317 | * @since 2014.03 |
||
318 | * @category Developer |
||
319 | * @see mshop/locale/manager/update/ansi |
||
320 | * @see mshop/locale/manager/newid/ansi |
||
321 | * @see mshop/locale/manager/delete/ansi |
||
322 | * @see mshop/locale/manager/search/ansi |
||
323 | * @see mshop/locale/manager/count/ansi |
||
324 | */ |
||
325 | $path = 'mshop/locale/manager/insert'; |
||
326 | $sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) ); |
||
|
|||
327 | } |
||
328 | else |
||
329 | { |
||
330 | /** mshop/locale/manager/update/mysql |
||
331 | * Updates an existing locale record in the database |
||
332 | * |
||
333 | * @see mshop/locale/manager/update/ansi |
||
334 | */ |
||
335 | |||
336 | /** mshop/locale/manager/update/ansi |
||
337 | * Updates an existing locale record in the database |
||
338 | * |
||
339 | * Items which already have an ID (i.e. the ID is not NULL) will |
||
340 | * be updated in the database. |
||
341 | * |
||
342 | * The SQL statement must be a string suitable for being used as |
||
343 | * prepared statement. It must include question marks for binding |
||
344 | * the values from the locale item to the statement before they are |
||
345 | * sent to the database server. The order of the columns must |
||
346 | * correspond to the order in the save() method, so the |
||
347 | * correct values are bound to the columns. |
||
348 | * |
||
349 | * The SQL statement should conform to the ANSI standard to be |
||
350 | * compatible with most relational database systems. This also |
||
351 | * includes using double quotes for table and column names. |
||
352 | * |
||
353 | * @param string SQL statement for updating records |
||
354 | * @since 2014.03 |
||
355 | * @category Developer |
||
356 | * @see mshop/locale/manager/insert/ansi |
||
357 | * @see mshop/locale/manager/newid/ansi |
||
358 | * @see mshop/locale/manager/delete/ansi |
||
359 | * @see mshop/locale/manager/search/ansi |
||
360 | * @see mshop/locale/manager/count/ansi |
||
361 | */ |
||
362 | $path = 'mshop/locale/manager/update'; |
||
363 | $sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false ); |
||
364 | } |
||
365 | |||
366 | $idx = 1; |
||
367 | $stmt = $this->getCachedStatement( $conn, $path, $sql ); |
||
368 | |||
369 | foreach( $columns as $name => $entry ) { |
||
370 | $stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() ); |
||
371 | } |
||
372 | |||
373 | $stmt->bind( $idx++, $item->getLanguageId() ); |
||
374 | $stmt->bind( $idx++, $item->getCurrencyId() ); |
||
375 | $stmt->bind( $idx++, $item->getPosition(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
376 | $stmt->bind( $idx++, $item->getStatus(), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
377 | $stmt->bind( $idx++, $date ); // mtime |
||
378 | $stmt->bind( $idx++, $context->editor() ); |
||
379 | $stmt->bind( $idx++, $item->get( 'site_id' ), \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
380 | $stmt->bind( $idx++, $item->getSiteId() ); |
||
381 | |||
382 | if( $id !== null ) { |
||
383 | $stmt->bind( $idx++, $id, \Aimeos\Base\DB\Statement\Base::PARAM_INT ); |
||
384 | } else { |
||
385 | $stmt->bind( $idx++, $date ); // ctime |
||
386 | } |
||
387 | |||
388 | $stmt->execute()->finish(); |
||
389 | |||
390 | if( $id === null && $fetch === true ) |
||
391 | { |
||
392 | /** mshop/locale/manager/newid/mysql |
||
393 | * Retrieves the ID generated by the database when inserting a new record |
||
394 | * |
||
395 | * @see mshop/locale/manager/newid/ansi |
||
396 | */ |
||
397 | |||
398 | /** mshop/locale/manager/newid/ansi |
||
399 | * Retrieves the ID generated by the database when inserting a new record |
||
400 | * |
||
401 | * As soon as a new record is inserted into the database table, |
||
402 | * the database server generates a new and unique identifier for |
||
403 | * that record. This ID can be used for retrieving, updating and |
||
404 | * deleting that specific record from the table again. |
||
405 | * |
||
406 | * For MySQL: |
||
407 | * SELECT LAST_INSERT_ID() |
||
408 | * For PostgreSQL: |
||
409 | * SELECT currval('seq_mloc_id') |
||
410 | * For SQL Server: |
||
411 | * SELECT SCOPE_IDENTITY() |
||
412 | * For Oracle: |
||
413 | * SELECT "seq_mloc_id".CURRVAL FROM DUAL |
||
414 | * |
||
415 | * There's no way to retrive the new ID by a SQL statements that |
||
416 | * fits for most database servers as they implement their own |
||
417 | * specific way. |
||
418 | * |
||
419 | * @param string SQL statement for retrieving the last inserted record ID |
||
420 | * @since 2014.03 |
||
421 | * @category Developer |
||
422 | * @see mshop/locale/manager/insert/ansi |
||
423 | * @see mshop/locale/manager/update/ansi |
||
424 | * @see mshop/locale/manager/delete/ansi |
||
425 | * @see mshop/locale/manager/search/ansi |
||
426 | * @see mshop/locale/manager/count/ansi |
||
427 | */ |
||
428 | $path = 'mshop/locale/manager/newid'; |
||
429 | $id = $this->newId( $conn, $path ); |
||
430 | } |
||
431 | |||
432 | $item->setId( $id ); |
||
433 | |||
434 | return $item; |
||
435 | } |
||
436 | |||
437 | |||
438 | /** |
||
439 | * Returns a new manager for locale extensions |
||
440 | * |
||
441 | * @param string $manager Name of the sub manager type in lower case |
||
442 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
443 | * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g site, language, currency. |
||
444 | */ |
||
445 | public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface |
||
446 | { |
||
447 | return $this->getSubManagerBase( 'locale', $manager, $name ); |
||
448 | } |
||
449 | |||
450 | |||
451 | /** |
||
452 | * Returns the available manager types |
||
453 | * |
||
454 | * @param bool $withsub Return also the resource type of sub-managers if true |
||
455 | * @return string[] Type of the manager and submanagers, subtypes are separated by slashes |
||
456 | */ |
||
457 | public function getResourceType( bool $withsub = true ) : array |
||
461 | } |
||
462 | |||
463 | |||
464 | /** |
||
465 | * Returns the attributes that can be used for searching. |
||
466 | * |
||
467 | * @param bool $withsub Return also attributes of sub-managers if true |
||
468 | * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items |
||
469 | */ |
||
470 | public function getSearchAttributes( bool $withsub = true ) : array |
||
471 | { |
||
472 | /** mshop/locale/manager/submanagers |
||
473 | * List of manager names that can be instantiated by the locale manager |
||
474 | * |
||
475 | * Managers provide a generic interface to the underlying storage. |
||
476 | * Each manager has or can have sub-managers caring about particular |
||
477 | * aspects. Each of these sub-managers can be instantiated by its |
||
478 | * parent manager using the getSubManager() method. |
||
479 | * |
||
480 | * The search keys from sub-managers can be normally used in the |
||
481 | * manager as well. It allows you to search for items of the manager |
||
482 | * using the search keys of the sub-managers to further limit the |
||
483 | * retrieved list of items. |
||
484 | * |
||
485 | * @param array List of sub-manager names |
||
486 | * @since 2014.03 |
||
487 | * @category Developer |
||
488 | */ |
||
489 | $path = 'mshop/locale/manager/submanagers'; |
||
490 | $default = array( 'language', 'currency', 'site' ); |
||
491 | |||
492 | return $this->getSearchAttributesBase( $this->searchConfig, $path, $default, $withsub ); |
||
493 | } |
||
494 | |||
495 | |||
496 | /** |
||
497 | * Returns the locale item for the given site code, language code and currency code. |
||
498 | * |
||
499 | * If the locale item is inherited from a parent site, the site ID of this locale item |
||
500 | * is changed to the site ID of the actual site. This ensures that items assigned to |
||
501 | * the same site as the site item are still used. |
||
502 | * |
||
503 | * @param string $site Site code |
||
504 | * @param string $lang Language code |
||
505 | * @param string $currency Currency code |
||
506 | * @param bool $active Flag to get only active items |
||
507 | * @param \Aimeos\MShop\Locale\Item\Site\Iface $siteItem Site item |
||
508 | * @param string $siteId Site ID |
||
509 | * @param array $sites Associative list of site constant as key and sites as values |
||
510 | * @param bool $bare Allow locale items with sites only |
||
511 | * @return \Aimeos\MShop\Locale\Item\Iface Locale item for the given parameters |
||
512 | * @throws \Aimeos\MShop\Locale\Exception If no locale item is found |
||
513 | */ |
||
514 | protected function bootstrapBase( string $site, string $lang, string $currency, bool $active, |
||
515 | \Aimeos\MShop\Locale\Item\Site\Iface $siteItem, string $siteId, array $sites, bool $bare ) : \Aimeos\MShop\Locale\Item\Iface |
||
516 | { |
||
517 | if( $result = $this->bootstrapMatch( $siteId, $lang, $currency, $active, $siteItem, $sites ) ) { |
||
518 | return $result; |
||
519 | } |
||
520 | |||
521 | if( $result = $this->bootstrapClosest( $siteId, $lang, $active, $siteItem, $sites ) ) { |
||
522 | return $result; |
||
523 | } |
||
524 | |||
525 | if( $bare === true ) { |
||
526 | return $this->createItemBase( ['locale.siteid' => $siteId], $siteItem, $sites ); |
||
527 | } |
||
528 | |||
529 | $msg = $this->context()->translate( 'mshop', 'Locale item for site "%1$s" not found' ); |
||
530 | throw new \Aimeos\MShop\Locale\Exception( sprintf( $msg, $site ) ); |
||
531 | } |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Returns the matching locale item for the given site code, language code and currency code. |
||
536 | * |
||
537 | * If the locale item is inherited from a parent site, the site ID of this locale item |
||
538 | * is changed to the site ID of the actual site. This ensures that items assigned to |
||
539 | * the same site as the site item are still used. |
||
540 | * |
||
541 | * @param string $siteId Site ID |
||
542 | * @param string $lang Language code |
||
543 | * @param string $currency Currency code |
||
544 | * @param bool $active Flag to get only active items |
||
545 | * @param \Aimeos\MShop\Locale\Item\Site\Iface $siteItem Site item |
||
546 | * @param array $sites Associative list of site constant as key and sites as values |
||
547 | * @return \Aimeos\MShop\Locale\Item\Iface|null Locale item for the given parameters or null if no item was found |
||
548 | */ |
||
549 | private function bootstrapMatch( string $siteId, string $lang, string $currency, bool $active, |
||
550 | \Aimeos\MShop\Locale\Item\Site\Iface $siteItem, array $sites ) : ?\Aimeos\MShop\Locale\Item\Iface |
||
551 | { |
||
552 | // Try to find exact match |
||
553 | $search = $this->object()->filter( $active ); |
||
554 | |||
555 | $expr = array( $search->compare( '==', 'locale.siteid', $sites[Base::SITE_PATH] ?? $sites[Base::SITE_ONE] ) ); |
||
556 | |||
557 | if( !empty( $lang ) ) |
||
558 | { |
||
559 | $langIds = strlen( $lang ) > 2 ? [$lang, substr( $lang, 0, 2 )] : [$lang]; |
||
560 | $expr[] = $search->compare( '==', 'locale.languageid', $langIds ); |
||
561 | } |
||
562 | |||
563 | if( !empty( $currency ) ) { |
||
564 | $expr[] = $search->compare( '==', 'locale.currencyid', $currency ); |
||
565 | } |
||
566 | |||
567 | $expr[] = $search->getConditions(); |
||
568 | |||
569 | |||
570 | if( $active === true ) |
||
571 | { |
||
572 | $expr[] = $search->compare( '>', 'locale.currency.status', 0 ); |
||
573 | $expr[] = $search->compare( '>', 'locale.language.status', 0 ); |
||
574 | $expr[] = $search->compare( '>', 'locale.site.status', 0 ); |
||
575 | } |
||
576 | |||
577 | $search->setConditions( $search->and( $expr ) ); |
||
578 | $search->setSortations( array( $search->sort( '+', 'locale.position' ) ) ); |
||
579 | $result = $this->searchEntries( $search ); |
||
580 | |||
581 | // Try to find first item where site matches |
||
582 | foreach( $result as $row ) |
||
583 | { |
||
584 | if( $row['locale.siteid'] === $siteId ) { |
||
585 | return $this->createItemBase( $row, $siteItem, $sites ); |
||
586 | } |
||
587 | } |
||
588 | |||
589 | if( ( $row = reset( $result ) ) !== false ) |
||
590 | { |
||
591 | $row['locale.siteid'] = $siteId; |
||
592 | return $this->createItemBase( $row, $siteItem, $sites ); |
||
593 | } |
||
594 | |||
595 | return null; |
||
596 | } |
||
597 | |||
598 | |||
599 | /** |
||
600 | * Returns the locale item for the given site code, language code and currency code. |
||
601 | * |
||
602 | * If the locale item is inherited from a parent site, the site ID of this locale item |
||
603 | * is changed to the site ID of the actual site. This ensures that items assigned to |
||
604 | * the same site as the site item are still used. |
||
605 | * |
||
606 | * @param string $siteId Site ID |
||
607 | * @param string $lang Language code |
||
608 | * @param bool $active Flag to get only active items |
||
609 | * @param \Aimeos\MShop\Locale\Item\Site\Iface $siteItem Site item |
||
610 | * @param array $sites Associative list of site constant as key and sites as values |
||
611 | * @return \Aimeos\MShop\Locale\Item\Iface|null Locale item for the given parameters or null if no item was found |
||
612 | */ |
||
613 | private function bootstrapClosest( string $siteId, string $lang, bool $active, |
||
671 | } |
||
672 | |||
673 | |||
674 | /** |
||
675 | * Instances a new locale item object. |
||
676 | * |
||
677 | * @param array $values Parameter to initialise the item |
||
678 | * @param \Aimeos\MShop\Locale\Item\Site\Iface|null $site Site item |
||
679 | * @param array $sites Associative list of site constant as key and sites as values |
||
680 | * @return \Aimeos\MShop\Locale\Item\Iface Locale item |
||
681 | */ |
||
682 | protected function createItemBase( array $values = [], \Aimeos\MShop\Locale\Item\Site\Iface $site = null, |
||
686 | } |
||
687 | |||
688 | |||
689 | /** |
||
690 | * Returns the search results for the given SQL statement. |
||
691 | * |
||
692 | * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection |
||
693 | * @param string $sql SQL statement |
||
694 | * @return \Aimeos\Base\DB\Result\Iface Search result object |
||
695 | */ |
||
696 | protected function getSearchResults( \Aimeos\Base\DB\Connection\Iface $conn, string $sql ) : \Aimeos\Base\DB\Result\Iface |
||
697 | { |
||
698 | $time = microtime( true ); |
||
699 | |||
700 | $stmt = $conn->create( $sql ); |
||
701 | $result = $stmt->execute(); |
||
702 | |||
703 | $msg = 'Time: ' . ( microtime( true ) - $time ) * 1000 . "ms\n" |
||
704 | . 'Class: ' . get_class( $this ) . "\n" |
||
705 | . str_replace( ["\t", "\n\n"], ['', "\n"], trim( (string) $stmt ) ); |
||
706 | |||
707 | $this->context()->logger()->debug( $msg, 'core/sql' ); |
||
708 | |||
709 | return $result; |
||
710 | } |
||
711 | |||
712 | |||
713 | /** |
||
714 | * Searches for all items matching the given critera. |
||
715 | * |
||
716 | * @param \Aimeos\Base\Criteria\Iface $search Criteria object with conditions, sortations, etc. |
||
717 | * @param string[] $ref List of domains to fetch list items and referenced items for |
||
718 | * @param int &$total Number of items that are available in total |
||
719 | * @return array Associative list of key/value pairs |
||
720 | */ |
||
721 | protected function searchEntries( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : array |
||
881 | } |
||
882 | } |
||
883 |