  |
1 | 1 | | <?php |
| |
2 | 2 | | /* |
  |
3 | | - | $Id: shipping.php 410 2006-01-26 09:17:09Z hpdl $ |
| |
| 3 | + | $Id: shipping.php 443 2006-02-19 23:01:01Z hpdl $ |
|
4 | 4 | | |
| |
5 | 5 | | osCommerce, Open Source E-Commerce Solutions |
| |
6 | 6 | | http://www.oscommerce.com |
| |
|
|
 |
… |
|
10 | 10 | | Released under the GNU General Public License |
| |
11 | 11 | | */ |
| |
12 | 12 | | |
  |
13 | | - | class shipping { |
| |
14 | | - | var $modules; |
| |
| 13 | + | class osC_Shipping { |
| |
| 14 | + | var $_modules = array(), |
| |
| 15 | + | $_selected_module, |
| |
| 16 | + | $_quotes = array(), |
| |
| 17 | + | $_keys, |
| |
| 18 | + | $_group = 'shipping'; |
|
15 | 19 | | |
| |
16 | 20 | | // class constructor |
  |
17 | | - | function shipping($module = '') { |
| |
18 | | - | global $osC_Language; |
| |
| 21 | + | function osC_Shipping($module = '') { |
| |
| 22 | + | global $osC_Database, $osC_Language; |
|
19 | 23 | | |
  |
20 | | - | if (defined('MODULE_SHIPPING_INSTALLED') && tep_not_null(MODULE_SHIPPING_INSTALLED)) { |
| |
21 | | - | $this->modules = explode(';', MODULE_SHIPPING_INSTALLED); |
| |
| 24 | + | if (isset($_SESSION['osC_Shipping_data']) === false) { |
| |
| 25 | + | $_SESSION['osC_Shipping_data'] = array('quotes' => array(), |
| |
| 26 | + | 'cartID' => null); |
| |
| 27 | + | } |
|
22 | 28 | | |
  |
23 | | - | $include_modules = array(); |
| |
| 29 | + | $this->_quotes =& $_SESSION['osC_Shipping_data']['quotes']; |
| |
| 30 | + | $this->_cartID =& $_SESSION['osC_Shipping_data']['cartID']; |
|
24 | 31 | | |
  |
25 | | - | if ( (tep_not_null($module)) && (in_array(substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($_SERVER['PHP_SELF'], (strrpos($_SERVER['PHP_SELF'], '.')+1)), $this->modules)) ) { |
| |
26 | | - | $include_modules[] = array('class' => substr($module['id'], 0, strpos($module['id'], '_')), 'file' => substr($module['id'], 0, strpos($module['id'], '_')) . '.' . substr($_SERVER['PHP_SELF'], (strrpos($_SERVER['PHP_SELF'], '.')+1))); |
| |
27 | | - | } else { |
| |
28 | | - | reset($this->modules); |
| |
29 | | - | while (list(, $value) = each($this->modules)) { |
| |
30 | | - | $class = substr($value, 0, strrpos($value, '.')); |
| |
31 | | - | $include_modules[] = array('class' => $class, 'file' => $value); |
| |
32 | | - | } |
| |
| 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, '_'))); |
|
33 | 47 | | } |
| |
34 | 48 | | |
| |
35 | 49 | | $osC_Language->load('modules-shipping'); |
| |
36 | 50 | | |
  |
37 | | - | for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) { |
| |
38 | | - | include('includes/modules/shipping/' . $include_modules[$i]['file']); |
| |
| 51 | + | foreach ($this->_modules as $module) { |
| |
| 52 | + | $module_class = 'osC_Shipping_' . $module; |
|
39 | 53 | | |
  |
40 | | - | $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class']; |
| |
| 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(); |
|
41 | 60 | | } |
  |
| 61 | + | |
| |
| 62 | + | usort($this->_modules, array('osC_Shipping', '_usortModules')); |
|
42 | 63 | | } |
  |
| 64 | + | |
| |
| 65 | + | $this->_calculate(); |
|
43 | 66 | | } |
| |
44 | 67 | | |
  |
45 | | - | function quote($method = '', $module = '') { |
| |
46 | | - | global $total_weight, $shipping_weight, $shipping_quoted, $shipping_num_boxes; |
| |
| 68 | + | // class methods |
| |
| 69 | + | function getCode() { |
| |
| 70 | + | return $this->_code; |
| |
| 71 | + | } |
|
47 | 72 | | |
  |
48 | | - | $quotes_array = array(); |
| |
| 73 | + | function getTitle() { |
| |
| 74 | + | return $this->_title; |
| |
| 75 | + | } |
|
49 | 76 | | |
  |
50 | | - | if (is_array($this->modules)) { |
| |
51 | | - | $shipping_quoted = ''; |
| |
52 | | - | $shipping_num_boxes = 1; |
| |
53 | | - | $shipping_weight = $total_weight; |
| |
| 77 | + | function getDescription() { |
| |
| 78 | + | return $this->_description; |
| |
| 79 | + | } |
|
54 | 80 | | |
  |
55 | | - | if (SHIPPING_BOX_WEIGHT >= $shipping_weight*SHIPPING_BOX_PADDING/100) { |
| |
56 | | - | $shipping_weight = $shipping_weight+SHIPPING_BOX_WEIGHT; |
| |
57 | | - | } else { |
| |
58 | | - | $shipping_weight = $shipping_weight + ($shipping_weight*SHIPPING_BOX_PADDING/100); |
| |
| 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 | + | } |
|
59 | 128 | | } |
  |
| 129 | + | } |
|
60 | 130 | | |
  |
