shop.balmet.com

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

category.php (20432B)


      1 <?php
      2 class ControllerCatalogCategory extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('catalog/category');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('catalog/category');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('catalog/category');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('catalog/category');
     21 
     22 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     23 			$this->model_catalog_category->addCategory($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/category', '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/category');
     49 
     50 		$this->document->setTitle($this->language->get('heading_title'));
     51 
     52 		$this->load->model('catalog/category');
     53 
     54 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     55 			$this->model_catalog_category->editCategory($this->request->get['category_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/category', '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/category');
     81 
     82 		$this->document->setTitle($this->language->get('heading_title'));
     83 
     84 		$this->load->model('catalog/category');
     85 
     86 		if (isset($this->request->post['selected']) && $this->validateDelete()) {
     87 			foreach ($this->request->post['selected'] as $category_id) {
     88 				$this->model_catalog_category->deleteCategory($category_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/category', 'user_token=' . $this->session->data['user_token'] . $url, true));
    108 		}
    109 
    110 		$this->getList();
    111 	}
    112 
    113 	public function repair() {
    114 		$this->load->language('catalog/category');
    115 
    116 		$this->document->setTitle($this->language->get('heading_title'));
    117 
    118 		$this->load->model('catalog/category');
    119 
    120 		if ($this->validateRepair()) {
    121 			$this->model_catalog_category->repairCategories();
    122 
    123 			$this->session->data['success'] = $this->language->get('text_success');
    124 
    125 			$url = '';
    126 
    127 			if (isset($this->request->get['sort'])) {
    128 				$url .= '&sort=' . $this->request->get['sort'];
    129 			}
    130 
    131 			if (isset($this->request->get['order'])) {
    132 				$url .= '&order=' . $this->request->get['order'];
    133 			}
    134 
    135 			if (isset($this->request->get['page'])) {
    136 				$url .= '&page=' . $this->request->get['page'];
    137 			}
    138 
    139 			$this->response->redirect($this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . $url, true));
    140 		}
    141 
    142 		$this->getList();
    143 	}
    144 
    145 	protected function getList() {
    146 		if (isset($this->request->get['sort'])) {
    147 			$sort = $this->request->get['sort'];
    148 		} else {
    149 			$sort = 'name';
    150 		}
    151 
    152 		if (isset($this->request->get['order'])) {
    153 			$order = $this->request->get['order'];
    154 		} else {
    155 			$order = 'ASC';
    156 		}
    157 
    158 		if (isset($this->request->get['page'])) {
    159 			$page = $this->request->get['page'];
    160 		} else {
    161 			$page = 1;
    162 		}
    163 
    164 		$url = '';
    165 
    166 		if (isset($this->request->get['sort'])) {
    167 			$url .= '&sort=' . $this->request->get['sort'];
    168 		}
    169 
    170 		if (isset($this->request->get['order'])) {
    171 			$url .= '&order=' . $this->request->get['order'];
    172 		}
    173 
    174 		if (isset($this->request->get['page'])) {
    175 			$url .= '&page=' . $this->request->get['page'];
    176 		}
    177 
    178 		$data['breadcrumbs'] = array();
    179 
    180 		$data['breadcrumbs'][] = array(
    181 			'text' => $this->language->get('text_home'),
    182 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    183 		);
    184 
    185 		$data['breadcrumbs'][] = array(
    186 			'text' => $this->language->get('heading_title'),
    187 			'href' => $this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . $url, true)
    188 		);
    189 
    190 		$data['add'] = $this->url->link('catalog/category/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    191 		$data['delete'] = $this->url->link('catalog/category/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
    192 		$data['repair'] = $this->url->link('catalog/category/repair', 'user_token=' . $this->session->data['user_token'] . $url, true);
    193 
    194 		$data['categories'] = array();
    195 
    196 		$filter_data = array(
    197 			'sort'  => $sort,
    198 			'order' => $order,
    199 			'start' => ($page - 1) * $this->config->get('config_limit_admin'),
    200 			'limit' => $this->config->get('config_limit_admin')
    201 		);
    202 
    203 		$category_total = $this->model_catalog_category->getTotalCategories();
    204 
    205 		$results = $this->model_catalog_category->getCategories($filter_data);
    206 
    207 		foreach ($results as $result) {
    208 			$data['categories'][] = array(
    209 				'category_id' => $result['category_id'],
    210 				'name'        => $result['name'],
    211 				'sort_order'  => $result['sort_order'],
    212 				'edit'        => $this->url->link('catalog/category/edit', 'user_token=' . $this->session->data['user_token'] . '&category_id=' . $result['category_id'] . $url, true),
    213 				'delete'      => $this->url->link('catalog/category/delete', 'user_token=' . $this->session->data['user_token'] . '&category_id=' . $result['category_id'] . $url, true)
    214 			);
    215 		}
    216 
    217 		if (isset($this->error['warning'])) {
    218 			$data['error_warning'] = $this->error['warning'];
    219 		} else {
    220 			$data['error_warning'] = '';
    221 		}
    222 
    223 		if (isset($this->session->data['success'])) {
    224 			$data['success'] = $this->session->data['success'];
    225 
    226 			unset($this->session->data['success']);
    227 		} else {
    228 			$data['success'] = '';
    229 		}
    230 
    231 		if (isset($this->request->post['selected'])) {
    232 			$data['selected'] = (array)$this->request->post['selected'];
    233 		} else {
    234 			$data['selected'] = array();
    235 		}
    236 
    237 		$url = '';
    238 
    239 		if ($order == 'ASC') {
    240 			$url .= '&order=DESC';
    241 		} else {
    242 			$url .= '&order=ASC';
    243 		}
    244 
    245 		if (isset($this->request->get['page'])) {
    246 			$url .= '&page=' . $this->request->get['page'];
    247 		}
    248 
    249 		$data['sort_name'] = $this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url, true);
    250 		$data['sort_sort_order'] = $this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . '&sort=sort_order' . $url, true);
    251 
    252 		$url = '';
    253 
    254 		if (isset($this->request->get['sort'])) {
    255 			$url .= '&sort=' . $this->request->get['sort'];
    256 		}
    257 
    258 		if (isset($this->request->get['order'])) {
    259 			$url .= '&order=' . $this->request->get['order'];
    260 		}
    261 
    262 		$pagination = new Pagination();
    263 		$pagination->total = $category_total;
    264 		$pagination->page = $page;
    265 		$pagination->limit = $this->config->get('config_limit_admin');
    266 		$pagination->url = $this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    267 
    268 		$data['pagination'] = $pagination->render();
    269 
    270 		$data['results'] = sprintf($this->language->get('text_pagination'), ($category_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($category_total - $this->config->get('config_limit_admin'))) ? $category_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $category_total, ceil($category_total / $this->config->get('config_limit_admin')));
    271 
    272 		$data['sort'] = $sort;
    273 		$data['order'] = $order;
    274 
    275 		$data['header'] = $this->load->controller('common/header');
    276 		$data['column_left'] = $this->load->controller('common/column_left');
    277 		$data['footer'] = $this->load->controller('common/footer');
    278 
    279 		$this->response->setOutput($this->load->view('catalog/category_list', $data));
    280 	}
    281 
    282 	protected function getForm() {
    283 		$data['text_form'] = !isset($this->request->get['category_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
    284 
    285 		if (isset($this->error['warning'])) {
    286 			$data['error_warning'] = $this->error['warning'];
    287 		} else {
    288 			$data['error_warning'] = '';
    289 		}
    290 
    291 		if (isset($this->error['name'])) {
    292 			$data['error_name'] = $this->error['name'];
    293 		} else {
    294 			$data['error_name'] = array();
    295 		}
    296 
    297 		if (isset($this->error['meta_title'])) {
    298 			$data['error_meta_title'] = $this->error['meta_title'];
    299 		} else {
    300 			$data['error_meta_title'] = array();
    301 		}
    302 
    303 		if (isset($this->error['keyword'])) {
    304 			$data['error_keyword'] = $this->error['keyword'];
    305 		} else {
    306 			$data['error_keyword'] = '';
    307 		}
    308 
    309 		if (isset($this->error['parent'])) {
    310 			$data['error_parent'] = $this->error['parent'];
    311 		} else {
    312 			$data['error_parent'] = '';
    313 		}
    314 		
    315 		$url = '';
    316 
    317 		if (isset($this->request->get['sort'])) {
    318 			$url .= '&sort=' . $this->request->get['sort'];
    319 		}
    320 
    321 		if (isset($this->request->get['order'])) {
    322 			$url .= '&order=' . $this->request->get['order'];
    323 		}
    324 
    325 		if (isset($this->request->get['page'])) {
    326 			$url .= '&page=' . $this->request->get['page'];
    327 		}
    328 
    329 		$data['breadcrumbs'] = array();
    330 
    331 		$data['breadcrumbs'][] = array(
    332 			'text' => $this->language->get('text_home'),
    333 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    334 		);
    335 
    336 		$data['breadcrumbs'][] = array(
    337 			'text' => $this->language->get('heading_title'),
    338 			'href' => $this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . $url, true)
    339 		);
    340 
    341 		if (!isset($this->request->get['category_id'])) {
    342 			$data['action'] = $this->url->link('catalog/category/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    343 		} else {
    344 			$data['action'] = $this->url->link('catalog/category/edit', 'user_token=' . $this->session->data['user_token'] . '&category_id=' . $this->request->get['category_id'] . $url, true);
    345 		}
    346 
    347 		$data['cancel'] = $this->url->link('catalog/category', 'user_token=' . $this->session->data['user_token'] . $url, true);
    348 
    349 		if (isset($this->request->get['category_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
    350 			$category_info = $this->model_catalog_category->getCategory($this->request->get['category_id']);
    351 		}
    352 
    353 		$data['user_token'] = $this->session->data['user_token'];
    354 
    355 		$this->load->model('localisation/language');
    356 
    357 		$data['languages'] = $this->model_localisation_language->getLanguages();
    358 
    359 		if (isset($this->request->post['category_description'])) {
    360 			$data['category_description'] = $this->request->post['category_description'];
    361 		} elseif (isset($this->request->get['category_id'])) {
    362 			$data['category_description'] = $this->model_catalog_category->getCategoryDescriptions($this->request->get['category_id']);
    363 		} else {
    364 			$data['category_description'] = array();
    365 		}
    366 
    367 		if (isset($this->request->post['path'])) {
    368 			$data['path'] = $this->request->post['path'];
    369 		} elseif (!empty($category_info)) {
    370 			$data['path'] = $category_info['path'];
    371 		} else {
    372 			$data['path'] = '';
    373 		}
    374 
    375 		if (isset($this->request->post['parent_id'])) {
    376 			$data['parent_id'] = $this->request->post['parent_id'];
    377 		} elseif (!empty($category_info)) {
    378 			$data['parent_id'] = $category_info['parent_id'];
    379 		} else {
    380 			$data['parent_id'] = 0;
    381 		}
    382 
    383 		$this->load->model('catalog/filter');
    384 
    385 		if (isset($this->request->post['category_filter'])) {
    386 			$filters = $this->request->post['category_filter'];
    387 		} elseif (isset($this->request->get['category_id'])) {
    388 			$filters = $this->model_catalog_category->getCategoryFilters($this->request->get['category_id']);
    389 		} else {
    390 			$filters = array();
    391 		}
    392 
    393 		$data['category_filters'] = array();
    394 
    395 		foreach ($filters as $filter_id) {
    396 			$filter_info = $this->model_catalog_filter->getFilter($filter_id);
    397 
    398 			if ($filter_info) {
    399 				$data['category_filters'][] = array(
    400 					'filter_id' => $filter_info['filter_id'],
    401 					'name'      => $filter_info['group'] . ' &gt; ' . $filter_info['name']
    402 				);
    403 			}
    404 		}
    405 
    406 		$this->load->model('setting/store');
    407 
    408 		$data['stores'] = array();
    409 		
    410 		$data['stores'][] = array(
    411 			'store_id' => 0,
    412 			'name'     => $this->language->get('text_default')
    413 		);
    414 		
    415 		$stores = $this->model_setting_store->getStores();
    416 
    417 		foreach ($stores as $store) {
    418 			$data['stores'][] = array(
    419 				'store_id' => $store['store_id'],
    420 				'name'     => $store['name']
    421 			);
    422 		}
    423 
    424 		if (isset($this->request->post['category_store'])) {
    425 			$data['category_store'] = $this->request->post['category_store'];
    426 		} elseif (isset($this->request->get['category_id'])) {
    427 			$data['category_store'] = $this->model_catalog_category->getCategoryStores($this->request->get['category_id']);
    428 		} else {
    429 			$data['category_store'] = array(0);
    430 		}
    431 
    432 		if (isset($this->request->post['image'])) {
    433 			$data['image'] = $this->request->post['image'];
    434 		} elseif (!empty($category_info)) {
    435 			$data['image'] = $category_info['image'];
    436 		} else {
    437 			$data['image'] = '';
    438 		}
    439 
    440 		$this->load->model('tool/image');
    441 
    442 		if (isset($this->request->post['image']) && is_file(DIR_IMAGE . $this->request->post['image'])) {
    443 			$data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
    444 		} elseif (!empty($category_info) && is_file(DIR_IMAGE . $category_info['image'])) {
    445 			$data['thumb'] = $this->model_tool_image->resize($category_info['image'], 100, 100);
    446 		} else {
    447 			$data['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
    448 		}
    449 
    450 		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);
    451 
    452 		if (isset($this->request->post['top'])) {
    453 			$data['top'] = $this->request->post['top'];
    454 		} elseif (!empty($category_info)) {
    455 			$data['top'] = $category_info['top'];
    456 		} else {
    457 			$data['top'] = 0;
    458 		}
    459 
    460 		if (isset($this->request->post['column'])) {
    461 			$data['column'] = $this->request->post['column'];
    462 		} elseif (!empty($category_info)) {
    463 			$data['column'] = $category_info['column'];
    464 		} else {
    465 			$data['column'] = 1;
    466 		}
    467 
    468 		if (isset($this->request->post['sort_order'])) {
    469 			$data['sort_order'] = $this->request->post['sort_order'];
    470 		} elseif (!empty($category_info)) {
    471 			$data['sort_order'] = $category_info['sort_order'];
    472 		} else {
    473 			$data['sort_order'] = 0;
    474 		}
    475 
    476 		if (isset($this->request->post['status'])) {
    477 			$data['status'] = $this->request->post['status'];
    478 		} elseif (!empty($category_info)) {
    479 			$data['status'] = $category_info['status'];
    480 		} else {
    481 			$data['status'] = true;
    482 		}
    483 		
    484 		if (isset($this->request->post['category_seo_url'])) {
    485 			$data['category_seo_url'] = $this->request->post['category_seo_url'];
    486 		} elseif (isset($this->request->get['category_id'])) {
    487 			$data['category_seo_url'] = $this->model_catalog_category->getCategorySeoUrls($this->request->get['category_id']);
    488 		} else {
    489 			$data['category_seo_url'] = array();
    490 		}
    491 				
    492 		if (isset($this->request->post['category_layout'])) {
    493 			$data['category_layout'] = $this->request->post['category_layout'];
    494 		} elseif (isset($this->request->get['category_id'])) {
    495 			$data['category_layout'] = $this->model_catalog_category->getCategoryLayouts($this->request->get['category_id']);
    496 		} else {
    497 			$data['category_layout'] = array();
    498 		}
    499 
    500 		$this->load->model('design/layout');
    501 
    502 		$data['layouts'] = $this->model_design_layout->getLayouts();
    503 
    504 		$data['header'] = $this->load->controller('common/header');
    505 		$data['column_left'] = $this->load->controller('common/column_left');
    506 		$data['footer'] = $this->load->controller('common/footer');
    507 
    508 		$this->response->setOutput($this->load->view('catalog/category_form', $data));
    509 	}
    510 
    511 	protected function validateForm() {
    512 		if (!$this->user->hasPermission('modify', 'catalog/category')) {
    513 			$this->error['warning'] = $this->language->get('error_permission');
    514 		}
    515 
    516 		foreach ($this->request->post['category_description'] as $language_id => $value) {
    517 			if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 255)) {
    518 				$this->error['name'][$language_id] = $this->language->get('error_name');
    519 			}
    520 
    521 			if ((utf8_strlen($value['meta_title']) < 1) || (utf8_strlen($value['meta_title']) > 255)) {
    522 				$this->error['meta_title'][$language_id] = $this->language->get('error_meta_title');
    523 			}
    524 		}
    525 
    526 		if (isset($this->request->get['category_id']) && $this->request->post['parent_id']) {
    527 			$results = $this->model_catalog_category->getCategoryPath($this->request->post['parent_id']);
    528 			
    529 			foreach ($results as $result) {
    530 				if ($result['path_id'] == $this->request->get['category_id']) {
    531 					$this->error['parent'] = $this->language->get('error_parent');
    532 					
    533 					break;
    534 				}
    535 			}
    536 		}
    537 
    538 		if ($this->request->post['category_seo_url']) {
    539 			$this->load->model('design/seo_url');
    540 			
    541 			foreach ($this->request->post['category_seo_url'] as $store_id => $language) {
    542 				foreach ($language as $language_id => $keyword) {
    543 					if (!empty($keyword)) {
    544 						if (count(array_keys($language, $keyword)) > 1) {
    545 							$this->error['keyword'][$store_id][$language_id] = $this->language->get('error_unique');
    546 						}
    547 
    548 						$seo_urls = $this->model_design_seo_url->getSeoUrlsByKeyword($keyword);
    549 	
    550 						foreach ($seo_urls as $seo_url) {
    551 							if (($seo_url['store_id'] == $store_id) && (!isset($this->request->get['category_id']) || ($seo_url['query'] != 'category_id=' . $this->request->get['category_id']))) {		
    552 								$this->error['keyword'][$store_id][$language_id] = $this->language->get('error_keyword');
    553 				
    554 								break;
    555 							}
    556 						}
    557 					}
    558 				}
    559 			}
    560 		}
    561 		
    562 		if ($this->error && !isset($this->error['warning'])) {
    563 			$this->error['warning'] = $this->language->get('error_warning');
    564 		}
    565 		
    566 		return !$this->error;
    567 	}
    568 
    569 	protected function validateDelete() {
    570 		if (!$this->user->hasPermission('modify', 'catalog/category')) {
    571 			$this->error['warning'] = $this->language->get('error_permission');
    572 		}
    573 
    574 		return !$this->error;
    575 	}
    576 
    577 	protected function validateRepair() {
    578 		if (!$this->user->hasPermission('modify', 'catalog/category')) {
    579 			$this->error['warning'] = $this->language->get('error_permission');
    580 		}
    581 
    582 		return !$this->error;
    583 	}
    584 
    585 	public function autocomplete() {
    586 		$json = array();
    587 
    588 		if (isset($this->request->get['filter_name'])) {
    589 			$this->load->model('catalog/category');
    590 
    591 			$filter_data = array(
    592 				'filter_name' => $this->request->get['filter_name'],
    593 				'sort'        => 'name',
    594 				'order'       => 'ASC',
    595 				'start'       => 0,
    596 				'limit'       => 5
    597 			);
    598 
    599 			$results = $this->model_catalog_category->getCategories($filter_data);
    600 
    601 			foreach ($results as $result) {
    602 				$json[] = array(
    603 					'category_id' => $result['category_id'],
    604 					'name'        => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
    605 				);
    606 			}
    607 		}
    608 
    609 		$sort_order = array();
    610 
    611 		foreach ($json as $key => $value) {
    612 			$sort_order[$key] = $value['name'];
    613 		}
    614 
    615 		array_multisort($sort_order, SORT_ASC, $json);
    616 
    617 		$this->response->addHeader('Content-Type: application/json');
    618 		$this->response->setOutput(json_encode($json));
    619 	}
    620 }