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

/**
 * searchEvents Class
 *
 * Class to process search events.
 *
 * @package clearFusionCMS\module\search
 */
class searchEvents {

	/**
	 * The search indexer.
	 *
	 * @var flSearchIndexer
	 */
	protected $indexer;

	/**
	 * The map of data to search fields.
	 *
	 * @var array
	 */
	protected $dataMap = null;

	/**
	 * The data field used for tagging.
	 *
	 * @var string
	 */
	protected $tagField = null;

	/**
	 * Construct the object.
	 */
	function __construct() {
		$this->indexer = new flSearchIndexer(clearFusionCMS::getDb(), 'cf_search_' . clearFusionCMS::getSite());
	}

	/**
	 * Event handler notified before reindx.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onBeforeSearchReindex($evt) {
		$this->indexer->clearIndex(true);
	}

	/**
	 * Event handler to index a document.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onSearchIndex($evt) {
		if(!$this->dataMap) {
			$this->dataMap = unserialize(clearFusionCMS::getDb()->fetchOne('SELECT value FROM cf_search_' . clearFusionCMS::getSite() . '_config WHERE keyname=?', 'cfFieldData'));
			$this->tagField = unserialize(clearFusionCMS::getDb()->fetchOne('SELECT value FROM cf_search_' . clearFusionCMS::getSite() . '_config WHERE keyname=?', 'cfTagField'));
		}

		// Build the search fields from the passed data
		$in = $evt->getParam('fields', array());
		$fields = array();
		foreach($this->dataMap as $field => $data) {
			$s = '';
			foreach($data as $f) {
				if(isset($in[$f]))
					$s .= ' ' . $in[$f];
			}
			if(!empty($s))
				$fields[$field] = $s;
		}

		// Index the data
		$this->indexer->indexDocument($evt->getParam('path'), $fields, $evt->getParam('accessPerm', ''));

		// If tag field(s) then tag the document
		if(count($this->tagField)) {
			$s = '';
			foreach($this->tagField as $f) {
				if(isset($in[$f]))
					$s .= ', ' . $in[$f];
			}
			$this->indexer->tagDocument($evt->getParam('path'), $s, $evt->getParam('accessPerm', ''));
		}

		// Set published at
		$this->indexer->setDocumentPublishedAt($evt->getParam('path'), $evt->getParam('publishedAt'));
	}

	/**
	 * Event handler to deindex a document.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onSearchDeindex($evt) {
		$this->indexer->dropDocumentIndex($evt->getParam('path'));
		$this->indexer->dropDocumentTags($evt->getParam('path'));
	}

	/**
	 * Event handler to deindex a document.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onSearchDelete($evt) {
		$this->indexer->deleteDocument($evt->getParam('path'));
	}

	/**
	 * Event handler to deindex a document.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onSearchMove($evt) {
		$this->indexer->moveDocument($evt->getParam('src'), $evt->getParam('dst'));
	}

	/**
	 * Event handler to deal with module being removed.
	 *
	 * @param flEvent $evt The event object.
	 */
	function onModuleRemoved($evt) {
		// Remove module from all sites
		$siteMgr = new cfSiteManager(clearFusionCMS::getDb());
		$sites = $siteMgr->listSites();
		foreach($sites as $site => $n) {
			$indexer = new flSearchIndexer(clearFusionCMS::getDb(), "cf_search_{$site}");
			$indexer->deleteDocument(array($evt->getParam('module')));
		}
	}
}

?>