Quick Search:

View

Revision:

Diff

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