<?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\module\testimonials
 * @copyright Copyright (c) 2012 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * testimonialsSearchHelper Class
 *
 * Class to provide support for search to gain access to testimonials.
 *
 * @package clearFusionCMS\module\testimonials
 */
class testimonialsSearchHelper extends cfSearchHelper {

	/**
	 * The table name.
	 *
	 * @var string
	 */
	protected $table;

	/**
	 * The route to the archive page.
	 *
	 * @var flRouterRoute
	 */
	protected $route;

	/**
	 * Create the helper object.
	 *
	 * @param flDb $db The database object.
	 */
	function __construct($db) {
		parent::__construct($db);
		$this->table = 'cf_testimonials_' . clearFusionCMS::getSite();

		$content = new cfContent($this->db, clearFusionCMS::getSite());
		$this->route = $content->getContainerRoute($content->getContainer('testimonials.archive'), 'testimonial/:id');
	}

	/**
	 * Get the number of content items.
	 */
	function getContentCount() {
		return $this->db->fetchOne("SELECT COUNT(*) FROM $this->table WHERE published");
	}

	/**
	 * Reindex the block of items.
	 *
	 * @param int $start The index to start working from.
	 * @param int $items The number of items to index.
	 */
	function reindex($start, $items) {
		// Grab the next block of studies and send to search
		$result = $this->db->query("SELECT id,dateoftestimonial,tags,author,testimonial FROM $this->table WHERE published LIMIT $start, $items");
		while($row = $result->fetch()) {
			// Send the story to search for indexing
			clearFusionCMS::getEventManager()->notify(new flEvent('system.searchIndex', array(
				'path' => array('testimonials', $row['id']),
				'accessPerm' => '',
				'publishedAt' => $row['dateoftestimonial'],
				'fields' => array(
					'testimonial.author' => $row['author'],
					'testimonial.body' => $row['testimonial'],
					'testimonial.tags' => $row['tags']
				))));
		}
	}

	/**
	 * 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.
	 */
	function getSearchSummary($id, $order = null, $words = 30) {
		if(!($testimonial = $this->db->fetchRow("SELECT author,testimonial FROM $this->table WHERE id=?", $id)))
			return false;

		return array(
			'title' => $testimonial['author'],
			'summary' => str_replace(array('[', ']'), array('&#91;', '&#93;'), flTextSplitWords::split($testimonial['testimonial'], $words)),
			'uri' => $this->route->assemble(array('id' => $id))
		);
	}

	/**
	 * 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.
	 */
	function getURI($id, $absolute = false) {
		if($this->db->fetchOne("SELECT author FROM $this->table WHERE id=?", $id))
			return $this->route->assemble(array('id' => $id), $absolute);
		return false;
	}
}

?>