hpdl
|
219
|
1
|
<?php
|
|
2
|
/*
|
|
3
|
$Id: $
|
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
hpdl
|
608
|
8
|
Copyright (c) 2006 osCommerce
|
hpdl
|
219
|
9
|
|
|
10
|
Released under the GNU General Public License
|
|
11
|
*/
|
|
12
|
|
|
13
|
require('includes/classes/products.php');
|
|
14
|
|
|
15
|
class osC_Search extends osC_Products {
|
|
16
|
var $_period_min_year,
|
|
17
|
$_period_max_year,
|
|
18
|
$_date_from,
|
|
19
|
$_date_to,
|
|
20
|
$_price_from,
|
|
21
|
$_price_to,
|
hpdl
|
298
|
22
|
$_keywords,
|
|
23
|
$_number_of_results;
|
hpdl
|
219
|
24
|
|
|
25
|
/* Class constructor */
|
|
26
|
|
|
27
|
function osC_Search() {
|
|
28
|
global $osC_Database;
|
|
29
|
|
|
30
|
$Qproducts = $osC_Database->query('select min(year(products_date_added)) as min_year, max(year(products_date_added)) as max_year from :table_products limit 1');
|
|
31
|
$Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
|
|
32
|
$Qproducts->execute();
|
|
33
|
|
|
34
|
$this->_period_min_year = $Qproducts->valueInt('min_year');
|
|
35
|
$this->_period_max_year = $Qproducts->valueInt('max_year');
|
|
36
|
}
|
|
37
|
|
|
38
|
/* Public methods */
|
|
39
|
|
|
40
|
function getMinYear() {
|
|
41
|
return $this->_period_min_year;
|
|
42
|
}
|
|
43
|
|
|
44
|
function getMaxYear() {
|
|
45
|
return $this->_period_max_year;
|
|
46
|
}
|
|
47
|
|
|
48
|
function getDateFrom() {
|
|
49
|
return $this->_date_from;
|
|
50
|
}
|
|
51
|
|
|
52
|
function getDateTo() {
|
|
53
|
return $this->_date_to;
|
|
54
|
}
|
|
55
|
|
|
56
|
function getPriceFrom() {
|
|
57
|
return $this->_price_from;
|
|
58
|
}
|
|
59
|
|
|
60
|
function getPriceTo() {
|
|
61
|
return $this->_price_to;
|
|
62
|
}
|
|
63
|
|
|
64
|
function getKeywords() {
|
|
65
|
return $this->_keywords;
|
|
66
|
}
|
|
67
|
|
hpdl
|
298
|
68
|
function getNumberOfResults() {
|
|
69
|
return $this->_number_of_results;
|
|
70
|
}
|
|
71
|
|
hpdl
|
219
|
72
|
function hasDateSet($flag = null) {
|
|
73
|
if ($flag == 'from') {
|
|
74
|
return isset($this->_date_from);
|
|
75
|
} elseif ($flag == 'to') {
|
|
76
|
return isset($this->_date_to);
|
|
77
|
}
|
|
78
|
|
|
79
|
return isset($this->_date_from) && isset($this->_date_to);
|
|
80
|
}
|
|
81
|
|
|
82
|
function hasPriceSet($flag = null) {
|
|
83
|
if ($flag == 'from') {
|
|
84
|
return isset($this->_price_from);
|
|
85
|
} elseif ($flag == 'to') {
|
|
86
|
return isset($this->_price_to);
|
|
87
|
}
|
|
88
|
|
|
89
|
return isset($this->_price_from) && isset($this->_price_to);
|
|
90
|
}
|
|
91
|
|
|
92
|
function hasKeywords() {
|
|
93
|
return isset($this->_keywords) && !empty($this->_keywords);
|
|
94
|
}
|
|
95
|
|
|
96
|
function setDateFrom($timestamp) {
|
|
97
|
$this->_date_from = $timestamp;
|
|
98
|
}
|
|
99
|
|
|
100
|
function setDateTo($timestamp) {
|
|
101
|
$this->_date_to = $timestamp;
|
|
102
|
}
|
|
103
|
|
|
104
|
function setPriceFrom($price) {
|
|
105
|
$this->_price_from = $price;
|
|
106
|
}
|
|
107
|
|
|
108
|
function setPriceTo($price) {
|
|
109
|
$this->_price_to = $price;
|
|
110
|
}
|
|
111
|
|
|
112
|
function setKeywords($keywords) {
|
|
113
|
$terms = explode(' ', trim($keywords));
|
|
114
|
|
|
115
|
$terms_array = array();
|
|
116
|
|
|
117
|
$counter = 0;
|
|
118
|
|
|
119
|
foreach ($terms as $word) {
|
|
120
|
$counter++;
|
|
121
|
|
|
122
|
if ($counter > 5) {
|
|
123
|
break;
|
|
124
|
} elseif (!empty($word)) {
|
|
125
|
if (!in_array($word, $terms_array)) {
|
|
126
|
$terms_array[] = $word;
|
|
127
|
}
|
|
128
|
}
|
|
129
|
}
|
|
130
|
|
|
131
|
$this->_keywords = implode(' ', $terms_array);
|
|
132
|
}
|
|
133
|
|
|
134
|
function &execute() {
|
hpdl
|
757
|
135
|
global $osC_Database, $osC_Customer, $osC_Currencies, $osC_Language, $osC_Image, $osC_CategoryTree;
|
hpdl
|
219
|
136
|
|
hpdl
|
608
|
137
|
$Qlisting = $osC_Database->query('select SQL_CALC_FOUND_ROWS distinct p.*, pd.*, m.*, i.image, if(s.status, s.specials_new_products_price, null) as specials_new_products_price, if(s.status, s.specials_new_products_price, p.products_price) as final_price');
|
hpdl
|
219
|
138
|
|
hpdl
|
554
|
139
|
if (($this->hasPriceSet('from') || $this->hasPriceSet('to')) && (DISPLAY_PRICE_WITH_TAX == '1')) {
|
hpdl
|
219
|
140
|
$Qlisting->appendQuery(', sum(tr.tax_rate) as tax_rate');
|
|
141
|
}
|
|
142
|
|
hpdl
|
608
|
143
|
$Qlisting->appendQuery('from :table_products p left join :table_manufacturers m using(manufacturers_id) left join :table_specials s on (p.products_id = s.products_id) left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag)');
|
hpdl
|
219
|
144
|
$Qlisting->bindTable(':table_products', TABLE_PRODUCTS);
|
|
145
|
$Qlisting->bindTable(':table_manufacturers', TABLE_MANUFACTURERS);
|
|
146
|
$Qlisting->bindTable(':table_specials', TABLE_SPECIALS);
|
hpdl
|
608
|
147
|
$Qlisting->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
|
|
148
|
$Qlisting->bindInt(':default_flag', 1);
|
hpdl
|
219
|
149
|
|
hpdl
|
554
|
150
|
if (($this->hasPriceSet('from') || $this->hasPriceSet('to')) && (DISPLAY_PRICE_WITH_TAX == '1')) {
|
hpdl
|
219
|
151
|
if ($osC_Customer->isLoggedOn()) {
|
|
152
|
$customer_country_id = $osC_Customer->getCountryID();
|
|
153
|
$customer_zone_id = $osC_Customer->getZoneID();
|
|
154
|
} else {
|
|
155
|
$customer_country_id = STORE_COUNTRY;
|
|
156
|
$customer_zone_id = STORE_ZONE;
|
|
157
|
}
|
|
158
|
|
|
159
|
$Qlisting->appendQuery('left join :table_tax_rates tr on p.products_tax_class_id = tr.tax_class_id left join :table_zones_to_geo_zones gz on tr.tax_zone_id = gz.geo_zone_id and (gz.zone_country_id is null or gz.zone_country_id = 0 or gz.zone_country_id = :zone_country_id) and (gz.zone_id is null or gz.zone_id = 0 or gz.zone_id = :zone_id)');
|
|
160
|
$Qlisting->bindTable(':table_tax_rates', TABLE_TAX_RATES);
|
|
161
|
$Qlisting->bindTable(':table_zones_to_geo_zones', TABLE_ZONES_TO_GEO_ZONES);
|
|
162
|
$Qlisting->bindInt(':zone_country_id', $customer_country_id);
|
|
163
|
$Qlisting->bindInt(':zone_id', $customer_zone_id);
|
|
164
|
}
|
|
165
|
|
hpdl
|
232
|
166
|
$Qlisting->appendQuery(', :table_products_description pd, :table_categories c, :table_products_to_categories p2c');
|
|
167
|
$Qlisting->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
|
|
168
|
$Qlisting->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
169
|
$Qlisting->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
|
|
170
|
|
hpdl
|
219
|
171
|
$Qlisting->appendQuery('where p.products_status = 1 and p.products_id = pd.products_id and pd.language_id = :language_id and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id');
|
hpdl
|
383
|
172
|
$Qlisting->bindInt(':language_id', $osC_Language->getID());
|
hpdl
|
219
|
173
|
|
|
174
|
if ($this->hasCategory()) {
|
|
175
|
if ($this->isRecursive()) {
|
hpdl
|
757
|
176
|
$subcategories_array = array($this->_category);
|
hpdl
|
219
|
177
|
|
hpdl
|
757
|
178
|
$Qlisting->appendQuery('and p2c.products_id = p.products_id and p2c.products_id = pd.products_id and p2c.categories_id in (:categories_id)');
|
|
179
|
$Qlisting->bindRaw(':categories_id', implode(',', $osC_CategoryTree->getChildren($this->_category, $subcategories_array)));
|
hpdl
|
219
|
180
|
} else {
|
|
181
|
$Qlisting->appendQuery('and p2c.products_id = p.products_id and p2c.products_id = pd.products_id and pd.language_id = :language_id and p2c.categories_id = :categories_id');
|
hpdl
|
383
|
182
|
$Qlisting->bindInt(':language_id', $osC_Language->getID());
|
hpdl
|
219
|
183
|
$Qlisting->bindInt(':categories_id', $this->_category);
|
|
184
|
}
|
|
185
|
}
|
|
186
|
|
|
187
|
if ($this->hasManufacturer()) {
|
|
188
|
$Qlisting->appendQuery('and m.manufacturers_id = :manufacturers_id');
|
|
189
|
$Qlisting->bindInt(':manufacturers_id', $this->_manufacturer);
|
|
190
|
}
|
|
191
|
|
|
192
|
if ($this->hasKeywords()) {
|
hpdl
|
220
|
193
|
$Qlisting->prepareSearch($this->_keywords, array('pd.products_name', 'pd.products_description'), true);
|
hpdl
|
219
|
194
|
}
|
|
195
|
|
|
196
|
if ($this->hasDateSet('from')) {
|
|
197
|
$Qlisting->appendQuery('and p.products_date_added >= :products_date_added');
|
hpdl
|
883
|
198
|
$Qlisting->bindValue(':products_date_added', date('Y-m-d H:i:s', $this->_date_from));
|
hpdl
|
219
|
199
|
}
|
|
200
|
|
|
201
|
if ($this->hasDateSet('to')) {
|
|
202
|
$Qlisting->appendQuery('and p.products_date_added <= :products_date_added');
|
hpdl
|
883
|
203
|
$Qlisting->bindValue(':products_date_added', date('Y-m-d H:i:s', $this->_date_to));
|
hpdl
|
219
|
204
|
}
|
|
205
|
|
|
206
|
if ($this->hasPriceSet('from')) {
|
|
207
|
if ($osC_Currencies->exists($_SESSION['currency'])) {
|
|
208
|
$this->_price_from = $this->_price_from / $osC_Currencies->value($_SESSION['currency']);
|
|
209
|
}
|
|
210
|
}
|
|
211
|
|
|
212
|
if ($this->hasPriceSet('to')) {
|
|
213
|
if ($osC_Currencies->exists($_SESSION['currency'])) {
|
|
214
|
$this->_price_to = $this->_price_to / $osC_Currencies->value($_SESSION['currency']);
|
|
215
|
}
|
|
216
|
}
|
|
217
|
|
hpdl
|
554
|
218
|
if (DISPLAY_PRICE_WITH_TAX == '1') {
|
hpdl
|
219
|
219
|
if ($this->_price_from > 0) {
|
|
220
|
$Qlisting->appendQuery('and (if(s.status, s.specials_new_products_price, p.products_price) * if(gz.geo_zone_id is null, 1, 1 + (tr.tax_rate / 100) ) >= :price_from)');
|
|
221
|
$Qlisting->bindFloat(':price_from', $this->_price_from);
|
|
222
|
}
|
|
223
|
|
|
224
|
if ($this->_price_to > 0) {
|
|
225
|
$Qlisting->appendQuery('and (if(s.status, s.specials_new_products_price, p.products_price) * if(gz.geo_zone_id is null, 1, 1 + (tr.tax_rate / 100) ) <= :price_to)');
|
|
226
|
$Qlisting->bindFloat(':price_to', $this->_price_to);
|
|
227
|
}
|
|
228
|
} else {
|
|
229
|
if ($this->_price_from > 0) {
|
|
230
|
$Qlisting->appendQuery('and (if(s.status, s.specials_new_products_price, p.products_price) >= :price_from)');
|
|
231
|
$Qlisting->bindFloat(':price_from', $this->_price_from);
|
|
232
|
}
|
|
233
|
|
|
234
|
if ($this->_price_to > 0) {
|
|
235
|
$Qlisting->appendQuery('and (if(s.status, s.specials_new_products_price, p.products_price) <= :price_to)');
|
|
236
|
$Qlisting->bindFloat(':price_to', $this->_price_to);
|
|
237
|
}
|
|
238
|
}
|
|
239
|
|
hpdl
|
554
|
240
|
if (($this->hasPriceSet('from') || $this->hasPriceSet('to')) && (DISPLAY_PRICE_WITH_TAX == '1')) {
|
hpdl
|
219
|
241
|
$Qlisting->appendQuery('group by p.products_id, tr.tax_priority');
|
|
242
|
}
|
|
243
|
|
|
244
|
$Qlisting->appendQuery('order by');
|
|
245
|
|
|
246
|
if (isset($this->_sort_by)) {
|
|
247
|
$Qlisting->appendQuery(':order_by :order_by_direction, pd.products_name');
|
|
248
|
$Qlisting->bindRaw(':order_by', $this->_sort_by);
|
|
249
|
$Qlisting->bindRaw(':order_by_direction', (($this->_sort_by_direction == '-') ? 'desc' : ''));
|
|
250
|
} else {
|
|
251
|
$Qlisting->appendQuery('pd.products_name :order_by_direction');
|
|
252
|
$Qlisting->bindRaw(':order_by_direction', (($this->_sort_by_direction == '-') ? 'desc' : ''));
|
|
253
|
}
|
|
254
|
|
|
255
|
$Qlisting->setBatchLimit((isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1), MAX_DISPLAY_SEARCH_RESULTS);
|
|
256
|
$Qlisting->execute();
|
|
257
|
|
hpdl
|
298
|
258
|
$this->_number_of_results = $Qlisting->getBatchSize();
|
|
259
|
|
hpdl
|
219
|
260
|
return $Qlisting;
|
|
261
|
}
|
|
262
|
}
|
|
263
|
?>
|