<?php 
/**
 * @package		YJ Module Engine
 * @author		Youjoomla.com
 * @website     Youjoomla.com 
 * @copyright	Copyright (c) 2007 - 2011 Youjoomla.com.
 * @license   PHP files are GNU/GPL V2. CSS / JS / IMAGES are Copyrighted Commercial
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
require_once (JPATH_SITE.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_content'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'route.php');

		$app 						= JFactory::getApplication();
		$get_items             		= $params->get ('get_items',NULL);
		$nitemLoad					= $params->get   ('nitemLoad');
		$chars                 		= $params->get ('chars',40);
		$ordering              		= $params->get ('ordering');
		$getspecific 	         	= $params->get ('getspecific');
		$allow_tags					= $params->get ('allow_tags');
		$allow_tags 				= str_replace(" />", ">", $allow_tags);	
		$show_frontpage				= $params->get ('show_frontpage');
		$Voting_System				= $params->get   ('Voting_System');
		
		
		global $access;
		global $access_redirect;
		
		$access_redirect 			= $params->get ('access_redirect',1); //  0 dont show | 1 show reg only items with redirect
		if($access_redirect == 1){
			$access = 1; //	0 dont show restricted | 1 show restricted
		}else{
			$access = 0;
		}
		$frontpage  = $show_frontpage == 0 ? 'AND f.content_id IS NULL':'';
		$a_accsess 	= 'AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid;
		//random option extra query
		$youbricks_existing_items = $app->getUserState("youbricks_existing_items", array());
		if( isset($youbricks_existing_items['joomla']) && !empty($youbricks_existing_items['joomla']) && $ordering == 3 ){
			$random_where  	= " AND a.id NOT IN (".implode(",",$youbricks_existing_items['joomla']).")";
			$sql_start		= 0;
		}else{
			$random_where = "";
		}	
		$where		= 'a.state = 1'
			. ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
			. ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
			;
		// select specific items
		if(!empty($getspecific)){
		$countitems = count($getspecific);
		}
		if(!empty($getspecific) && $countitems > 0 ){
			$specificitems = implode(",", $getspecific);
			$specific_order= 'field(a.id,'.$specificitems.')';
			$where .= ' AND a.id IN ('.$specificitems.')';
		}else{
			$specificitems='';
			$specific_order='NULL';
			// 10-8-2011 multicats addon |  was $where .= ' AND cc.id = '.$get_items.'';
			$count_cats = count($get_items);
			if($count_cats > 1){
				$multi_cats = implode(",", $get_items);
				$where .= ' AND cc.id IN ('.$multi_cats.')';
			}elseif($count_cats == 1){
				if(is_array($get_items)){
					$multi_cats = implode(",", $get_items);
					$where .= ' AND cc.id ='.$multi_cats.'';
				}else{
					$where .= ' AND cc.id ='.$get_items.'';
				}
			}else{
				$where .= '';
			}
		}   // end multicats addon
		/* set items order */
		$ord = array(
			1=>'ordering',
			2=>'hits',
			3=>'RAND()',
			4=>'created ASC',
			5=>'created DESC',
			6=>$specific_order
		);
		if($Voting_System == 0){
			$get_compV = 'SELECT SUM(rating_count) FROM #__content_rating as sd WHERE sd.content_id = a.id';
		}else{
			$get_compV ='SELECT SUM(likes) FROM #__yj_news_pop_details as sd WHERE sd.Itemid = a.id AND sd.source = 1';
		}
		if(JPluginHelper::getPlugin('system', 'jcomments')){
  			$comments = JPATH_SITE . DIRECTORY_SEPARATOR .'components' . DIRECTORY_SEPARATOR . 'com_jcomments' . DIRECTORY_SEPARATOR . 'jcomments.php';
			require_once($comments);
			$comment_count 	= '(SELECT COUNT(*) FROM #__jcomments as cm WHERE cm.object_id=a.id AND published = 1) as ccount,';
		}else{
			$comment_count ='';
		}
		$order = $ord[$ordering];
		/* get items */
		$sql = 	'SELECT a.*, ('.$get_compV.') as likes,'.$comment_count.' ' .
				' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'. 
				' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
				'cc.title as cattitle,'.
				'u.username as username,'.
				'u.name as realname,'.
				'f.content_id as frontpageid'.
				
				' FROM #__content AS a' .
				' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
				' INNER JOIN #__users AS u ON u.id = a.created_by' .
				' LEFT JOIN #__content_frontpage f ON f.content_id = a.id' .
				' WHERE '. $where .'' .
				($access == 0 ? $a_accsess : '').
				' AND cc.published = 1' .
				' '.$frontpage.' '.$random_where.
				' ORDER BY '.$order .' LIMIT '.$sql_start.','.$nitemLoad.'';
					
		$db->setQuery( $sql );
		$load_items = $db->loadObjectList();

		// Make sure there aren't any errors
		if ($db->getErrorNum()) {
			echo $db->getErrorMsg();
			exit;
		}	

		//keep the loaded id
		$youbricks_existing_items = $app->getUserState("youbricks_existing_items", array());
		if(!isset($youbricks_existing_items['joomla'])){
			$youbricks_existing_items['joomla'] = array();
		}
		if(!empty($load_items)){
			foreach($load_items as $item){
				$youbricks_existing_items['joomla'][] = $item->id;
			}
			$app->setUserState("youbricks_existing_items", $youbricks_existing_items);			
		}		
