<?php
///////////////////////////////////////////////////////////////////////////////////////
// opencart1.4.9【日本語版】?
// opencart.jp
// japan_post.php
// トークン対応版です
// (admin/controller/shipping)
// ver 1.0.2 Coding By sae
// GPL ver 3
///////////////////////////////////////////////////////////////////////////////////////
class ControllerShippingJapanPost extends Controller {
	private $error = array();

	public function index() {

		$this->load->language('shipping/japan_post');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model( 'setting/setting' );

		$this->load->model('shipping/japan_post');

		$this->data['select_zone'] = $this->model_shipping_japan_post->getZone(107);

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {

			$this->model_setting_setting->editSetting('japan_post', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/shipping&token=' . $this->session->data['token']);
		}

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

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_shipping'] = $this->language->get('text_shipping');
		$this->data['text_exe'] = $this->language->get('text_exe');
		$this->data['text_stop'] = $this->language->get('text_stop');

		$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_time'] = $this->language->get('entry_time');
		$this->data['entry_district'] = $this->language->get('entry_district');
		$this->data['hokkaido'] = $this->language->get('hokkaido');
		$this->data['touhoku'] = $this->language->get('touhoku');
		$this->data['kantou'] = $this->language->get('kantou');
		$this->data['shinetsu'] = $this->language->get('shinetsu');
		$this->data['hokuriku'] = $this->language->get('hokuriku');
		$this->data['toukai'] = $this->language->get('toukai');
		$this->data['kinki'] = $this->language->get('kinki');
		$this->data['chugoku'] = $this->language->get('chugoku');
		$this->data['shikoku'] = $this->language->get('shikoku');
		$this->data['kyusyu'] = $this->language->get('kyusyu');
		$this->data['okinawa'] = $this->language->get('okinawa');
		$this->data['zenkoku'] = $this->language->get('zenkoku');
		$this->data['entry_evenly'] = $this->language->get('entry_evenly');
		$this->data['entry_purchase'] = $this->language->get('entry_purchase');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['selected_default'] = $this->language->get('selected_default');
		$this->data['entry_district_free'] = $this->language->get('entry_district_free');
		$this->data['entry_evenly_free'] = $this->language->get('entry_evenly_free');

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

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

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=extension/shipping&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_shipping'),
      		'separator' => ' :: '
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=shipping/japan_post&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);

		$this->data['action'] = HTTPS_SERVER . 'index.php?route=shipping/japan_post&token=' . $this->session->data['token'];

		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=extension/shipping&token=' . $this->session->data['token'];

		if (isset($this->request->post['japan_post_zone'])) {
			$this->data['japan_post_zone'] = $this->request->post['japan_post_zone'];
		} else {
			$this->data['japan_post_zone'] = $this->config->get('japan_post_zone');
		}

		if (isset($this->request->post['japan_post_price'])) {
			$this->data['japan_post_price'] = $this->request->post['japan_post_price'];
		} else {
			$this->data['japan_post_price'] = $this->config->get('japan_post_price');
		}

		$this->data['checked_ev'] = '';

		$this->data['checked_di'] = '';

		$this->data['checked_pu'] = '';

		if ( $this->data['japan_post_price'] == 'ev' || $this->data['japan_post_price'] == '' ){

			$this->data['checked_ev'] = 'checked="checked"';

		}elseif( $this->data['japan_post_price'] == 'di' ){

			$this->data['checked_di'] = 'checked="checked"';

		}elseif( $this->data['japan_post_price'] == 'pu' ){

			$this->data['checked_pu'] = 'checked="checked"';

		}

		if( isset($this->request->post['japan_post_time']) ){
			$this->data['japan_post_time'] = $this->request->post['japan_post_time'];
		}else{
			$this->data['japan_post_time'] = $this->config->get('japan_post_time');
		}

		if( isset($this->request->post['japan_post_stop']) ){

			$this->data['japan_post_stop'] = 'checked="checked"';

		}else{

			if( $this->config->get('japan_post_stop') == 1 ){

				$this->data['japan_post_stop'] = 'checked="checked"';

			}else{

				$this->data['japan_post_stop'] = '';

			}

		}

		if( isset($this->request->post['japan_post_district_free']) ){

			$this->data['japan_post_district_free'] = $this->request->post['japan_post_district_free'];

		}else{

			$this->data['japan_post_district_free'] = $this->config->get('japan_post_district_free');

		}

		if( isset($this->request->post['japan_post_evenly_free']) ){

			$this->data['japan_post_evenly_free'] = $this->request->post['japan_post_evenly_free'];

		}else{

			$this->data['japan_post_evenly_free'] = $this->config->get('japan_post_evenly_free');

		}

		if (isset($this->request->post['japan_post_evenly'])) {
			$this->data['japan_post_evenly'] = $this->request->post['japan_post_evenly'];
		} else {
			$this->data['japan_post_evenly'] = $this->config->get('japan_post_evenly');
		}

		if (isset($this->request->post['japan_post_purchase'])) {
			$this->data['japan_post_purchase'] = $this->request->post['japan_post_purchase'];

		} else {
			$this->data['japan_post_purchase'] = $this->config->get('japan_post_purchase');
		}

		//ゆうパックの設定を取得
		$japan_post_setting = $this->model_setting_setting->getSetting('japan_post');

		if (isset($this->request->post['ho_p'])) {
			$this->data['ho_p'] = $this->request->post['ho_p'];
		}elseif(!empty($japan_post_setting['ho_p'])){
			$this->data['ho_p'] = $japan_post_setting['ho_p'];
		}else{
			$this->data['ho_p'] = '';
		}

		if (isset($this->request->post['ho_p_free'])) {
			$this->data['ho_p_free'] = $this->request->post['ho_p_free'];
		}elseif(!empty($japan_post_setting['ho_p_free'])){
			$this->data['ho_p_free'] = $japan_post_setting['ho_p_free'];
		}else{
			$this->data['ho_p_free'] = '';
		}

		if (isset($this->request->post['to_p'])) {
			$this->data['to_p'] = $this->request->post['to_p'];
		}elseif(!empty($japan_post_setting['to_p'])){
			$this->data['to_p'] = $japan_post_setting['to_p'];
		}else{
			$this->data['to_p'] = '';
		}

		if (isset($this->request->post['to_p_free'])) {
			$this->data['to_p_free'] = $this->request->post['to_p_free'];
		}elseif(!empty($japan_post_setting['to_p_free'])){
			$this->data['to_p_free'] = $japan_post_setting['to_p_free'];
		}else{
			$this->data['to_p_free'] = '';
		}

		if (isset($this->request->post['ka_p'])) {
			$this->data['ka_p'] = $this->request->post['ka_p'];
		}elseif(!empty($japan_post_setting['ka_p'])){
			$this->data['ka_p'] = $japan_post_setting['ka_p'];
		}else{
			$this->data['ka_p'] = '';
		}

		if (isset($this->request->post['ka_p_free'])) {
			$this->data['ka_p_free'] = $this->request->post['ka_p_free'];
		}elseif(!empty($japan_post_setting['ka_p_free'])){
			$this->data['ka_p_free'] = $japan_post_setting['ka_p_free'];
		}else{
			$this->data['ka_p_free'] = '';
		}

		if (isset($this->request->post['shi_p'])) {
			$this->data['shi_p'] = $this->request->post['shi_p'];
		}elseif(!empty($japan_post_setting['shi_p'])){
			$this->data['shi_p'] = $japan_post_setting['shi_p'];
		}else{
			$this->data['shi_p'] = '';
		}

		if (isset($this->request->post['shi_p_free'])) {
			$this->data['shi_p_free'] = $this->request->post['shi_p_free'];
		}elseif(!empty($japan_post_setting['shi_p_free'])){
			$this->data['shi_p_free'] = $japan_post_setting['shi_p_free'];
		}else{
			$this->data['shi_p_free'] = '';
		}

		if (isset($this->request->post['hoku_p'])) {
			$this->data['hoku_p'] = $this->request->post['hoku_p'];
		}elseif(!empty($japan_post_setting['hoku_p'])){
			$this->data['hoku_p'] = $japan_post_setting['hoku_p'];
		}else{
			$this->data['hoku_p'] = '';
		}

		if (isset($this->request->post['hoku_p_free'])) {
			$this->data['hoku_p_free'] = $this->request->post['hoku_p_free'];
		}elseif(!empty($japan_post_setting['hoku_p_free'])){
			$this->data['hoku_p_free'] = $japan_post_setting['hoku_p_free'];
		}else{
			$this->data['hoku_p_free'] = '';
		}

		if (isset($this->request->post['tou_p'])) {
			$this->data['tou_p'] = $this->request->post['tou_p'];
		}elseif(!empty($japan_post_setting['tou_p'])){
			$this->data['tou_p'] = $japan_post_setting['tou_p'];
		}else{
			$this->data['tou_p'] = '';
		}

		if (isset($this->request->post['tou_p_free'])) {
			$this->data['tou_p_free'] = $this->request->post['tou_p_free'];
		}elseif(!empty($japan_post_setting['tou_p_free'])){
			$this->data['tou_p_free'] = $japan_post_setting['tou_p_free'];
		}else{
			$this->data['tou_p_free'] = '';
		}

		if (isset($this->request->post['ki_p'])) {
			$this->data['ki_p'] = $this->request->post['ki_p'];
		}elseif(!empty($japan_post_setting['ki_p'])){
			$this->data['ki_p'] = $japan_post_setting['ki_p'];
		}else{
			$this->data['ki_p'] = '';
		}

		if (isset($this->request->post['ki_p_free'])) {
			$this->data['ki_p_free'] = $this->request->post['ki_p_free'];
		}elseif(!empty($japan_post_setting['ki_p_free'])){
			$this->data['ki_p_free'] = $japan_post_setting['ki_p_free'];
		}else{
			$this->data['ki_p_free'] = '';
		}

		if (isset($this->request->post['chu_p'])) {
			$this->data['chu_p'] = $this->request->post['chu_p'];
		}elseif(!empty($japan_post_setting['chu_p'])){
			$this->data['chu_p'] = $japan_post_setting['chu_p'];
		}else{
			$this->data['chu_p'] = '';
		}

		if (isset($this->request->post['chu_p_free'])) {
			$this->data['chu_p_free'] = $this->request->post['chu_p_free'];
		}elseif(!empty($japan_post_setting['chu_p_free'])){
			$this->data['chu_p_free'] = $japan_post_setting['chu_p_free'];
		}else{
			$this->data['chu_p_free'] = '';
		}

		if (isset($this->request->post['shiko_p'])) {
			$this->data['shiko_p'] = $this->request->post['shiko_p'];
		}elseif(!empty($japan_post_setting['shiko_p'])){
			$this->data['shiko_p'] = $japan_post_setting['shiko_p'];
		}else{
			$this->data['shiko_p'] = '';
		}

		if (isset($this->request->post['shiko_p_free'])) {
			$this->data['shiko_p_free'] = $this->request->post['shiko_p_free'];
		}elseif(!empty($japan_post_setting['shiko_p_free'])){
			$this->data['shiko_p_free'] = $japan_post_setting['shiko_p_free'];
		}else{
			$this->data['shiko_p_free'] = '';
		}

		if (isset($this->request->post['kyu_p'])) {
			$this->data['kyu_p'] = $this->request->post['kyu_p'];
		}elseif(!empty($japan_post_setting['kyu_p'])){
			$this->data['kyu_p'] = $japan_post_setting['kyu_p'];
		}else{
			$this->data['kyu_p'] = '';
		}

		if (isset($this->request->post['kyu_p_free'])) {
			$this->data['kyu_p_free'] = $this->request->post['kyu_p_free'];
		}elseif(!empty($japan_post_setting['kyu_p_free'])){
			$this->data['kyu_p_free'] = $japan_post_setting['kyu_p_free'];
		}else{
			$this->data['kyu_p_free'] = '';
		}

		if (isset($this->request->post['oki_p'])) {
			$this->data['oki_p'] = $this->request->post['oki_p'];
		} elseif(!empty($japan_post_setting['oki_p'])){
			$this->data['oki_p'] = $japan_post_setting['oki_p'];
		}else{
			$this->data['oki_p'] = '';
		}

		if (isset($this->request->post['oki_p_free'])) {
			$this->data['oki_p_free'] = $this->request->post['oki_p_free'];
		}elseif(!empty($japan_post_setting['oki_p_free'])){
			$this->data['oki_p_free'] = $japan_post_setting['oki_p_free'];
		}else{
			$this->data['oki_p_free'] = '';
		}

		if (isset($this->request->post['japan_post_status'])) {
			$this->data['japan_post_status'] = $this->request->post['japan_post_status'];
		} else {
			$this->data['japan_post_status'] = $this->config->get('japan_post_status');
		}

		if (isset($this->request->post['japan_post_sort_order'])) {
			$this->data['japan_post_sort_order'] = $this->request->post['japan_post_sort_order'];
		} else {
			$this->data['japan_post_sort_order'] = $this->config->get('japan_post_sort_order');
		}

		$this->template = 'shipping/japan_post.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'shipping/japan_post')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>