shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

shipping.php (9399B)


      1 <?php
      2 class ControllerApiShipping extends Controller {
      3 	public function address() {
      4 		$this->load->language('api/shipping');
      5 
      6 		// Delete old shipping address, shipping methods and method so not to cause any issues if there is an error
      7 		unset($this->session->data['shipping_address']);
      8 		unset($this->session->data['shipping_methods']);
      9 		unset($this->session->data['shipping_method']);
     10 
     11 		$json = array();
     12 
     13 		if ($this->cart->hasShipping()) {
     14 			if (!isset($this->session->data['api_id'])) {
     15 				$json['error']['warning'] = $this->language->get('error_permission');
     16 			} else {
     17 				// Add keys for missing post vars
     18 				$keys = array(
     19 					'firstname',
     20 					'lastname',
     21 					'company',
     22 					'address_1',
     23 					'address_2',
     24 					'postcode',
     25 					'city',
     26 					'zone_id',
     27 					'country_id'
     28 				);
     29 
     30 				foreach ($keys as $key) {
     31 					if (!isset($this->request->post[$key])) {
     32 						$this->request->post[$key] = '';
     33 					}
     34 				}
     35 
     36 				if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
     37 					$json['error']['firstname'] = $this->language->get('error_firstname');
     38 				}
     39 
     40 				if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
     41 					$json['error']['lastname'] = $this->language->get('error_lastname');
     42 				}
     43 
     44 				if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) {
     45 					$json['error']['address_1'] = $this->language->get('error_address_1');
     46 				}
     47 
     48 				if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) {
     49 					$json['error']['city'] = $this->language->get('error_city');
     50 				}
     51 
     52 				$this->load->model('localisation/country');
     53 
     54 				$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
     55 
     56 				if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) {
     57 					$json['error']['postcode'] = $this->language->get('error_postcode');
     58 				}
     59 
     60 				if ($this->request->post['country_id'] == '') {
     61 					$json['error']['country'] = $this->language->get('error_country');
     62 				}
     63 
     64 				if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
     65 					$json['error']['zone'] = $this->language->get('error_zone');
     66 				}
     67 
     68 				// Custom field validation
     69 				$this->load->model('account/custom_field');
     70 
     71 				$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
     72 
     73 				foreach ($custom_fields as $custom_field) {
     74 					if ($custom_field['location'] == 'address') {
     75 						if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) {
     76 							$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
     77 						} elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
     78 							$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
     79 						}
     80 					}
     81 				}
     82 
     83 				if (!$json) {
     84 					$this->load->model('localisation/country');
     85 
     86 					$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
     87 
     88 					if ($country_info) {
     89 						$country = $country_info['name'];
     90 						$iso_code_2 = $country_info['iso_code_2'];
     91 						$iso_code_3 = $country_info['iso_code_3'];
     92 						$address_format = $country_info['address_format'];
     93 					} else {
     94 						$country = '';
     95 						$iso_code_2 = '';
     96 						$iso_code_3 = '';
     97 						$address_format = '';
     98 					}
     99 
    100 					$this->load->model('localisation/zone');
    101 
    102 					$zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
    103 
    104 					if ($zone_info) {
    105 						$zone = $zone_info['name'];
    106 						$zone_code = $zone_info['code'];
    107 					} else {
    108 						$zone = '';
    109 						$zone_code = '';
    110 					}
    111 
    112 					$this->session->data['shipping_address'] = array(
    113 						'firstname'      => $this->request->post['firstname'],
    114 						'lastname'       => $this->request->post['lastname'],
    115 						'company'        => $this->request->post['company'],
    116 						'address_1'      => $this->request->post['address_1'],
    117 						'address_2'      => $this->request->post['address_2'],
    118 						'postcode'       => $this->request->post['postcode'],
    119 						'city'           => $this->request->post['city'],
    120 						'zone_id'        => $this->request->post['zone_id'],
    121 						'zone'           => $zone,
    122 						'zone_code'      => $zone_code,
    123 						'country_id'     => $this->request->post['country_id'],
    124 						'country'        => $country,
    125 						'iso_code_2'     => $iso_code_2,
    126 						'iso_code_3'     => $iso_code_3,
    127 						'address_format' => $address_format,
    128 						'custom_field'   => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array()
    129 					);
    130 
    131 					$json['success'] = $this->language->get('text_address');
    132 
    133 					unset($this->session->data['shipping_method']);
    134 					unset($this->session->data['shipping_methods']);
    135 				}
    136 			}
    137 		}
    138 		
    139 		$this->response->addHeader('Content-Type: application/json');
    140 		$this->response->setOutput(json_encode($json));
    141 	}
    142 
    143 	public function methods() {
    144 		$this->load->language('api/shipping');
    145 
    146 		// Delete past shipping methods and method just in case there is an error
    147 		unset($this->session->data['shipping_methods']);
    148 		unset($this->session->data['shipping_method']);
    149 
    150 		$json = array();
    151 
    152 		if (!isset($this->session->data['api_id'])) {
    153 			$json['error'] = $this->language->get('error_permission');
    154 		} elseif ($this->cart->hasShipping()) {
    155 			if (!isset($this->session->data['shipping_address'])) {
    156 				$json['error'] = $this->language->get('error_address');
    157 			}
    158 
    159 			if (!$json) {
    160 				// Shipping Methods
    161 				$json['shipping_methods'] = array();
    162 
    163 				$this->load->model('setting/extension');
    164 
    165 				$results = $this->model_setting_extension->getExtensions('shipping');
    166 
    167 				foreach ($results as $result) {
    168 					if ($this->config->get('shipping_' . $result['code'] . '_status')) {
    169 						$this->load->model('extension/shipping/' . $result['code']);
    170 
    171 						$quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']);
    172 
    173 						if ($quote) {
    174 							$json['shipping_methods'][$result['code']] = array(
    175 								'title'      => $quote['title'],
    176 								'quote'      => $quote['quote'],
    177 								'sort_order' => $quote['sort_order'],
    178 								'error'      => $quote['error']
    179 							);
    180 						}
    181 					}
    182 				}
    183 
    184 				$sort_order = array();
    185 
    186 				foreach ($json['shipping_methods'] as $key => $value) {
    187 					$sort_order[$key] = $value['sort_order'];
    188 				}
    189 
    190 				array_multisort($sort_order, SORT_ASC, $json['shipping_methods']);
    191 
    192 				if ($json['shipping_methods']) {
    193 					$this->session->data['shipping_methods'] = $json['shipping_methods'];
    194 				} else {
    195 					$json['error'] = $this->language->get('error_no_shipping');
    196 				}
    197 			}
    198 		} else {
    199 			$json['shipping_methods'] = array();
    200 		}
    201 
    202 		$this->response->addHeader('Content-Type: application/json');
    203 		$this->response->setOutput(json_encode($json));
    204 	}
    205 
    206 	public function method() {
    207 		$this->load->language('api/shipping');
    208 
    209 		// Delete old shipping method so not to cause any issues if there is an error
    210 		unset($this->session->data['shipping_method']);
    211 
    212 		$json = array();
    213 
    214 		if (!isset($this->session->data['api_id'])) {
    215 			$json['error'] = $this->language->get('error_permission');
    216 		} else {
    217 			if ($this->cart->hasShipping()) {
    218 				// Shipping Address
    219 				if (!isset($this->session->data['shipping_address'])) {
    220 					$json['error'] = $this->language->get('error_address');
    221 				}
    222 
    223 				// Shipping Method
    224 				if (empty($this->session->data['shipping_methods'])) {
    225 					$json['error'] = $this->language->get('error_no_shipping');
    226 				} elseif (!isset($this->request->post['shipping_method'])) {
    227 					$json['error'] = $this->language->get('error_method');
    228 				} else {
    229 					$shipping = explode('.', $this->request->post['shipping_method']);
    230 
    231 					if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
    232 						$json['error'] = $this->language->get('error_method');
    233 					}
    234 				}
    235 
    236 				if (!$json) {
    237 					$this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
    238 
    239 					$json['success'] = $this->language->get('text_method');
    240 				}
    241 			} else {
    242 				unset($this->session->data['shipping_address']);
    243 				unset($this->session->data['shipping_method']);
    244 				unset($this->session->data['shipping_methods']);
    245 			}
    246 		}
    247 
    248 		$this->response->addHeader('Content-Type: application/json');
    249 		$this->response->setOutput(json_encode($json));
    250 	}
    251 }