Quick Search:

View

Revision:

Diff

Diff from 443 to:

Annotations

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

Annotated File View

hpdl
1
1 <?php
2 /*
mattice
151
3   $Id: shipping.php 443 2006-02-19 23:01:01Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
410
8   Copyright (c) 2006 osCommerce
hpdl
1
9
10   Released under the GNU General Public License
11 */
12
hpdl
443
13   class osC_Shipping {
14     var $_modules = array(),
15         $_selected_module,
16         $_quotes = array(),
17         $_keys,
18         $_group = 'shipping';
hpdl
1
19
20 // class constructor
hpdl
443
21     function osC_Shipping($module = '') {
22       global $osC_Database, $osC_Language;
hpdl
383
23
hpdl
443
24       if (isset($_SESSION['osC_Shipping_data']) === false) {
25         $_SESSION['osC_Shipping_data'] = array('quotes' => array(),
26                                                'cartID' => null);
27       }
hpdl
1
28
hpdl
443
29       $this->_quotes =& $_SESSION['osC_Shipping_data']['quotes'];
30       $this->_cartID =& $_SESSION['osC_Shipping_data']['cartID'];
hpdl
1
31
hpdl
443
32       $Qmodules = $osC_Database->query('select code from :table_templates_boxes where modules_group = "shipping"');
33       $Qmodules->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
34       $Qmodules->setCache('modules-shipping');
35       $Qmodules->execute();
36
37       while ($Qmodules->next()) {
38         $this->_modules[] = $Qmodules->value('code');
39       }
40
41       $Qmodules->freeResult();
42
43       if (empty($this->_modules) === false) {
44         if ((empty($module) === false) && in_array(substr($module, 0, strpos($module, '_')), $this->_modules)) {
45           $this->_selected_module = $module;
46           $this->_modules = array(substr($module, 0, strpos($module, '_')));
hpdl
1
47         }
48
hpdl
410
49         $osC_Language->load('modules-shipping');
50
hpdl
443
51         foreach ($this->_modules as $module) {
52           $module_class = 'osC_Shipping_' . $module;
hpdl
1
53
hpdl
443
54           if (class_exists($module_class) === false) {
55             include('includes/modules/shipping/' . $module . '.' . substr(basename(__FILE__), (strrpos(basename(__FILE__), '.')+1)));
56           }
57
58           $GLOBALS[$module_class] = new $module_class();
59           $GLOBALS[$module_class]->initialize();
hpdl
1
60         }
hpdl
443
61
62         usort($this->_modules, array('osC_Shipping', '_usortModules'));
hpdl
1
63       }
hpdl
443
64
65       $this->_calculate();
hpdl
1
66     }
67
hpdl
443
68 // class methods
69     function getCode() {
70       return $this->_code;
71     }
hpdl
1
72
hpdl
443
73     function getTitle() {
74       return $this->_title;
75     }
hpdl
1
76
hpdl
443
77     function getDescription() {
78       return $this->_description;
79     }
hpdl
1
80
hpdl
443
81     function getStatus() {
82       return $this->_status;
83     }
84
85     function getSortOrder() {
86       return $this->_sort_order;
87     }
88
89     function hasQuotes() {
90       return !empty($this->_quotes);
91     }
92
93     function numberOfQuotes() {
94       $total_quotes = 0;
95
96       foreach ($this->_quotes as $quotes) {
97         $total_quotes += sizeof($quotes['methods']);
98       }
99
100       return $total_quotes;
101     }
102
103     function getQuotes() {
104       return $this->_quotes;
105     }
106
107     function getQuote($module = '') {
108       if (empty($module)) {
109         $module = $this->_selected_module;
110       }
111
112       list($module_id, $method_id) = explode('_', $module);
113
114       $rate = array();
115
116       foreach ($this->_quotes as $quote) {
117         if ($quote['id'] == $module_id) {
118           foreach ($quote['methods'] as $method) {
119             if ($method['id'] == $method_id) {
120               $rate = array('id' => $module,
121                             'title' => $quote['module'] . ((empty($method['title']) === false) ? ' (' . $method['title'] . ')' : ''),
122                             'cost' => $method['cost'],
123                             'tax_class_id' => $quote['tax_class_id']);
124
125               break 2;
126             }
127           }
hpdl
1
128         }
hpdl
443
129       }
hpdl
1
130
hpdl
443
131       return $rate;
132     }
133
134     function getCheapestQuote() {
135       $rate = array();
136
137       foreach ($this->_quotes as $quote) {
138         foreach ($quote['methods'] as $method) {
139           if (empty($rate) || ($method['cost'] < $rate['cost'])) {
140             $rate = array('id' => $quote['id'] . '_' . $method['id'],
141                           'title' => $quote['module'] . ((empty($method['title']) === false) ? ' (' . $method['title'] . ')' : ''),
142                           'cost' => $method['cost'],
143                           'tax_class_id' => $quote['tax_class_id'],
144                           'is_cheapest' => true);
145           }
hpdl
1
146         }
hpdl
443
147       }
hpdl
1
148
hpdl
443
149       return $rate;
150     }
hpdl
1
151
hpdl
443
152     function hasActive() {
153       static $has_active;
154
155       if (isset($has_active) === false) {
156         $has_active = false;
157
158         foreach ($this->_modules as $module) {
159           if ($GLOBALS['osC_Shipping_' . $module]->getStatus() === true) {
160             $has_active = true;
161             break;
hpdl
1
162           }
163         }
hpdl
443
164       }
hpdl
1
165
hpdl
443
166       return $has_active;
167     }
168
169     function hasKeys() {
170       static $has_keys;
171
172       if (isset($has_keys) === false) {
173         $has_keys = (sizeof($this->getKeys()) > 0) ? true : false;
174       }
175
176       return $has_keys;
177     }
178
179     function install() {
180       global $osC_Database, $osC_Language;
181
182       $Qinstall = $osC_Database->query('insert into :table_templates_boxes (title, code, author_name, author_www, modules_group) values (:title, :code, :author_name, :author_www, :modules_group)');
183       $Qinstall->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
184       $Qinstall->bindValue(':title', $this->_title);
185       $Qinstall->bindValue(':code', $this->_code);
186       $Qinstall->bindValue(':author_name', $this->_author_name);
187       $Qinstall->bindValue(':author_www', $this->_author_www);
188       $Qinstall->bindValue(':modules_group', $this->_group);
189       $Qinstall->execute();
190
191       foreach ($osC_Language->getAll() as $key => $value) {
192         if (file_exists(dirname(__FILE__) . '/../languages/' . $key . '/modules/' . $this->_group . '/' . $this->_code . '.xml')) {
193           foreach ($osC_Language->extractDefinitions($key . '/modules/' . $this->_group . '/' . $this->_code . '.xml') as $def) {
194             $Qcheck = $osC_Database->query('select id from :table_languages_definitions where definition_key = :definition_key and content_group = :content_group and languages_id = :languages_id limit 1');
195             $Qcheck->bindTable(':table_languages_definitions', TABLE_LANGUAGES_DEFINITIONS);
196             $Qcheck->bindValue(':definition_key', $def['key']);
197             $Qcheck->bindValue(':content_group', $def['group']);
198             $Qcheck->bindInt(':languages_id', $value['id']);
199             $Qcheck->execute();
200
201             if ($Qcheck->numberOfRows() === 1) {
202               $Qdef = $osC_Database->query('update :table_languages_definitions set definition_value = :definition_value where definition_key = :definition_key and content_group = :content_group and languages_id = :languages_id');
203             } else {
204               $Qdef = $osC_Database->query('insert into :table_languages_definitions (languages_id, content_group, definition_key, definition_value) values (:languages_id, :content_group, :definition_key, :definition_value)');
205             }
206             $Qdef->bindTable(':table_languages_definitions', TABLE_LANGUAGES_DEFINITIONS);
207             $Qdef->bindInt(':languages_id', $value['id']);
208             $Qdef->bindValue(':content_group', $def['group']);
209             $Qdef->bindValue(':definition_key', $def['key']);
210             $Qdef->bindValue(':definition_value', $def['value']);
211             $Qdef->execute();
212           }
hpdl
1
213         }
214       }
215
hpdl
443
216       osC_Cache::clear('languages');
hpdl
1
217     }
218
hpdl
443
219     function remove() {
220       global $osC_Database, $osC_Language;
hpdl
1
221
hpdl
443
222       $Qdel = $osC_Database->query('delete from :table_templates_boxes where code = :code and modules_group = :modules_group');
223       $Qdel->bindTable(':table_templates_boxes', TABLE_TEMPLATES_BOXES);
224       $Qdel->bindValue(':code', $this->_code);
225       $Qdel->bindValue(':modules_group', $this->_group);
226       $Qdel->execute();
227
228       if ($this->hasKeys()) {
229         $Qdel = $osC_Database->query('delete from :table_configuration where configuration_key in (":configuration_key")');
230         $Qdel->bindTable(':table_configuration', TABLE_CONFIGURATION);
231         $Qdel->bindRaw(':configuration_key', implode('", "', $this->getKeys()));
232         $Qdel->execute();
233       }
234
235       if (file_exists(dirname(__FILE__) . '/../languages/' . $osC_Language->getCode() . '/modules/' . $this->_group . '/' . $this->_code . '.xml')) {
236         foreach ($osC_Language->extractDefinitions($osC_Language->getCode() . '/modules/' . $this->_group . '/' . $this->_code . '.xml') as $def) {
237           $Qdel = $osC_Database->query('delete from :table_languages_definitions where definition_key = :definition_key and content_group = :content_group');
238           $Qdel->bindTable(':table_languages_definitions', TABLE_LANGUAGES_DEFINITIONS);
239           $Qdel->bindValue(':definition_key', $def['key']);
240           $Qdel->bindValue(':content_group', $def['group']);
241           $Qdel->execute();
242         }
243
244         osC_Cache::clear('languages');
245       }
246     }
247
248     function _calculate() {
249       global $osC_ShoppingCart;
250
251       if ($this->_cartID != $osC_ShoppingCart->getCartID()) {
252         $this->_cartID = $osC_ShoppingCart->getCartID();
253
254         $this->_quotes = array();
255
256         if (is_array($this->_modules)) {
257           $include_quotes = array();
258
259           if (defined('MODULE_SHIPPING_FREE_STATUS') && (MODULE_SHIPPING_FREE_STATUS == 'True') && ($GLOBALS['osC_Shipping_free']->getStatus() === true)) {
260             $include_quotes[] = 'osC_Shipping_free';
261           } else {
262             foreach ($this->_modules as $module) {
263               if ($GLOBALS['osC_Shipping_' . $module]->getStatus() === true) {
264                 $include_quotes[] = 'osC_Shipping_' . $module;
hpdl
1
265               }
266             }
267           }
268
hpdl
443
269           foreach ($include_quotes as $module) {
270             $quotes = $GLOBALS[$module]->quote();
271
272             if (is_array($quotes)) {
273               $this->_quotes[] = $quotes;
hpdl
1
274             }
275           }
276         }
hpdl
443
277       }
278     }
hpdl
1
279
hpdl
443
280     function _usortModules($a, $b) {
281       if ($GLOBALS['osC_Shipping_' . $a]->getSortOrder() == $GLOBALS['osC_Shipping_' . $b]->getSortOrder()) {
282         return strnatcasecmp($GLOBALS['osC_Shipping_' . $a]->getTitle(), $GLOBALS['osC_Shipping_' . $a]->getTitle());
hpdl
1
283       }
hpdl
443
284
285       return ($GLOBALS['osC_Shipping_' . $a]->getSortOrder() < $GLOBALS['osC_Shipping_' . $b]->getSortOrder()) ? -1 : 1;
hpdl
1
286     }
287   }
288 ?>