<?php
class ControllerCommonLanguage extends Controller {
	public function index() {
		$this->load->language('common/language');

		$data['text_language'] = $this->language->get('text_language');

		$data['action'] = $this->url->link('common/language/language', '', $this->request->server['HTTPS']);

		$data['code'] = $this->session->data['language'];

		$data['languages'] = array();

		$results = $this->model_localisation_language->getLanguages();

		$this->load->model('localisation/language');

		if (isset($this->request->get['route_fithout_lang'])){
			$route_fithout_lang = urldecode(urldecode($this->request->get['route_fithout_lang']));
		}else{
			$route_fithout_lang = '';
		}

		foreach ($results as $result) {
			if ($result['status']) {
				$data['languages'][] = array(
					'name' => $result['name'],
					'code' => $result['code'],
					'href' => HTTP_SERVER . strtolower($result['name']) . '/' . $route_fithout_lang
				);
			}
		}

		return $this->load->view('common/language', $data);
	}

	public function language() {
		if (isset($this->request->get['code'])) {
			$this->session->data['language'] = $this->request->get['code'];
		}
		if (isset($this->request->get['redirect'])) {
			$this->response->redirect($this->request->get['redirect']);
		} else {
			$this->response->redirect($this->url->link('common/home'));
		}
	}
}