<?php
class ControllerOutrosMessageStatus extends Controller {
  private $error = array();

  public function index() {
    $this->getList();
  }

  public function insert() {
    $this->language->load('outros/message_status');
    $this->load->model('localisation/order_status');
    if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
      $this->model_localisation_order_status->addOrderStatusMessage($this->request->post);

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

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

    $this->getlist();
  }

  protected function getList() {
    $this->language->load('outros/message_status');

    $this->document->setTitle($this->language->get('heading_title'));
    $this->load->model('localisation/order_status');

    $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('outros/message_status', 'token=' . $this->session->data['token'] , 'SSL'),
      'separator' => ' :: '
    );

    $this->data['insert'] = $this->url->link('outros/message_status/insert', 'token=' . $this->session->data['token'] , 'SSL');
    $this->data['heading_title'] = $this->language->get('heading_title');
    $this->data['text_no_results'] = $this->language->get('text_no_results');
    $this->data['button_insert'] = $this->language->get('text_save');
    $this->data['action'] = $this->url->link('outros/message_status/insert', 'token=' . $this->session->data['token'], 'SSL');

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

    $this->data['order_statuses'] = array();
    $results = $this->model_localisation_order_status->getOrderStatuses();
    $resultsMessages = $this->model_localisation_order_status->getOrderStatusMessage();
    foreach ($results as $result) {
      $action = array();
      $message = '';
      foreach ($resultsMessages as $message_info) {
        if ($result['order_status_id'] == $message_info['order_status_id']) {
          $message = $message_info['message'];
          break;
        }
      }

      $this->data['order_statuses'][] = array(
        'order_status_id' => $result['order_status_id'],
        'name'            => $result['name'] . (($result['order_status_id'] == $this->config->get('config_order_status_id')) ? $this->language->get('text_default') : null),
        'message'         => $message
      );
    }

    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'] = '';
    }

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

    $this->response->setOutput($this->render('outros/message_status'));
  }

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

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