-
Notifications
You must be signed in to change notification settings - Fork 36
/
Attachment.php
149 lines (129 loc) · 4.71 KB
/
Attachment.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
<?php
declare(strict_types = 1);
/**
* File: Attachment.php
*
* @author Bartosz Kubicki bartosz.kubicki@lizardmedia.pl>
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
*/
namespace LizardMedia\ProductAttachment\Model\ResourceModel;
use Exception;
use LizardMedia\ProductAttachment\Model\Attachment as AttachmentModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* Class Attachment
* @package LizardMedia\ProductAttachment\Model\ResourceModel
*/
class Attachment extends AbstractDb
{
/**
* @return void
*/
protected function _construct(): void
{
$this->_init(AttachmentModel::MAIN_TABLE, AttachmentModel::ID);
}
/**
* @param AttachmentModel $attachment
* @return Attachment
*/
public function saveItemTitle(AttachmentModel $attachment): Attachment
{
$connection = $this->getConnection();
$attachmentTitleTable = $this->getTable(AttachmentModel::TITLE_TABLE);
$bind = [':attachment_id' => (int) $attachment->getId(), ':store_id' => (int) $attachment->getStoreId()];
$select = $connection->select()->from(
$attachmentTitleTable
)->where(
'attachment_id = :attachment_id AND store_id = :store_id'
);
if ($connection->fetchOne($select, $bind)) {
$where = [
'attachment_id = ?' => (int) $attachment->getId(),
'store_id = ?' => (int) $attachment->getStoreId(),
];
if ($attachment->getUseDefaultTitle()) {
$connection->delete($attachmentTitleTable, $where);
} else {
$connection->update($attachmentTitleTable, [AttachmentModel::TITLE => $attachment->getTitle()], $where);
}
} else {
if (!$attachment->getUseDefaultTitle()) {
$connection->insert(
$attachmentTitleTable,
[
AttachmentModel::ATTACHMENT_ID => (int) $attachment->getId(),
AttachmentModel::STORE_ID => (int) $attachment->getStoreId(),
AttachmentModel::TITLE => $attachment->getTitle()
]
);
}
}
return $this;
}
/**
* @param AttachmentModel $attachment
* @return Attachment
*/
public function loadItemTitle(AttachmentModel $attachment): Attachment
{
$connection = $this->getConnection();
$attachmentTitleTable = $this->getTable(AttachmentModel::TITLE_TABLE);
$bindPerStore = [':attachment_id' => (int) $attachment->getId(), ':store_id' => (int) $attachment->getStoreId()];
$defaultBind = [':attachment_id' => (int) $attachment->getId(), ':store_id' => 0];
$selectStore = $connection->select()->from(
$attachmentTitleTable,
AttachmentModel::TITLE
)->where(
'attachment_id = :attachment_id AND store_id = :store_id'
);
$select = $connection->select()->from(
$attachmentTitleTable,
AttachmentModel::TITLE
)->where(
'attachment_id = :attachment_id'
)->where('store_id = :store_id');
if ($storeTitle = $connection->fetchOne($selectStore, $bindPerStore)) {
$attachment->setTitle($storeTitle);
$attachment->setStoreTitle($storeTitle);
}
if ($title = $connection->fetchOne($select, $defaultBind)) {
$attachment->setTitle($title);
}
return $this;
}
/**
* Retrieve attachments searchable data
*
* @param int $productId
* @param int $storeId
* @throws Exception
* @return array
*/
public function getSearchableData(int $productId, int $storeId): array
{
$connection = $this->getConnection();
$ifNullDefaultTitle = $connection->getIfNullSql('st.title', 'd.title');
$select = $connection->select()->from(
['m' => $this->getMainTable()],
null
)->join(
['d' => $this->getTable(AttachmentModel::TITLE_TABLE)],
'd.attachment_id = m.id AND d.store_id = 0',
[]
)->join(
['cpe' => $this->getTable('catalog_product_entity')],
'cpe.entity_id = m.product_id',
[]
)->joinLeft(
['st' => $this->getTable(AttachmentModel::TITLE_TABLE)],
'st.attachment_id = m.id AND st.store_id = :store_id',
[AttachmentModel::TITLE => $ifNullDefaultTitle]
)->where(
'cpe.entity_id = :product_id',
$productId
);
$bind = [':store_id' => $storeId, ':product_id' => $productId];
return $connection->fetchCol($select, $bind);
}
}