<?php

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

// Include abstract classes
include_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'search_item.php';
include_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'search_model_abstract.php';

class search_site extends MX_Controller
{

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

        $items = array();
        foreach ($libs as $lib) {
            $tmp = explode('/', $lib);
            $alias = (!empty($tmp[1])) ? $tmp[1] : $lib;
            $this->load->model($lib, $alias);
            $newItems = $this->{$alias}->find($this->input->get('search_text'));
            if (!empty($newItems)) {
                $items = array_merge($items, $newItems);
            }
        }

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

}
