<?php
/*
  MODULOOM Package Tracking Service version 2.3
  This module has been adapted to fit in OpenCart 1.5.x.

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

  ------------------------------------------------------------------------------
*/

class ControllerLocalisationPackageTracker extends Controller {
  private $error = array();

  public function index() {
    $this->load->language('localisation/package_tracker');

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

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

    $this->getList();
  }

  public function insert() {
    $this->load->language('localisation/package_tracker');

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

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

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
      $this->model_localisation_package_tracker->addTracker($this->request->post);

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

      $url = '';

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

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

    $this->getForm();
  }

  public function update() {
    $this->load->language('localisation/package_tracker');

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

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

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
      $this->model_localisation_package_tracker->editTracker($this->request->get['tracker_id'], $this->request->post);

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

      $url = '';

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

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

    $this->getForm();
  }

  public function delete() {
    $this->load->language('localisation/package_tracker');

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

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

    if (isset($this->request->post['selected']) && $this->validateDelete()) {
      foreach ($this->request->post['selected'] as $tracker_id) {
        $this->model_localisation_package_tracker->deleteTracker($tracker_id);
      }

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

      $url = '';

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

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

    $this->getList();
  }

  private function getList() {
    if (isset($this->request->get['sort'])) {
      $sort = $this->request->get['sort'];
    } else {
      $sort = 'tracker_carrier_name';
    }

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

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

    $url = '';

    if (isset($this->request->get['sort'])) {
      $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
      $url .= '&order=' . $this->request->get['order'];
    }

    if (isset($this->request->get['page'])) {
      $url .= '&page=' . $this->request->get['page'];
    }

    $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('heading_title'),
        'href'      => $this->url->link('localisation/package_tracker', 'token=' . $this->session->data['token'] . $url, 'SSL'),
        'separator' => ' :: '
    );

    $this->data['insert'] = $this->url->link('localisation/package_tracker/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
    $this->data['delete'] = $this->url->link('localisation/package_tracker/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

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

    $data = array(
      'sort'  => $sort,
      'order' => $order,
      'start' => ($page - 1) * $this->config->get('config_admin_limit'),
      'limit' => $this->config->get('config_admin_limit')
    );

		$this->load->model('tool/image');

    $tracker_total = $this->model_localisation_package_tracker->getTotalTrackers();

    $results = $this->model_localisation_package_tracker->getTrackers($data);

    foreach ($results as $result) {
      $action = array();

      $action[] = array(
        'text' => $this->language->get('text_edit'),
        'href' => $this->url->link('localisation/package_tracker/update', 'token=' . $this->session->data['token'] . '&tracker_id=' . $result['tracker_id'] . $url, 'SSL')
      );

			if ($result['tracker_image'] && file_exists(DIR_IMAGE.$result['tracker_image'])) {
				$image = $this->model_tool_image->resize($result['tracker_image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}

      $this->data['trackers'][] = array(
        'tracker_id'           => $result['tracker_id'],
        'tracker_carrier_name' => $result['tracker_carrier_name'] . (($result['tracker_id'] == $this->config->get('config_tracker_id')) ? $this->language->get('text_default') : null),
        'tracker_carrier_link' => $result['tracker_carrier_link'],
        'tracker_status'       => ($result['tracker_status']) ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
				'tracker_image'        => $image,
        'last_modified'        => $result['last_modified'],
        'date_added'           => $result['date_added'],
        'selected'             => isset($this->request->post['selected']) && in_array($result['tracker_id'], $this->request->post['selected']),
        'action'               => $action
      );
    }

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

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

    $this->data['column_tracker_carrier_name'] = $this->language->get('column_tracker_carrier_name');
    $this->data['column_tracker_carrier_link'] = $this->language->get('column_tracker_carrier_link');
    $this->data['column_tracker_status'] = $this->language->get('column_tracker_status');
    $this->data['column_tracker_image'] = $this->language->get('column_tracker_image');
    $this->data['column_action'] = $this->language->get('column_action');

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

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

    if (isset($this->session->data['success'])) {
      $this->data['success'] = $this->session->data['success'];

      unset($this->session->data['success']);
    } else {
      $this->data['success'] = '';
    }

    $url = '';

    if ($order == 'ASC') {
      $url .= '&order=DESC';
    } else {
      $url .= '&order=ASC';
    }

    if (isset($this->request->get['page'])) {
      $url .= '&page=' . $this->request->get['page'];
    }

    $this->data['sort_tracker_carrier_name'] = $this->url->link('localisation/package_tracker', 'token=' . $this->session->data['token'] . '&sort=tracker_carrier_name' . $url, 'SSL');
    $this->data['sort_tracker_carrier_link'] = $this->url->link('localisation/package_tracker', 'token=' . $this->session->data['token'] . '&sort=tracker_carrier_link' . $url, 'SSL');

    $url = '';

    if (isset($this->request->get['sort'])) {
      $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
      $url .= '&order=' . $this->request->get['order'];
    }

    $pagination = new Pagination();
    $pagination->total = $tracker_total;
    $pagination->page = $page;
    $pagination->limit = $this->config->get('config_admin_limit');
    $pagination->text = $this->language->get('text_pagination');
    $pagination->url = $this->url->link('localisation/package_tracker', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

    $this->data['pagination'] = $pagination->render();

    $this->data['sort'] = $sort;
    $this->data['order'] = $order;

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

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

  private function getForm() {
    $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_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');

    $this->data['entry_tracker_carrier_name'] = $this->language->get('entry_tracker_carrier_name');
    $this->data['entry_tracker_carrier_link'] = $this->language->get('entry_tracker_carrier_link');
   	$this->data['entry_tracker_image'] = $this->language->get('entry_tracker_image');
    $this->data['entry_tracker_status'] = $this->language->get('entry_tracker_status');
    $this->data['entry_last_modified'] = $this->language->get('entry_last_modified');
    $this->data['entry_date_added'] = $this->language->get('entry_date_added');

    $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['tracker_carrier_name'])) {
      $this->data['error_tracker_carrier_name'] = $this->error['tracker_carrier_name'];
    } else {
      $this->data['error_tracker_carrier_name'] = '';
    }

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

    $url = '';

    if (isset($this->request->get['sort'])) {
      $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
      $url .= '&order=' . $this->request->get['order'];
    }

    if (isset($this->request->get['page'])) {
      $url .= '&page=' . $this->request->get['page'];
    }

    $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('heading_title'),
      'href'      => $this->url->link('localisation/package_tracker', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      'separator' => ' :: '
    );

    if (!isset($this->request->get['tracker_id'])) {
      $this->data['action'] = $this->url->link('localisation/package_tracker/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
    } else {
      $this->data['action'] = $this->url->link('localisation/package_tracker/update', 'token=' . $this->session->data['token'] . '&tracker_id=' . $this->request->get['tracker_id'] . $url, 'SSL');
    }

    $this->data['cancel'] = $this->url->link('localisation/package_tracker', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['token'] = $this->session->data['token'];
		
    if (isset($this->request->get['tracker_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      $tracker_info = $this->model_localisation_package_tracker->getTracker($this->request->get['tracker_id']);
    }

    if (isset($this->request->post['tracker_carrier_name'])) {
      $this->data['tracker_carrier_name'] = $this->request->post['tracker_carrier_name'];
    } elseif (isset($tracker_info)) {
      $this->data['tracker_carrier_name'] = $tracker_info['tracker_carrier_name'];
    } else {
      $this->data['tracker_carrier_name'] = '';
    }

    if (isset($this->request->post['tracker_carrier_link'])) {
      $this->data['tracker_carrier_link'] = $this->request->post['tracker_carrier_link'];
    } elseif (isset($tracker_info)) {
      $this->data['tracker_carrier_link'] = $tracker_info['tracker_carrier_link'];
    } else {
      $this->data['tracker_carrier_link'] = '';
    }

    if (isset($this->request->post['tracker_status'])) {
      $this->data['tracker_status'] = $this->request->post['tracker_status'];
    } elseif (isset($tracker_info)) {
      $this->data['tracker_status'] = $tracker_info['tracker_status'];
    } else {
      $this->data['tracker_status'] = 1;
    }

		if (isset($this->request->post['tracker_image'])) {
			$this->data['tracker_image'] = $this->request->post['tracker_image'];
		} elseif (isset($tracker_info)) {
			$this->data['tracker_image'] = $tracker_info['tracker_image'];
		} else {
			$this->data['tracker_image'] = '';
		}

		$this->load->model('tool/image');

		if (isset($this->request->post['tracker_image']) && file_exists(DIR_IMAGE . $this->request->post['tracker_image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($this->request->post['tracker_image'], 210, 70);
		} elseif (!empty($tracker_info) && $tracker_info['tracker_image'] && file_exists(DIR_IMAGE . $tracker_info['tracker_image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($tracker_info['tracker_image'], 210, 70);
		} else {
			$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 210, 70);
		}

		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 210, 70);

    if (isset($this->request->post['date_added'])) {
      $this->data['date_added'] = $this->request->post['date_added'];
    } elseif (isset($tracker_info)) {
      $this->data['date_added'] = $tracker_info['date_added'];
    } else {
      $this->data['date_added'] = '';
    }

    if (isset($this->request->post['last_modified'])) {
      $this->data['last_modified'] = $this->request->post['last_modified'];
    } elseif (isset($tracker_info)) {
      $this->data['last_modified'] = $tracker_info['last_modified'];
    } else {
      $this->data['last_modified'] = '';
    }

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

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

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

    if ((strlen(utf8_decode($this->request->post['tracker_carrier_name'])) < 3) || (strlen(utf8_decode($this->request->post['tracker_carrier_name'])) > 32)) {
      $this->error['tracker_carrier_name'] = $this->language->get('error_tracker_carrier_name');
    }

    if (strlen(utf8_decode($this->request->post['tracker_carrier_link'])) < 2) {
      $this->error['tracker_carrier_link'] = $this->language->get('error_tracker_carrier_link');
    }

    if (!$this->error) {
      return true;
    } else {
      return false;
    }
  }

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

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

    foreach ($this->request->post['selected'] as $tracker_id) {
      $tracker_info = $this->model_localisation_package_tracker->getTracker($tracker_id);

      if ($tracker_info) {
        if ($this->config->get('config_tracker_id') == $tracker_info['tracker_id']) {
          $this->error['warning'] = $this->language->get('error_tracker_default');
        }
      }

      $order_history_total = $this->model_sale_order->getTotalOrderHistoriesByTrackerId($tracker_id);

      if ($order_history_total) {
        $this->error['warning'] = sprintf($this->language->get('error_order_history'), $order_history_total);
      }
    }

    if (!$this->error) {
      return true;
    } else {
      return false;
    }
  }
}
?>