<?php

/**
* @id $Id$
* @author  Joomla Bamboo
* @package  JB Slideshow3
* @copyright Copyright (C) 2006 - 2010 Joomla Bamboo. http://www.joomlabamboo.com  All rights reserved.
* @license  GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
*/

// no direct access

defined('_JEXEC') or die('Restricted access');

require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');

class mod_slideshow3Helper

{

	function getList(&$params)

	{

		global $mainframe;
		
		$db			=& JFactory::getDBO();

		$user		=& JFactory::getUser();

		$userId		= (int) $user->get('id');

		$stripTags = $params->get(	'striptags','1');

		$tags	= $params->get( 'tags', '<p><a><ul><li><div><h1><h2><h3><h4><h5><h6><b><em><i><strong><blockquote><dd><dt><small><u><br><br />' );

		$count		= (int) $params->get('count', 5);
		$catid		= $params->get('catid');

		$artid		= $params->get('artid');

		$show_front	= $params->get('show_front', 1);

		$aid		= $user->get('aid', 0);

		$dateFormat		= $params->get('dateFormat', 'j M, y');

		$articleSource = $params->get('article_source','cat');

		$linkTarget = $params->get('linkTarget','article');

		$showCategory = $params->get('showCategory',0);

		$catlink = $params->get('catLink',0);
		$jbIntroText	= $params->get( 'jbIntroText', 1 );

		$contentConfig = &JComponentHelper::getParams( 'com_content' );

		$access		= !$contentConfig->get('shownoauth');

		$nullDate	= $db->getNullDate();

		$date =& JFactory::getDate();

		$now = $date->toMySQL();

		$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).' )'

			;

		// User Filter

		switch ($params->get( 'user_id' ))

		{

			case 'by_me':

				$where .= ' AND (created_by = ' . (int) $userId . ' OR modified_by = ' . (int) $userId . ')';

				break;

			case 'not_me':

				$where .= ' AND (created_by <> ' . (int) $userId . ' AND modified_by <> ' . (int) $userId . ')';

				break;

		}

		// Ordering

		switch ($params->get( 'ordering' ))

		{

			case 'm_dsc':

				$ordering		= 'a.modified DESC, a.created DESC';

				break;

			case 'c_dsc':

			default:

				$ordering		= 'a.created DESC';

				break;

		case 'mostread':

				$ordering		= 'a.hits DESC';

				break;

		case 'item_order':

				$ordering		= 'a.ordering ASC ';

				break;

			case 'alpha' :

				$ordering 		= 'a.title ';

				break;

			case 'ralpha' :

				$ordering 		= 'a.title DESC ';

				break;
				
			case 'rand' :

				$ordering 		= 'RAND() ';

				break;

		}
		
		$frontCondition1 = ($show_front == '0' ? ' LEFT JOIN #__content_frontpage AS f ON f.content_id = a.id' : '');
		
		$frontCondition2 = ($show_front == '0' ? ' AND f.content_id IS NULL ' : '');
		

			//we get articles in the selected categories
			$catCondition = '';
			
			if (($catid)&&($articleSource=='cat'))
	
			{ 
	
				if( is_array( $catid ) ) {
	
					$catCondition = ' AND (cc.id IN ( ' . implode( ',', $catid ) . ') )';
	
				} else {
	
					$catCondition = ' AND (cc.id = '.$catid.')';
	
				}	
			} 			
		
		
			//we get the selected articles
			$artCondition = '';
	
			if ($artid)
	
			{ 
	
				if( is_array( $artid ) ) {
	
					$artCondition = ' AND (a.id IN ( ' . implode( ',', $artid ) . ') )';
	
				} else {
	
					$artCondition = ' AND (a.id = '.$artid.')';
	
				}	
	
			}
						
		//Articles query
		$query = 'SELECT a.*, cc.title as catname, s.title as sectitle, ' .

			' 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'.

			' FROM #__content AS a' .

			$frontCondition1.
			
			' INNER JOIN #__categories AS cc ON cc.id = a.catid' .

			' INNER JOIN #__sections AS s ON s.id = a.sectionid' .

			' WHERE '. $where .' AND s.id > 0' .

