shop.balmet.com

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

edit.php (7363B)


      1 <?php
      2 class ControllerAccountEdit extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		if (!$this->customer->isLogged()) {
      7 			$this->session->data['redirect'] = $this->url->link('account/edit', '', true);
      8 
      9 			$this->response->redirect($this->url->link('account/login', '', true));
     10 		}
     11 
     12 		$this->load->language('account/edit');
     13 
     14 		$this->document->setTitle($this->language->get('heading_title'));
     15 
     16 		$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment.min.js');
     17 		$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment-with-locales.min.js');
     18 		$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js');
     19 		$this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css');
     20 
     21 		$this->load->model('account/customer');
     22 
     23 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
     24 			$this->model_account_customer->editCustomer($this->customer->getId(), $this->request->post);
     25 
     26 			$this->session->data['success'] = $this->language->get('text_success');
     27 
     28 			$this->response->redirect($this->url->link('account/account', '', true));
     29 		}
     30 
     31 		$data['breadcrumbs'] = array();
     32 
     33 		$data['breadcrumbs'][] = array(
     34 			'text' => $this->language->get('text_home'),
     35 			'href' => $this->url->link('common/home')
     36 		);
     37 
     38 		$data['breadcrumbs'][] = array(
     39 			'text' => $this->language->get('text_account'),
     40 			'href' => $this->url->link('account/account', '', true)
     41 		);
     42 
     43 		$data['breadcrumbs'][] = array(
     44 			'text' => $this->language->get('text_edit'),
     45 			'href' => $this->url->link('account/edit', '', true)
     46 		);
     47 
     48 		if (isset($this->error['warning'])) {
     49 			$data['error_warning'] = $this->error['warning'];
     50 		} else {
     51 			$data['error_warning'] = '';
     52 		}
     53 
     54 		if (isset($this->error['firstname'])) {
     55 			$data['error_firstname'] = $this->error['firstname'];
     56 		} else {
     57 			$data['error_firstname'] = '';
     58 		}
     59 
     60 		if (isset($this->error['lastname'])) {
     61 			$data['error_lastname'] = $this->error['lastname'];
     62 		} else {
     63 			$data['error_lastname'] = '';
     64 		}
     65 
     66 		if (isset($this->error['email'])) {
     67 			$data['error_email'] = $this->error['email'];
     68 		} else {
     69 			$data['error_email'] = '';
     70 		}
     71 
     72 		if (isset($this->error['telephone'])) {
     73 			$data['error_telephone'] = $this->error['telephone'];
     74 		} else {
     75 			$data['error_telephone'] = '';
     76 		}
     77 
     78 		if (isset($this->error['custom_field'])) {
     79 			$data['error_custom_field'] = $this->error['custom_field'];
     80 		} else {
     81 			$data['error_custom_field'] = array();
     82 		}
     83 
     84 		$data['action'] = $this->url->link('account/edit', '', true);
     85 
     86 		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
     87 			$customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
     88 		}
     89 
     90 		if (isset($this->request->post['firstname'])) {
     91 			$data['firstname'] = $this->request->post['firstname'];
     92 		} elseif (!empty($customer_info)) {
     93 			$data['firstname'] = $customer_info['firstname'];
     94 		} else {
     95 			$data['firstname'] = '';
     96 		}
     97 
     98 		if (isset($this->request->post['lastname'])) {
     99 			$data['lastname'] = $this->request->post['lastname'];
    100 		} elseif (!empty($customer_info)) {
    101 			$data['lastname'] = $customer_info['lastname'];
    102 		} else {
    103 			$data['lastname'] = '';
    104 		}
    105 
    106 		if (isset($this->request->post['email'])) {
    107 			$data['email'] = $this->request->post['email'];
    108 		} elseif (!empty($customer_info)) {
    109 			$data['email'] = $customer_info['email'];
    110 		} else {
    111 			$data['email'] = '';
    112 		}
    113 
    114 		if (isset($this->request->post['telephone'])) {
    115 			$data['telephone'] = $this->request->post['telephone'];
    116 		} elseif (!empty($customer_info)) {
    117 			$data['telephone'] = $customer_info['telephone'];
    118 		} else {
    119 			$data['telephone'] = '';
    120 		}
    121 
    122 		// Custom Fields
    123 		$data['custom_fields'] = array();
    124 		
    125 		$this->load->model('account/custom_field');
    126 
    127 		$custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
    128 
    129 		foreach ($custom_fields as $custom_field) {
    130 			if ($custom_field['location'] == 'account') {
    131 				$data['custom_fields'][] = $custom_field;
    132 			}
    133 		}
    134 
    135 		if (isset($this->request->post['custom_field']['account'])) {
    136 			$data['account_custom_field'] = $this->request->post['custom_field']['account'];
    137 		} elseif (isset($customer_info)) {
    138 			$data['account_custom_field'] = json_decode($customer_info['custom_field'], true);
    139 		} else {
    140 			$data['account_custom_field'] = array();
    141 		}
    142 
    143 		$data['back'] = $this->url->link('account/account', '', true);
    144 
    145 		$data['column_left'] = $this->load->controller('common/column_left');
    146 		$data['column_right'] = $this->load->controller('common/column_right');
    147 		$data['content_top'] = $this->load->controller('common/content_top');
    148 		$data['content_bottom'] = $this->load->controller('common/content_bottom');
    149 		$data['footer'] = $this->load->controller('common/footer');
    150 		$data['header'] = $this->load->controller('common/header');
    151 
    152 		$this->response->setOutput($this->load->view('account/edit', $data));
    153 	}
    154 
    155 	protected function validate() {
    156 		if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
    157 			$this->error['firstname'] = $this->language->get('error_firstname');
    158 		}
    159 
    160 		if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
    161 			$this->error['lastname'] = $this->language->get('error_lastname');
    162 		}
    163 
    164 		if ((utf8_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
    165 			$this->error['email'] = $this->language->get('error_email');
    166 		}
    167 
    168 		if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
    169 			$this->error['warning'] = $this->language->get('error_exists');
    170 		}
    171 
    172 		if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
    173 			$this->error['telephone'] = $this->language->get('error_telephone');
    174 		}
    175 
    176 		// Custom field validation
    177 		$this->load->model('account/custom_field');
    178 
    179 		$custom_fields = $this->model_account_custom_field->getCustomFields('account', $this->config->get('config_customer_group_id'));
    180 
    181 		foreach ($custom_fields as $custom_field) {
    182 			if ($custom_field['location'] == 'account') {
    183 				if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) {
    184 					$this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
    185 				} 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'])))) {
    186 					$this->error['custom_field'][$custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
    187 				}
    188 			}
    189 		}
    190 
    191 		return !$this->error;
    192 	}
    193 }