forked from gmoigneu/magento2-platformsh
/
Collection.php
229 lines (211 loc) · 5.71 KB
/
Collection.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
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Model\ResourceModel\Theme;
/**
* Theme collection
*/
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection implements
\Magento\Framework\View\Design\Theme\Label\ListInterface,
\Magento\Framework\View\Design\Theme\ListInterface
{
/**
* Default page size
*/
const DEFAULT_PAGE_SIZE = 6;
/**
* Collection initialization
*
* @return void
*/
protected function _construct()
{
$this->_init('Magento\Theme\Model\Theme', 'Magento\Theme\Model\ResourceModel\Theme');
}
/**
* Add title for parent themes
*
* @return $this
*/
public function addParentTitle()
{
$this->getSelect()->joinLeft(
['parent' => $this->getMainTable()],
'main_table.parent_id = parent.theme_id',
['parent_theme_title' => 'parent.theme_title']
);
return $this;
}
/**
* Add area filter
*
* @param string $area
* @return $this
*/
public function addAreaFilter($area = \Magento\Framework\App\Area::AREA_FRONTEND)
{
$this->getSelect()->where('main_table.area=?', $area);
return $this;
}
/**
* Add type filter in relations
*
* @param int $typeParent
* @param int $typeChild
* @return $this
*/
public function addTypeRelationFilter($typeParent, $typeChild)
{
$this->getSelect()->join(
['parent' => $this->getMainTable()],
'main_table.parent_id = parent.theme_id',
['parent_type' => 'parent.type']
)->where(
'parent.type = ?',
$typeParent
)->where(
'main_table.type = ?',
$typeChild
);
return $this;
}
/**
* Add type filter
*
* @param string|array $type
* @return $this
*/
public function addTypeFilter($type)
{
$this->addFieldToFilter('main_table.type', ['in' => $type]);
return $this;
}
/**
* Filter visible themes in backend (physical and virtual only)
*
* @return $this
*/
public function filterVisibleThemes()
{
$this->addTypeFilter(
[
\Magento\Framework\View\Design\ThemeInterface::TYPE_PHYSICAL,
\Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL,
]
);
return $this;
}
/**
* Return array for select field
*
* @return array
*/
public function toOptionArray()
{
return $this->_toOptionArray('theme_id', 'theme_title');
}
/**
* Return array for grid column
*
* @return array
*/
public function toOptionHash()
{
return $this->_toOptionHash('theme_id', 'theme_title');
}
/**
* Get theme from DB by area and theme_path
*
* @param string $fullPath
* @return \Magento\Theme\Model\Theme
*/
public function getThemeByFullPath($fullPath)
{
$this->_reset()->clear();
list($area, $themePath) = explode('/', $fullPath, 2);
$this->addFieldToFilter('area', $area);
$this->addFieldToFilter('theme_path', $themePath);
return $this->getFirstItem();
}
/**
* Set page size
*
* @param int $size
* @return $this
*/
public function setPageSize($size = self::DEFAULT_PAGE_SIZE)
{
return parent::setPageSize($size);
}
/**
* Update all child themes relations
*
* @param \Magento\Framework\View\Design\ThemeInterface $themeModel
* @return $this
*/
public function updateChildRelations(\Magento\Framework\View\Design\ThemeInterface $themeModel)
{
$parentThemeId = $themeModel->getParentId();
$this->addFieldToFilter('parent_id', ['eq' => $themeModel->getId()])->load();
/** @var $theme \Magento\Framework\View\Design\ThemeInterface */
foreach ($this->getItems() as $theme) {
$theme->setParentId($parentThemeId)->save();
}
return $this;
}
/**
* Filter frontend physical theme.
* All themes or per page if set page and page size (page size is optional)
*
* @param int $page
* @param int $pageSize
* @return $this
*/
public function filterPhysicalThemes(
$page = null,
$pageSize = \Magento\Theme\Model\ResourceModel\Theme\Collection::DEFAULT_PAGE_SIZE
) {
$this->addAreaFilter(
\Magento\Framework\App\Area::AREA_FRONTEND
)->addTypeFilter(
\Magento\Framework\View\Design\ThemeInterface::TYPE_PHYSICAL
);
if ($page) {
$this->setPageSize($pageSize)->setCurPage($page);
}
return $this;
}
/**
* Filter theme customization
*
* @param string $area
* @param int $type
* @return $this
*/
public function filterThemeCustomizations(
$area = \Magento\Framework\App\Area::AREA_FRONTEND,
$type = \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL
) {
$this->addAreaFilter($area)->addTypeFilter($type);
return $this;
}
/**
* @inheritdoc
*/
public function getLabels()
{
$labels = $this->loadRegisteredThemes();
return $labels->toOptionArray();
}
/**
* @return $this
*/
public function loadRegisteredThemes()
{
$this->_reset()->clear();
return $this->setOrder('theme_title', \Magento\Framework\Data\Collection::SORT_ORDER_ASC)
->filterVisibleThemes()->addAreaFilter(\Magento\Framework\App\Area::AREA_FRONTEND);
}
}