<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class search_site extends MX_Controller
{
    
    public function __construct()
    {
        // Load language file
        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');
    }

    public function search_form()
    {
        $content = array();

        return $this->parser->parse('search/search_form', $content, TRUE);
    }

    public function result()
    {
        $this->load->allBootstrap();

        $libs = $this->config->item('search_models');
        if (empty($libs)) {

            return '';
        }

        $this->load->model('search/search_collection');

        return $this->parser->parse('search/result', array('items' => $this->search_collection->get_items($libs)), TRUE);
    }

    public function autocomplate()
    {
        $this->load->allBootstrap();
        $this->load->model('search/search_collection');
        $items = $this->search_collection->find_in_titles($this->input->get('term'));
        if (empty($items)) {
            $data = array();
        } else {
            foreach ($items as $title) {
                $data[] = array(
                    'id'    => $title,
                    'label' => $title,
                    'value' => $title,
                );
            }
        }

        return $this->output->set_content_type('application/json')->set_output(json_encode($data));
    }

}
