<?php
/*
  French Colissimo Shipping module version 20120301.
  This module has been adapted to fit in OpenCart 1.5.x.

  @package shippingMethod
  @copyright Copyright © 2011,2012 Philippe BLAIN <pblain@NOSPAMfree.fr>.

  ------------------------------------------------------------------------------
*/
class ControllerShippingColissimo extends Controller {
  private $error = array();

  public function index() {
    // Chargement des intitulés
    $this->language->load('shipping/colissimo');

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

    // Charge le modèle et créé une instance dans la registry.
    $this->load->model('setting/setting');

    // Saving Settings
    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
      $this->model_setting_setting->editSetting('colissimo', $this->request->post);

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

      $this->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
    }

    // Language
    $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_yes'] = $this->language->get('text_yes');
    $this->data['text_no'] = $this->language->get('text_no');

    $this->data['entry_colissimo_status']                         = $this->language->get('entry_colissimo_status');
    $this->data['entry_colissimo_tax_zone_id']                    = $this->language->get('entry_colissimo_tax_zone_id');
    $this->data['entry_colissimo_tax_class_id']                   = $this->language->get('entry_colissimo_tax_class_id');
    $this->data['entry_colissimo_tare']                           = $this->language->get('entry_colissimo_tare');
    $this->data['entry_colissimo_handling']                       = $this->language->get('entry_colissimo_handling');
    $this->data['entry_colissimo_normal_enabled']                 = $this->language->get('entry_colissimo_normal_enabled');
    $this->data['entry_colissimo_econom_enabled']                 = $this->language->get('entry_colissimo_econom_enabled');
    $this->data['entry_colissimo_recomm_enabled']                 = $this->language->get('entry_colissimo_recomm_enabled');
    $this->data['entry_colissimo_recomm_crbt_enabled']            = $this->language->get('entry_colissimo_recomm_crbt_enabled');
    $this->data['entry_colissimo_international_enabled']          = $this->language->get('entry_colissimo_international_enabled');
    $this->data['entry_colissimo_international_crbt_enabled']     = $this->language->get('entry_colissimo_international_crbt_enabled');
    $this->data['entry_colissimo_internet_enabled']               = $this->language->get('entry_colissimo_internet_enabled');
    $this->data['entry_colissimo_internet_recomm_enabled']        = $this->language->get('entry_colissimo_internet_recomm_enabled');
    $this->data['entry_colissimo_internet_international_enabled'] = $this->language->get('entry_colissimo_internet_international_enabled');
    $this->data['entry_colissimo_taux_recommandation']            = $this->language->get('entry_colissimo_taux_recommandation');
    $this->data['entry_colissimo_avis_reception']                 = $this->language->get('entry_colissimo_avis_reception');
    $this->data['entry_colissimo_outside_ue_enabled']             = $this->language->get('entry_colissimo_outside_ue_enabled');
    $this->data['entry_colissimo_international_ad_valorem']       = $this->language->get('entry_colissimo_international_ad_valorem');
    $this->data['entry_colissimo_delays_enabled']                 = $this->language->get('entry_colissimo_delays_enabled');
    $this->data['entry_colissimo_box_weight_display']             = $this->language->get('entry_colissimo_box_weight_display');
    $this->data['entry_colissimo_sort_order']                     = $this->language->get('entry_colissimo_sort_order');

    $this->data['mesg_colissimo_text_title']                      = $this->language->get('mesg_colissimo_text_title');
    $this->data['mesg_colissimo_text_description']                = $this->language->get('mesg_colissimo_text_description');
    $this->data['mesg_colissimo_text_boxes']                      = $this->language->get('mesg_colissimo_text_boxes');
    $this->data['mesg_colissimo_text_way']                        = $this->language->get('mesg_colissimo_text_way');
    $this->data['mesg_colissimo_text_internet']                   = $this->language->get('mesg_colissimo_text_internet');
    $this->data['mesg_colissimo_text_eco']                        = $this->language->get('mesg_colissimo_text_eco');
    $this->data['mesg_colissimo_text_rec']                        = $this->language->get('mesg_colissimo_text_rec');
    $this->data['mesg_colissimo_text_crbt']                       = $this->language->get('mesg_colissimo_text_crbt');
    $this->data['mesg_colissimo_text_ar']                         = $this->language->get('mesg_colissimo_text_ar');
    $this->data['mesg_colissimo_invalid_destination']             = $this->language->get('mesg_colissimo_invalid_destination');
    $this->data['mesg_colissimo_invalid_origin']                  = $this->language->get('mesg_colissimo_invalid_origin');
    $this->data['mesg_colissimo_too_heavy']                       = $this->language->get('mesg_colissimo_too_heavy');
    $this->data['mesg_colissimo_delay_fixed']                     = $this->language->get('mesg_colissimo_delay_fixed');
    $this->data['mesg_colissimo_delay_range']                     = $this->language->get('mesg_colissimo_delay_range');

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


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

