/
ShopViewValidator.php
290 lines (246 loc) · 6.52 KB
/
ShopViewValidator.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
<?php
/**
* This file is part of OXID eShop Community Edition.
*
* OXID eShop Community Edition is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OXID eShop Community Edition is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OXID eShop Community Edition. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://www.oxid-esales.com
* @copyright (C) OXID eSales AG 2003-2016
* @version OXID eShop CE
*/
namespace OxidEsales\EshopCommunity\Application\Model;
use oxDb;
/**
* Shop view validator.
* checks which views are valid / invalid
*
*/
class ShopViewValidator
{
protected $_aMultiLangTables = array();
protected $_aMultiShopTables = array();
protected $_aLanguages = array();
protected $_aAllShopLanguages = array();
protected $_iShopId = null;
protected $_aAllViews = array();
protected $_aShopViews = array();
protected $_aValidShopViews = array();
/**
* Sets multi language tables.
*
* @param null $aMultiLangTables
*/
public function setMultiLangTables($aMultiLangTables)
{
$this->_aMultiLangTables = $aMultiLangTables;
}
/**
* Returns multi lang tables
*
* @return array
*/
public function getMultiLangTables()
{
return $this->_aMultiLangTables;
}
/**
* Sets multi shop tables.
*
* @param array $aMultiShopTables
*/
public function setMultiShopTables($aMultiShopTables)
{
$this->_aMultiShopTables = $aMultiShopTables;
}
/**
* Returns multi shop tables
*
* @return array
*/
public function getMultiShopTables()
{
return $this->_aMultiShopTables;
}
/**
* Returns list of active languages in shop
*
* @param array $aLanguages
*/
public function setLanguages($aLanguages)
{
$this->_aLanguages = $aLanguages;
}
/**
* Gets languages.
*
* @return array
*/
public function getLanguages()
{
return $this->_aLanguages;
}
/**
* Returns list of active languages in shop
*
* @param array $aAllShopLanguages
*/
public function setAllShopLanguages($aAllShopLanguages)
{
$this->_aAllShopLanguages = $aAllShopLanguages;
}
/**
* Gets all shop languages.
*
* @return array
*/
public function getAllShopLanguages()
{
return $this->_aAllShopLanguages;
}
/**
* Sets shop id.
*
* @param integer $iShopId
*/
public function setShopId($iShopId)
{
$this->_iShopId = $iShopId;
}
/**
* Returns list of available shops
*
* @return integer
*/
public function getShopId()
{
return $this->_iShopId;
}
/**
* Returns list of all shop views
*
* @return array
*/
protected function _getAllViews()
{
if (empty($this->_aAllViews)) {
$this->_aAllViews = oxDb::getDb()->getCol("SHOW TABLES LIKE 'oxv\_%'");
}
return $this->_aAllViews;
}
/**
* Checks if given view name belongs to current subshop or is general view
*
* @param string $sViewName View name
*
* @return bool
*/
protected function _isCurrentShopView($sViewName)
{
$blResult = false;
$blEndsWithShopId = preg_match("/[_]([0-9]+)$/", $sViewName, $aMatchEndsWithShopId);
$blContainsShopId = preg_match("/[_]([0-9]+)[_]/", $sViewName, $aMatchContainsShopId);
if ((!$blEndsWithShopId && !$blContainsShopId) ||
($blEndsWithShopId && $aMatchEndsWithShopId[1] == $this->getShopId()) ||
($blContainsShopId && $aMatchContainsShopId[1] == $this->getShopId())
) {
$blResult = true;
}
return $blResult;
}
/**
* Returns list of shop specific views currently in database
*
* @return array
*/
protected function _getShopViews()
{
if (empty($this->_aShopViews)) {
$this->_aShopViews = array();
$aAllViews = $this->_getAllViews();
foreach ($aAllViews as $sView) {
if ($this->_isCurrentShopView($sView)) {
$this->_aShopViews[] = $sView;
}
}
}
return $this->_aShopViews;
}
/**
* Returns list of valid shop views
*
* @return array
*/
protected function _getValidShopViews()
{
if (empty($this->_aValidShopViews)) {
$aTables = $this->getShopTables();
$this->_aValidShopViews = array();
foreach ($aTables as $sTable) {
$this->prepareShopTableViewNames($sTable);
}
}
return $this->_aValidShopViews;
}
/**
* Get list of shop tables
*
* @return array
*/
protected function getShopTables()
{
$shopTables = $this->getMultilangTables();
return $shopTables;
}
/**
* Appends possible table views to $this->_aValidShopViews variable.
*
* @param string $tableName
*/
protected function prepareShopTableViewNames($tableName)
{
$this->_aValidShopViews[] = 'oxv_' . $tableName;
if (in_array($tableName, $this->getMultiLangTables())) {
foreach ($this->getAllShopLanguages() as $sLang) {
$this->_aValidShopViews[] = 'oxv_' . $tableName . '_' . $sLang;
}
}
}
/**
* Checks if view name is valid according to current config
*
* @param string $sViewName View name
*
* @return bool
*/
protected function _isViewValid($sViewName)
{
return in_array($sViewName, $this->_getValidShopViews());
}
/**
* Returns list of invalid views
*
* @return array
*/
public function getInvalidViews()
{
$aInvalidViews = array();
$aShopViews = $this->_getShopViews();
foreach ($aShopViews as $sView) {
if (!$this->_isViewValid($sView)) {
$aInvalidViews[] = $sView;
}
}
return $aInvalidViews;
}
}