<?php
class ControllerCommonMaintenance extends Controller {
  public function index() {
    if ($this->config->get('config_maintenance')) {

      $this->load->type('tipocomum');
      $this->load->helper('ftr_geral');      

      $route = '';
      if (isset($this->request->get['route']) || isset($this->request->get['_route_'])) {

        $part = (isset($this->request->get['route'])) ? explode('/', $this->request->get['route']) : explode('/', $this->request->get['_route_']);

        if (isset($part[0])) {
          $route .= $part[0];
        }

        if($route == Type_DefaultValues::Tp_UrlLiberada){
          return false;
        }
        
        $route2 = (isset($this->request->get['route'])) ? $this->request->get['route'] : $this->request->get['_route_'];

        if($this->ftrgeralregistry->getUrlRelease($route2)){
          return false;
        }

      }
      
      // Show site if logged in as admin
      $this->load->library('user');

      $this->user = new User($this->registry);

      if (($route != 'payment') && !$this->user->isLogged()) {
        return $this->forward('common/maintenance/info');
      }
    }
  }

  public function info() {

    $this->language->load('common/maintenance');
    $this->load->model('localisation/language');
    $this->load->dmodel('layout_personalizado/custom_layout');
    $this->load->type('tipocomum');
    $this->load->helper('ftr_geral');

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

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

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

    $this->document->breadcrumbs[] = array(
      'text'      => $this->language->get('text_maintenance'),
      'href'      => $this->url->link('common/maintenance'),
      'separator' => false
    );

    $this->breadcrumbs->setbreadcumbs($this->document->breadcrumbs);

    $layout = $this->dmodel_layout_personalizado_custom_layout->getContents(Type_LayoutPersonalizado::Tp_LayoutMaintenance);

    if (ftr_geral::isIsset($layout[0]['conteudo_layout'])) {
      $title_maintenance = $layout[0]['conteudo_layout'];
      $message = html_entity_decode($title_maintenance);
    } else {
      $message = $this->language->get('text_message');
    }

    $this->data['text_message'] = $message;
    $this->data['text_maintenance'] = $this->language->get('text_maintenance');
    $this->data['url'] = $this->url->link('common/home','','NONSSL','false');
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/maintenance.twig')) {
      $this->template = $this->config->get('config_template') . '/template/common/maintenance.twig';
    } else {
      $this->template = 'default/template/common/maintenance.twig';
    }

    $this->children = array(
      'common/breadcrumbs',
      'common/footer',
      'common/header'
    );

    $this->response->setOutput($this->render('common/maintenance'));
  }
}
