shop.balmet.com

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

custom_field.php (17723B)


      1 <?php
      2 class ControllerCustomerCustomField extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('customer/custom_field');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('customer/custom_field');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('customer/custom_field');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('customer/custom_field');
     21 
     22 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     23 			$this->model_customer_custom_field->addCustomField($this->request->post);
     24 
     25 			$this->session->data['success'] = $this->language->get('text_success');
     26 
     27 			$url = '';
     28 
     29 			if (isset($this->request->get['sort'])) {
     30 				$url .= '&sort=' . $this->request->get['sort'];
     31 			}
     32 
     33 			if (isset($this->request->get['order'])) {
     34 				$url .= '&order=' . $this->request->get['order'];
     35 			}
     36 
     37 			if (isset($this->request->get['page'])) {
     38 				$url .= '&page=' . $this->request->get['page'];
     39 			}
     40 
     41 			$this->response->redirect($this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url, true));
     42 		}
     43 
     44 		$this->getForm();
     45 	}
     46 
     47 	public function edit() {
     48 		$this->load->language('customer/custom_field');
     49 
     50 		$this->document->setTitle($this->language->get('heading_title'));
     51 
     52 		$this->load->model('customer/custom_field');
     53 
     54 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     55 			$this->model_customer_custom_field->editCustomField($this->request->get['custom_field_id'], $this->request->post);
     56 
     57 			$this->session->data['success'] = $this->language->get('text_success');
     58 
     59 			$url = '';
     60 
     61 			if (isset($this->request->get['sort'])) {
     62 				$url .= '&sort=' . $this->request->get['sort'];
     63 			}
     64 
     65 			if (isset($this->request->get['order'])) {
     66 				$url .= '&order=' . $this->request->get['order'];
     67 			}
     68 
     69 			if (isset($this->request->get['page'])) {
     70 				$url .= '&page=' . $this->request->get['page'];
     71 			}
     72 
     73 			$this->response->redirect($this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url, true));
     74 		}
     75 
     76 		$this->getForm();
     77 	}
     78 
     79 	public function delete() {
     80 		$this->load->language('customer/custom_field');
     81 
     82 		$this->document->setTitle($this->language->get('heading_title'));
     83 
     84 		$this->load->model('customer/custom_field');
     85 
     86 		if (isset($this->request->post['selected']) && $this->validateDelete()) {
     87 			foreach ($this->request->post['selected'] as $custom_field_id) {
     88 				$this->model_customer_custom_field->deleteCustomField($custom_field_id);
     89 			}
     90 
     91 			$this->session->data['success'] = $this->language->get('text_success');
     92 
     93 			$url = '';
     94 
     95 			if (isset($this->request->get['sort'])) {
     96 				$url .= '&sort=' . $this->request->get['sort'];
     97 			}
     98 
     99 			if (isset($this->request->get['order'])) {
    100 				$url .= '&order=' . $this->request->get['order'];
    101 			}
    102 
    103 			if (isset($this->request->get['page'])) {
    104 				$url .= '&page=' . $this->request->get['page'];
    105 			}
    106 
    107 			$this->response->redirect($this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url, true));
    108 		}
    109 
    110 		$this->getList();
    111 	}
    112 
    113 	protected function getList() {
    114 		if (isset($this->request->get['sort'])) {
    115 			$sort = $this->request->get['sort'];
    116 		} else {
    117 			$sort = 'cfd.name';
    118 		}
    119 
    120 		if (isset($this->request->get['order'])) {
    121 			$order = $this->request->get['order'];
    122 		} else {
    123 			$order = 'ASC';
    124 		}
    125 
    126 		if (isset($this->request->get['page'])) {
    127 			$page = $this->request->get['page'];
    128 		} else {
    129 			$page = 1;
    130 		}
    131 
    132 		$url = '';
    133 
    134 		if (isset($this->request->get['sort'])) {
    135 			$url .= '&sort=' . $this->request->get['sort'];
    136 		}
    137 
    138 		if (isset($this->request->get['order'])) {
    139 			$url .= '&order=' . $this->request->get['order'];
    140 		}
    141 
    142 		if (isset($this->request->get['page'])) {
    143 			$url .= '&page=' . $this->request->get['page'];
    144 		}
    145 
    146 		$data['breadcrumbs'] = array();
    147 
    148 		$data['breadcrumbs'][] = array(
    149 			'text' => $this->language->get('text_home'),
    150 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    151 		);
    152 
    153 		$data['breadcrumbs'][] = array(
    154 			'text' => $this->language->get('heading_title'),
    155 			'href' => $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url, true)
    156 		);
    157 
    158 		$data['add'] = $this->url->link('customer/custom_field/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    159 		$data['delete'] = $this->url->link('customer/custom_field/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
    160 
    161 		$data['custom_fields'] = array();
    162 
    163 		$filter_data = array(
    164 			'sort'  => $sort,
    165 			'order' => $order,
    166 			'start' => ($page - 1) * $this->config->get('config_limit_admin'),
    167 			'limit' => $this->config->get('config_limit_admin')
    168 		);
    169 
    170 		$custom_field_total = $this->model_customer_custom_field->getTotalCustomFields();
    171 
    172 		$results = $this->model_customer_custom_field->getCustomFields($filter_data);
    173 
    174 		foreach ($results as $result) {
    175 			$type = '';
    176 
    177 			switch ($result['type']) {
    178 				case 'select':
    179 					$type = $this->language->get('text_select');
    180 					break;
    181 				case 'radio':
    182 					$type = $this->language->get('text_radio');
    183 					break;
    184 				case 'checkbox':
    185 					$type = $this->language->get('text_checkbox');
    186 					break;
    187 				case 'input':
    188 					$type = $this->language->get('text_input');
    189 					break;
    190 				case 'text':
    191 					$type = $this->language->get('text_text');
    192 					break;
    193 				case 'textarea':
    194 					$type = $this->language->get('text_textarea');
    195 					break;
    196 				case 'file':
    197 					$type = $this->language->get('text_file');
    198 					break;
    199 				case 'date':
    200 					$type = $this->language->get('text_date');
    201 					break;
    202 				case 'datetime':
    203 					$type = $this->language->get('text_datetime');
    204 					break;
    205 				case 'time':
    206 					$type = $this->language->get('text_time');
    207 					break;
    208 			}
    209 
    210 			$data['custom_fields'][] = array(
    211 				'custom_field_id' => $result['custom_field_id'],
    212 				'name'            => $result['name'],
    213 				'location'        => $this->language->get('text_' . $result['location']),
    214 				'type'            => $type,
    215 				'status'          => $result['status'],
    216 				'sort_order'      => $result['sort_order'],
    217 				'edit'            => $this->url->link('customer/custom_field/edit', 'user_token=' . $this->session->data['user_token'] . '&custom_field_id=' . $result['custom_field_id'] . $url, true)
    218 			);
    219 		}
    220 
    221 		if (isset($this->error['warning'])) {
    222 			$data['error_warning'] = $this->error['warning'];
    223 		} else {
    224 			$data['error_warning'] = '';
    225 		}
    226 
    227 		if (isset($this->session->data['success'])) {
    228 			$data['success'] = $this->session->data['success'];
    229 
    230 			unset($this->session->data['success']);
    231 		} else {
    232 			$data['success'] = '';
    233 		}
    234 
    235 		if (isset($this->request->post['selected'])) {
    236 			$data['selected'] = (array)$this->request->post['selected'];
    237 		} else {
    238 			$data['selected'] = array();
    239 		}
    240 
    241 		$url = '';
    242 
    243 		if ($order == 'ASC') {
    244 			$url .= '&order=DESC';
    245 		} else {
    246 			$url .= '&order=ASC';
    247 		}
    248 
    249 		if (isset($this->request->get['page'])) {
    250 			$url .= '&page=' . $this->request->get['page'];
    251 		}
    252 
    253 		$data['sort_name'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . '&sort=cfd.name' . $url, true);
    254 		$data['sort_location'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.location' . $url, true);
    255 		$data['sort_type'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.type' . $url, true);
    256 		$data['sort_status'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.status' . $url, true);
    257 		$data['sort_sort_order'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . '&sort=cf.sort_order' . $url, true);
    258 
    259 		$url = '';
    260 
    261 		if (isset($this->request->get['sort'])) {
    262 			$url .= '&sort=' . $this->request->get['sort'];
    263 		}
    264 
    265 		if (isset($this->request->get['order'])) {
    266 			$url .= '&order=' . $this->request->get['order'];
    267 		}
    268 
    269 		$pagination = new Pagination();
    270 		$pagination->total = $custom_field_total;
    271 		$pagination->page = $page;
    272 		$pagination->limit = $this->config->get('config_limit_admin');
    273 		$pagination->url = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    274 
    275 		$data['pagination'] = $pagination->render();
    276 
    277 		$data['results'] = sprintf($this->language->get('text_pagination'), ($custom_field_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($custom_field_total - $this->config->get('config_limit_admin'))) ? $custom_field_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $custom_field_total, ceil($custom_field_total / $this->config->get('config_limit_admin')));
    278 
    279 		$data['sort'] = $sort;
    280 		$data['order'] = $order;
    281 
    282 		$data['header'] = $this->load->controller('common/header');
    283 		$data['column_left'] = $this->load->controller('common/column_left');
    284 		$data['footer'] = $this->load->controller('common/footer');
    285 
    286 		$this->response->setOutput($this->load->view('customer/custom_field_list', $data));
    287 	}
    288 
    289 	protected function getForm() {
    290 		$data['text_form'] = !isset($this->request->get['custom_field_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
    291 
    292 		if (isset($this->error['warning'])) {
    293 			$data['error_warning'] = $this->error['warning'];
    294 		} else {
    295 			$data['error_warning'] = '';
    296 		}
    297 
    298 		if (isset($this->error['name'])) {
    299 			$data['error_name'] = $this->error['name'];
    300 		} else {
    301 			$data['error_name'] = array();
    302 		}
    303 
    304 		if (isset($this->error['custom_field_value'])) {
    305 			$data['error_custom_field_value'] = $this->error['custom_field_value'];
    306 		} else {
    307 			$data['error_custom_field_value'] = array();
    308 		}
    309 
    310 		$url = '';
    311 
    312 		if (isset($this->request->get['sort'])) {
    313 			$url .= '&sort=' . $this->request->get['sort'];
    314 		}
    315 
    316 		if (isset($this->request->get['order'])) {
    317 			$url .= '&order=' . $this->request->get['order'];
    318 		}
    319 
    320 		if (isset($this->request->get['page'])) {
    321 			$url .= '&page=' . $this->request->get['page'];
    322 		}
    323 
    324 		$data['breadcrumbs'] = array();
    325 
    326 		$data['breadcrumbs'][] = array(
    327 			'text' => $this->language->get('text_home'),
    328 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    329 		);
    330 
    331 		$data['breadcrumbs'][] = array(
    332 			'text' => $this->language->get('heading_title'),
    333 			'href' => $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url, true)
    334 		);
    335 
    336 		if (!isset($this->request->get['custom_field_id'])) {
    337 			$data['action'] = $this->url->link('customer/custom_field/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    338 		} else {
    339 			$data['action'] = $this->url->link('customer/custom_field/edit', 'user_token=' . $this->session->data['user_token'] . '&custom_field_id=' . $this->request->get['custom_field_id'] . $url, true);
    340 		}
    341 
    342 		$data['cancel'] = $this->url->link('customer/custom_field', 'user_token=' . $this->session->data['user_token'] . $url, true);
    343 
    344 		if (isset($this->request->get['custom_field_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
    345 			$custom_field_info = $this->model_customer_custom_field->getCustomField($this->request->get['custom_field_id']);
    346 		}
    347 
    348 		$data['user_token'] = $this->session->data['user_token'];
    349 
    350 		$this->load->model('localisation/language');
    351 
    352 		$data['languages'] = $this->model_localisation_language->getLanguages();
    353 
    354 		if (isset($this->request->post['custom_field_description'])) {
    355 			$data['custom_field_description'] = $this->request->post['custom_field_description'];
    356 		} elseif (isset($this->request->get['custom_field_id'])) {
    357 			$data['custom_field_description'] = $this->model_customer_custom_field->getCustomFieldDescriptions($this->request->get['custom_field_id']);
    358 		} else {
    359 			$data['custom_field_description'] = array();
    360 		}
    361 
    362 		if (isset($this->request->post['location'])) {
    363 			$data['location'] = $this->request->post['location'];
    364 		} elseif (!empty($custom_field_info)) {
    365 			$data['location'] = $custom_field_info['location'];
    366 		} else {
    367 			$data['location'] = '';
    368 		}
    369 
    370 		if (isset($this->request->post['type'])) {
    371 			$data['type'] = $this->request->post['type'];
    372 		} elseif (!empty($custom_field_info)) {
    373 			$data['type'] = $custom_field_info['type'];
    374 		} else {
    375 			$data['type'] = '';
    376 		}
    377 
    378 		if (isset($this->request->post['value'])) {
    379 			$data['value'] = $this->request->post['value'];
    380 		} elseif (!empty($custom_field_info)) {
    381 			$data['value'] = $custom_field_info['value'];
    382 		} else {
    383 			$data['value'] = '';
    384 		}
    385 
    386 		if (isset($this->request->post['validation'])) {
    387 			$data['validation'] = $this->request->post['validation'];
    388 		} elseif (!empty($custom_field_info)) {
    389 			$data['validation'] = $custom_field_info['validation'];
    390 		} else {
    391 			$data['validation'] = '';
    392 		}
    393 
    394 		if (isset($this->request->post['status'])) {
    395 			$data['status'] = $this->request->post['status'];
    396 		} elseif (!empty($custom_field_info)) {
    397 			$data['status'] = $custom_field_info['status'];
    398 		} else {
    399 			$data['status'] = '';
    400 		}
    401 
    402 		if (isset($this->request->post['sort_order'])) {
    403 			$data['sort_order'] = $this->request->post['sort_order'];
    404 		} elseif (!empty($custom_field_info)) {
    405 			$data['sort_order'] = $custom_field_info['sort_order'];
    406 		} else {
    407 			$data['sort_order'] = '';
    408 		}
    409 
    410 		if (isset($this->request->post['custom_field_value'])) {
    411 			$custom_field_values = $this->request->post['custom_field_value'];
    412 		} elseif (isset($this->request->get['custom_field_id'])) {
    413 			$custom_field_values = $this->model_customer_custom_field->getCustomFieldValueDescriptions($this->request->get['custom_field_id']);
    414 		} else {
    415 			$custom_field_values = array();
    416 		}
    417 
    418 		$data['custom_field_values'] = array();
    419 
    420 		foreach ($custom_field_values as $custom_field_value) {
    421 			$data['custom_field_values'][] = array(
    422 				'custom_field_value_id'          => $custom_field_value['custom_field_value_id'],
    423 				'custom_field_value_description' => $custom_field_value['custom_field_value_description'],
    424 				'sort_order'                     => $custom_field_value['sort_order']
    425 			);
    426 		}
    427 
    428 		if (isset($this->request->post['custom_field_customer_group'])) {
    429 			$custom_field_customer_groups = $this->request->post['custom_field_customer_group'];
    430 		} elseif (isset($this->request->get['custom_field_id'])) {
    431 			$custom_field_customer_groups = $this->model_customer_custom_field->getCustomFieldCustomerGroups($this->request->get['custom_field_id']);
    432 		} else {
    433 			$custom_field_customer_groups = array();
    434 		}
    435 
    436 		$data['custom_field_customer_group'] = array();
    437 
    438 		foreach ($custom_field_customer_groups as $custom_field_customer_group) {
    439 			$data['custom_field_customer_group'][] = $custom_field_customer_group['customer_group_id'];
    440 		}
    441 
    442 		$data['custom_field_required'] = array();
    443 
    444 		foreach ($custom_field_customer_groups as $custom_field_customer_group) {
    445 			if ($custom_field_customer_group['required']) {
    446 				$data['custom_field_required'][] = $custom_field_customer_group['customer_group_id'];
    447 			}
    448 		}
    449 
    450 		$this->load->model('customer/customer_group');
    451 
    452 		$data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
    453 
    454 		$data['header'] = $this->load->controller('common/header');
    455 		$data['column_left'] = $this->load->controller('common/column_left');
    456 		$data['footer'] = $this->load->controller('common/footer');
    457 
    458 		$this->response->setOutput($this->load->view('customer/custom_field_form', $data));
    459 	}
    460 
    461 	protected function validateForm() {
    462 		if (!$this->user->hasPermission('modify', 'customer/custom_field')) {
    463 			$this->error['warning'] = $this->language->get('error_permission');
    464 		}
    465 
    466 		foreach ($this->request->post['custom_field_description'] as $language_id => $value) {
    467 			if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 128)) {
    468 				$this->error['name'][$language_id] = $this->language->get('error_name');
    469 			}
    470 		}
    471 
    472 		if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox')) {
    473 			if (!isset($this->request->post['custom_field_value'])) {
    474 				$this->error['warning'] = $this->language->get('error_type');
    475 			}
    476 
    477 			if (isset($this->request->post['custom_field_value'])) {
    478 				foreach ($this->request->post['custom_field_value'] as $custom_field_value_id => $custom_field_value) {
    479 					foreach ($custom_field_value['custom_field_value_description'] as $language_id => $custom_field_value_description) {
    480 						if ((utf8_strlen($custom_field_value_description['name']) < 1) || (utf8_strlen($custom_field_value_description['name']) > 128)) {
    481 							$this->error['custom_field_value'][$custom_field_value_id][$language_id] = $this->language->get('error_custom_value');
    482 						}
    483 					}
    484 				}
    485 			}
    486 		}
    487 
    488 		return !$this->error;
    489 	}
    490 
    491 	protected function validateDelete() {
    492 		if (!$this->user->hasPermission('modify', 'customer/custom_field')) {
    493 			$this->error['warning'] = $this->language->get('error_permission');
    494 		}
    495 
    496 		return !$this->error;
    497 	}
    498 }