Quick Search:

View

Revision:

Diff

Diff from 232 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/search.php

Annotated File View

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