shop.balmet.com

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

customer_group.php (13299B)


      1 <?php
      2 class ControllerCustomerCustomerGroup extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('customer/customer_group');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('customer/customer_group');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('customer/customer_group');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('customer/customer_group');
     21 
     22 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     23 			$this->model_customer_customer_group->addCustomerGroup($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/customer_group', '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/customer_group');
     49 
     50 		$this->document->setTitle($this->language->get('heading_title'));
     51 
     52 		$this->load->model('customer/customer_group');
     53 
     54 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     55 			$this->model_customer_customer_group->editCustomerGroup($this->request->get['customer_group_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/customer_group', '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/customer_group');
     81 
     82 		$this->document->setTitle($this->language->get('heading_title'));
     83 
     84 		$this->load->model('customer/customer_group');
     85 
     86 		if (isset($this->request->post['selected']) && $this->validateDelete()) {
     87 			foreach ($this->request->post['selected'] as $customer_group_id) {
     88 				$this->model_customer_customer_group->deleteCustomerGroup($customer_group_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/customer_group', '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 = 'cgd.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/customer_group', 'user_token=' . $this->session->data['user_token'] . $url, true)
    156 		);
    157 
    158 		$data['add'] = $this->url->link('customer/customer_group/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    159 		$data['delete'] = $this->url->link('customer/customer_group/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
    160 
    161 		$data['customer_groups'] = 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 		$customer_group_total = $this->model_customer_customer_group->getTotalCustomerGroups();
    171 
    172 		$results = $this->model_customer_customer_group->getCustomerGroups($filter_data);
    173 
    174 		foreach ($results as $result) {
    175 			$data['customer_groups'][] = array(
    176 				'customer_group_id' => $result['customer_group_id'],
    177 				'name'              => $result['name'] . (($result['customer_group_id'] == $this->config->get('config_customer_group_id')) ? $this->language->get('text_default') : null),
    178 				'sort_order'        => $result['sort_order'],
    179 				'edit'              => $this->url->link('customer/customer_group/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_group_id=' . $result['customer_group_id'] . $url, true)
    180 			);
    181 		}
    182 
    183 		if (isset($this->error['warning'])) {
    184 			$data['error_warning'] = $this->error['warning'];
    185 		} else {
    186 			$data['error_warning'] = '';
    187 		}
    188 
    189 		if (isset($this->session->data['success'])) {
    190 			$data['success'] = $this->session->data['success'];
    191 
    192 			unset($this->session->data['success']);
    193 		} else {
    194 			$data['success'] = '';
    195 		}
    196 
    197 		if (isset($this->request->post['selected'])) {
    198 			$data['selected'] = (array)$this->request->post['selected'];
    199 		} else {
    200 			$data['selected'] = array();
    201 		}
    202 
    203 		$url = '';
    204 
    205 		if ($order == 'ASC') {
    206 			$url .= '&order=DESC';
    207 		} else {
    208 			$url .= '&order=ASC';
    209 		}
    210 
    211 		if (isset($this->request->get['page'])) {
    212 			$url .= '&page=' . $this->request->get['page'];
    213 		}
    214 
    215 		$data['sort_name'] = $this->url->link('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . '&sort=cgd.name' . $url, true);
    216 		$data['sort_sort_order'] = $this->url->link('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . '&sort=cg.sort_order' . $url, true);
    217 
    218 		$url = '';
    219 
    220 		if (isset($this->request->get['sort'])) {
    221 			$url .= '&sort=' . $this->request->get['sort'];
    222 		}
    223 
    224 		if (isset($this->request->get['order'])) {
    225 			$url .= '&order=' . $this->request->get['order'];
    226 		}
    227 
    228 		$pagination = new Pagination();
    229 		$pagination->total = $customer_group_total;
    230 		$pagination->page = $page;
    231 		$pagination->limit = $this->config->get('config_limit_admin');
    232 		$pagination->url = $this->url->link('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    233 
    234 		$data['pagination'] = $pagination->render();
    235 
    236 		$data['results'] = sprintf($this->language->get('text_pagination'), ($customer_group_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($customer_group_total - $this->config->get('config_limit_admin'))) ? $customer_group_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $customer_group_total, ceil($customer_group_total / $this->config->get('config_limit_admin')));
    237 
    238 		$data['sort'] = $sort;
    239 		$data['order'] = $order;
    240 
    241 		$data['header'] = $this->load->controller('common/header');
    242 		$data['column_left'] = $this->load->controller('common/column_left');
    243 		$data['footer'] = $this->load->controller('common/footer');
    244 
    245 		$this->response->setOutput($this->load->view('customer/customer_group_list', $data));
    246 	}
    247 
    248 	protected function getForm() {
    249 		$data['text_form'] = !isset($this->request->get['customer_group_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
    250 
    251 		if (isset($this->error['warning'])) {
    252 			$data['error_warning'] = $this->error['warning'];
    253 		} else {
    254 			$data['error_warning'] = '';
    255 		}
    256 
    257 		if (isset($this->error['name'])) {
    258 			$data['error_name'] = $this->error['name'];
    259 		} else {
    260 			$data['error_name'] = array();
    261 		}
    262 
    263 		$url = '';
    264 
    265 		if (isset($this->request->get['sort'])) {
    266 			$url .= '&sort=' . $this->request->get['sort'];
    267 		}
    268 
    269 		if (isset($this->request->get['order'])) {
    270 			$url .= '&order=' . $this->request->get['order'];
    271 		}
    272 
    273 		if (isset($this->request->get['page'])) {
    274 			$url .= '&page=' . $this->request->get['page'];
    275 		}
    276 
    277 		$data['breadcrumbs'] = array();
    278 
    279 		$data['breadcrumbs'][] = array(
    280 			'text' => $this->language->get('text_home'),
    281 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    282 		);
    283 
    284 		$data['breadcrumbs'][] = array(
    285 			'text' => $this->language->get('heading_title'),
    286 			'href' => $this->url->link('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . $url, true)
    287 		);
    288 
    289 		if (!isset($this->request->get['customer_group_id'])) {
    290 			$data['action'] = $this->url->link('customer/customer_group/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    291 		} else {
    292 			$data['action'] = $this->url->link('customer/customer_group/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_group_id=' . $this->request->get['customer_group_id'] . $url, true);
    293 		}
    294 
    295 		$data['cancel'] = $this->url->link('customer/customer_group', 'user_token=' . $this->session->data['user_token'] . $url, true);
    296 
    297 		if (isset($this->request->get['customer_group_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
    298 			$customer_group_info = $this->model_customer_customer_group->getCustomerGroup($this->request->get['customer_group_id']);
    299 		}
    300 
    301 		$this->load->model('localisation/language');
    302 
    303 		$data['languages'] = $this->model_localisation_language->getLanguages();
    304 
    305 		if (isset($this->request->post['customer_group_description'])) {
    306 			$data['customer_group_description'] = $this->request->post['customer_group_description'];
    307 		} elseif (isset($this->request->get['customer_group_id'])) {
    308 			$data['customer_group_description'] = $this->model_customer_customer_group->getCustomerGroupDescriptions($this->request->get['customer_group_id']);
    309 		} else {
    310 			$data['customer_group_description'] = array();
    311 		}
    312 
    313 		if (isset($this->request->post['approval'])) {
    314 			$data['approval'] = $this->request->post['approval'];
    315 		} elseif (!empty($customer_group_info)) {
    316 			$data['approval'] = $customer_group_info['approval'];
    317 		} else {
    318 			$data['approval'] = '';
    319 		}
    320 
    321 		if (isset($this->request->post['sort_order'])) {
    322 			$data['sort_order'] = $this->request->post['sort_order'];
    323 		} elseif (!empty($customer_group_info)) {
    324 			$data['sort_order'] = $customer_group_info['sort_order'];
    325 		} else {
    326 			$data['sort_order'] = '';
    327 		}
    328 
    329 		$data['header'] = $this->load->controller('common/header');
    330 		$data['column_left'] = $this->load->controller('common/column_left');
    331 		$data['footer'] = $this->load->controller('common/footer');
    332 
    333 		$this->response->setOutput($this->load->view('customer/customer_group_form', $data));
    334 	}
    335 
    336 	protected function validateForm() {
    337 		if (!$this->user->hasPermission('modify', 'customer/customer_group')) {
    338 			$this->error['warning'] = $this->language->get('error_permission');
    339 		}
    340 
    341 		foreach ($this->request->post['customer_group_description'] as $language_id => $value) {
    342 			if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 32)) {
    343 				$this->error['name'][$language_id] = $this->language->get('error_name');
    344 			}
    345 		}
    346 
    347 		return !$this->error;
    348 	}
    349 
    350 	protected function validateDelete() {
    351 		if (!$this->user->hasPermission('modify', 'customer/customer_group')) {
    352 			$this->error['warning'] = $this->language->get('error_permission');
    353 		}
    354 
    355 		$this->load->model('setting/store');
    356 		$this->load->model('customer/customer');
    357 
    358 		foreach ($this->request->post['selected'] as $customer_group_id) {
    359 			if ($this->config->get('config_customer_group_id') == $customer_group_id) {
    360 				$this->error['warning'] = $this->language->get('error_default');
    361 			}
    362 
    363 			$store_total = $this->model_setting_store->getTotalStoresByCustomerGroupId($customer_group_id);
    364 
    365 			if ($store_total) {
    366 				$this->error['warning'] = sprintf($this->language->get('error_store'), $store_total);
    367 			}
    368 
    369 			$customer_total = $this->model_customer_customer->getTotalCustomersByCustomerGroupId($customer_group_id);
    370 
    371 			if ($customer_total) {
    372 				$this->error['warning'] = sprintf($this->language->get('error_customer'), $customer_total);
    373 			}
    374 		}
    375 
    376 		return !$this->error;
    377 	}
    378 }