Quick Search:

View

Revision:

Diff

Diff from 1677 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
1674
112               $Qvariants = $osC_Database->query('select 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.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');
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'),
126                                                                                                                                                                        'module' => $Qvariants->value('module'));
hpdl
1674
127               }
hpdl
248
128             }
hpdl
246
129           }
hpdl
213
130
hpdl
1674
131           if ( $osC_Services->isStarted('reviews') ) {
hpdl
248
132             $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');
133             $Qavg->bindTable(':table_reviews', TABLE_REVIEWS);
hpdl
1674
134             $Qavg->bindInt(':products_id', $this->_data['master_id']);
hpdl
377
135             $Qavg->bindInt(':languages_id', $osC_Language->getID());
hpdl
248
136             $Qavg->execute();
hpdl
213
137
hpdl
248
138             $this->_data['reviews_average_rating'] = round($Qavg->value('rating'));
139           }
hpdl
246
140         }
hpdl
213
141       }
hpdl
210
142     }
143
hpdl
246
144     function isValid() {
hpdl
1674
145       return !empty($this->_data);
hpdl
246
146     }
147
hpdl
1674
148     function getData($key = null) {
149       if ( isset($this->_data[$key]) ) {
hpdl
213
150         return $this->_data[$key];
151       }
152
hpdl
1674
153       return $this->_data;
hpdl
213
154     }
155
hpdl
210
156     function getID() {
157       return $this->_data['id'];
158     }
159
hpdl
1674
160     function getMasterID() {
161       return $this->_data['master_id'];
162     }
163
hpdl
210
164     function getTitle() {
165       return $this->_data['name'];
166     }
167
168     function getDescription() {
169       return $this->_data['description'];
170     }
171
172     function hasModel() {
173       return (isset($this->_data['model']) && !empty($this->_data['model']));
174     }
175
176     function getModel() {
177       return $this->_data['model'];
178     }
179
hpdl
248
180     function hasKeyword() {
181       return (isset($this->_data['keyword']) && !empty($this->_data['keyword']));
182     }
183
hpdl
246
184     function getKeyword() {
185       return $this->_data['keyword'];
186     }
187
hpdl
248
188     function hasTags() {
189       return (isset($this->_data['tags']) && !empty($this->_data['tags']));
190     }
191
192     function getTags() {
193       return $this->_data['tags'];
194     }
195
hpdl
210
196     function getPrice() {
197     }
198
199     function getPriceFormated($with_special = false) {
200       global $osC_Services, $osC_Specials, $osC_Currencies;
201
202       if (($with_special === true) && $osC_Services->isStarted('specials') && ($new_price = $osC_Specials->getPrice($this->_data['id']))) {
203         $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>';
204       } else {
hpdl
1674
205         if ( $this->hasVariants() ) {
206           $price = 'from&nbsp;' . $osC_Currencies->displayPrice($this->getVariantMinPrice(), $this->_data['tax_class_id']);
207         } else {
208           $price = $osC_Currencies->displayPrice($this->_data['price'], $this->_data['tax_class_id']);
209         }
hpdl
210
210       }
211
212       return $price;
213     }
214
hpdl
1674
215     function getVariantMinPrice() {
216       $price = null;
217
218       foreach ( $this->_data['variants'] as $variant ) {
219         if ( ($price === null) || ($variant['data']['price'] < $price) ) {
220           $price = $variant['data']['price'];
221         }
222       }
223
224       return ( $price !== null ) ? $price : 0;
225     }
226
227     function getVariantMaxPrice() {
228       $price = 0;
229
230       foreach ( $this->_data['variants'] as $variant ) {
231         if ( $variant['data']['price'] > $price ) {
232           $price = $variant['data']['price'];
233         }
234       }
235
236       return $price;
237     }
238
hpdl
300
239     function getCategoryID() {
240       return $this->_data['category_id'];
241     }
242
hpdl
612
243     function getImages() {
244       return $this->_data['images'];
245     }
246
hpdl
210
247     function hasImage() {
hpdl
612
248       foreach ($this->_data['images'] as $image) {
249         if ($image['default_flag'] == '1') {
250           return true;
251         }
252       }
hpdl
210
253     }
254
255     function getImage() {
hpdl
612
256       foreach ($this->_data['images'] as $image) {
257         if ($image['default_flag'] == '1') {
258           return $image['image'];
259         }
260       }
hpdl
210
261     }
262
263     function hasURL() {
264       return (isset($this->_data['url']) && !empty($this->_data['url']));
265     }
266
267     function getURL() {
268       return $this->_data['url'];
269     }
270
271     function getDateAvailable() {
272       return $this->_data['date_available'];
273     }
274
275     function getDateAdded() {
276       return $this->_data['date_added'];
277     }
278
hpdl
1674
279     function hasVariants() {
280       return (isset($this->_data['variants']) && !empty($this->_data['variants']));
hpdl
210
281     }
282
hpdl
1674
283     function getVariants($filter_duplicates = true) {
284       if ( $filter_duplicates === true ) {
285         $values_array = array();
hpdl
210
286
hpdl
1674
287         foreach ( $this->_data['variants'] as $product_id => $variants ) {
288           foreach ( $variants['values'] as $group_id => $values ) {
hpdl
1677
289             foreach ( $values as $value_id => $value ) {
290               if ( !isset($values_array[$group_id]) ) {
291                 $values_array[$group_id]['group_id'] = $group_id;
292                 $values_array[$group_id]['title'] = $value['group_title'];
293                 $values_array[$group_id]['module'] = $value['module'];
294               }
hpdl
210
295
hpdl
1677
296               $value_exists = false;
hpdl
1674
297
hpdl
1677
298               if ( isset($values_array[$group_id]['data']) ) {
299                 foreach ( $values_array[$group_id]['data'] as $data ) {
300                   if ( $data['id'] == $value_id ) {
301                     $value_exists = true;
hpdl
1674
302
hpdl
1677
303                     break;
304                   }
hpdl
1674
305                 }
306               }
307
hpdl
1677
308               if ( $value_exists === false ) {
309                 $values_array[$group_id]['data'][] = array('id' => $value_id,
310                                                            'text' => $value['value_title']);
311               }
hpdl
1674
312             }
313           }
hpdl
210
314         }
315
hpdl
1674
316         return $values_array;
317       }
hpdl
210
318
hpdl
1674
319       return $this->_data['variants'];
320     }
321
322     function variantExists($variant) {
323       return is_numeric($this->getProductVariantID($variant));
324     }
325
326     function getProductVariantID($variant) {
327       $_product_id = false;
328
329       $_size = sizeof($variant);
330
331       foreach ( $this->_data['variants'] as $product_id => $variants ) {
332         if ( sizeof($variants['values']) === $_size ) {
333           $_array = array();
334
335           foreach ( $variants['values'] as $group_id => $value ) {
hpdl
1677
336             $n = sizeof($value);
337
338             foreach ( $value as $value_id => $value_data ) {
339               if ( $n > 1 ) {
340                 $_array[$group_id][$value_id] = $variant[$group_id][$value_id];
341               } else {
342                 $_array[$group_id] = $value_id;
343               }
344             }
hpdl
1674
345           }
346
347           if ( sizeof(array_diff_assoc($_array, $variant)) === 0 ) {
348             $_product_id = $product_id;
349
350             break;
351           }
352         }
hpdl
210
353       }
354
hpdl
1674
355       return $_product_id;
356
357 /*HPDL; Useful for static version
358
359         $Qcheck = $osC_Database->query('select products_id from :table_products where parent_id = :parent_id limit 1');
360         $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
361         $Qcheck->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
362         $Qcheck->execute();
363
364         if ( $Qcheck->numberOfRows() < 1 ) {
365           return true;
366         } else {
367           $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.variants_values_id in (":variants_values_id") group by pv.products_id');
368           $Qvariants->bindTable(':table_products', TABLE_PRODUCTS);
369           $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
370           $Qvariants->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
371           $Qvariants->bindRaw(':variants_values_id', implode('", "', $variants));
372           $Qvariants->execute();
373
374           while ( $Qvariants->next() ) {
375             $Qvcheck = $osC_Database->query('select count(*) as total from :table_products_variants where products_id = :products_id');
376             $Qvcheck->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
377             $Qvcheck->bindInt(':products_id', $Qvariants->valueInt('products_id'));
378             $Qvcheck->execute();
379
380             if ( $Qvcheck->valueInt('total') === sizeof($variants) ) {
381               return true;
382             }
383           }
384         }
385 */
386
hpdl
210
387     }
388
389     function checkEntry($id) {
390       global $osC_Database;
391
hpdl
1674
392       $Qproduct = $osC_Database->query('select p.products_id from :table_products p');
393       $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
245
394
hpdl
1674
395       if ( is_numeric($id) ) {
396         $Qproduct->appendQuery('where p.products_id = :products_id');
397         $Qproduct->bindInt(':products_id', $id);
hpdl
245
398       } else {
hpdl
1674
399         $Qproduct->appendQuery(', :table_products_description pd where pd.products_keyword = :products_keyword and pd.products_id = p.products_id');
400         $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
401         $Qproduct->bindValue(':products_keyword', $id);
hpdl
245
402       }
403
hpdl
1674
404       $Qproduct->appendQuery('and p.products_status = 1 limit 1');
405       $Qproduct->execute();
hpdl
210
406
hpdl
1674
407       return ( $Qproduct->numberOfRows() === 1 );
hpdl
210
408     }
409
410     function incrementCounter() {
hpdl
377
411       global $osC_Database, $osC_Language;
hpdl
210
412
413       $Qupdate = $osC_Database->query('update :table_products_description set products_viewed = products_viewed+1 where products_id = :products_id and language_id = :language_id');
414       $Qupdate->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
734
415       $Qupdate->bindInt(':products_id', osc_get_product_id($this->_data['id']));
hpdl
377
416       $Qupdate->bindInt(':language_id', $osC_Language->getID());
hpdl
210
417       $Qupdate->execute();
418     }
hpdl
211
419
hpdl
612
420     function numberOfImages() {
421       return sizeof($this->_data['images']);
422     }
423
hpdl
211
424     function &getListingNew() {
hpdl
555
425       global $osC_Database, $osC_Language, $osC_Image;
hpdl
211
426
hpdl
599
427       $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
428       $Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
429       $Qproducts->bindTable(':table_manufacturers', TABLE_MANUFACTURERS);
hpdl
556
430       $Qproducts->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
hpdl
211
431       $Qproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
583
432       $Qproducts->bindInt(':default_flag', 1);
hpdl
377
433       $Qproducts->bindInt(':language_id', $osC_Language->getID());
hpdl
1046
434       $Qproducts->setBatchLimit((isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1), MAX_DISPLAY_PRODUCTS_NEW);
hpdl
211
435       $Qproducts->execute();
436
437       return $Qproducts;
438     }
hpdl
210
439   }
440 ?>