<?php
// $Id: mod_related_items.php,v 1.7 2004/04/07 11:56:03 rcastley Exp $
/**
* Related Items Module
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.7 $
**/


defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$option = trim( mosGetParam( $_REQUEST, 'option', null ) );
$task = trim( mosGetParam( $_REQUEST, 'task', null ) );
$id = intval( mosGetParam( $_REQUEST, 'id', null ) );

if ($option == 'content' && $task == 'view' && $id) {
	
	// select the meta keywords from the item
	$query = "SELECT metakey FROM #__content WHERE id='$id'";
	$database->setQuery( $query );
	
	if ($metakey = trim( $database->loadResult() )) {
		// explode the meta keys on a comma
		$keys = explode( ',', $metakey );
		$likes = array();
		
		// assemble any non-blank word(s)
		foreach ($keys as $key) {
			$key = trim( $key );
			if ($key) {
				$likes[] = $database->getEscaped( $key );
			}
		}
		
		if (count( $likes )) {
			// select other items based on the metakey field 'like' the keys found
			$query = "SELECT id, title"
			. "\nFROM #__content"
			. "\nWHERE id<>$id AND state=1 AND access <=$my->gid AND (metakey LIKE '%";
			$query .= implode( "%' OR metakey LIKE '%", $likes );
			$query .= "%')";
			
			$database->setQuery( $query );
			if ($related = $database->loadObjectList()) {
				foreach ($related as $item) {
					echo "<a href=\"index.php?option=content&task=view&id=$item->id\">$item->title</a><br />";
				}
			}
			echo $database->getErrorMsg();
		}
	}
}
?>