			($access ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').

			$artCondition.

			$catCondition.

			$frontCondition2.

			' AND s.published = 1' .

			' AND cc.published = 1' .

			' ORDER BY '. $ordering;

		$db->setQuery($query, 0, $count);

		$rows = $db->loadObjectList();			
		
		$i		= 0;

		$lists	= array();

		foreach ( $rows as $row )

		{

			if($row->access <= $aid)

			{
				
			if($linkTarget == 'article') $lists[$i]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));

			if($linkTarget == 'default') $lists[$i]->link = JRoute::_(ContentHelperRoute::getCategoryRoute($row->catslug, $row->sectionid).'&layout=default');

			if($linkTarget == 'blog') $lists[$i]->link = JRoute::_(ContentHelperRoute::getCategoryRoute($row->catslug, $row->sectionid).'&layout=blog');

			$lists[$i]->catlink = JRoute::_(ContentHelperRoute::getCategoryRoute($row->catslug,$row->sectionid).'&layout=blog');

			} else {

				$lists[$i]->link = JRoute::_('index.php?option=com_user&view=login');

			}
			
			$lists[$i]->title = $row->title;

			// Date Variables
			$lists[$i]->dateCreated = date($dateFormat,  (strtotime($row->created)));

			$lists[$i]->firstimage = $row->introtext;

			if($showCategory)$lists[$i]->catname = $row->catname;

			if ($stripTags){

				$lists[$i]->text = strip_tags( $row->introtext, $tags  );

			}else{

				$lists[$i]->text = $row->introtext;

			}

			$i++;

		}

		return $lists;

	}

}

//Class modified from K2 Content Module helper

class modSSK2ContentHelper {

