|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: Collection.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\ProductAttachment\Model\ResourceModel\Attachment; |
|
13
|
|
|
|
|
14
|
|
|
use \LizardMedia\ProductAttachment\Model\Attachment; |
|
15
|
|
|
use \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment as AttachmentResource; |
|
16
|
|
|
use \LizardMedia\ProductAttachment\Model\ResourceModel\Db\Collection\EavAttributeJoiner; |
|
17
|
|
|
use \Magento\Catalog\Api\Data\ProductAttributeInterface; |
|
18
|
|
|
use \Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
|
19
|
|
|
use \Magento\Framework\Data\Collection\EntityFactoryInterface; |
|
20
|
|
|
use \Magento\Framework\DB\Adapter\AdapterInterface; |
|
21
|
|
|
use \Magento\Framework\Event\ManagerInterface; |
|
22
|
|
|
use \Magento\Framework\EntityManager\MetadataPool; |
|
23
|
|
|
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
|
24
|
|
|
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; |
|
25
|
|
|
use \Psr\Log\LoggerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Collection |
|
29
|
|
|
* @package LizardMedia\ProductAttachment\Model\ResourceModel\Attachment |
|
30
|
|
|
*/ |
|
31
|
|
|
class Collection extends AbstractCollection |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var \LizardMedia\ProductAttachment\Model\ResourceModel\Db\Collection\EavAttributeJoiner |
|
35
|
|
|
*/ |
|
36
|
|
|
private $eavAttributeJoiner; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Magento\Framework\EntityManager\MetadataPool |
|
41
|
|
|
*/ |
|
42
|
|
|
private $metadataPool; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \LizardMedia\ProductAttachment\Model\ResourceModel\Db\Collection\EavAttributeJoiner $eavAttributeJoiner |
|
47
|
|
|
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy |
|
48
|
|
|
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory |
|
49
|
|
|
* @param \Magento\Framework\Event\ManagerInterface $eventManager |
|
50
|
|
|
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool |
|
51
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
|
52
|
|
|
* @param \Magento\Framework\DB\Adapter\AdapterInterface | null $connection |
|
53
|
|
|
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb | null $resource |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
EavAttributeJoiner $eavAttributeJoiner, |
|
57
|
|
|
FetchStrategyInterface $fetchStrategy, |
|
58
|
|
|
EntityFactoryInterface $entityFactory, |
|
59
|
|
|
ManagerInterface $eventManager, |
|
60
|
|
|
MetadataPool $metadataPool, |
|
61
|
|
|
LoggerInterface $logger, |
|
62
|
|
|
AdapterInterface $connection = null, |
|
63
|
|
|
AbstractDb $resource = null |
|
64
|
|
|
) { |
|
65
|
|
|
parent::__construct( |
|
66
|
|
|
$entityFactory, |
|
67
|
|
|
$logger, |
|
68
|
|
|
$fetchStrategy, |
|
69
|
|
|
$eventManager, |
|
70
|
|
|
$connection, |
|
71
|
|
|
$resource |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$this->eavAttributeJoiner = $eavAttributeJoiner; |
|
75
|
|
|
$this->metadataPool = $metadataPool; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function _construct() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->_init( |
|
85
|
|
|
Attachment::class, |
|
86
|
|
|
AttachmentResource::class |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param array $productIds |
|
93
|
|
|
* |
|
94
|
|
|
* @throws \Exception |
|
95
|
|
|
* |
|
96
|
|
|
* @return \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection |
|
97
|
|
|
*/ |
|
98
|
|
|
public function addProductToFilter(array $productIds) : Collection |
|
99
|
|
|
{ |
|
100
|
|
|
if (empty($productIds)) { |
|
101
|
|
|
$this->addFieldToFilter(Attachment::PRODUCT_ID, ''); |
|
102
|
|
|
} else { |
|
103
|
|
|
$this->addFieldToFilter(Attachment::PRODUCT_ID, ['in' => $productIds]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param int $storeId |
|
112
|
|
|
* |
|
113
|
|
|
* @return \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection $this |
|
114
|
|
|
*/ |
|
115
|
|
|
public function addTitleToResult(int $storeId = 0) : Collection |
|
116
|
|
|
{ |
|
117
|
|
|
$ifNullDefaultTitle = $this->getConnection()->getIfNullSql('st.title', 'd.title'); |
|
118
|
|
|
$this->getSelect()->joinLeft( |
|
119
|
|
|
['d' => $this->getTable(Attachment::TITLE_TABLE)], |
|
120
|
|
|
'd.attachment_id = main_table.id AND d.store_id = 0', |
|
121
|
|
|
['default_title' => 'title'] |
|
122
|
|
|
)->joinLeft( |
|
123
|
|
|
['st' => $this->getTable(Attachment::TITLE_TABLE)], |
|
124
|
|
|
'st.attachment_id = main_table.id AND st.store_id = ' . $storeId, |
|
125
|
|
|
['store_title' => 'title', 'title' => $ifNullDefaultTitle] |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param int $storeId |
|
135
|
|
|
* |
|
136
|
|
|
* @throws \Magento\Framework\Exception\NoSuchEntityException |
|
137
|
|
|
* @throws \Magento\Framework\Exception\LocalizedException |
|
138
|
|
|
* |
|
139
|
|
|
* @return \LizardMedia\ProductAttachment\Model\ResourceModel\Attachment\Collection $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function joinProductTitle(int $storeId = 0) : Collection |
|
142
|
|
|
{ |
|
143
|
|
|
$this->eavAttributeJoiner->joinScopeable( |
|
144
|
|
|
$this, |
|
145
|
|
|
Attachment::PRODUCT_ID, |
|
146
|
|
|
ProductAttributeInterface::ENTITY_TYPE_CODE, |
|
147
|
|
|
['product_name' => 'name'], |
|
148
|
|
|
$storeId |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|