<?php
/*********************************************************************************************/
//																				             //
//							 InstantCMS v1.10   (c) 2012 FREEWARE							 //
//							 http://www.instantcms.ru/, info@instantcms.ru					 //
//							Modules ConfixSlider v 0.2										 //
//							Автор OLL GENHIR ноябрь 2012							         //
//							email ogenrix@gmail.com											 //
/*********************************************************************************************/


	function mod_confix_slider($module_id){
        $inCore = cmsCore::getInstance();
        $inDB = cmsDatabase::getInstance();
		$cfg = $inCore->loadModuleConfig($module_id);
global $_LANG;
		
		
		
		
		$catsql = $cfg['album_id'] ? ' AND album_id = '.$cfg['album_id'] : '';
		
		$today = date("d-m-Y");

		switch ($cfg['whatphoto']){

			case 'all':   $periodsql = ''; break;
			case 'day':   $periodsql = " AND DATE_FORMAT(f.pubdate, '%d-%m-%Y')='$today'"; break;
			case 'week':  $periodsql = " AND DATEDIFF(NOW(), f.pubdate) <= 7"; break;
			case 'month': $periodsql = " AND DATE_SUB(NOW(), INTERVAL 1 MONTH) < f.pubdate"; break;

		}

		if ($cfg['userphotoalb']==0){
		$sql = "SELECT f.id, f.album_id, f.title, f.pubdate, f.file, f.hits, f.comments, a.title as album,
					   IFNULL(r.total_rating, 0) as rating
				FROM cms_photo_files f
				LEFT JOIN cms_ratings_total r ON r.item_id=f.id AND r.target = 'photo'
				LEFT JOIN cms_photo_albums a ON f.album_id = a.id
					WHERE f.published = 1 ".$catsql." ".$periodsql." 
				ORDER BY ".$cfg['sort']." DESC 
				LIMIT ".$cfg['shownum'];
				
		$result = $inDB->query($sql) ;

		$is_best = false;	

		} else {
		
		$sql = "SELECT f.*, f.id as fid, f.pubdate as fpubdate, u.login as login, u.nickname as nickname,
			IFNULL(r.total_rating, 0) as rating
				FROM cms_users u, cms_user_photos f
				LEFT JOIN cms_ratings_total r ON r.item_id=f.id AND r.target = 'userphoto'
				WHERE f.allow_who = 'all' AND f.user_id = u.id
				GROUP BY f.id
				ORDER BY ".$cfg['sort']." DESC
				LIMIT ".$cfg['shownum'];
				
				
				$result = $inDB->query($sql) ;
		        $is_best = false;
		}
		if (!function_exists('cmsKarmaFormat') && $cfg['showrating']){ //if not included earlier
			include_once($_SERVER['DOCUMENT_ROOT'].'/core/lib_karma.php');
		}

		if ($inDB->num_rows($result)){	
			$is_best = true;
			$cons = array();
			while($con = $inDB->fetch_assoc($result)){
				if ($cfg['showtype']=='full'){
					if ($cfg['showrating']){
									if ($cfg['sort'] == 'rating'){
										$con['votes'] = cmsKarmaFormat($con['rating']);
									} else {
										$con['votes'] = $con[$cfg['sort']];
									}
								}
					if ($cfg['showdate']){
						$con['pubdate'] = cmsCore::dateFormat($con['pubdate']);
					}
					if ($cfg['showcom']){
					      if ($cfg['userphotoalb'] == 1){
						$con['comments'] = $inCore->getCommentsCount('userphoto', $con['id']);
						}else{
						$con['comments'] = $inCore->getCommentsCount('photo', $con['id']);
								}
						}
						}
				$cons[] = $con;
			}			
			}
			
			/* slider content*/
			if (!isset($cfg['maxlen'])) { $cfg['maxlen'] = 200;}
			$sql = "
				SELECT con.*, con.id as slid, con.pubdate as fdate, u.nickname as author, u.login as author_login 
				FROM cms_content con 
				LEFT JOIN cms_category cat ON cat.id = con.category_id 
				LEFT JOIN cms_users u ON u.id = con.user_id 
				WHERE con.published = 1 AND con.showlatest = 1 
				AND (con.is_end=0 OR (con.is_end=1 AND con.enddate >= '".$today."' AND con.pubdate <= '".$today."'))
				ORDER BY con.id DESC LIMIT ".$cfg['shownum'];
			
			$result = $inDB->query($sql);
			$is_slider = false;
			if ($inDB->num_rows($result)){
				$slider = array();
				$is_slider = true;
				while($con = $inDB->fetch_assoc($result)){
					$next = sizeof($slider);
					if(strlen($con['content'])>$cfg['maxlen']){
						$con['content'] = substr($con['content'], 0, $cfg['maxlen']) . '...';
					}
					$slider[$next]['title'] = $con['title'];
					$slider[$next]['content'] = $con['content'];
					$slider[$next]['id'] = $con['id'];
					$slider[$next]['seolink'] = $con['seolink'];
				}
			}
			
			
			
		
		/* end slider content*/
		
		/* Подключение CSS */
		$css = $cfg['folder_css'];
		//echo '<link rel="stylesheet" href="/modules/mod_confix_slider/css/'.$css.'/slideshow.css" type="text/css" media="screen">';    
		
		/*Указание пути к большим/маленьким фотографиям*/
		if($cfg['foto_size']==1){$path='small';}
		if($cfg['foto_size']==0){$path='medium';}

		
		$eff=$cfg['effect']; 
		$speed=$cfg['speed'];
		$timeout=$cfg['timeout'];
        $delay=$cfg['delay'];		
		$easingconf=$cfg['easingeffect'];
		if ($cfg['easing']==1){$eas=', easing: "'.$easingconf.'"';} else {$eas='';}
		$resizablewidth=$cfg['resizablewidth']; 
		$resizableheight=$cfg['resizableheight'];
		$resizable_plus=$cfg['resizable_plus'];
        $image_vid=$cfg['image_vid'];
		$image_vid_show=$cfg['image_vid_show'];
		$resizable_padtop=$cfg['resizable_padtop'];
 		
		
        /*smarty*/
		$smarty = $inCore->initSmarty('modules', 'mod_confix_slider.tpl');
		$smarty->assign('cfg', $cfg);
		$smarty->assign('resizablewidth', $resizablewidth);
        $smarty->assign('resizableheight', $resizableheight);
		$smarty->assign('resizable_plus', $resizable_plus);
		$smarty->assign('resizable_padtop', $resizable_padtop);
		$smarty->assign('image_vid', $image_vid);
		$smarty->assign('image_vid_show', $image_vid_show);
        $smarty->assign('eff', $eff);	
        $smarty->assign('timeout', $timeout);	
        $smarty->assign('speed', $speed);	
        $smarty->assign('delay', $delay);	
        $smarty->assign('eas', $eas);			
		$smarty->assign('cons', $cons);
		$smarty->assign('path', $path);
		$smarty->assign('users', $users);
		$smarty->assign('is_best', $is_best);
		/*content*/
		$smarty->assign('slider', $slider);
			$smarty->assign('is_slider', $is_slider);
        $smarty->display('mod_confix_slider.tpl');
		return true;
		
	}
?>
