shop.balmet.com

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

option.php (16849B)


      1 <?php
      2 class ControllerCatalogOption extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('catalog/option');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('catalog/option');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('catalog/option');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('catalog/option');
     21 
     22 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     23 			$this->model_catalog_option->addOption($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('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url, true));
     42 		}
     43 
     44 		$this->getForm();
     45 	}
     46 
     47 	public function edit() {
     48 		$this->load->language('catalog/option');
     49 
     50 		$this->document->setTitle($this->language->get('heading_title'));
     51 
     52 		$this->load->model('catalog/option');
     53 
     54 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     55 			$this->model_catalog_option->editOption($this->request->get['option_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('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url, true));
     74 		}
     75 
     76 		$this->getForm();
     77 	}
     78 
     79 	public function delete() {
     80 		$this->load->language('catalog/option');
     81 
     82 		$this->document->setTitle($this->language->get('heading_title'));
     83 
     84 		$this->load->model('catalog/option');
     85 
     86 		if (isset($this->request->post['selected']) && $this->validateDelete()) {
     87 			foreach ($this->request->post['selected'] as $option_id) {
     88 				$this->model_catalog_option->deleteOption($option_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('catalog/option', '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 = 'od.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('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url, true)
    156 		);
    157 
    158 		$data['add'] = $this->url->link('catalog/option/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    159 		$data['delete'] = $this->url->link('catalog/option/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
    160 
    161 		$data['options'] = 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 		$option_total = $this->model_catalog_option->getTotalOptions();
    171 
    172 		$results = $this->model_catalog_option->getOptions($filter_data);
    173 
    174 		foreach ($results as $result) {
    175 			$data['options'][] = array(
    176 				'option_id'  => $result['option_id'],
    177 				'name'       => $result['name'],
    178 				'sort_order' => $result['sort_order'],
    179 				'edit'       => $this->url->link('catalog/option/edit', 'user_token=' . $this->session->data['user_token'] . '&option_id=' . $result['option_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('catalog/option', 'user_token=' . $this->session->data['user_token'] . '&sort=od.name' . $url, true);
    216 		$data['sort_sort_order'] = $this->url->link('catalog/option', 'user_token=' . $this->session->data['user_token'] . '&sort=o.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 = $option_total;
    230 		$pagination->page = $page;
    231 		$pagination->limit = $this->config->get('config_limit_admin');
    232 		$pagination->url = $this->url->link('catalog/option', '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'), ($option_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($option_total - $this->config->get('config_limit_admin'))) ? $option_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $option_total, ceil($option_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('catalog/option_list', $data));
    246 	}
    247 
    248 	protected function getForm() {
    249 		$data['text_form'] = !isset($this->request->get['option_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 		if (isset($this->error['option_value'])) {
    264 			$data['error_option_value'] = $this->error['option_value'];
    265 		} else {
    266 			$data['error_option_value'] = array();
    267 		}
    268 
    269 		$url = '';
    270 
    271 		if (isset($this->request->get['sort'])) {
    272 			$url .= '&sort=' . $this->request->get['sort'];
    273 		}
    274 
    275 		if (isset($this->request->get['order'])) {
    276 			$url .= '&order=' . $this->request->get['order'];
    277 		}
    278 
    279 		if (isset($this->request->get['page'])) {
    280 			$url .= '&page=' . $this->request->get['page'];
    281 		}
    282 
    283 		$data['breadcrumbs'] = array();
    284 
    285 		$data['breadcrumbs'][] = array(
    286 			'text' => $this->language->get('text_home'),
    287 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    288 		);
    289 
    290 		$data['breadcrumbs'][] = array(
    291 			'text' => $this->language->get('heading_title'),
    292 			'href' => $this->url->link('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url, true)
    293 		);
    294 
    295 		if (!isset($this->request->get['option_id'])) {
    296 			$data['action'] = $this->url->link('catalog/option/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    297 		} else {
    298 			$data['action'] = $this->url->link('catalog/option/edit', 'user_token=' . $this->session->data['user_token'] . '&option_id=' . $this->request->get['option_id'] . $url, true);
    299 		}
    300 
    301 		$data['cancel'] = $this->url->link('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url, true);
    302 
    303 		if (isset($this->request->get['option_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
    304 			$option_info = $this->model_catalog_option->getOption($this->request->get['option_id']);
    305 		}
    306 
    307 		$data['user_token'] = $this->session->data['user_token'];
    308 
    309 		$this->load->model('localisation/language');
    310 
    311 		$data['languages'] = $this->model_localisation_language->getLanguages();
    312 
    313 		if (isset($this->request->post['option_description'])) {
    314 			$data['option_description'] = $this->request->post['option_description'];
    315 		} elseif (isset($this->request->get['option_id'])) {
    316 			$data['option_description'] = $this->model_catalog_option->getOptionDescriptions($this->request->get['option_id']);
    317 		} else {
    318 			$data['option_description'] = array();
    319 		}
    320 
    321 		if (isset($this->request->post['type'])) {
    322 			$data['type'] = $this->request->post['type'];
    323 		} elseif (!empty($option_info)) {
    324 			$data['type'] = $option_info['type'];
    325 		} else {
    326 			$data['type'] = '';
    327 		}
    328 
    329 		if (isset($this->request->post['sort_order'])) {
    330 			$data['sort_order'] = $this->request->post['sort_order'];
    331 		} elseif (!empty($option_info)) {
    332 			$data['sort_order'] = $option_info['sort_order'];
    333 		} else {
    334 			$data['sort_order'] = '';
    335 		}
    336 
    337 		if (isset($this->request->post['option_value'])) {
    338 			$option_values = $this->request->post['option_value'];
    339 		} elseif (isset($this->request->get['option_id'])) {
    340 			$option_values = $this->model_catalog_option->getOptionValueDescriptions($this->request->get['option_id']);
    341 		} else {
    342 			$option_values = array();
    343 		}
    344 
    345 		$this->load->model('tool/image');
    346 
    347 		$data['option_values'] = array();
    348 
    349 		foreach ($option_values as $option_value) {
    350 			if (is_file(DIR_IMAGE . $option_value['image'])) {
    351 				$image = $option_value['image'];
    352 				$thumb = $option_value['image'];
    353 			} else {
    354 				$image = '';
    355 				$thumb = 'no_image.png';
    356 			}
    357 
    358 			$data['option_values'][] = array(
    359 				'option_value_id'          => $option_value['option_value_id'],
    360 				'option_value_description' => $option_value['option_value_description'],
    361 				'image'                    => $image,
    362 				'thumb'                    => $this->model_tool_image->resize($thumb, 100, 100),
    363 				'sort_order'               => $option_value['sort_order']
    364 			);
    365 		}
    366 
    367 		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);
    368 
    369 		$data['header'] = $this->load->controller('common/header');
    370 		$data['column_left'] = $this->load->controller('common/column_left');
    371 		$data['footer'] = $this->load->controller('common/footer');
    372 
    373 		$this->response->setOutput($this->load->view('catalog/option_form', $data));
    374 	}
    375 
    376 	protected function validateForm() {
    377 		if (!$this->user->hasPermission('modify', 'catalog/option')) {
    378 			$this->error['warning'] = $this->language->get('error_permission');
    379 		}
    380 
    381 		foreach ($this->request->post['option_description'] as $language_id => $value) {
    382 			if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 128)) {
    383 				$this->error['name'][$language_id] = $this->language->get('error_name');
    384 			}
    385 		}
    386 
    387 		if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox') && !isset($this->request->post['option_value'])) {
    388 			$this->error['warning'] = $this->language->get('error_type');
    389 		}
    390 
    391 		if (isset($this->request->post['option_value'])) {
    392 			foreach ($this->request->post['option_value'] as $option_value_id => $option_value) {
    393 				foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
    394 					if ((utf8_strlen($option_value_description['name']) < 1) || (utf8_strlen($option_value_description['name']) > 128)) {
    395 						$this->error['option_value'][$option_value_id][$language_id] = $this->language->get('error_option_value');
    396 					}
    397 				}
    398 			}
    399 		}
    400 
    401 		return !$this->error;
    402 	}
    403 
    404 	protected function validateDelete() {
    405 		if (!$this->user->hasPermission('modify', 'catalog/option')) {
    406 			$this->error['warning'] = $this->language->get('error_permission');
    407 		}
    408 
    409 		$this->load->model('catalog/product');
    410 
    411 		foreach ($this->request->post['selected'] as $option_id) {
    412 			$product_total = $this->model_catalog_product->getTotalProductsByOptionId($option_id);
    413 
    414 			if ($product_total) {
    415 				$this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
    416 			}
    417 		}
    418 
    419 		return !$this->error;
    420 	}
    421 
    422 	public function autocomplete() {
    423 		$json = array();
    424 
    425 		if (isset($this->request->get['filter_name'])) {
    426 			$this->load->language('catalog/option');
    427 
    428 			$this->load->model('catalog/option');
    429 
    430 			$this->load->model('tool/image');
    431 
    432 			$filter_data = array(
    433 				'filter_name' => $this->request->get['filter_name'],
    434 				'start'       => 0,
    435 				'limit'       => 5
    436 			);
    437 
    438 			$options = $this->model_catalog_option->getOptions($filter_data);
    439 
    440 			foreach ($options as $option) {
    441 				$option_value_data = array();
    442 
    443 				if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
    444 					$option_values = $this->model_catalog_option->getOptionValues($option['option_id']);
    445 
    446 					foreach ($option_values as $option_value) {
    447 						if (is_file(DIR_IMAGE . $option_value['image'])) {
    448 							$image = $this->model_tool_image->resize($option_value['image'], 50, 50);
    449 						} else {
    450 							$image = $this->model_tool_image->resize('no_image.png', 50, 50);
    451 						}
    452 
    453 						$option_value_data[] = array(
    454 							'option_value_id' => $option_value['option_value_id'],
    455 							'name'            => strip_tags(html_entity_decode($option_value['name'], ENT_QUOTES, 'UTF-8')),
    456 							'image'           => $image
    457 						);
    458 					}
    459 
    460 					$sort_order = array();
    461 
    462 					foreach ($option_value_data as $key => $value) {
    463 						$sort_order[$key] = $value['name'];
    464 					}
    465 
    466 					array_multisort($sort_order, SORT_ASC, $option_value_data);
    467 				}
    468 
    469 				$type = '';
    470 
    471 				if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox') {
    472 					$type = $this->language->get('text_choose');
    473 				}
    474 
    475 				if ($option['type'] == 'text' || $option['type'] == 'textarea') {
    476 					$type = $this->language->get('text_input');
    477 				}
    478 
    479 				if ($option['type'] == 'file') {
    480 					$type = $this->language->get('text_file');
    481 				}
    482 
    483 				if ($option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
    484 					$type = $this->language->get('text_date');
    485 				}
    486 
    487 				$json[] = array(
    488 					'option_id'    => $option['option_id'],
    489 					'name'         => strip_tags(html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8')),
    490 					'category'     => $type,
    491 					'type'         => $option['type'],
    492 					'option_value' => $option_value_data
    493 				);
    494 			}
    495 		}
    496 
    497 		$sort_order = array();
    498 
    499 		foreach ($json as $key => $value) {
    500 			$sort_order[$key] = $value['name'];
    501 		}
    502 
    503 		array_multisort($sort_order, SORT_ASC, $json);
    504 
    505 		$this->response->addHeader('Content-Type: application/json');
    506 		$this->response->setOutput(json_encode($json));
    507 	}
    508 }