/
Standard.php
1104 lines (973 loc) · 37.3 KB
/
Standard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
* @copyright Metaways Infosystems GmbH, 2011
* @copyright Aimeos (aimeos.org), 2015-2020
* @package MShop
* @subpackage Catalog
*/
namespace Aimeos\MShop\Catalog\Manager;
/**
* Catalog manager with methods for managing categories products, text, media.
*
* @package MShop
* @subpackage Catalog
*/
class Standard extends Base
implements \Aimeos\MShop\Catalog\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
{
private $searchConfig = array(
'id' => array(
'code' => 'catalog.id',
'internalcode' => 'mcat."id"',
'label' => 'ID',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
),
'catalog.siteid' => array(
'code' => 'catalog.siteid',
'internalcode' => 'mcat."siteid"',
'label' => 'Site ID',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'parentid' => array(
'code' => 'catalog.parentid',
'internalcode' => 'mcat."parentid"',
'label' => 'Parent ID',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
),
'level' => array(
'code' => 'catalog.level',
'internalcode' => 'mcat."level"',
'label' => 'Tree level',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
),
'left' => array(
'code' => 'catalog.left',
'internalcode' => 'mcat."nleft"',
'label' => 'Left value',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
),
'right' => array(
'code' => 'catalog.right',
'internalcode' => 'mcat."nright"',
'label' => 'Right value',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
'public' => false,
),
'label' => array(
'code' => 'catalog.label',
'internalcode' => 'mcat."label"',
'label' => 'Label',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'code' => array(
'code' => 'catalog.code',
'internalcode' => 'mcat."code"',
'label' => 'Code',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'status' => array(
'code' => 'catalog.status',
'internalcode' => 'mcat."status"',
'label' => 'Status',
'type' => 'integer',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
),
'catalog.url' => array(
'code' => 'catalog.url',
'internalcode' => 'mcat."url"',
'label' => 'URL segment',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'catalog.target' => array(
'code' => 'catalog.target',
'internalcode' => 'mcat."target"',
'label' => 'URL target',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
),
'catalog.config' => array(
'code' => 'catalog.config',
'internalcode' => 'mcat."config"',
'label' => 'Config',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'catalog.ctime' => array(
'label' => 'Create date/time',
'code' => 'catalog.ctime',
'internalcode' => 'mcat."ctime"',
'type' => 'datetime',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'catalog.mtime' => array(
'label' => 'Modify date/time',
'code' => 'catalog.mtime',
'internalcode' => 'mcat."mtime"',
'type' => 'datetime',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'catalog.editor' => array(
'code' => 'catalog.editor',
'internalcode' => 'mcat."editor"',
'label' => 'Editor',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'catalog:has' => array(
'code' => 'catalog:has()',
'internalcode' => ':site AND :key AND mcatli."id"',
'internaldeps' => ['LEFT JOIN "mshop_catalog_list" AS mcatli ON ( mcatli."parentid" = mcat."id" )'],
'label' => 'Catalog has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
'type' => 'null',
'internaltype' => 'null',
'public' => false,
),
);
/**
* Initializes the object.
*
* @param \Aimeos\MShop\Context\Item\Iface $context Context object
*/
public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
{
parent::__construct( $context, $this->searchConfig );
$this->setResourceName( 'db-catalog' );
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
$level = $context->getConfig()->get( 'mshop/catalog/manager/sitemode', $level );
$this->searchConfig['catalog:has']['function'] = function( &$source, array $params ) use ( $level ) {
array_walk_recursive( $params, function( &$v ) {
$v = trim( $v, '\'' );
} );
$keys = [];
$params[1] = isset( $params[1] ) ? $params[1] : '';
$params[2] = isset( $params[2] ) ? $params[2] : '';
foreach( (array) $params[1] as $type ) {
foreach( (array) $params[2] as $id ) {
$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
}
}
$sitestr = $this->getSiteString( 'mcatli."siteid"', $level );
$keystr = $this->toExpression( 'mcatli."key"', $keys, $params[2] !== '' ? '==' : '=~' );
$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
return $params;
};
}
/**
* Removes old entries from the storage.
*
* @param string[] $siteids List of IDs for sites whose entries should be deleted
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
public function clear( array $siteids ) : \Aimeos\MShop\Common\Manager\Iface
{
$context = $this->getContext();
$config = $context->getConfig();
$search = $this->getObject()->filter();
foreach( $config->get( 'mshop/catalog/manager/submanagers', ['lists'] ) as $domain ) {
$this->getObject()->getSubManager( $domain )->clear( $siteids );
}
$dbm = $context->getDatabaseManager();
$dbname = $this->getResourceName();
$conn = $dbm->acquire( $dbname );
try
{
/** mshop/catalog/manager/standard/cleanup/mysql
* Deletes the categories for the given site from the database
*
* @see mshop/catalog/manager/standard/cleanup/ansi
*/
/** mshop/catalog/manager/standard/cleanup/ansi
* Deletes the categories for the given site from the database
*
* Removes the records matched by the given site ID from the catalog
* database.
*
* The ":siteid" placeholder is replaced by the name and value of the
* site ID column and the given ID or list of IDs.
*
* The SQL statement should conform to the ANSI standard to be
* compatible with most relational database systems. This also
* includes using double quotes for table and column names.
*
* @param string SQL statement for removing the records
* @since 2014.03
* @category Developer
* @see mshop/catalog/manager/standard/delete/ansi
* @see mshop/catalog/manager/standard/insert/ansi
* @see mshop/catalog/manager/standard/update/ansi
* @see mshop/catalog/manager/standard/newid/ansi
* @see mshop/catalog/manager/standard/search/ansi
* @see mshop/catalog/manager/standard/count/ansi
*/
$path = 'mshop/catalog/manager/standard/cleanup';
$sql = $this->getSqlConfig( $path );
$types = array( 'siteid' => \Aimeos\MW\DB\Statement\Base::PARAM_STR );
$translations = array( 'siteid' => '"siteid"' );
$search->setConditions( $search->compare( '==', 'siteid', $siteids ) );
$sql = str_replace( ':siteid', $search->getConditionSource( $types, $translations ), $sql );
$stmt = $conn->create( $sql );
$stmt->bind( 1, 0, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
$stmt->bind( 2, 0x7FFFFFFF, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
$stmt->execute()->finish();
$dbm->release( $conn, $dbname );
}
catch( \Exception $e )
{
$dbm->release( $conn, $dbname );
throw $e;
}
return $this;
}
/**
* Creates a new empty item instance
*
* @param array $values Values the item should be initialized with
* @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object
*/
public function createItem( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
{
$values['siteid'] = $this->getContext()->getLocale()->getSiteId();
return $this->createItemBase( $values );
}
/**
* Creates a filter object.
*
* @param bool $default Add default criteria
* @param bool $site TRUE for adding site criteria to limit items by the site of related items
* @return \Aimeos\MW\Criteria\Iface Returns the filter object
*/
public function filter( bool $default = false, bool $site = false ) : \Aimeos\MW\Criteria\Iface
{
if( $default === true ) {
return $this->filterBase( 'catalog' );
}
return parent::filter();
}
/**
* Deletes the item.
*
* @param \Aimeos\MShop\Common\Item\Iface|string $itemId Item object or ID of the item object
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
public function deleteItem( $id ) : \Aimeos\MShop\Common\Manager\Iface
{
$this->begin();
$this->lock();
try
{
$siteid = $this->getContext()->getLocale()->getSiteId();
$this->createTreeManager( $siteid )->deleteNode( $id );
$this->unlock();
$this->commit();
}
catch( \Exception $e )
{
$this->unlock();
$this->rollback();
throw $e;
}
return $this;
}
/**
* Removes multiple items.
*
* @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
public function deleteItems( array $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
{
foreach( $itemIds as $itemId ) {
$this->getObject()->deleteItem( $itemId );
}
return $this->deleteRefItems( $itemIds );
}
/**
* Returns the item specified by its code and domain/type if necessary
*
* @param string $code Code of the item
* @param string[] $ref List of domains to fetch list items and referenced items for
* @param string|null $domain Domain of the item if necessary to identify the item uniquely
* @param string|null $type Type code of the item if necessary to identify the item uniquely
* @param bool $default True to add default criteria
* @return \Aimeos\MShop\Catalog\Item\Iface Catalog item object
*/
public function find( string $code, array $ref = [], string $domain = null, string $type = null,
bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
{
return $this->findBase( array( 'catalog.code' => $code ), $ref, $default );
}
/**
* Returns the item specified by its ID.
*
* @param string $id Unique ID of the catalog item
* @param string[] $ref List of domains to fetch list items and referenced items for
* @param bool $default Add default criteria
* @return \Aimeos\MShop\Catalog\Item\Iface Catalog item of the given ID
* @throws \Aimeos\MShop\Exception If item couldn't be found
*/
public function get( string $id, array $ref = [], bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
{
return $this->getItemBase( 'catalog.id', $id, $ref, $default );
}
/**
* Returns the available manager types
*
* @param bool $withsub Return also the resource type of sub-managers if true
* @return string[] Type of the manager and submanagers, subtypes are separated by slashes
*/
public function getResourceType( bool $withsub = true ) : array
{
$path = 'mshop/catalog/manager/submanagers';
return $this->getResourceTypeBase( 'catalog', $path, array( 'lists' ), $withsub );
}
/**
* Returns the attributes that can be used for searching.
*
* @param bool $withsub Return also attributes of sub-managers if true
* @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items
*/
public function getSearchAttributes( bool $withsub = true ) : array
{
/** mshop/catalog/manager/submanagers
* List of manager names that can be instantiated by the catalog manager
*
* Managers provide a generic interface to the underlying storage.
* Each manager has or can have sub-managers caring about particular
* aspects. Each of these sub-managers can be instantiated by its
* parent manager using the getSubManager() method.
*
* The search keys from sub-managers can be normally used in the
* manager as well. It allows you to search for items of the manager
* using the search keys of the sub-managers to further limit the
* retrieved list of items.
*
* @param array List of sub-manager names
* @since 2014.03
* @category Developer
*/
$path = 'mshop/catalog/manager/submanagers';
return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
}
/**
* Adds a new item object.
*
* @param \Aimeos\MShop\Catalog\Item\Iface $item Item which should be inserted
* @param string|null $parentId ID of the parent item where the item should be inserted into
* @param string|null $refId ID of the item where the item should be inserted before (null to append)
* @return \Aimeos\MShop\Catalog\Item\Iface $item Updated item including the generated ID
*/
public function insertItem( \Aimeos\MShop\Catalog\Item\Iface $item, string $parentId = null,
string $refId = null ) : \Aimeos\MShop\Catalog\Item\Iface
{
$this->begin();
$this->lock();
try
{
$node = $item->getNode();
$siteid = $this->getContext()->getLocale()->getSiteId();
$this->createTreeManager( $siteid )->insertNode( $node, $parentId, $refId );
$this->updateUsage( $node->getId(), $item, true );
$this->unlock();
$this->commit();
}
catch( \Exception $e )
{
$this->unlock();
$this->rollback();
throw $e;
}
$item = $this->saveListItems( $item, 'catalog' );
return $this->saveChildren( $item );
}
/**
* Moves an existing item to the new parent in the storage.
*
* @param string $id ID of the item that should be moved
* @param string|null $oldParentId ID of the old parent item which currently contains the item that should be removed
* @param string|null $newParentId ID of the new parent item where the item should be moved to
* @param string|null $refId ID of the item where the item should be inserted before (null to append)
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
public function moveItem( string $id, string $oldParentId = null, string $newParentId = null,
string $refId = null ) : \Aimeos\MShop\Catalog\Manager\Iface
{
$this->begin();
$this->lock();
try
{
$item = $this->getObject()->get( $id );
$siteid = $this->getContext()->getLocale()->getSiteId();
$this->createTreeManager( $siteid )->moveNode( $id, $oldParentId, $newParentId, $refId );
$this->updateUsage( $id, $item );
$this->unlock();
$this->commit();
}
catch( \Exception $e )
{
$this->unlock();
$this->rollback();
throw $e;
}
return $this;
}
/**
* Updates an item object.
*
* @param \Aimeos\MShop\Catalog\Item\Iface $item Item object whose data should be saved
* @param bool $fetch True if the new ID should be returned in the item
* @return \Aimeos\MShop\Catalog\Item\Iface $item Updated item including the generated ID
*/
public function saveItem( \Aimeos\MShop\Catalog\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Catalog\Item\Iface
{
if( !$item->isModified() )
{
$item = $this->saveListItems( $item, 'catalog', $fetch );
return $this->saveChildren( $item );
}
$node = $item->getNode();
$siteid = $this->getContext()->getLocale()->getSiteId();
$this->createTreeManager( $siteid )->saveNode( $node );
$this->updateUsage( $node->getId(), $item );
$item = $this->saveListItems( $item, 'catalog', $fetch );
return $this->saveChildren( $item );
}
/**
* Searches for all items matching the given critera.
*
* @param \Aimeos\MW\Criteria\Iface $search Search criteria object
* @param string[] $ref List of domains to fetch list items and referenced items for
* @param int|null &$total Number of items that are available in total
* @return \Aimeos\Map List of items implementing \Aimeos\MShop\Catalog\Item\Iface with ids as keys
*/
public function search( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
{
$nodeMap = $siteMap = [];
$context = $this->getContext();
$dbname = $this->getResourceName();
$dbm = $context->getDatabaseManager();
$conn = $dbm->acquire( $dbname );
try
{
$required = array( 'catalog' );
/** mshop/catalog/manager/sitemode
* Mode how items from levels below or above in the site tree are handled
*
* By default, only items from the current site are fetched from the
* storage. If the ai-sites extension is installed, you can create a
* tree of sites. Then, this setting allows you to define for the
* whole catalog domain if items from parent sites are inherited,
* sites from child sites are aggregated or both.
*
* Available constants for the site mode are:
* * 0 = only items from the current site
* * 1 = inherit items from parent sites
* * 2 = aggregate items from child sites
* * 3 = inherit and aggregate items at the same time
*
* You also need to set the mode in the locale manager
* (mshop/locale/manager/standard/sitelevel) to one of the constants.
* If you set it to the same value, it will work as described but you
* can also use different modes. For example, if inheritance and
* aggregation is configured the locale manager but only inheritance
* in the domain manager because aggregating items makes no sense in
* this domain, then items wil be only inherited. Thus, you have full
* control over inheritance and aggregation in each domain.
*
* @param int Constant from Aimeos\MShop\Locale\Manager\Base class
* @category Developer
* @since 2018.01
* @see mshop/locale/manager/standard/sitelevel
*/
$level = \Aimeos\MShop\Locale\Manager\Base::SITE_PATH;
$level = $context->getConfig()->get( 'mshop/catalog/manager/sitemode', $level );
/** mshop/catalog/manager/standard/search-item/mysql
* Retrieves the records matched by the given criteria in the database
*
* @see mshop/catalog/manager/standard/search-item/ansi
*/
/** mshop/catalog/manager/standard/search-item/ansi
* Retrieves the records matched by the given criteria in the database
*
* Fetches the records matched by the given criteria from the catalog
* database. The records must be from one of the sites that are
* configured via the context item. If the current site is part of
* a tree of sites, the SELECT statement can retrieve all records
* from the current site and the complete sub-tree of sites.
*
* As the records can normally be limited by criteria from sub-managers,
* their tables must be joined in the SQL context. This is done by
* using the "internaldeps" property from the definition of the ID
* column of the sub-managers. These internal dependencies specify
* the JOIN between the tables and the used columns for joining. The
* ":joins" placeholder is then replaced by the JOIN strings from
* the sub-managers.
*
* To limit the records matched, conditions can be added to the given
* criteria object. It can contain comparisons like column names that
* must match specific values which can be combined by AND, OR or NOT
* operators. The resulting string of SQL conditions replaces the
* ":cond" placeholder before the statement is sent to the database
* server.
*
* If the records that are retrieved should be ordered by one or more
* columns, the generated string of column / sort direction pairs
* replaces the ":order" placeholder. In case no ordering is required,
* the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
* markers is removed to speed up retrieving the records. Columns of
* sub-managers can also be used for ordering the result set but then
* no index can be used.
*
* The number of returned records can be limited and can start at any
* number between the begining and the end of the result set. For that
* the ":size" and ":start" placeholders are replaced by the
* corresponding values from the criteria object. The default values
* are 0 for the start and 100 for the size value.
*
* The SQL statement should conform to the ANSI standard to be
* compatible with most relational database systems. This also
* includes using double quotes for table and column names.
*
* @param string SQL statement for searching items
* @since 2014.03
* @category Developer
* @see mshop/catalog/manager/standard/delete/ansi
* @see mshop/catalog/manager/standard/get/ansi
* @see mshop/catalog/manager/standard/insert/ansi
* @see mshop/catalog/manager/standard/update/ansi
* @see mshop/catalog/manager/standard/newid/ansi
* @see mshop/catalog/manager/standard/search/ansi
* @see mshop/catalog/manager/standard/count/ansi
* @see mshop/catalog/manager/standard/move-left/ansi
* @see mshop/catalog/manager/standard/move-right/ansi
* @see mshop/catalog/manager/standard/update-parentid/ansi
*/
$cfgPathSearch = 'mshop/catalog/manager/standard/search-item';
/** mshop/catalog/manager/standard/count/mysql
* Counts the number of records matched by the given criteria in the database
*
* @see mshop/catalog/manager/standard/count/ansi
*/
/** mshop/catalog/manager/standard/count/ansi
* Counts the number of records matched by the given criteria in the database
*
* Counts all records matched by the given criteria from the catalog
* database. The records must be from one of the sites that are
* configured via the context item. If the current site is part of
* a tree of sites, the statement can count all records from the
* current site and the complete sub-tree of sites.
*
* As the records can normally be limited by criteria from sub-managers,
* their tables must be joined in the SQL context. This is done by
* using the "internaldeps" property from the definition of the ID
* column of the sub-managers. These internal dependencies specify
* the JOIN between the tables and the used columns for joining. The
* ":joins" placeholder is then replaced by the JOIN strings from
* the sub-managers.
*
* To limit the records matched, conditions can be added to the given
* criteria object. It can contain comparisons like column names that
* must match specific values which can be combined by AND, OR or NOT
* operators. The resulting string of SQL conditions replaces the
* ":cond" placeholder before the statement is sent to the database
* server.
*
* Both, the strings for ":joins" and for ":cond" are the same as for
* the "search" SQL statement.
*
* Contrary to the "search" statement, it doesn't return any records
* but instead the number of records that have been found. As counting
* thousands of records can be a long running task, the maximum number
* of counted records is limited for performance reasons.
*
* The SQL statement should conform to the ANSI standard to be
* compatible with most relational database systems. This also
* includes using double quotes for table and column names.
*
* @param string SQL statement for counting items
* @since 2014.03
* @category Developer
* @see mshop/catalog/manager/standard/delete/ansi
* @see mshop/catalog/manager/standard/get/ansi
* @see mshop/catalog/manager/standard/insert/ansi
* @see mshop/catalog/manager/standard/update/ansi
* @see mshop/catalog/manager/standard/newid/ansi
* @see mshop/catalog/manager/standard/search/ansi
* @see mshop/catalog/manager/standard/search-item/ansi
* @see mshop/catalog/manager/standard/move-left/ansi
* @see mshop/catalog/manager/standard/move-right/ansi
* @see mshop/catalog/manager/standard/update-parentid/ansi
*/
$cfgPathCount = 'mshop/catalog/manager/standard/count';
if( $search->getSortations() === [] ) {
$search->setSortations( [$search->sort( '+', 'catalog.left' )] );
}
$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
while( ( $row = $results->fetch() ) !== null ) {
$siteMap[(string) $row['siteid']][(string) $row['id']] = new \Aimeos\MW\Tree\Node\Standard( $row );
}
$sitePath = array_reverse( (array) $this->getContext()->getLocale()->getSitePath() );
foreach( $sitePath as $siteId )
{
if( isset( $siteMap[$siteId] ) && !empty( $siteMap[$siteId] ) )
{
$nodeMap = $siteMap[$siteId];
break;
}
}
$dbm->release( $conn, $dbname );
}
catch( \Exception $e )
{
$dbm->release( $conn, $dbname );
throw $e;
}
return $this->buildItems( $nodeMap, $ref, 'catalog' );
}
/**
* Returns a list of items starting with the given category that are in the path to the root node
*
* @param string $id ID of item to get the path for
* @param string[] $ref List of domains to fetch list items and referenced items for
* @return \Aimeos\Map Associative list of catalog items implementing \Aimeos\MShop\Catalog\Item\Iface with IDs as keys
*/
public function getPath( string $id, array $ref = [] ) : \Aimeos\Map
{
$sitePath = array_reverse( (array) $this->getContext()->getLocale()->getSitePath() );
foreach( $sitePath as $siteId )
{
try {
$path = $this->createTreeManager( $siteId )->getPath( $id );
} catch( \Exception $e ) {
continue;
}
if( !empty( $path ) )
{
$itemMap = [];
foreach( $path as $node ) {
$itemMap[$node->getId()] = $node;
}
return $this->buildItems( $itemMap, $ref, 'catalog' );
}
}
throw new \Aimeos\MShop\Catalog\Exception( sprintf( 'Catalog path for ID "%1$s" not found', $id ) );
}
/**
* Returns a node and its descendants depending on the given resource.
*
* @param string|null $id Retrieve nodes starting from the given ID
* @param string[] List of domains (e.g. text, media, etc.) whose referenced items should be attached to the objects
* @param int $level One of the level constants from \Aimeos\MW\Tree\Manager\Base
* @param \Aimeos\MW\Criteria\Iface|null $criteria Optional criteria object with conditions
* @return \Aimeos\MShop\Catalog\Item\Iface Catalog item, maybe with subnodes
*/
public function getTree( string $id = null, array $ref = [], int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE,
\Aimeos\MW\Criteria\Iface $criteria = null ) : \Aimeos\MShop\Catalog\Item\Iface
{
$sitePath = array_reverse( (array) $this->getContext()->getLocale()->getSitePath() );
foreach( $sitePath as $siteId )
{
try {
$node = $this->createTreeManager( $siteId )->getNode( $id, $level, $criteria );
} catch( \Aimeos\MW\Tree\Exception $e ) {
continue;
}
$listItems = $listItemMap = $refIdMap = [];
$nodeMap = $this->getNodeMap( $node );
if( count( $ref ) > 0 ) {
$listItems = $this->getListItems( array_keys( $nodeMap ), $ref, 'catalog' );
}
foreach( $listItems as $listItem )
{
$domain = $listItem->getDomain();
$parentid = $listItem->getParentId();
$listItemMap[$parentid][$domain][$listItem->getId()] = $listItem;
$refIdMap[$domain][$listItem->getRefId()][] = $parentid;
}
$refItemMap = $this->getRefItems( $refIdMap, $ref );
$nodeid = $node->getId();
$listItems = [];
if( array_key_exists( $nodeid, $listItemMap ) ) {
$listItems = $listItemMap[$nodeid];
}
$refItems = [];
if( array_key_exists( $nodeid, $refItemMap ) ) {
$refItems = $refItemMap[$nodeid];
}
if( $item = $this->applyFilter( $this->createItemBase( [], $listItems, $refItems, [], $node ) ) )
{
$this->createTree( $node, $item, $listItemMap, $refItemMap );
return $item;
}
}
throw new \Aimeos\MShop\Catalog\Exception( sprintf( 'No catalog node for ID "%1$s"', $id ) );
}
/**
* Creates a new extension manager in the domain.
*
* @param string $manager Name of the sub manager type
* @param string|null $name Name of the implementation, will be from configuration (or Default)
* @return \Aimeos\MShop\Common\Manager\Iface Manager extending the domain functionality
*/
public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
{
return $this->getSubManagerBase( 'catalog', $manager, $name );
}
/**
* Saves the children of the given node
*
* @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object incl. child items
* @return \Aimeos\MShop\Catalog\Item\Iface Catalog item with saved child items
*/
protected function saveChildren( \Aimeos\MShop\Catalog\Item\Iface $item ) : \Aimeos\MShop\Catalog\Item\Iface
{
$rmIds = [];
foreach( $item->getChildrenDeleted() as $child ) {
$rmIds[] = $child->getId();
}
$this->deleteItems( $rmIds );
foreach( $item->getChildren() as $child )
{
if( $child->getId() !== null )
{
$this->saveItem( $child );
if( $child->getParentId() !== $item->getParentId() ) {
$this->moveItem( $child->getId(), $item->getParentId(), $child->getParentId() );
}
}
else
{
$this->insertItem( $child, $item->getId() );
}
}
return $item;
}
/**
* Locks the catalog table against modifications from other connections
*
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
protected function lock() : \Aimeos\MShop\Catalog\Manager\Iface
{
/** mshop/catalog/manager/standard/lock/mysql
* SQL statement for locking the catalog table
*
* @see mshop/catalog/manager/standard/lock/ansi
*/
/** mshop/catalog/manager/standard/lock/ansi
* SQL statement for locking the catalog table
*
* Updating the nested set of categories in the catalog table requires locking
* the whole table to avoid data corruption. This statement will be followed by
* insert or update statements and closed by an unlock statement.
*
* @param string Lock SQL statement
* @since 2019.04
* @category Developer
*/
$path = 'mshop/catalog/manager/standard/lock';
if( ( $sql = $this->getSqlConfig( $path ) ) !== $path )
{
$dbname = $this->getResourceName();
$dbm = $this->getContext()->getDatabaseManager();
$conn = $dbm->acquire( $dbname );
$conn->create( $sql )->execute()->finish();
$dbm->release( $conn, $dbname );
}
return $this;
}
/**
* Unlocks the catalog table for modifications from other connections
*
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
protected function unlock() : \Aimeos\MShop\Catalog\Manager\Iface
{
/** mshop/catalog/manager/standard/unlock/mysql
* SQL statement for unlocking the catalog table
*
* @see mshop/catalog/manager/standard/unlock/ansi
*/
/** mshop/catalog/manager/standard/unlock/ansi
* SQL statement for unlocking the catalog table
*
* Updating the nested set of categories in the catalog table requires locking
* the whole table to avoid data corruption. This statement will be executed
* after the table is locked and insert or update statements have been sent to
* the database.
*
* @param string Lock SQL statement
* @since 2019.04
* @category Developer
*/
$path = 'mshop/catalog/manager/standard/unlock';
if( ( $sql = $this->getSqlConfig( $path ) ) !== $path )
{
$dbname = $this->getResourceName();
$dbm = $this->getContext()->getDatabaseManager();
$conn = $dbm->acquire( $dbname );
$conn->create( $sql )->execute()->finish();
$dbm->release( $conn, $dbname );
}
return $this;
}
/**
* Updates the usage information of a node.
*
* @param string $id Id of the record
* @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item
* @param bool $case True if the record shoud be added or false for an update
* @return \Aimeos\MShop\Catalog\Manager\Iface Manager object for chaining method calls
*/
private function updateUsage( string $id, \Aimeos\MShop\Catalog\Item\Iface $item,
bool $case = false ) : \Aimeos\MShop\Catalog\Manager\Iface
{
$date = date( 'Y-m-d H:i:s' );
$context = $this->getContext();
$dbm = $context->getDatabaseManager();
$dbname = $this->getResourceName();
$conn = $dbm->acquire( $dbname );
try
{
$siteid = $context->getLocale()->getSiteId();
$columns = $this->getObject()->getSaveAttributes();
if( $case !== true )
{
/** mshop/catalog/manager/standard/update-usage/mysql
* Updates the config, editor and mtime value of an updated record
*
* @see mshop/catalog/manager/standard/update-usage/ansi
*/
/** mshop/catalog/manager/standard/update-usage/ansi
* Updates the config, editor and mtime value of an updated record
*
* Each record contains some usage information like when it was
* created, last modified and by whom. These information are part
* of the catalog items and the generic tree manager doesn't care
* about this information. Thus, they are updated after the tree
* manager saved the basic record information.
*
* The SQL statement must be a string suitable for being used as
* prepared statement. It must include question marks for binding
* the values from the catalog item to the statement before they are
* sent to the database server. The order of the columns must
* correspond to the order in the method using this statement,
* so the correct values are bound to the columns.
*
* The SQL statement should conform to the ANSI standard to be
* compatible with most relational database systems. This also
* includes using double quotes for table and column names.