Quick Search:

View

Revision:

Diff

Diff from 1862 to:

Annotations

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

Annotated File View

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