<?php
/**
 * JComments plugin for standart content objects support
 *
 * @version 2.3
 * @package JComments
 * @author Sergey M. Litvinov (smart@joomlatune.ru)
 * @copyright (C) 2006-2009 by Sergey M. Litvinov (http://www.joomlatune.ru)
 * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
 **/

class jc_com_content_category extends JCommentsPlugin
{
	function getObjectInfo($id, $language = null)
	{
		if (JCOMMENTS_JVERSION != '1.0') {
			require_once(JPATH_ROOT.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');

			$db = & JFactory::getDBO();
			$user =& JFactory::getUser();

			if (JCOMMENTS_JVERSION == '1.5') {
				$query = 'SELECT c.id, c.title, c.access, 0 as owner FROM #__categories AS c WHERE c.id = ' . intval($id);
				$db->setQuery( $query );
				$category = $db->loadObject();

				$checkAccess = $category->access <= $user->get('aid', 0);
				
				if ($checkAccess) {
					$link = JRoute::_(ContentHelperRoute::getCategoryRoute($category->id));
				} else {
					$link = JRoute::_('index.php?option=com_user&task=register');
				}
			} else {
				$query = 'SELECT c.id, c.title, c.access, c.created_user_id as owner FROM #__categories AS c WHERE c.id = ' . intval($id);
				$db->setQuery( $query );
				$category = $db->loadObject();

				$authorised = JAccess::getAuthorisedViewLevels($user->get('id'));
				$checkAccess = in_array($category->access, $authorised);

				if ($checkAccess) {
					$link = JRoute::_(ContentHelperRoute::getCategoryRoute($category->id));
				} else {
				        $returnURL = JRoute::_(ContentHelperRoute::getCategoryRoute($category->id));

					$menu = JFactory::getApplication()->getMenu();
					$active = $menu->getActive();
					$itemId = $active->id;
					$link1 = JRoute::_('index.php?option=com_users&view=login&Itemid=' . $itemId);
					$link = new JURI($link1);
					$link->setVar('return', base64_encode($returnURL));
				}
			}
		}

		$info = new JCommentsObjectInfo();

		if (!empty($category)) {
			$info->title = $category->title;
			$info->access = $category->access;
			$info->userid = $category->owner;
			$info->link = $link;
		}

		return $info;
	}
}
?>