|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: Attachment.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; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use LizardMedia\ProductAttachment\Model\Attachment as AttachmentModel; |
|
16
|
|
|
use Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Attachment |
|
20
|
|
|
* @package LizardMedia\ProductAttachment\Model\ResourceModel |
|
21
|
|
|
*/ |
|
22
|
|
|
class Attachment extends AbstractDb |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function _construct(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->_init(AttachmentModel::MAIN_TABLE, AttachmentModel::ID); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param AttachmentModel $attachment |
|
34
|
|
|
* @return Attachment |
|
35
|
|
|
*/ |
|
36
|
|
|
public function saveItemTitle(AttachmentModel $attachment): Attachment |
|
37
|
|
|
{ |
|
38
|
|
|
$connection = $this->getConnection(); |
|
39
|
|
|
$attachmentTitleTable = $this->getTable(AttachmentModel::TITLE_TABLE); |
|
40
|
|
|
$bind = [':id' => (int) $attachment->getId(), ':store_id' => (int) $attachment->getStoreId()]; |
|
41
|
|
|
|
|
42
|
|
|
$select = $connection->select()->from( |
|
43
|
|
|
$attachmentTitleTable |
|
44
|
|
|
)->where( |
|
45
|
|
|
'id = :id AND store_id = :store_id' |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
if ($connection->fetchOne($select, $bind)) { |
|
49
|
|
|
$where = [ |
|
50
|
|
|
'id = ?' => (int) $attachment->getId(), |
|
51
|
|
|
'store_id = ?' => (int) $attachment->getStoreId(), |
|
52
|
|
|
]; |
|
53
|
|
|
if ($attachment->getUseDefaultTitle()) { |
|
54
|
|
|
$connection->delete($attachmentTitleTable, $where); |
|
55
|
|
|
} else { |
|
56
|
|
|
$connection->update($attachmentTitleTable, [AttachmentModel::TITLE => $attachment->getTitle()], $where); |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
if (!$attachment->getUseDefaultTitle()) { |
|
60
|
|
|
$connection->insert( |
|
61
|
|
|
$attachmentTitleTable, |
|
62
|
|
|
[ |
|
63
|
|
|
AttachmentModel::ATTACHMENT_ID => (int) $attachment->getId(), |
|
64
|
|
|
AttachmentModel::STORE_ID => (int) $attachment->getStoreId(), |
|
65
|
|
|
AttachmentModel::TITLE => $attachment->getTitle() |
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param AttachmentModel $attachment |
|
76
|
|
|
* @return Attachment |
|
77
|
|
|
*/ |
|
78
|
|
|
public function loadItemTitle(AttachmentModel $attachment): Attachment |
|
79
|
|
|
{ |
|
80
|
|
|
$connection = $this->getConnection(); |
|
81
|
|
|
$attachmentTitleTable = $this->getTable(AttachmentModel::TITLE_TABLE); |
|
82
|
|
|
$bindPerStore = [':attachment_id' => (int) $attachment->getId(), ':store_id' => (int) $attachment->getStoreId()]; |
|
83
|
|
|
$defaultBind = [':attachment_id' => (int) $attachment->getId(), ':store_id' => 0]; |
|
84
|
|
|
|
|
85
|
|
|
$selectStore = $connection->select()->from( |
|
86
|
|
|
$attachmentTitleTable, |
|
87
|
|
|
AttachmentModel::TITLE |
|
88
|
|
|
)->where( |
|
89
|
|
|
'attachment_id = :attachment_id AND store_id = :store_id' |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$select = $connection->select()->from( |
|
93
|
|
|
$attachmentTitleTable, |
|
94
|
|
|
AttachmentModel::TITLE |
|
95
|
|
|
)->where( |
|
96
|
|
|
'attachment_id = :attachment_id' |
|
97
|
|
|
)->where('store_id = :store_id'); |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
if ($storeTitle = $connection->fetchOne($selectStore, $bindPerStore)) { |
|
101
|
|
|
$attachment->setTitle($storeTitle); |
|
102
|
|
|
$attachment->setStoreTitle($storeTitle); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if ($title = $connection->fetchOne($select, $defaultBind)) { |
|
106
|
|
|
$attachment->setTitle($title); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Retrieve attachments searchable data |
|
115
|
|
|
* |
|
116
|
|
|
* @param int $productId |
|
117
|
|
|
* @param int $storeId |
|
118
|
|
|
* @throws Exception |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getSearchableData(int $productId, int $storeId): array |
|
122
|
|
|
{ |
|
123
|
|
|
$connection = $this->getConnection(); |
|
124
|
|
|
$ifNullDefaultTitle = $connection->getIfNullSql('st.title', 'd.title'); |
|
125
|
|
|
$select = $connection->select()->from( |
|
126
|
|
|
['m' => $this->getMainTable()], |
|
127
|
|
|
null |
|
128
|
|
|
)->join( |
|
129
|
|
|
['d' => $this->getTable(AttachmentModel::TITLE_TABLE)], |
|
130
|
|
|
'd.attachment_id = m.id AND d.store_id = 0', |
|
131
|
|
|
[] |
|
132
|
|
|
)->join( |
|
133
|
|
|
['cpe' => $this->getTable('catalog_product_entity')], |
|
134
|
|
|
'cpe.entity_id = m.product_id', |
|
135
|
|
|
[] |
|
136
|
|
|
)->joinLeft( |
|
137
|
|
|
['st' => $this->getTable(AttachmentModel::TITLE_TABLE)], |
|
138
|
|
|
'st.attachment_id = m.id AND st.store_id = :store_id', |
|
139
|
|
|
[AttachmentModel::TITLE => $ifNullDefaultTitle] |
|
140
|
|
|
)->where( |
|
141
|
|
|
'cpe.entity_id = :product_id', |
|
142
|
|
|
$productId |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$bind = [':store_id' => $storeId, ':product_id' => $productId]; |
|
146
|
|
|
|
|
147
|
|
|
return $connection->fetchCol($select, $bind); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|