Quick Search:

View

Revision:

Diff

Diff from 219 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/trunk/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
138       $Qlisting->appendQuery('from :table_products p left join :table_manufacturers m using(manufacturers_id), :table_products_description pd left join :table_specials s on p.products_id = s.products_id, :table_categories c, :table_products_to_categories p2c');
139       $Qlisting->bindTable(':table_products', TABLE_PRODUCTS);
140       $Qlisting->bindTable(':table_manufacturers', TABLE_MANUFACTURERS);
141       $Qlisting->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
142       $Qlisting->bindTable(':table_specials', TABLE_SPECIALS);
143       $Qlisting->bindTable(':table_categories', TABLE_CATEGORIES);
144       $Qlisting->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
145
146       if (($this->hasPriceSet('from') || $this->hasPriceSet('to')) && (DISPLAY_PRICE_WITH_TAX == 'true')) {
147         if ($osC_Customer->isLoggedOn()) {
148           $customer_country_id = $osC_Customer->getCountryID();
149           $customer_zone_id = $osC_Customer->getZoneID();
150         } else {
151           $customer_country_id = STORE_COUNTRY;
152           $customer_zone_id = STORE_ZONE;
153         }
154
155         $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)');
156         $Qlisting->bindTable(':table_tax_rates', TABLE_TAX_RATES);
157         $Qlisting->bindTable(':table_zones_to_geo_zones', TABLE_ZONES_TO_GEO_ZONES);
158         $Qlisting->bindInt(':zone_country_id', $customer_country_id);
159         $Qlisting->bindInt(':zone_id', $customer_zone_id);
160       }
161
162       $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');
163       $Qlisting->bindInt(':language_id', $_SESSION['languages_id']);
164
165       if ($this->hasCategory()) {
166         if ($this->isRecursive()) {
167           $subcategories_array = array();
168           tep_get_subcategories($subcategories_array, $this->_category);
169
170           $Qlisting->appendQuery('and p2c.products_id = p.products_id and p2c.products_id = pd.products_id and (p2c.categories_id = :categories_id');
171           $Qlisting->bindInt(':categories_id', $this->_category);
172
173           foreach ($subcategories_array as $sc) {
174             $Qlisting->appendQuery('or p2c.categories_id = :categories_id');
175             $Qlisting->bindInt(':categories_id', $sc);
176           }
177
178           $Qlisting->appendQuery(')');
179         } else {
180           $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');
181           $Qlisting->bindInt(':language_id', $_SESSION['languages_id']);
182           $Qlisting->bindInt(':categories_id', $this->_category);
183         }
184       }
185
186       if ($this->hasManufacturer()) {
187         $Qlisting->appendQuery('and m.manufacturers_id = :manufacturers_id');
188         $Qlisting->bindInt(':manufacturers_id', $this->_manufacturer);
189       }
190
191       if ($this->hasKeywords()) {
192         foreach (explode(' ', $this->_keywords) as $keyword) {
193           $Qlisting->appendQuery('and (p.products_model like :keyword or pd.products_name like :keyword or pd.products_description like :keyword)');
194           $Qlisting->bindValue(':keyword', '%' . $keyword . '%');
195           $Qlisting->bindValue(':keyword', '%' . $keyword . '%');
196           $Qlisting->bindValue(':keyword', '%' . $keyword . '%');
197         }
198       }
199
200       if ($this->hasDateSet('from')) {
201         $Qlisting->appendQuery('and p.products_date_added >= :products_date_added');
202         $Qlisting->bindValue(':products_date_added', date('Ymd', $this->_date_from));
203       }
204
205       if ($this->hasDateSet('to')) {
206         $Qlisting->appendQuery('and p.products_date_added <= :products_date_added');
207         $Qlisting->bindValue(':products_date_added', date('Ymd', $this->_date_to));
208       }
209
210       if ($this->hasPriceSet('from')) {
211         if ($osC_Currencies->exists($_SESSION['currency'])) {
212           $this->_price_from = $this->_price_from / $osC_Currencies->value($_SESSION['currency']);
213         }
214       }
215
216       if ($this->hasPriceSet('to')) {
217         if ($osC_Currencies->exists($_SESSION['currency'])) {
218           $this->_price_to = $this->_price_to / $osC_Currencies->value($_SESSION['currency']);
219         }
220       }
221
222       if (DISPLAY_PRICE_WITH_TAX == 'true') {
223         if ($this->_price_from > 0) {
224           $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)');
225           $Qlisting->bindFloat(':price_from', $this->_price_from);
226         }
227
228         if ($this->_price_to > 0) {
229           $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)');
230           $Qlisting->bindFloat(':price_to', $this->_price_to);
231         }
232       } else {
233         if ($this->_price_from > 0) {
234           $Qlisting->appendQuery('and (if(s.status, s.specials_new_products_price, p.products_price) >= :price_from)');
235           $Qlisting->bindFloat(':price_from', $this->_price_from);
236         }
237
238         if ($this->_price_to > 0) {
239           $Qlisting->appendQuery('and (if(s.status, s.specials_new_products_price, p.products_price) <= :price_to)');
240           $Qlisting->bindFloat(':price_to', $this->_price_to);
241         }
242       }
243
244       if (($this->hasPriceSet('from') || $this->hasPriceSet('to')) && (DISPLAY_PRICE_WITH_TAX == 'true')) {
245         $Qlisting->appendQuery('group by p.products_id, tr.tax_priority');
246       }
247
248       $Qlisting->appendQuery('order by');
249
250       if (isset($this->_sort_by)) {
251         $Qlisting->appendQuery(':order_by :order_by_direction, pd.products_name');
252         $Qlisting->bindRaw(':order_by', $this->_sort_by);
253         $Qlisting->bindRaw(':order_by_direction', (($this->_sort_by_direction == '-') ? 'desc' : ''));
254       } else {
255         $Qlisting->appendQuery('pd.products_name :order_by_direction');
256         $Qlisting->bindRaw(':order_by_direction', (($this->_sort_by_direction == '-') ? 'desc' : ''));
257       }
258
259       $Qlisting->setBatchLimit((isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1), MAX_DISPLAY_SEARCH_RESULTS);
260       $Qlisting->execute();
261
262       return $Qlisting;
263     }
264   }
265 ?>