<?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_collection extends CI_Model
{

    public function get_items()
    {
        $items = array();
        foreach ($this->config->item('search_models') 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 $items;
    }

    public function find_in_titles($term = '')
    {
        if (empty($term)) {
            
            return false;
        }
        $findData = array();
        foreach ($this->config->item('search_models') as $lib) {
            $tmp = explode('/', $lib);
            $alias = (!empty($tmp[1])) ? $tmp[1] : $lib;
            $this->load->model($lib, $alias);
            $newItems = $this->{$alias}->findInTitles($term);
            if (!empty($newItems)) {
                $findData = array_merge($findData, $newItems);
            }
        }
        
        return array_unique($findData);
    }
    
}
