<?php

class ControllerReportSearchTerms extends Controller
{
    public function index()
    {
        $language = $this->language->load('report/searchterms');
        $this->data = array_merge($this->data, $language);

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

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

        $url = '';

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

        $this->load->dmodel('catalog/searchterms');

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

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

        foreach ($results as $result) {
            $this->data['terms'][] = array(
                'term' => $result['term'],
                'score' => $result['score']
            );
        }

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

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

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

        $this->template = 'report/searchterms.twig';
        $this->children = array(
            'common/header',
            'common/footer'
        );

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

}
