path_manager.php
7.15 KB
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
<?php
class ModelToolPathManager extends Model {
public $cachedPath = array();
public function getFullProductPath($product_id, $breadcrumbs_mode = false) {
$path_mode = 'mlseo_fpp_mode';
if ($breadcrumbs_mode) {
$path_mode = 'mlseo_fpp_bc_mode';
}
if (!$this->config->get($path_mode)) {
return array();
}
if ($this->config->get($path_mode) == '3') {
$man_id = $this->db->query("SELECT manufacturer_id FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'")->row;
if (!empty($man_id['manufacturer_id'])) {
return array('manufacturer_id' => $man_id['manufacturer_id']);
}
return array();
} else if ($this->config->get($path_mode) == '4') {
$category = $this->db->query("SELECT p2c.category_id FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category_path cp ON (p2c.category_id = cp.category_id) WHERE p2c.product_id = '" . (int)$product_id . "' ORDER BY cp.level DESC LIMIT 1")->row;
if (!empty($category['category_id'])) {
return array('path' => $category['category_id']);
}
return array();
} else if ($this->config->get($path_mode) == '5') {
$category = $this->db->query("SELECT cp.path_id FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category_path cp ON (p2c.category_id = cp.category_id) WHERE p2c.product_id = '" . (int)$product_id . "' AND cp.level = 0 LIMIT 1")->row;
if (!empty($category['path_id'])) {
return array('path' => $category['path_id']);
}
return array();
}
$path = array();
$categories = $this->db->query("SELECT c.category_id, c.parent_id, p.seo_canonical FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category c ON (p2c.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id) WHERE p2c.product_id = '" . (int)$product_id . "'")->rows;
$catsId = '';
foreach ($categories as $key => $category) {
if (!empty($category['seo_canonical']) && $category['seo_canonical'] != $category['category_id']) {
unset($categories[$key]);
continue;
}
$catsId .= ($catsId ? '|' : '') . $category['category_id'];
}
if (isset($this->cachedPath[($breadcrumbs_mode ? 'bc' : '') . $catsId])) {
// pathis cached, get it
$path = $this->cachedPath[($breadcrumbs_mode ? 'bc' : '') . $catsId];
} else {
$banned_cats = $this->config->get('mlseo_fpp_categories') ? $this->config->get('mlseo_fpp_categories') : array();
// path not in cache, generate it
foreach ($categories as $key => $category) {
// check if top category is in banned cats
if (in_array($category['category_id'], $banned_cats)) continue;
$path[$key] = '';
if (!$category) continue;
$path[$key] = $category['category_id'];
$i = 0;
while (!empty($category['parent_id']) && $category['parent_id'] !== $category['category_id']) {
if ($i++ > 12) break; // category is in loop
if (!in_array($category['parent_id'], $banned_cats)) {
$path[$key] = $category['parent_id'] . '_' . $path[$key];
}
$category = $this->db->query("SELECT category_id, parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $category['parent_id']. "'")->row;
}
$path[$key] = $path[$key];
/*
if (is_array($banned_cats) && is_array($categories) && count($banned_cats) && (count($categories) > 1)) {
if (in_array($path[$key], $banned_cats)) {
unset($path[$key]);
} else if (preg_match('#[_=](\d+)$#', $path[$key], $cat)) {
if (in_array($cat[1], $banned_cats)) {
unset($path[$key]);
}
}
}
*/
}
if (!count($path)) return array();
// wich one is the largest ?
$whichone = array_map('strlen', $path);
asort($whichone);
$whichone = array_keys($whichone);
if ($this->config->get($path_mode) == '2') {
$whichone = array_pop($whichone);
} else {
$whichone = array_shift($whichone);
}
$path = $path[$whichone];
if ($breadcrumbs_mode) {
$depth = (int) $this->config->get('mlseo_fpp_bc_depth');
} else {
$depth = (int) $this->config->get('mlseo_fpp_depth');
}
if ($depth) {
$path_parts = explode('_', $path);
while (count($path_parts) > (int) $this->config->get('mlseo_fpp_depth')) {
array_pop($path_parts);
}
$path = implode('_', $path_parts);
}
$this->cachedPath[($breadcrumbs_mode ? 'bc' : '') . $catsId] = $path;
}
return array('path' => $path);
}
public function getOneProductPath($product_id, $mode = 'path') {
$path = $text = '';
$category = $this->db->query("SELECT c.category_id, c.parent_id, p.seo_canonical FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category c ON (p2c.category_id = c.category_id) LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id) WHERE p2c.product_id = '" . (int)$product_id . "'")->row;
if (!empty($category['category_id'])) {
$path = $category['category_id'];
}
$i = 0;
while (!empty($category['parent_id']) && $category['parent_id'] !== $category['category_id']) {
if ($i++ > 12) break; // category is in loop
$path = $category['parent_id'] . '_' . $path;
$category = $this->db->query("SELECT category_id, parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $category['parent_id']. "'")->row;
}
return $path;
}
public function getFullCategoryPath($category_id) {
$path = '';
$category = $this->db->query("SELECT category_id, parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$category_id . "'")->row;
if (!$category) {
return '';
}
$path = $category['category_id'];
$i = 0;
while (!empty($category['parent_id']) && $category['parent_id'] !== $category['category_id']) {
if ($i++ > 12) break; // category is in loop
$path = $category['parent_id'] . '_' . $path;
$category = $this->db->query("SELECT category_id, parent_id FROM " . DB_PREFIX . "category WHERE category_id = '" . $category['parent_id']. "'")->row;
}
return $path;
}
public function getManufacturerKeyword() {
if ($this->config->get('mlseo_ml_mode')) {
$ml_mode = "AND (`language_id` = '" . (int)$this->config->get('config_language_id') . "' OR `language_id` = 0)";
} else {
$ml_mode = '';
}
if (version_compare(VERSION, '3', '>=')) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE `query` = 'route=product/manufacturer'". $ml_mode ." LIMIT 1")->row;
} else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` LIKE '%product/manufacturer'". $ml_mode ." LIMIT 1")->row;
}
if (!empty($query['keyword'])) {
return '/' . $query['keyword'];
}
return '';
}
}