<?php

class ControllerModuleMercadolivreNoticies extends Controller
{

  public function index()
  {

    $language = $this->load->language('module/mercadolivre/noticies');
    $this->data = array_merge($this->data, $language);

    $this->load->dmodel('mercadolivre/melinoticies');

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

    $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('module/mercadolivre/noticies', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
      'separator' => ' :: '
    );

    $this->data['breadcrumbs'][] = array(
      'text'      => $this->language->get('text_heading_title_noticies'),
      'href'      => $this->url->link('module/mercadolivre/noticies', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
      'separator' => ' :: '
    );

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

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

    if($results){

      $noticies = array();
      foreach ($results as $result){
        $actions = json_decode($result['actions'], true);
        if (!empty($actions)) {
            $actions = current($actions);
        }

        $noticies[] = array(
          'mercadolivre_noticies_id' => $result['mercadolivre_noticies_id'],
          'noticies_id' => $result['noticies_id'],
          'label' => html_entity_decode($result['label'], ENT_QUOTES, 'UTF-8'),
          'description' => html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'),
          'actions' => $actions
        );
      }

      $pagination = new Pagination();
      $pagination->total = $this->dmodel_mercadolivre_melinoticies->getTotalNoticies();
      $pagination->page = $page;
      $pagination->limit = $this->config->get('config_admin_limit');
      $pagination->text = $this->language->get('text_pagination');
      $pagination->url = $this->url->link('\'module/mercadolivre/noticies', 'token=' . $this->session->data['token'] . '&page={page}', 'SSL');

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

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

    }

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

    $this->response->setOutput($this->render('module/mercadolivre/noticies'));


  }


}