customer.php (4108B)
1 <?php 2 class ControllerApiCustomer extends Controller { 3 public function index() { 4 $this->load->language('api/customer'); 5 6 // Delete past customer in case there is an error 7 unset($this->session->data['customer']); 8 9 $json = array(); 10 11 if (!isset($this->session->data['api_id'])) { 12 $json['error']['warning'] = $this->language->get('error_permission'); 13 } else { 14 // Add keys for missing post vars 15 $keys = array( 16 'customer_id', 17 'customer_group_id', 18 'firstname', 19 'lastname', 20 'email', 21 'telephone', 22 ); 23 24 foreach ($keys as $key) { 25 if (!isset($this->request->post[$key])) { 26 $this->request->post[$key] = ''; 27 } 28 } 29 30 // Customer 31 if ($this->request->post['customer_id']) { 32 $this->load->model('account/customer'); 33 34 $customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']); 35 36 if (!$customer_info || !$this->customer->login($customer_info['email'], '', true)) { 37 $json['error']['warning'] = $this->language->get('error_customer'); 38 } 39 } 40 41 if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { 42 $json['error']['firstname'] = $this->language->get('error_firstname'); 43 } 44 45 if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { 46 $json['error']['lastname'] = $this->language->get('error_lastname'); 47 } 48 49 if ((utf8_strlen($this->request->post['email']) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) { 50 $json['error']['email'] = $this->language->get('error_email'); 51 } 52 53 if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) { 54 $json['error']['telephone'] = $this->language->get('error_telephone'); 55 } 56 57 // Customer Group 58 if (is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) { 59 $customer_group_id = $this->request->post['customer_group_id']; 60 } else { 61 $customer_group_id = $this->config->get('config_customer_group_id'); 62 } 63 64 // Custom field validation 65 $this->load->model('account/custom_field'); 66 67 $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id); 68 69 foreach ($custom_fields as $custom_field) { 70 if ($custom_field['location'] == 'account') { 71 if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { 72 $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 73 } 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'])))) { 74 $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 75 } 76 } 77 } 78 79 if (!$json) { 80 $this->session->data['customer'] = array( 81 'customer_id' => $this->request->post['customer_id'], 82 'customer_group_id' => $customer_group_id, 83 'firstname' => $this->request->post['firstname'], 84 'lastname' => $this->request->post['lastname'], 85 'email' => $this->request->post['email'], 86 'telephone' => $this->request->post['telephone'], 87 'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array() 88 ); 89 90 $json['success'] = $this->language->get('text_success'); 91 } 92 } 93 94 $this->response->addHeader('Content-Type: application/json'); 95 $this->response->setOutput(json_encode($json)); 96 } 97 }