Quick Search:

View

Revision:

Diff

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