<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2012 - 2013 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfSearchHelper Class
 *
 * Base class for helper object used by search modules to access data within other modules.
 *
 * @package clearFusionCMS
 */
abstract class cfSearchHelper {
	/**
	 * The database object.
	 *
	 * @var flDb
	 */
	protected $db;

	/**
	 * Create the helper object.
	 *
	 * @param flDb $db The database object.
	 */
	function __construct($db) {
		$this->db = $db;
	}

	/**
	 * Get the number of content items.
	 */
	abstract function getContentCount();

	/**
	 * Reindex the block of items.
	 *
	 * @param int $start The index to start working from.
	 * @param int $items The number of items to index.
	 */
	abstract function reindex($start, $items);

	/**
	 * Get the search summary for the given search path.
	 *
	 * @param string $id The ID of the element to get a summary of.
	 * @param string $order The order to use data to generate the summary or null for the default.
	 * @param int $words The number of words to return in the summary.
	 * @return array The summary data.
	 */
	abstract function getSearchSummary($id, $order = null, $words = 30);

	/**
	 * Get the URI of the page holding the content.
	 *
	 * @param string $id The ID of the element to get the page URI of.
	 * @param bool $absolute true if an absolute URI should be returned; else false for a relative URI.
	 * @return string The URI of the page or false if not found.
	 */
	abstract function getURI($id, $absolute = true);

	/**
	 * Get the search helper for a module.
	 *
	 * @param string $module The name of the module to get the search helper for.
	 * @return cfSearchHelper The helper object or null if not found.
	 */
	static function getHelper($module) {
		static $cache = array();
		if(!isset($cache[$module])) {
			try {
				// Get the modules search helper
				class_exists($cls = $module . 'SearchHelper');
				$cache[$module] = new $cls(clearFusionCMS::getDb());
			} catch(flExceptionLoadClass $e) {
				$cache[$module] = null;
			}
		}
		return $cache[$module];
	}
}