  function getList(&$params) {

    jimport('joomla.filesystem.file');		
	$dateFormat	= $params->get('dateFormat', 'j M, y');
    $limit = $params->get('countK2', 5);
	$itemFilter = $params->get('itemFilter', 'items');
    $cid = $params->get('k2catid', NULL);
	$itemid = $params->get('itemid','');
    $ordering = $params->get('orderingK2');
    $limitstart = JRequest::getInt('limitstart');
	$stripTags = $params->get(	'striptags','1');
	$showCommentCount	= $params->get('showCommentCount','1');
	$display_slideshow = $params->get('display_slideshow','1');
	$tags	= $params->get( 'tags', '<p><a><ul><li><div><h1><h2><h3><h4><h5><h6><b><em><i><strong><blockquote><dd><dt><small><u><br><br />' );
	$linkTarget = $params->get('linkTarget','article');
    $user = &JFactory::getUser();
    $aid = $user->get('aid');
    $db = &JFactory::getDBO();
    $jnow = &JFactory::getDate();
    $now = $jnow->toMySQL();
    $nullDate = $db->getNullDate();
    $query = "SELECT i.*, c.name AS categoryname,c.id AS categoryid, c.alias AS categoryalias, c.params AS categoryparams";
    if ($ordering == 'best')
      $query .= ", (r.rating_sum/r.rating_count) AS rating";
    $query .= " FROM #__k2_items as i LEFT JOIN #__k2_categories c ON c.id = i.catid";
    if ($ordering == 'best')
      $query .= " LEFT JOIN #__k2_rating r ON r.itemID = i.id";
    $query .= " WHERE i.published = 1 AND i.access <= {$aid} AND i.trash = 0 AND c.published = 1 AND c.access <= {$aid} AND c.trash = 0";
    $query .= " AND ( i.publish_up = ".$db->Quote($nullDate)." OR i.publish_up <= ".$db->Quote($now)." )";
    $query .= " AND ( i.publish_down = ".$db->Quote($nullDate)." OR i.publish_down >= ".$db->Quote($now)." )";
    if($itemFilter != 'item'){
		if (!is_null($cid)) {
	        if (is_array($cid)) {
	         if ($params->get('getChildren')) {
	           require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'models'.DS.'itemlist.php');
	           $allChildren = array();
	           foreach ($cid as $id) {
	             $categories = K2ModelItemlist::getCategoryChilds($id);
	             $categories[] = $id;
	             $categories = @array_unique($categories);
	             $allChildren = @array_merge($allChildren, $categories);
	           }
	           $allChildren = @array_unique($allChildren);
	           $sql = @implode(',', $allChildren);
	           $query .= " AND i.catid IN ({$sql})";
	         } else {
	           $query .= " AND i.catid IN(".implode(',', $cid).")";
	         }
	       } else {
	         if ($params->get('getChildren')) {
	           require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'models'.DS.'itemlist.php');
	           $categories = K2ModelItemlist::getCategoryChilds($cid);
	           $categories[] = $cid;
	           $categories = @array_unique($categories);
	           $sql = @implode(',', $categories);
	           $query .= " AND i.catid IN ({$sql})";
	         } else {
	           $query .= " AND i.catid={$cid}";
	         }
	       }
	     }
	}
    if($itemFilter == 'item'){
		if ($itemid)
		{ 
			if( is_array( $itemid ) ) {
				$query .= ' AND (i.id IN ( ' . implode( ',', $itemid ) . ') )';
			} else {
				$query .= ' AND (i.id = '.$itemid.')';
			}	
		}
	}

	if($itemFilter == 'catitem'){
		if ($itemid)
		{ 
			if( is_array( $itemid ) ) {
				$query .= ' AND (i.id IN ( ' . implode( ',', $itemid ) . ') )';
			} else {
				$query .= ' AND (i.id = '.$itemid.')';
			}	
		}
	}

    if ($itemFilter == 'feat')
      $query .= " AND i.featured = 1";
    switch ($ordering) {
      case 'date':
        $orderby = 'i.created ASC';
        break;
      case 'rdate':
        $orderby = 'i.created DESC';
        break;
      case 'alpha':
        $orderby = 'i.title';
        break;
      case 'ralpha':
        $orderby = 'i.title DESC';
        break;
      case 'order':
        if ($itemFilter == 'feat')
          $orderby = 'i.featured_ordering';
        else
          $orderby = 'i.ordering';
        break;
      case 'hits':
        $orderby = 'i.hits DESC';
        break;
      case 'rand':
        $orderby = 'RAND()';
        break;
      case 'best':
        $orderby = 'rating DESC';
        break;
      default:
        $orderby = 'i.id DESC';
        break;
    }
    $query .= " ORDER BY ".$orderby;
    $db->setQuery($query, 0, $limit);
    $items = $db->loadObjectList();
	require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');
	require_once (JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'models'.DS.'item.php');
	$model = new K2ModelItem;
    if (count($items)) {
      	$k2ImageSource = $params->get('content_image','k2content');
	if (!(($k2ImageSource == 'k2content') or ($k2ImageSource == 'k2item') or ($k2ImageSource == 'directory'))) $k2ImageSource = 'k2content';
		foreach ($items as $item) {
        //Images
		if(($k2ImageSource == "k2item") and ($display_slideshow))
		{
			$itemImageSize = $params->get('itemImageSize');
			switch($itemImageSize){
				case "Original":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'src'.DS.md5("Image".$item->id).'.jpg'))
		            $item->imageOriginal = 'media/k2/items/src/'.md5("Image".$item->id).'.jpg';
				break;
				
				case "XSmall":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_XS.jpg'))
		            $item->imageXSmall = 'media/k2/items/cache/'.md5("Image".$item->id).'_XS.jpg';
				break;
				
				case "Small":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_S.jpg'))
		            $item->imageSmall = 'media/k2/items/cache/'.md5("Image".$item->id).'_S.jpg';
				break;
				
				case "Medium":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_M.jpg'))
		            $item->imageMedium = 'media/k2/items/cache/'.md5("Image".$item->id).'_M.jpg';
				break;
				
				case "Large":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_L.jpg'))
		            $item->imageLarge = 'media/k2/items/cache/'.md5("Image".$item->id).'_L.jpg';
				break;
				
				case "Xlarge":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_XL.jpg'))
		            $item->imageXLarge = 'media/k2/items/cache/'.md5("Image".$item->id).'_XL.jpg';
				break;
				
				case "Generic":
				if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$item->id).'_Generic.jpg'))
		            $item->imageGeneric = 'media/k2/items/cache/'.md5("Image".$item->id).'_Generic.jpg';
				break;				
			}
          $image = 'image'.$params->get('itemImageSize');
          if (isset($item->$image))
            $item->firstimage = $item->$image;
		}
		elseif($k2ImageSource == "k2content")
		{
			$item->firstimage = $item->introtext;
		}
        //Read more link
        if($linkTarget == 'article'){$item->link = urldecode(JRoute::_(K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias), $item->catid.':'.urlencode($item->categoryalias))));
		}else{
			$item->link = urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($item->catid.':'.urlencode($item->categoryalias))));
		}

		//K2 Comment Count
        if ($showCommentCount) $item->numOfComments = $model->countItemComments($item->id);
        // Introtext
		$item->text .= $item->introtext;
		if ($stripTags){
			$item->text = strip_tags( $item->text, $tags );
		}
		// Date Variables
		$item->dateCreated = date($dateFormat, (strtotime($item->created)));        
		$item->catname = $item->categoryname;
		$item->catlink = urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($item->catid.':'.urlencode($item->categoryalias))));
        $rows[] = $item;
      }

      return $rows;
    }
  }
}

