shop.balmet.com

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

currency.php (15599B)


      1 <?php
      2 class ControllerLocalisationCurrency extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('localisation/currency');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('localisation/currency');
     11 
     12 		$this->getList();
     13 	}
     14 
     15 	public function add() {
     16 		$this->load->language('localisation/currency');
     17 
     18 		$this->document->setTitle($this->language->get('heading_title'));
     19 
     20 		$this->load->model('localisation/currency');
     21 
     22 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     23 			$this->model_localisation_currency->addCurrency($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('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url, true));
     42 		}
     43 
     44 		$this->getForm();
     45 	}
     46 
     47 	public function edit() {
     48 		$this->load->language('localisation/currency');
     49 
     50 		$this->document->setTitle($this->language->get('heading_title'));
     51 
     52 		$this->load->model('localisation/currency');
     53 
     54 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
     55 			$this->model_localisation_currency->editCurrency($this->request->get['currency_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('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url, true));
     74 		}
     75 
     76 		$this->getForm();
     77 	}
     78 
     79 	public function delete() {
     80 		$this->load->language('localisation/currency');
     81 
     82 		$this->document->setTitle($this->language->get('heading_title'));
     83 
     84 		$this->load->model('localisation/currency');
     85 
     86 		if (isset($this->request->post['selected']) && $this->validateDelete()) {
     87 			foreach ($this->request->post['selected'] as $currency_id) {
     88 				$this->model_localisation_currency->deleteCurrency($currency_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('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url, true));
    108 		}
    109 
    110 		$this->getList();
    111 	}
    112 
    113 	public function refresh() {
    114 		$this->load->language('localisation/currency');
    115 
    116 		$this->document->setTitle($this->language->get('heading_title'));
    117 
    118 		$this->load->model('localisation/currency');
    119 
    120 		if ($this->validateRefresh()) {
    121 			$this->model_localisation_currency->refresh(true);
    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('localisation/currency', '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 = 'title';
    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('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url, true)
    188 		);
    189 
    190 		$data['add'] = $this->url->link('localisation/currency/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    191 		$data['delete'] = $this->url->link('localisation/currency/delete', 'user_token=' . $this->session->data['user_token'] . $url, true);
    192 		$data['refresh'] = $this->url->link('localisation/currency/refresh', 'user_token=' . $this->session->data['user_token'] . $url, true);
    193 
    194 		$data['currencies'] = 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 		$currency_total = $this->model_localisation_currency->getTotalCurrencies();
    204 
    205 		$results = $this->model_localisation_currency->getCurrencies($filter_data);
    206 
    207 		foreach ($results as $result) {
    208 			$data['currencies'][] = array(
    209 				'currency_id'   => $result['currency_id'],
    210 				'title'         => $result['title'] . (($result['code'] == $this->config->get('config_currency')) ? $this->language->get('text_default') : null),
    211 				'code'          => $result['code'],
    212 				'value'         => $result['value'],
    213 				'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
    214 				'edit'          => $this->url->link('localisation/currency/edit', 'user_token=' . $this->session->data['user_token'] . '&currency_id=' . $result['currency_id'] . $url, true)
    215 			);
    216 		}
    217 
    218 		if (isset($this->error['warning'])) {
    219 			$data['error_warning'] = $this->error['warning'];
    220 		} else {
    221 			$data['error_warning'] = '';
    222 		}
    223 
    224 		if (isset($this->session->data['success'])) {
    225 			$data['success'] = $this->session->data['success'];
    226 
    227 			unset($this->session->data['success']);
    228 		} else {
    229 			$data['success'] = '';
    230 		}
    231 
    232 		if (isset($this->request->post['selected'])) {
    233 			$data['selected'] = (array)$this->request->post['selected'];
    234 		} else {
    235 			$data['selected'] = array();
    236 		}
    237 
    238 		$url = '';
    239 
    240 		if ($order == 'ASC') {
    241 			$url .= '&order=DESC';
    242 		} else {
    243 			$url .= '&order=ASC';
    244 		}
    245 
    246 		if (isset($this->request->get['page'])) {
    247 			$url .= '&page=' . $this->request->get['page'];
    248 		}
    249 
    250 		$data['sort_title'] = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url, true);
    251 		$data['sort_code'] = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . '&sort=code' . $url, true);
    252 		$data['sort_value'] = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $url, true);
    253 		$data['sort_date_modified'] = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . '&sort=date_modified' . $url, true);
    254 
    255 		$url = '';
    256 
    257 		if (isset($this->request->get['sort'])) {
    258 			$url .= '&sort=' . $this->request->get['sort'];
    259 		}
    260 
    261 		if (isset($this->request->get['order'])) {
    262 			$url .= '&order=' . $this->request->get['order'];
    263 		}
    264 
    265 		$pagination = new Pagination();
    266 		$pagination->total = $currency_total;
    267 		$pagination->page = $page;
    268 		$pagination->limit = $this->config->get('config_limit_admin');
    269 		$pagination->url = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', true);
    270 
    271 		$data['pagination'] = $pagination->render();
    272 
    273 		$data['results'] = sprintf($this->language->get('text_pagination'), ($currency_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($currency_total - $this->config->get('config_limit_admin'))) ? $currency_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $currency_total, ceil($currency_total / $this->config->get('config_limit_admin')));
    274 
    275 		$data['sort'] = $sort;
    276 		$data['order'] = $order;
    277 
    278 		$data['header'] = $this->load->controller('common/header');
    279 		$data['column_left'] = $this->load->controller('common/column_left');
    280 		$data['footer'] = $this->load->controller('common/footer');
    281 
    282 		$this->response->setOutput($this->load->view('localisation/currency_list', $data));
    283 	}
    284 
    285 	protected function getForm() {
    286 		$data['text_form'] = !isset($this->request->get['currency_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
    287 
    288 		if (isset($this->error['warning'])) {
    289 			$data['error_warning'] = $this->error['warning'];
    290 		} else {
    291 			$data['error_warning'] = '';
    292 		}
    293 
    294 		if (isset($this->error['title'])) {
    295 			$data['error_title'] = $this->error['title'];
    296 		} else {
    297 			$data['error_title'] = '';
    298 		}
    299 
    300 		if (isset($this->error['code'])) {
    301 			$data['error_code'] = $this->error['code'];
    302 		} else {
    303 			$data['error_code'] = '';
    304 		}
    305 
    306 		$url = '';
    307 
    308 		if (isset($this->request->get['sort'])) {
    309 			$url .= '&sort=' . $this->request->get['sort'];
    310 		}
    311 
    312 		if (isset($this->request->get['order'])) {
    313 			$url .= '&order=' . $this->request->get['order'];
    314 		}
    315 
    316 		if (isset($this->request->get['page'])) {
    317 			$url .= '&page=' . $this->request->get['page'];
    318 		}
    319 
    320 		$data['breadcrumbs'] = array();
    321 
    322 		$data['breadcrumbs'][] = array(
    323 			'text' => $this->language->get('text_home'),
    324 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    325 		);
    326 
    327 		$data['breadcrumbs'][] = array(
    328 			'text' => $this->language->get('heading_title'),
    329 			'href' => $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url, true)
    330 		);
    331 
    332 		if (!isset($this->request->get['currency_id'])) {
    333 			$data['action'] = $this->url->link('localisation/currency/add', 'user_token=' . $this->session->data['user_token'] . $url, true);
    334 		} else {
    335 			$data['action'] = $this->url->link('localisation/currency/edit', 'user_token=' . $this->session->data['user_token'] . '&currency_id=' . $this->request->get['currency_id'] . $url, true);
    336 		}
    337 
    338 		$data['cancel'] = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url, true);
    339 
    340 		if (isset($this->request->get['currency_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
    341 			$currency_info = $this->model_localisation_currency->getCurrency($this->request->get['currency_id']);
    342 		}
    343 
    344 		if (isset($this->request->post['title'])) {
    345 			$data['title'] = $this->request->post['title'];
    346 		} elseif (!empty($currency_info)) {
    347 			$data['title'] = $currency_info['title'];
    348 		} else {
    349 			$data['title'] = '';
    350 		}
    351 
    352 		if (isset($this->request->post['code'])) {
    353 			$data['code'] = $this->request->post['code'];
    354 		} elseif (!empty($currency_info)) {
    355 			$data['code'] = $currency_info['code'];
    356 		} else {
    357 			$data['code'] = '';
    358 		}
    359 
    360 		if (isset($this->request->post['symbol_left'])) {
    361 			$data['symbol_left'] = $this->request->post['symbol_left'];
    362 		} elseif (!empty($currency_info)) {
    363 			$data['symbol_left'] = $currency_info['symbol_left'];
    364 		} else {
    365 			$data['symbol_left'] = '';
    366 		}
    367 
    368 		if (isset($this->request->post['symbol_right'])) {
    369 			$data['symbol_right'] = $this->request->post['symbol_right'];
    370 		} elseif (!empty($currency_info)) {
    371 			$data['symbol_right'] = $currency_info['symbol_right'];
    372 		} else {
    373 			$data['symbol_right'] = '';
    374 		}
    375 
    376 		if (isset($this->request->post['decimal_place'])) {
    377 			$data['decimal_place'] = $this->request->post['decimal_place'];
    378 		} elseif (!empty($currency_info)) {
    379 			$data['decimal_place'] = $currency_info['decimal_place'];
    380 		} else {
    381 			$data['decimal_place'] = '';
    382 		}
    383 
    384 		if (isset($this->request->post['value'])) {
    385 			$data['value'] = $this->request->post['value'];
    386 		} elseif (!empty($currency_info)) {
    387 			$data['value'] = $currency_info['value'];
    388 		} else {
    389 			$data['value'] = '';
    390 		}
    391 
    392 		if (isset($this->request->post['status'])) {
    393 			$data['status'] = $this->request->post['status'];
    394 		} elseif (!empty($currency_info)) {
    395 			$data['status'] = $currency_info['status'];
    396 		} else {
    397 			$data['status'] = '';
    398 		}
    399 
    400 		$data['header'] = $this->load->controller('common/header');
    401 		$data['column_left'] = $this->load->controller('common/column_left');
    402 		$data['footer'] = $this->load->controller('common/footer');
    403 
    404 		$this->response->setOutput($this->load->view('localisation/currency_form', $data));
    405 	}
    406 
    407 	protected function validateForm() {
    408 		if (!$this->user->hasPermission('modify', 'localisation/currency')) {
    409 			$this->error['warning'] = $this->language->get('error_permission');
    410 		}
    411 
    412 		if ((utf8_strlen($this->request->post['title']) < 3) || (utf8_strlen($this->request->post['title']) > 32)) {
    413 			$this->error['title'] = $this->language->get('error_title');
    414 		}
    415 
    416 		if (utf8_strlen($this->request->post['code']) != 3) {
    417 			$this->error['code'] = $this->language->get('error_code');
    418 		}
    419 
    420 		return !$this->error;
    421 	}
    422 
    423 	protected function validateDelete() {
    424 		if (!$this->user->hasPermission('modify', 'localisation/currency')) {
    425 			$this->error['warning'] = $this->language->get('error_permission');
    426 		}
    427 
    428 		$this->load->model('setting/store');
    429 		$this->load->model('sale/order');
    430 
    431 		foreach ($this->request->post['selected'] as $currency_id) {
    432 			$currency_info = $this->model_localisation_currency->getCurrency($currency_id);
    433 
    434 			if ($currency_info) {
    435 				if ($this->config->get('config_currency') == $currency_info['code']) {
    436 					$this->error['warning'] = $this->language->get('error_default');
    437 				}
    438 
    439 				$store_total = $this->model_setting_store->getTotalStoresByCurrency($currency_info['code']);
    440 
    441 				if ($store_total) {
    442 					$this->error['warning'] = sprintf($this->language->get('error_store'), $store_total);
    443 				}
    444 			}
    445 
    446 			$order_total = $this->model_sale_order->getTotalOrdersByCurrencyId($currency_id);
    447 
    448 			if ($order_total) {
    449 				$this->error['warning'] = sprintf($this->language->get('error_order'), $order_total);
    450 			}
    451 		}
    452 
    453 		return !$this->error;
    454 	}
    455 
    456 	protected function validateRefresh() {
    457 		if (!$this->user->hasPermission('modify', 'localisation/currency')) {
    458 			$this->error['warning'] = $this->language->get('error_permission');
    459 		}
    460 
    461 		return !$this->error;
    462 	}
    463 }