Quick Search:

View

Revision:

Diff

Diff from 1680 to:

Annotations

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

Annotated File View

hpdl
210
1 <?php
2 /*
hpdl
1674
3   $Id: $
hpdl
210
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1674
8   Copyright (c) 2007 osCommerce
hpdl
210
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
210
13 */
14
15   class osC_Product {
16     var $_data = array();
17
hpdl
1674
18     function __construct($id) {
hpdl
555
19       global $osC_Database, $osC_Services, $osC_Language, $osC_Image;
hpdl
210
20
hpdl
1674
21       if ( !empty($id) ) {
22         if ( is_numeric($id) ) {
23           $Qproduct = $osC_Database->query('select products_id as id, parent_id, products_quantity as quantity, products_price as price, products_model as model, products_tax_class_id as tax_class_id, products_date_added as date_added, products_date_available as date_available, manufacturers_id, has_children from :table_products where products_id = :products_id and products_status = :products_status');
24           $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
25           $Qproduct->bindInt(':products_id', $id);
26           $Qproduct->bindInt(':products_status', 1);
27           $Qproduct->execute();
hpdl
245
28
hpdl
1674
29           if ( $Qproduct->numberOfRows() === 1 ) {
30             $this->_data = $Qproduct->toArray();
31             $this->_data['master_id'] = $Qproduct->valueInt('id');
32             $this->_data['has_children'] = $Qproduct->valueInt('has_children');
33
34             if ( $Qproduct->valueInt('parent_id') > 0 ) {
35               $Qmaster = $osC_Database->query('select products_id, has_children from :table_products where products_id = :products_id and products_status = :products_status');
36               $Qmaster->bindTable(':table_products', TABLE_PRODUCTS);
37               $Qmaster->bindInt(':products_id', $Qproduct->valueInt('parent_id'));
38               $Qmaster->bindInt(':products_status', 1);
39               $Qmaster->execute();
40
41               if ( $Qmaster->numberOfRows() === 1 ) {
42                 $this->_data['master_id'] = $Qmaster->valueInt('products_id');
43                 $this->_data['has_children'] = $Qmaster->valueInt('has_children');
44               } else { // master product is disabled so invalidate the product variant
45                 $this->_data = array();
46               }
47             }
48
49             if ( !empty($this->_data) ) {
50               $Qdesc = $osC_Database->query('select products_name as name, products_description as description, products_keyword as keyword, products_tags as tags, products_url as url from :table_products_description where products_id = :products_id and language_id = :language_id');
51               $Qdesc->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
52               $Qdesc->bindInt(':products_id', $this->_data['master_id']);
53               $Qdesc->bindInt(':language_id', $osC_Language->getID());
54               $Qdesc->execute();
55
56               $this->_data = array_merge($this->_data, $Qdesc->toArray());
57             }
58           }
hpdl
246
59         } else {
hpdl
1674
60           $Qproduct = $osC_Database->query('select p.products_id as id, p.parent_id, p.products_quantity as quantity, p.products_price as price, p.products_model as model, p.products_tax_class_id as tax_class_id, p.products_date_added as date_added, p.products_date_available as date_available, p.manufacturers_id, p.has_children, pd.products_name as name, pd.products_description as description, pd.products_keyword as keyword, pd.products_tags as tags, pd.products_url as url from :table_products p, :table_products_description pd where pd.products_keyword = :products_keyword and pd.language_id = :language_id and pd.products_id = p.products_id and p.products_status = :products_status');
61           $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
62           $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
246
63           $Qproduct->bindValue(':products_keyword', $id);
hpdl
1674
64           $Qproduct->bindInt(':language_id', $osC_Language->getID());
65           $Qproduct->bindInt(':products_status', 1);
66           $Qproduct->execute();
hpdl
245
67
hpdl
1674
68           if ($Qproduct->numberOfRows() === 1) {
69             $this->_data = $Qproduct->toArray();
hpdl
210
70
hpdl
1674
71             $this->_data['master_id'] = $Qproduct->valueInt('id');
72             $this->_data['has_children'] = $Qproduct->valueInt('has_children');
73           }
74         }
hpdl
210
75
hpdl
1674
76         if ( !empty($this->_data) ) {
hpdl
612
77           $this->_data['images'] = array();
78
79           $Qimages = $osC_Database->query('select id, image, default_flag from :table_products_images where products_id = :products_id order by sort_order');
80           $Qimages->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
hpdl
1674
81           $Qimages->bindInt(':products_id', $this->_data['master_id']);
hpdl
612
82           $Qimages->execute();
83
84           while ($Qimages->next()) {
85             $this->_data['images'][] = $Qimages->toArray();
86           }
87
hpdl
300
88           $Qcategory = $osC_Database->query('select categories_id from :table_products_to_categories where products_id = :products_id limit 1');
89           $Qcategory->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
hpdl
1674
90           $Qcategory->bindInt(':products_id', $this->_data['master_id']);
hpdl
300
91           $Qcategory->execute();
92
93           $this->_data['category_id'] = $Qcategory->valueInt('categories_id');
94
hpdl
1674
95           if ( $this->_data['has_children'] === 1 ) {
96             $this->_data['variants'] = array();
hpdl
210
97
hpdl
1674
98             $Qsubproducts = $osC_Database->query('select * from :table_products where parent_id = :parent_id and products_status = :products_status');
99             $Qsubproducts->bindTable(':table_products', TABLE_PRODUCTS);
100             $Qsubproducts->bindInt(':parent_id', $this->_data['master_id']);
101             $Qsubproducts->bindInt(':products_status', 1);
102             $Qsubproducts->execute();
hpdl
210
103
hpdl
1674
104             while ( $Qsubproducts->next() ) {
105               $this->_data['variants'][$Qsubproducts->valueInt('products_id')]['data'] = array('price' => $Qsubproducts->value('products_price'),
106                                                                                                'tax_class_id' => $Qsubproducts->valueInt('products_tax_class_id'),
107                                                                                                'model' => $Qsubproducts->value('products_model'),
108                                                                                                'quantity' => $Qsubproducts->value('products_quantity'),
109                                                                                                'weight' => $Qsubproducts->value('products_weight'),
110                                                                                                'weight_class_id' => $Qsubproducts->valueInt('products_weight_class'));
hpdl
210
111
hpdl
1680
112               $Qvariants = $osC_Database->query('select pv.default_combo, pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title, pvv.sort_order as value_sort_order from :table_products_variants pv, :table_products_variants_groups pvg, :table_products_variants_values pvv where pv.products_id = :products_id and pv.products_variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id order by pvg.sort_order, pvg.title');
hpdl
1674
113               $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
114               $Qvariants->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
115               $Qvariants->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
116               $Qvariants->bindInt(':products_id', $Qsubproducts->valueInt('products_id'));
117               $Qvariants->bindInt(':languages_id', $osC_Language->getID());
118               $Qvariants->bindInt(':languages_id', $osC_Language->getID());
119               $Qvariants->execute();
120
121               while ( $Qvariants->next() ) {
hpdl
1677
122                 $this->_data['variants'][$Qsubproducts->valueInt('products_id')]['values'][$Qvariants->valueInt('group_id')][$Qvariants->valueInt('value_id')] = array('value_id' => $Qvariants->valueInt('value_id'),
123                                                                                                                                                                        'group_title' => $Qvariants->value('group_title'),
124                                                                                                                                                                        'value_title' => $Qvariants->value('value_title'),
125                                                                                                                                                                        'sort_order' => $Qvariants->value('value_sort_order'),
hpdl
1680
126                                                                                                                                                                        'default' => (bool)$Qvariants->valueInt('default_combo'),
hpdl
1677
127                                                                                                                                                                        'module' => $Qvariants->value('module'));
hpdl
1674
128               }
hpdl
248
129             }
hpdl
246
130           }
hpdl
213
131
hpdl
1674
132           if ( $osC_Services->isStarted('reviews') ) {
hpdl
248
133             $Qavg = $osC_Database->query('select avg(reviews_rating) as rating from :table_reviews where products_id = :products_id and languages_id = :languages_id and reviews_status = 1');
134             $Qavg->bindTable(':table_reviews', TABLE_REVIEWS);
hpdl
1674
135             $Qavg->bindInt(':products_id', $this->_data['master_id']);
hpdl
377
136             $Qavg->bindInt(':languages_id', $osC_Language->getID());
hpdl
248
137             $Qavg->execute();
hpdl
213
138
hpdl
248
139             $this->_data['reviews_average_rating'] = round($Qavg->value('rating'));
140           }
hpdl
246
141         }
hpdl
213
142       }
hpdl
210
143     }
144
hpdl
246
145     function isValid() {
hpdl
1674
146       return !empty($this->_data);
hpdl
246
147     }
148
hpdl
1674
149     function getData($key = null) {
150       if ( isset($this->_data[$key]) ) {
hpdl
213
151         return $this->_data[$key];
152       }
153
hpdl
1674
154       return $this->_data;
hpdl
213
155     }
156
hpdl
210
157     function getID() {
158       return $this->_data['id'];
159     }
160
hpdl
1674
161     function getMasterID() {
162       return $this->_data['master_id'];
163     }
164
hpdl
210
165     function getTitle() {
166       return $this->_data['name'];
167     }
168
169     function getDescription() {
170       return $this->_data['description'];
171     }
172
173     function hasModel() {
174       return (isset($this->_data['model']) && !empty($this->_data['model']));
175     }
176
177     function getModel() {
178       return $this->_data['model'];
179     }
180
hpdl
248
181     function hasKeyword() {
182       return (isset($this->_data['keyword']) && !empty($this->_data['keyword']));
183     }
184
hpdl
246
185     function getKeyword() {
186       return $this->_data['keyword'];
187     }
188
hpdl
248
189     function hasTags() {
190       return (isset($this->_data['tags']) && !empty($this->_data['tags']));
191     }
192
193     function getTags() {
194       return $this->_data['tags'];
195     }
196
hpdl
210
197     function getPrice() {
198     }
199
200     function getPriceFormated($with_special = false) {
201       global $osC_Services, $osC_Specials, $osC_Currencies;
202
203       if (($with_special === true) && $osC_Services->isStarted('specials') && ($new_price = $osC_Specials->getPrice($this->_data['id']))) {
204         $price = '<s>' . $osC_Currencies->displayPrice($this->_data['price'], $this->_data['tax_class_id']) . '</s> <span class="productSpecialPrice">' . $osC_Currencies->displayPrice($new_price, $this->_data['tax_class_id']) . '</span>';
205       } else {
hpdl
1674
206         if ( $this->hasVariants() ) {
207           $price = 'from&nbsp;' . $osC_Currencies->displayPrice($this->getVariantMinPrice(), $this->_data['tax_class_id']);
208         } else {
209           $price = $osC_Currencies->displayPrice($this->_data['price'], $this->_data['tax_class_id']);
210         }
hpdl
210
211       }
212
213       return $price;
214     }
215
hpdl
1674
216     function getVariantMinPrice() {
217       $price = null;
218
219       foreach ( $this->_data['variants'] as $variant ) {
220         if ( ($price === null) || ($variant['data']['price'] < $price) ) {
221           $price = $variant['data']['price'];
222         }
223       }
224
225       return ( $price !== null ) ? $price : 0;
226     }
227
228     function getVariantMaxPrice() {
229       $price = 0;
230
231       foreach ( $this->_data['variants'] as $variant ) {
232         if ( $variant['data']['price'] > $price ) {
233           $price = $variant['data']['price'];
234         }
235       }
236
237       return $price;
238     }
239
hpdl
300
240     function getCategoryID() {
241       return $this->_data['category_id'];
242     }
243
hpdl
612
244     function getImages() {
245       return $this->_data['images'];
246     }
247
hpdl
210
248     function hasImage() {
hpdl
612
249       foreach ($this->_data['images'] as $image) {
250         if ($image['default_flag'] == '1') {
251           return true;
252         }
253       }
hpdl
210
254     }
255
256     function getImage() {
hpdl
612
257       foreach ($this->_data['images'] as $image) {
258         if ($image['default_flag'] == '1') {
259           return $image['image'];
260         }
261       }
hpdl
210
262     }
263
264     function hasURL() {
265       return (isset($this->_data['url']) && !empty($this->_data['url']));
266     }
267
268     function getURL() {
269       return $this->_data['url'];
270     }
271
272     function getDateAvailable() {
273       return $this->_data['date_available'];
274     }
275
276     function getDateAdded() {
277       return $this->_data['date_added'];
278     }
279
hpdl
1674
280     function hasVariants() {
281       return (isset($this->_data['variants']) && !empty($this->_data['variants']));
hpdl
210
282     }
283
hpdl
1674
284     function getVariants($filter_duplicates = true) {
285       if ( $filter_duplicates === true ) {
286         $values_array = array();
hpdl
210
287
hpdl
1674
288         foreach ( $this->_data['variants'] as $product_id => $variants ) {
289           foreach ( $variants['values'] as $group_id => $values ) {
hpdl
1677
290             foreach ( $values as $value_id => $value ) {
291               if ( !isset($values_array[$group_id]) ) {
292                 $values_array[$group_id]['group_id'] = $group_id;
293                 $values_array[$group_id]['title'] = $value['group_title'];
294                 $values_array[$group_id]['module'] = $value['module'];
295               }
hpdl
210
296
hpdl
1677
297               $value_exists = false;
hpdl
1674
298
hpdl
1677
299               if ( isset($values_array[$group_id]['data']) ) {
300                 foreach ( $values_array[$group_id]['data'] as $data ) {
301                   if ( $data['id'] == $value_id ) {
302                     $value_exists = true;
hpdl
1674
303
hpdl
1677
304                     break;
305                   }
hpdl
1674
306                 }
307               }
308
hpdl
1677
309               if ( $value_exists === false ) {
310                 $values_array[$group_id]['data'][] = array('id' => $value_id,
hpdl
1680
311                                                            'text' => $value['value_title'],
312                                                            'default' => $value['default']);
313               } elseif ( $value['default'] === true ) {
314                 foreach ( $values_array[$group_id]['data'] as &$existing_data ) {
315                   if ( $existing_data['id'] == $value_id ) {
316                     $existing_data['default'] = true;
317
318                     break;
319                   }
320                 }
hpdl
1677
321               }
hpdl
1674
322             }
323           }
hpdl
210
324         }
325
hpdl
1674
326         return $values_array;
327       }
hpdl
210
328
hpdl
1674
329       return $this->_data['variants'];
330     }
331
332     function variantExists($variant) {
333       return is_numeric($this->getProductVariantID($variant));
334     }
335
336     function getProductVariantID($variant) {
337       $_product_id = false;
338
339       $_size = sizeof($variant);
340
341       foreach ( $this->_data['variants'] as $product_id => $variants ) {
342         if ( sizeof($variants['values']) === $_size ) {
343           $_array = array();
344
345           foreach ( $variants['values'] as $group_id => $value ) {
hpdl
1677
346             $n = sizeof($value);
347
348             foreach ( $value as $value_id => $value_data ) {
349               if ( $n > 1 ) {
350                 $_array[$group_id][$value_id] = $variant[$group_id][$value_id];
351               } else {
352                 $_array[$group_id] = $value_id;
353               }
354             }
hpdl
1674
355           }
356
357           if ( sizeof(array_diff_assoc($_array, $variant)) === 0 ) {
358             $_product_id = $product_id;
359
360             break;
361           }
362         }
hpdl
210
363       }
364
hpdl
1674
365       return $_product_id;
366
367 /*HPDL; Useful for static version
368
369         $Qcheck = $osC_Database->query('select products_id from :table_products where parent_id = :parent_id limit 1');
370         $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
371         $Qcheck->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
372         $Qcheck->execute();
373
374         if ( $Qcheck->numberOfRows() < 1 ) {
375           return true;
376         } else {
hpdl
1680
377           $Qvariants = $osC_Database->query('select p.products_id from :table_products p, :table_products_variants pv where p.parent_id = :parent_id and p.products_id = pv.products_id and pv.products_variants_values_id in (":products_variants_values_id") group by pv.products_id');
hpdl
1674
378           $Qvariants->bindTable(':table_products', TABLE_PRODUCTS);
379           $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
380           $Qvariants->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
hpdl
1680
381           $Qvariants->bindRaw(':products_variants_values_id', implode('", "', $variants));
hpdl
1674
382           $Qvariants->execute();
383
384           while ( $Qvariants->next() ) {
385             $Qvcheck = $osC_Database->query('select count(*) as total from :table_products_variants where products_id = :products_id');
386             $Qvcheck->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
387             $Qvcheck->bindInt(':products_id', $Qvariants->valueInt('products_id'));
388             $Qvcheck->execute();
389
390             if ( $Qvcheck->valueInt('total') === sizeof($variants) ) {
391               return true;
392             }
393           }
394         }
395 */
396
hpdl
210
397     }
398
399     function checkEntry($id) {
400       global $osC_Database;
401
hpdl
1674
402       $Qproduct = $osC_Database->query('select p.products_id from :table_products p');
403       $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
245
404
hpdl
1674
405       if ( is_numeric($id) ) {
406         $Qproduct->appendQuery('where p.products_id = :products_id');
407         $Qproduct->bindInt(':products_id', $id);
hpdl
245
408       } else {
hpdl
1674
409         $Qproduct->appendQuery(', :table_products_description pd where pd.products_keyword = :products_keyword and pd.products_id = p.products_id');
410         $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
411         $Qproduct->bindValue(':products_keyword', $id);
hpdl
245
412       }
413
hpdl
1674
414       $Qproduct->appendQuery('and p.products_status = 1 limit 1');
415       $Qproduct->execute();
hpdl
210
416
hpdl
1674
417       return ( $Qproduct->numberOfRows() === 1 );
hpdl
210
418     }
419
420     function incrementCounter() {
hpdl
377
421       global $osC_Database, $osC_Language;
hpdl
210
422
423       $Qupdate = $osC_Database->query('update :table_products_description set products_viewed = products_viewed+1 where products_id = :products_id and language_id = :language_id');
424       $Qupdate->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
734
425       $Qupdate->bindInt(':products_id', osc_get_product_id($this->_data['id']));
hpdl
377
426       $Qupdate->bindInt(':language_id', $osC_Language->getID());
hpdl
210
427       $Qupdate->execute();
428     }
hpdl
211
429
hpdl
612
430     function numberOfImages() {
431       return sizeof($this->_data['images']);
432     }
433
hpdl
211
434     function &getListingNew() {
hpdl
555
435       global $osC_Database, $osC_Language, $osC_Image;
hpdl
211
436
hpdl
599
437       $Qproducts = $osC_Database->query('select p.products_id, p.products_price, p.products_tax_class_id, p.products_date_added, pd.products_name, pd.products_keyword, m.manufacturers_name, i.image from :table_products p left join :table_manufacturers m on (p.manufacturers_id = m.manufacturers_id) left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag), :table_products_description pd where p.products_status = 1 and p.products_id = pd.products_id and pd.language_id = :language_id order by p.products_date_added desc, pd.products_name');
hpdl
211
438       $Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
439       $Qproducts->bindTable(':table_manufacturers', TABLE_MANUFACTURERS);
hpdl
556
440       $Qproducts->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
hpdl
211
441       $Qproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
583
442       $Qproducts->bindInt(':default_flag', 1);
hpdl
377
443       $Qproducts->bindInt(':language_id', $osC_Language->getID());
hpdl
1046
444       $Qproducts->setBatchLimit((isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1), MAX_DISPLAY_PRODUCTS_NEW);
hpdl
211
445       $Qproducts->execute();
446
447       return $Qproducts;
448     }
hpdl
210
449   }
450 ?>