    $this->data['breadcrumbs'] = array();

    $this->data['breadcrumbs'][] = array(
      'text'      => $this->language->get('text_home'),
      'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      'separator' => false
    );

    $this->data['breadcrumbs'][] = array(
      'text'      => $this->language->get('text_shipping'),
      'href'      => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'),
      'separator' => ' :: '
    );

    $this->data['breadcrumbs'][] = array(
      'text'      => $this->language->get('heading_title'),
      'href'      => $this->url->link('shipping/colissimo', 'token=' . $this->session->data['token'], 'SSL'),
      'separator' => ' :: '
    );

    $this->data['action'] = $this->url->link('shipping/colissimo', 'token=' . $this->session->data['token'], 'SSL');
    $this->data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');


    if (isset($this->request->post['colissimo_status'])) {
      $this->data['colissimo_status'] = $this->request->post['colissimo_status'];
    } else {
      $this->data['colissimo_status'] = $this->config->get('colissimo_status');
    }
    if (isset($this->request->post['colissimo_tax_zone_id'])) {
      $this->data['colissimo_tax_zone_id'] = $this->request->post['colissimo_tax_zone_id'];
    } else {
      $this->data['colissimo_tax_zone_id'] = $this->config->get('colissimo_tax_zone_id');
    }
    if (isset($this->request->post['colissimo_tax_class_id'])) {
      $this->data['colissimo_tax_class_id'] = $this->request->post['colissimo_tax_class_id'];
    } else {
      $this->data['colissimo_tax_class_id'] = $this->config->get('colissimo_tax_class_id');
    }
    if (isset($this->request->post['colissimo_tare'])) {
      $this->data['colissimo_tare'] = $this->request->post['colissimo_tare'];
    } else {
      $this->data['colissimo_tare'] = $this->config->get('colissimo_tare');
    }
    if (isset($this->request->post['colissimo_handling'])) {
      $this->data['colissimo_handling'] = $this->request->post['colissimo_handling'];
    } else {
      $this->data['colissimo_handling'] = $this->config->get('colissimo_handling');
    }
    if (isset($this->request->post['colissimo_normal_enabled'])) {
      $this->data['colissimo_normal_enabled'] = $this->request->post['colissimo_normal_enabled'];
    } else {
      $this->data['colissimo_normal_enabled'] = $this->config->get('colissimo_normal_enabled');
    }
    if (isset($this->request->post['colissimo_econom_enabled'])) {
      $this->data['colissimo_econom_enabled'] = $this->request->post['colissimo_econom_enabled'];
    } else {
      $this->data['colissimo_econom_enabled'] = $this->config->get('colissimo_econom_enabled');
    }
    if (isset($this->request->post['colissimo_recomm_enabled'])) {
      $this->data['colissimo_recomm_enabled'] = $this->request->post['colissimo_recomm_enabled'];
    } else {
      $this->data['colissimo_recomm_enabled'] = $this->config->get('colissimo_recomm_enabled');
    }
    if (isset($this->request->post['colissimo_recomm_crbt_enabled'])) {
      $this->data['colissimo_recomm_crbt_enabled'] = $this->request->post['colissimo_recomm_crbt_enabled'];
    } else {
      $this->data['colissimo_recomm_crbt_enabled'] = $this->config->get('colissimo_recomm_crbt_enabled');
    }
    if (isset($this->request->post['colissimo_international_enabled'])) {
      $this->data['colissimo_international_enabled'] = $this->request->post['colissimo_international_enabled'];
    } else {
      $this->data['colissimo_international_enabled'] = $this->config->get('colissimo_international_enabled');
    }
    if (isset($this->request->post['colissimo_international_crbt_enabled'])) {
      $this->data['colissimo_international_crbt_enabled'] = $this->request->post['colissimo_international_crbt_enabled'];
    } else {
      $this->data['colissimo_international_crbt_enabled'] = $this->config->get('colissimo_international_crbt_enabled');
    }
    if (isset($this->request->post['colissimo_internet_enabled'])) {
      $this->data['colissimo_internet_enabled'] = $this->request->post['colissimo_internet_enabled'];
    } else {
      $this->data['colissimo_internet_enabled'] = $this->config->get('colissimo_internet_enabled');
    }
    if (isset($this->request->post['colissimo_internet_recomm_enabled'])) {
      $this->data['colissimo_internet_recomm_enabled'] = $this->request->post['colissimo_internet_recomm_enabled'];
    } else {
      $this->data['colissimo_internet_recomm_enabled'] = $this->config->get('colissimo_internet_recomm_enabled');
    }
    if (isset($this->request->post['colissimo_internet_international_enabled'])) {
      $this->data['colissimo_internet_international_enabled'] = $this->request->post['colissimo_internet_international_enabled'];
    } else {
      $this->data['colissimo_internet_international_enabled'] = $this->config->get('colissimo_internet_international_enabled');
    }
    if (isset($this->request->post['colissimo_taux_recommandation'])) {
      $this->data['colissimo_taux_recommandation'] = $this->request->post['colissimo_taux_recommandation'];
    } else {
      $this->data['colissimo_taux_recommandation'] = $this->config->get('colissimo_taux_recommandation');
    }
    if (isset($this->request->post['colissimo_avis_reception'])) {
      $this->data['colissimo_avis_reception'] = $this->request->post['colissimo_avis_reception'];
    } else {
      $this->data['colissimo_avis_reception'] = $this->config->get('colissimo_avis_reception');
    }
    if (isset($this->request->post['colissimo_outside_ue_enabled'])) {
      $this->data['colissimo_outside_ue_enabled'] = $this->request->post['colissimo_outside_ue_enabled'];
    } else {
      $this->data['colissimo_outside_ue_enabled'] = $this->config->get('colissimo_outside_ue_enabled');
    }
    if (isset($this->request->post['colissimo_international_ad_valorem'])) {
      $this->data['colissimo_international_ad_valorem'] = $this->request->post['colissimo_international_ad_valorem'];
    } else {
      $this->data['colissimo_international_ad_valorem'] = $this->config->get('colissimo_international_ad_valorem');
    }
    if (isset($this->request->post['colissimo_delays_enabled'])) {
      $this->data['colissimo_delays_enabled'] = $this->request->post['colissimo_delays_enabled'];
    } else {
      $this->data['colissimo_delays_enabled'] = $this->config->get('colissimo_delays_enabled');
    }
    if (isset($this->request->post['colissimo_box_weight_display'])) {
      $this->data['colissimo_box_weight_display'] = $this->request->post['colissimo_box_weight_display'];
    } else {
      $this->data['colissimo_box_weight_display'] = $this->config->get('colissimo_box_weight_display');
    }
    if (isset($this->request->post['colissimo_sort_order'])) {
      $this->data['colissimo_sort_order'] = $this->request->post['colissimo_sort_order'];
    } else {
      $this->data['colissimo_sort_order'] = $this->config->get('colissimo_sort_order');
    }


