Quick Search:

View

Revision:

Diff

Diff from 1674 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() ) {
122                 $this->_data['variants'][$Qsubproducts->valueInt('products_id')]['values'][$Qvariants->valueInt('group_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'));
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 ) {
289             if ( !isset($values_array[$group_id]) ) {
290               $values_array[$group_id]['group_id'] = $group_id;
291               $values_array[$group_id]['title'] = $values['group_title'];
292               $values_array[$group_id]['module'] = $values['module'];
293             }
hpdl
210
294
hpdl
1674
295             $value_exists = false;
296
297             if ( isset($values_array[$group_id]['data']) ) {
298               foreach ( $values_array[$group_id]['data'] as $value ) {
299                 if ( $value['id'] == $values['value_id'] ) {
300                   $value_exists = true;
301
302                   break;
303                 }
304               }
305             }
306
307             if ( $value_exists === false ) {
308               $values_array[$group_id]['data'][] = array('id' => $values['value_id'],
309                                                          'text' => $values['value_title']);
310             }
311           }
hpdl
210
312         }
313
hpdl
1674
314         return $values_array;
315       }
hpdl
210
316
hpdl
1674
317       return $this->_data['variants'];
318     }
319
320     function variantExists($variant) {
321       return is_numeric($this->getProductVariantID($variant));
322     }
323
324     function getProductVariantID($variant) {
325       $_product_id = false;
326
327       $_size = sizeof($variant);
328
329       foreach ( $this->_data['variants'] as $product_id => $variants ) {
330         if ( sizeof($variants['values']) === $_size ) {
331           $_array = array();
332
333           foreach ( $variants['values'] as $group_id => $value ) {
334             $_array[$group_id] = $value['value_id'];
335           }
336
337           if ( sizeof(array_diff_assoc($_array, $variant)) === 0 ) {
338             $_product_id = $product_id;
339
340             break;
341           }
342         }
hpdl
210
343       }
344
hpdl
1674
345       return $_product_id;
346
347 /*HPDL; Useful for static version
348
349         $Qcheck = $osC_Database->query('select products_id from :table_products where parent_id = :parent_id limit 1');
350         $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
351         $Qcheck->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
352         $Qcheck->execute();
353
354         if ( $Qcheck->numberOfRows() < 1 ) {
355           return true;
356         } else {
357           $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');
358           $Qvariants->bindTable(':table_products', TABLE_PRODUCTS);
359           $Qvariants->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
360           $Qvariants->bindInt(':parent_id', $Qproduct->valueInt('products_id'));
361           $Qvariants->bindRaw(':variants_values_id', implode('", "', $variants));
362           $Qvariants->execute();
363
364           while ( $Qvariants->next() ) {
365             $Qvcheck = $osC_Database->query('select count(*) as total from :table_products_variants where products_id = :products_id');
366             $Qvcheck->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
367             $Qvcheck->bindInt(':products_id', $Qvariants->valueInt('products_id'));
368             $Qvcheck->execute();
369
370             if ( $Qvcheck->valueInt('total') === sizeof($variants) ) {
371               return true;
372             }
373           }
374         }
375 */
376
hpdl
210
377     }
378
379     function checkEntry($id) {
380       global $osC_Database;
381
hpdl
1674
382       $Qproduct = $osC_Database->query('select p.products_id from :table_products p');
383       $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
245
384
hpdl
1674
385       if ( is_numeric($id) ) {
386         $Qproduct->appendQuery('where p.products_id = :products_id');
387         $Qproduct->bindInt(':products_id', $id);
hpdl
245
388       } else {
hpdl
1674
389         $Qproduct->appendQuery(', :table_products_description pd where pd.products_keyword = :products_keyword and pd.products_id = p.products_id');
390         $Qproduct->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
391         $Qproduct->bindValue(':products_keyword', $id);
hpdl
245
392       }
393
hpdl
1674
394       $Qproduct->appendQuery('and p.products_status = 1 limit 1');
395       $Qproduct->execute();
hpdl
210
396
hpdl
1674
397       return ( $Qproduct->numberOfRows() === 1 );
hpdl
210
398     }
399
400     function incrementCounter() {
hpdl
377
401       global $osC_Database, $osC_Language;
hpdl
210
402
403       $Qupdate = $osC_Database->query('update :table_products_description set products_viewed = products_viewed+1 where products_id = :products_id and language_id = :language_id');
404       $Qupdate->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
734
405       $Qupdate->bindInt(':products_id', osc_get_product_id($this->_data['id']));
hpdl
377
406       $Qupdate->bindInt(':language_id', $osC_Language->getID());
hpdl
210
407       $Qupdate->execute();
408     }
hpdl
211
409
hpdl
612
410     function numberOfImages() {
411       return sizeof($this->_data['images']);
412     }
413
hpdl
211
414     function &getListingNew() {
hpdl
555
415       global $osC_Database, $osC_Language, $osC_Image;
hpdl
211
416
hpdl
599
417       $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
418       $Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
419       $Qproducts->bindTable(':table_manufacturers', TABLE_MANUFACTURERS);
hpdl
556
420       $Qproducts->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
hpdl
211
421       $Qproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
hpdl
583
422       $Qproducts->bindInt(':default_flag', 1);
hpdl
377
423       $Qproducts->bindInt(':language_id', $osC_Language->getID());
hpdl
1046
424       $Qproducts->setBatchLimit((isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1), MAX_DISPLAY_PRODUCTS_NEW);
hpdl
211
425       $Qproducts->execute();
426
427       return $Qproducts;
428     }
hpdl
210
429   }
430 ?>