61 | | - | if ($shipping_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes |
| |
62 | | - | $shipping_num_boxes = ceil($shipping_weight/SHIPPING_MAX_WEIGHT); |
| |
63 | | - | $shipping_weight = $shipping_weight/$shipping_num_boxes; |
| |
| 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 | + | } |
|
64 | 146 | | } |
  |
| 147 | + | } |
|
65 | 148 | | |
  |
66 | | - | $include_quotes = array(); |
| |
| 149 | + | return $rate; |
| |
| 150 | + | } |
|
67 | 151 | | |
  |
68 | | - | reset($this->modules); |
| |
69 | | - | while (list(, $value) = each($this->modules)) { |
| |
70 | | - | $class = substr($value, 0, strrpos($value, '.')); |
| |
71 | | - | if (tep_not_null($module)) { |
| |
72 | | - | if ( ($module == $class) && ($GLOBALS[$class]->enabled) ) { |
| |
73 | | - | $include_quotes[] = $class; |
| |
74 | | - | } |
| |
75 | | - | } elseif ($GLOBALS[$class]->enabled) { |
| |
76 | | - | $include_quotes[] = $class; |
| |
| 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; |
|
77 | 162 | | } |
| |
78 | 163 | | } |
  |
| 164 | + | } |
|
79 | 165 | | |
  |
80 | | - | $size = sizeof($include_quotes); |
| |
81 | | - | for ($i=0; $i<$size; $i++) { |
| |
82 | | - | $quotes = $GLOBALS[$include_quotes[$i]]->quote($method); |
| |
83 | | - | if (is_array($quotes)) $quotes_array[] = $quotes; |
| |
| 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 | + | } |
|
84 | 213 | | } |
| |
85 | 214 | | } |
| |
86 | 215 | | |
  |
87 | | - | return $quotes_array; |
| |
| 216 | + | osC_Cache::clear('languages'); |
|
88 | 217 | | } |
| |
89 | 218 | | |
  |
90 | | - | function cheapest() { |
| |
91 | | - | if (is_array($this->modules)) { |
| |
92 | | - | $rates = array(); |
| |
| 219 | + | function remove() { |
| |
| 220 | + | global $osC_Database, $osC_Language; |
|
93 | 221 | | |
  |
94 | | - | reset($this->modules); |
| |
95 | | - | while (list(, $value) = each($this->modules)) { |
| |
96 | | - | $class = substr($value, 0, strrpos($value, '.')); |
| |
97 | | - | if ($GLOBALS[$class]->enabled) { |
| |
98 | | - | $quotes = $GLOBALS[$class]->quotes; |
| |
99 | | - | for ($i=0, $n=sizeof($quotes['methods']); $i<$n; $i++) { |
| |
100 | | - | if (isset($quotes['methods'][$i]['cost']) && tep_not_null($quotes['methods'][$i]['cost'])) { |
| |
101 | | - | $rates[] = array('id' => $quotes['id'] . '_' . $quotes['methods'][$i]['id'], |
| |
102 | | - | 'title' => $quotes['module'] . ' (' . $quotes['methods'][$i]['title'] . ')', |
| |
103 | | - | 'cost' => $quotes['methods'][$i]['cost']); |
| |
| 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; |
|
104 | 265 | | } |
| |
105 | 266 | | } |
| |
106 | 267 | | } |
  |
107 | | - | } |
|
108 | 268 | | |
  |
109 | | - | $cheapest = false; |
| |
110 | | - | for ($i=0, $n=sizeof($rates); $i<$n; $i++) { |
| |
111 | | - | if (is_array($cheapest)) { |
| |
112 | | - | if ($rates[$i]['cost'] < $cheapest['cost']) { |
| |
113 | | - | $cheapest = $rates[$i]; |
| |
| 269 | + | foreach ($include_quotes as $module) { |
| |
| 270 | + | $quotes = $GLOBALS[$module]->quote(); |
| |
| 271 | + | |
| |
| 272 | + | if (is_array($quotes)) { |
| |
| 273 | + | $this->_quotes[] = $quotes; |
|
114 | 274 | | } |
  |
115 | | - | } else { |
| |
116 | | - | $cheapest = $rates[$i]; |
|
117 | 275 | | } |
| |
118 | 276 | | } |
  |
| 277 | + | } |
| |
| 278 | + | } |
|
119 | 279 | | |
  |
120 | | - | return $cheapest; |
| |
| 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()); |
|
121 | 283 | | } |
  |
| 284 | + | |
| |
| 285 | + | return ($GLOBALS['osC_Shipping_' . $a]->getSortOrder() < $GLOBALS['osC_Shipping_' . $b]->getSortOrder()) ? -1 : 1; |
  |
122 | 286 | | } |
| |
123 | 287 | | } |
| |
124 | 288 | | ?> |