    $this->load->model('localisation/tax_class');
    $this->data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();

    $this->load->model('localisation/geo_zone');
    $this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

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

    $this->response->setOutput($this->render());
  }

  private function validate() {
    if (!$this->user->hasPermission('modify', 'shipping/colissimo')) {
      $this->error['warning'] = $this->language->get('error_permission');
    }
    if (!preg_match('/^([0-9]+(\.[0-9]{1,3})?:[0-9]+(\.[0-9]{1,2})?,?)*$/', $this->request->post['colissimo_tare'])) {
      $this->error['colissimo_tare'] = $this->language->get('error_colissimo_tare');
    }
    if (!preg_match('/^([0-9]+(\.[0-9]{1,3})?:[0-9]+(\.[0-9]{1,2})?,?)*$/', $this->request->post['colissimo_handling'])) {
      $this->error['colissimo_handling'] = $this->language->get('error_colissimo_handling');
    }

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

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

    $defaults = array() ;
    $defaults['colissimo_status'] = '0';
    $defaults['colissimo_tax_zone_id'] = '0';
    $defaults['colissimo_tax_class_id'] ='0' ;
    /* Tare par défaut selon le poids */
    $defaults['colissimo_tare'] = '0:0';
    /* Frais par défaut selon le poids */
    $defaults['colissimo_handling'] = '0:0';
    $defaults['colissimo_normal_enabled'] = '1';
    $defaults['colissimo_econom_enabled'] = '1';
    $defaults['colissimo_recomm_enabled'] = '1';
    $defaults['colissimo_recomm_crbt_enabled'] = '0';
    $defaults['colissimo_international_enabled'] = '0';
    $defaults['colissimo_international_crbt_enabled'] = '0';
    $defaults['colissimo_internet_enabled'] = '0';
    $defaults['colissimo_internet_recomm_enabled'] = '0';
    $defaults['colissimo_internet_international_enabled'] = '0';
    $defaults['colissimo_taux_recommandation'] = 'Auto';
    $defaults['colissimo_avis_reception'] = '0';
    $defaults['colissimo_outside_ue_enabled'] = '0';
    $defaults['colissimo_international_ad_valorem'] = '0';
    $defaults['colissimo_delays_enabled'] = '0';
    $defaults['colissimo_box_weight_display'] = '3';
    $defaults['colissimo_sort_order'] = '0';

    $this->model_setting_setting->editSetting('colissimo', $defaults);
  }
}
?>