shop.balmet.com

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

coupon.php (18923B)


      1 <?php
      2 class ControllerMarketingCoupon extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('marketing/coupon');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('marketing/coupon');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('marketing/coupon');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('marketing/coupon');
     21 
     22 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     23 			$this->model_marketing_coupon->addCoupon($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('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url, true));
     42 		}
     43 
     44 		$this->getForm();
     45 	}
     46 
     47 	public function edit() {
     48 		$this->load->language('marketing/coupon');
     49 
     50 		$this->document->setTitle($this->language->get('heading_title'));
     51 
     52 		$this->load->model('marketing/coupon');
     53 
     54 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     55 			$this->model_marketing_coupon->editCoupon($this->request->get['coupon_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('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url, true));
     74 		}
     75 
     76 		$this->getForm();
     77 	}
     78 
     79 	public function delete() {
     80 		$this->load->language('marketing/coupon');
     81 
     82 		$this->document->setTitle($this->language->get('heading_title'));
     83 
     84 		$this->load->model('marketing/coupon');
     85 
     86 		if (isset($this->request->post['selected']) && $this->validateDelete()) {
     87 			foreach ($this->request->post['selected'] as $coupon_id) {
     88 				$this->model_marketing_coupon->deleteCoupon($coupon_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('marketing/coupon', '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 = '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('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url, true)
    156 		);
    157 
    158 		$data['add'] = $this->url->link('marketing/coupon/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    159 		$data['delete'] = $this->url->link('marketing/coupon/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
    160 
    161 		$data['coupons'] = 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 		$coupon_total = $this->model_marketing_coupon->getTotalCoupons();
    171 
    172 		$results = $this->model_marketing_coupon->getCoupons($filter_data);
    173 
    174 		foreach ($results as $result) {
    175 			$data['coupons'][] = array(
    176 				'coupon_id'  => $result['coupon_id'],
    177 				'name'       => $result['name'],
    178 				'code'       => $result['code'],
    179 				'discount'   => $result['discount'],
    180 				'date_start' => date($this->language->get('date_format_short'), strtotime($result['date_start'])),
    181 				'date_end'   => date($this->language->get('date_format_short'), strtotime($result['date_end'])),
    182 				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
    183 				'edit'       => $this->url->link('marketing/coupon/edit', 'user_token=' . $this->session->data['user_token'] . '&coupon_id=' . $result['coupon_id'] . $url, true)
    184 			);
    185 		}
    186 
    187 		if (isset($this->error['warning'])) {
    188 			$data['error_warning'] = $this->error['warning'];
    189 		} else {
    190 			$data['error_warning'] = '';
    191 		}
    192 
    193 		if (isset($this->session->data['success'])) {
    194 			$data['success'] = $this->session->data['success'];
    195 
    196 			unset($this->session->data['success']);
    197 		} else {
    198 			$data['success'] = '';
    199 		}
    200 
    201 		if (isset($this->request->post['selected'])) {
    202 			$data['selected'] = (array)$this->request->post['selected'];
    203 		} else {
    204 			$data['selected'] = array();
    205 		}
    206 
    207 		$url = '';
    208 
    209 		if ($order == 'ASC') {
    210 			$url .= '&order=DESC';
    211 		} else {
    212 			$url .= '&order=ASC';
    213 		}
    214 
    215 		if (isset($this->request->get['page'])) {
    216 			$url .= '&page=' . $this->request->get['page'];
    217 		}
    218 
    219 		$data['sort_name'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url, true);
    220 		$data['sort_code'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . '&sort=code' . $url, true);
    221 		$data['sort_discount'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . '&sort=discount' . $url, true);
    222 		$data['sort_date_start'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . '&sort=date_start' . $url, true);
    223 		$data['sort_date_end'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . '&sort=date_end' . $url, true);
    224 		$data['sort_status'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . '&sort=status' . $url, true);
    225 
    226 		$url = '';
    227 
    228 		if (isset($this->request->get['sort'])) {
    229 			$url .= '&sort=' . $this->request->get['sort'];
    230 		}
    231 
    232 		if (isset($this->request->get['order'])) {
    233 			$url .= '&order=' . $this->request->get['order'];
    234 		}
    235 
    236 		$pagination = new Pagination();
    237 		$pagination->total = $coupon_total;
    238 		$pagination->page = $page;
    239 		$pagination->limit = $this->config->get('config_limit_admin');
    240 		$pagination->url = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    241 
    242 		$data['pagination'] = $pagination->render();
    243 
    244 		$data['results'] = sprintf($this->language->get('text_pagination'), ($coupon_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($coupon_total - $this->config->get('config_limit_admin'))) ? $coupon_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $coupon_total, ceil($coupon_total / $this->config->get('config_limit_admin')));
    245 
    246 		$data['sort'] = $sort;
    247 		$data['order'] = $order;
    248 
    249 		$data['header'] = $this->load->controller('common/header');
    250 		$data['column_left'] = $this->load->controller('common/column_left');
    251 		$data['footer'] = $this->load->controller('common/footer');
    252 
    253 		$this->response->setOutput($this->load->view('marketing/coupon_list', $data));
    254 	}
    255 
    256 	protected function getForm() {
    257 		$data['text_form'] = !isset($this->request->get['coupon_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
    258 
    259 		$data['user_token'] = $this->session->data['user_token'];
    260 
    261 		if (isset($this->request->get['coupon_id'])) {
    262 			$data['coupon_id'] = $this->request->get['coupon_id'];
    263 		} else {
    264 			$data['coupon_id'] = 0;
    265 		}
    266 
    267 		if (isset($this->error['warning'])) {
    268 			$data['error_warning'] = $this->error['warning'];
    269 		} else {
    270 			$data['error_warning'] = '';
    271 		}
    272 
    273 		if (isset($this->error['name'])) {
    274 			$data['error_name'] = $this->error['name'];
    275 		} else {
    276 			$data['error_name'] = '';
    277 		}
    278 
    279 		if (isset($this->error['code'])) {
    280 			$data['error_code'] = $this->error['code'];
    281 		} else {
    282 			$data['error_code'] = '';
    283 		}
    284 
    285 		if (isset($this->error['date_start'])) {
    286 			$data['error_date_start'] = $this->error['date_start'];
    287 		} else {
    288 			$data['error_date_start'] = '';
    289 		}
    290 
    291 		if (isset($this->error['date_end'])) {
    292 			$data['error_date_end'] = $this->error['date_end'];
    293 		} else {
    294 			$data['error_date_end'] = '';
    295 		}
    296 
    297 		$url = '';
    298 
    299 		if (isset($this->request->get['page'])) {
    300 			$url .= '&page=' . $this->request->get['page'];
    301 		}
    302 
    303 		if (isset($this->request->get['sort'])) {
    304 			$url .= '&sort=' . $this->request->get['sort'];
    305 		}
    306 
    307 		if (isset($this->request->get['order'])) {
    308 			$url .= '&order=' . $this->request->get['order'];
    309 		}
    310 
    311 		$data['breadcrumbs'] = array();
    312 
    313 		$data['breadcrumbs'][] = array(
    314 			'text' => $this->language->get('text_home'),
    315 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    316 		);
    317 
    318 		$data['breadcrumbs'][] = array(
    319 			'text' => $this->language->get('heading_title'),
    320 			'href' => $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url, true)
    321 		);
    322 
    323 		if (!isset($this->request->get['coupon_id'])) {
    324 			$data['action'] = $this->url->link('marketing/coupon/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    325 		} else {
    326 			$data['action'] = $this->url->link('marketing/coupon/edit', 'user_token=' . $this->session->data['user_token'] . '&coupon_id=' . $this->request->get['coupon_id'] . $url, true);
    327 		}
    328 
    329 		$data['cancel'] = $this->url->link('marketing/coupon', 'user_token=' . $this->session->data['user_token'] . $url, true);
    330 
    331 		if (isset($this->request->get['coupon_id']) && (!$this->request->server['REQUEST_METHOD'] != 'POST')) {
    332 			$coupon_info = $this->model_marketing_coupon->getCoupon($this->request->get['coupon_id']);
    333 		}
    334 
    335 		if (isset($this->request->post['name'])) {
    336 			$data['name'] = $this->request->post['name'];
    337 		} elseif (!empty($coupon_info)) {
    338 			$data['name'] = $coupon_info['name'];
    339 		} else {
    340 			$data['name'] = '';
    341 		}
    342 
    343 		if (isset($this->request->post['code'])) {
    344 			$data['code'] = $this->request->post['code'];
    345 		} elseif (!empty($coupon_info)) {
    346 			$data['code'] = $coupon_info['code'];
    347 		} else {
    348 			$data['code'] = '';
    349 		}
    350 
    351 		if (isset($this->request->post['type'])) {
    352 			$data['type'] = $this->request->post['type'];
    353 		} elseif (!empty($coupon_info)) {
    354 			$data['type'] = $coupon_info['type'];
    355 		} else {
    356 			$data['type'] = '';
    357 		}
    358 
    359 		if (isset($this->request->post['discount'])) {
    360 			$data['discount'] = $this->request->post['discount'];
    361 		} elseif (!empty($coupon_info)) {
    362 			$data['discount'] = $coupon_info['discount'];
    363 		} else {
    364 			$data['discount'] = '';
    365 		}
    366 
    367 		if (isset($this->request->post['logged'])) {
    368 			$data['logged'] = $this->request->post['logged'];
    369 		} elseif (!empty($coupon_info)) {
    370 			$data['logged'] = $coupon_info['logged'];
    371 		} else {
    372 			$data['logged'] = '';
    373 		}
    374 
    375 		if (isset($this->request->post['shipping'])) {
    376 			$data['shipping'] = $this->request->post['shipping'];
    377 		} elseif (!empty($coupon_info)) {
    378 			$data['shipping'] = $coupon_info['shipping'];
    379 		} else {
    380 			$data['shipping'] = '';
    381 		}
    382 
    383 		if (isset($this->request->post['total'])) {
    384 			$data['total'] = $this->request->post['total'];
    385 		} elseif (!empty($coupon_info)) {
    386 			$data['total'] = $coupon_info['total'];
    387 		} else {
    388 			$data['total'] = '';
    389 		}
    390 
    391 		if (isset($this->request->post['coupon_product'])) {
    392 			$products = $this->request->post['coupon_product'];
    393 		} elseif (isset($this->request->get['coupon_id'])) {
    394 			$products = $this->model_marketing_coupon->getCouponProducts($this->request->get['coupon_id']);
    395 		} else {
    396 			$products = array();
    397 		}
    398 
    399 		$this->load->model('catalog/product');
    400 
    401 		$data['coupon_product'] = array();
    402 
    403 		foreach ($products as $product_id) {
    404 			$product_info = $this->model_catalog_product->getProduct($product_id);
    405 
    406 			if ($product_info) {
    407 				$data['coupon_product'][] = array(
    408 					'product_id' => $product_info['product_id'],
    409 					'name'       => $product_info['name']
    410 				);
    411 			}
    412 		}
    413 
    414 		if (isset($this->request->post['coupon_category'])) {
    415 			$categories = $this->request->post['coupon_category'];
    416 		} elseif (isset($this->request->get['coupon_id'])) {
    417 			$categories = $this->model_marketing_coupon->getCouponCategories($this->request->get['coupon_id']);
    418 		} else {
    419 			$categories = array();
    420 		}
    421 
    422 		$this->load->model('catalog/category');
    423 
    424 		$data['coupon_category'] = array();
    425 
    426 		foreach ($categories as $category_id) {
    427 			$category_info = $this->model_catalog_category->getCategory($category_id);
    428 
    429 			if ($category_info) {
    430 				$data['coupon_category'][] = array(
    431 					'category_id' => $category_info['category_id'],
    432 					'name'        => ($category_info['path'] ? $category_info['path'] . ' &gt; ' : '') . $category_info['name']
    433 				);
    434 			}
    435 		}
    436 
    437 		if (isset($this->request->post['date_start'])) {
    438 			$data['date_start'] = $this->request->post['date_start'];
    439 		} elseif (!empty($coupon_info)) {
    440 			$data['date_start'] = ($coupon_info['date_start'] != '0000-00-00' ? $coupon_info['date_start'] : '');
    441 		} else {
    442 			$data['date_start'] = date('Y-m-d', time());
    443 		}
    444 
    445 		if (isset($this->request->post['date_end'])) {
    446 			$data['date_end'] = $this->request->post['date_end'];
    447 		} elseif (!empty($coupon_info)) {
    448 			$data['date_end'] = ($coupon_info['date_end'] != '0000-00-00' ? $coupon_info['date_end'] : '');
    449 		} else {
    450 			$data['date_end'] = date('Y-m-d', strtotime('+1 month'));
    451 		}
    452 
    453 		if (isset($this->request->post['uses_total'])) {
    454 			$data['uses_total'] = $this->request->post['uses_total'];
    455 		} elseif (!empty($coupon_info)) {
    456 			$data['uses_total'] = $coupon_info['uses_total'];
    457 		} else {
    458 			$data['uses_total'] = 1;
    459 		}
    460 
    461 		if (isset($this->request->post['uses_customer'])) {
    462 			$data['uses_customer'] = $this->request->post['uses_customer'];
    463 		} elseif (!empty($coupon_info)) {
    464 			$data['uses_customer'] = $coupon_info['uses_customer'];
    465 		} else {
    466 			$data['uses_customer'] = 1;
    467 		}
    468 
    469 		if (isset($this->request->post['status'])) {
    470 			$data['status'] = $this->request->post['status'];
    471 		} elseif (!empty($coupon_info)) {
    472 			$data['status'] = $coupon_info['status'];
    473 		} else {
    474 			$data['status'] = true;
    475 		}
    476 
    477 		$data['header'] = $this->load->controller('common/header');
    478 		$data['column_left'] = $this->load->controller('common/column_left');
    479 		$data['footer'] = $this->load->controller('common/footer');
    480 
    481 		$this->response->setOutput($this->load->view('marketing/coupon_form', $data));
    482 	}
    483 
    484 	protected function validateForm() {
    485 		if (!$this->user->hasPermission('modify', 'marketing/coupon')) {
    486 			$this->error['warning'] = $this->language->get('error_permission');
    487 		}
    488 
    489 		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 128)) {
    490 			$this->error['name'] = $this->language->get('error_name');
    491 		}
    492 
    493 		if ((utf8_strlen($this->request->post['code']) < 3) || (utf8_strlen($this->request->post['code']) > 10)) {
    494 			$this->error['code'] = $this->language->get('error_code');
    495 		}
    496 
    497 		$coupon_info = $this->model_marketing_coupon->getCouponByCode($this->request->post['code']);
    498 
    499 		if ($coupon_info) {
    500 			if (!isset($this->request->get['coupon_id'])) {
    501 				$this->error['warning'] = $this->language->get('error_exists');
    502 			} elseif ($coupon_info['coupon_id'] != $this->request->get['coupon_id']) {
    503 				$this->error['warning'] = $this->language->get('error_exists');
    504 			}
    505 		}
    506 
    507 		return !$this->error;
    508 	}
    509 
    510 	protected function validateDelete() {
    511 		if (!$this->user->hasPermission('modify', 'marketing/coupon')) {
    512 			$this->error['warning'] = $this->language->get('error_permission');
    513 		}
    514 
    515 		return !$this->error;
    516 	}
    517 
    518 	public function history() {
    519 		$this->load->language('marketing/coupon');
    520 
    521 		$this->load->model('marketing/coupon');
    522 
    523 		if (isset($this->request->get['page'])) {
    524 			$page = $this->request->get['page'];
    525 		} else {
    526 			$page = 1;
    527 		}
    528 
    529 		$data['histories'] = array();
    530 
    531 		$results = $this->model_marketing_coupon->getCouponHistories($this->request->get['coupon_id'], ($page - 1) * 10, 10);
    532 
    533 		foreach ($results as $result) {
    534 			$data['histories'][] = array(
    535 				'order_id'   => $result['order_id'],
    536 				'customer'   => $result['customer'],
    537 				'amount'     => $result['amount'],
    538 				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
    539 			);
    540 		}
    541 
    542 		$history_total = $this->model_marketing_coupon->getTotalCouponHistories($this->request->get['coupon_id']);
    543 
    544 		$pagination = new Pagination();
    545 		$pagination->total = $history_total;
    546 		$pagination->page = $page;
    547 		$pagination->limit = 10;
    548 		$pagination->url = $this->url->link('marketing/coupon/history', 'user_token=' . $this->session->data['user_token'] . '&coupon_id=' . $this->request->get['coupon_id'] . '&page={page}', true);
    549 
    550 		$data['pagination'] = $pagination->render();
    551 
    552 		$data['results'] = sprintf('Showing %d to %d of %d (%d Pages)', ($history_total) ? (($page - 1) * 10) + 1 : 0, ((($page - 1) * 10) > ($history_total - 10)) ? $history_total : ((($page - 1) * 10) + 10), $history_total, ceil($history_total / 10));
    553 
    554 		$this->response->setOutput($this->load->view('marketing/coupon_history', $data));
    555 	}
    556 }