Quick Search:

View

Revision:

Diff

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