<?php

/*
 * @author		Martijn Polak
 * @since		18-jan-2005
 * @name		Artsennet Gericht Zoeken module; zoeken binnen mappensets in de contentcore
 */

define('AGZ_ERROR_INVALID_SET', 10010);

require_once 'class.stdModule.php';

class artsennetGerichtZoeken extends stdModule {

	// Constructor
	function artsennetGerichtZoeken() {
		
		$this->stdModule('cagz0100');
		
		$this->_variables->public->search_word_length = $this->_tcc->_variables->public->search_word_length;
		$this->_variables->public->results_per_page = $this->_tcc->_variables->public->results_per_page;

		$this->_smrt->template_dir = $this->_tcc->_smrt->template_dir;
		$this->_settings->templates->results	= 'search_result_agz.tpl';
		$this->_settings->templates->form		= 'search_form.tpl';
		$this->_settings->templates->error		= 'error.tpl';
		
	}

	// Fetch category set
	function _getCategorySet($set_id) {
		
		$rst = $this->_db->Query("select gzs_id as id, gzs_setname as name, gzs_uri as uri from agz_category_sets where gzs_org_id = ".$this->_variables->public->org_id." and gzs_enabled = 1 and gzs_id = ".(is_numeric($set_id) ? $set_id : '0'));
		$return = $rst->Row();
		$rst->Close();
		
		if (!is_array($return)) $this->triggerError(AGZ_ERROR_INVALID_SET);
		elseif ($return['uri']) $return['uri'] = &$this->_tcc->getResourceByURI($return['uri']);
		return $return;
	}

	// Fetch all category sets
	function _getCategorySets() {

		$rst = $this->_db->Query("select gzs_id as id, gzs_setname as name, gzs_uri as uri from agz_category_sets where gzs_org_id = ".$this->_variables->public->org_id." and gzs_enabled = 1");
		while (!$rst->EOF()) {
			$return[] = $rst->Row();
			$rst->MoveNext();
		}
		$rst->Close();

		return $return;

	}

	// Get all interchange categories within a set
	function _getCategoryIDs($set_id) {

		if (!is_numeric($set_id)) {
			$this->triggerError(AGZ_ERROR_INVALID_SET);
			return false;
		}
		
		$return = null;
		$rst = $this->_db->Query("select gzc_cat_id as cat_id, gzc_cat_ids as cat_ids, gzc_recursive as recursive from agz_categories inner join agz_category_sets on gzc_gzs_id = gzs_id and gzs_id = ".$set_id." and gzs_org_id = ".$this->_variables->public->org_id." and gzs_enabled = 1");
		while ($rst->Row()) {

			// A recursive setting on the root means we have to search the entire content core in which case it's better to omit the extra sql
			if ($rst->Row('recursive') && $rst->Row('cat_id') == 1) {
				$return = null;
				break;
			}
			
			$return .= ($return ? ',' : '').$rst->Row('cat_ids');
			$rst->MoveNext();

		}
		$rst->Close();
		return $return;

	}

	// Print search result
	function printSearch($set_id, $search = null, $page = 1) {

		$this->_smrt->clear_all_assign();
		
		if (($set = &$this->_getCategorySet($set_id)) === false) return;
		$cat_ids = &$this->_getCategoryIDs($set_id);

		$this->_tcc->_smrt->assign('set', $set);
		$this->_tcc->setTemplateVariableByRef("css", $this->_variables->template->custom['css']);
		$this->_tcc->setSetting('templates->search_result', $this->_settings->templates->results);

		$this->_tcc->_variables->public->search->minimum_word_length = $this->_variables->public->search->minimum_word_length;
		$this->_tcc->_variables->public->search->results_per_page = $this->_variables->public->search->results_per_page;
		$this->_tcc->_variables->public->search->sql = ($cat_ids ? "rsc_cat_id in (".$cat_ids.")" : '');
		$this->_tcc->_variables->public->search->order = 'score desc';
		$this->_tcc->_variables->public->search->include_dossiers = false;

		$this->_tcc->_variables->public->check_authorization = false;
		$this->_tcc->printSearchResult($search, null, $page);
		$this->_tcc->_variables->public->check_authorization = true;

	}
	
	function printSearchForm($set_id = '') {
		
		$this->_smrt->clear_all_assign();
		
		if (is_numeric($set_id))
			$this->_smrt->assign('set', $this->_getCategorySet($set_id));
		else
			$this->_smrt->assign('sets', $this->_getCategorySets());

		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->form);

	}
	
}

?>