<?php

/****************************************/
// ИНФОРМАЦИЯ:
// ==== Форум: LogicBoard
// ==== Автор: Никита Курдин (ShapeShifter)
// ==== Copyright © Никита Курдин Игоревич 2011
// ==== Данный код защищен авторскими правами
// ==== Официальный сайт: http://logicboard.ru

/****************************************/

if (! defined ( 'LogicBoard' ))
{
	@include '../logs/save_log.php';
	exit ( "Error, wrong way to file.<br><a href=\"/\">Go to main</a>." );
}

if ($cache_config['blockmod_dle_topnews']['conf_value'])
{  
    $cache_dle_cat_info = $cache->take("cat_info", "", "dle_modules");

    if (!is_array($cache_dle_cat_info))
    {
        $cache_dle_cat_info = array ();
       	
        $DB->prefix = DLE_PREFIX;
        $DB->select( "id, parentid, posi, name, alt_name, news_number", "category", "", "ORDER BY posi ASC" );
       	while ( $row = $DB->get_row () )
        {		
      		$cache_dle_cat_info[$row['id']] = array ();
      		foreach ( $row as $key => $value )
            {
     			$cache_dle_cat_info[$row['id']][$key] = stripslashes($value);
      		}
       	}
        $cache->take("cat_info", $cache_dle_cat_info, "dle_modules");
       	$DB->free ();
    }
    
    $num = intval($cache_config['blockmod_dle_topnews_num']['conf_value']);
    if ($num <= 0 OR $num > 100) $num = 7;
    
    $type_sort = intval($cache_config['blockmod_dle_topnews_sort']['conf_value']);
        
    $cache_dle_news = "";
    if ($type_sort != 7)
        $cache_dle_news = $cache->take("dle_news_".$type_sort, "", "dle_modules", 1);
    
    if( !$cache_dle_news )
    {
    	$this_month = date( 'Y-m-d H:i:s', $time );
    	
        $order = "ORDER BY date DESC";
        
        $where = array();
        $where[] = "approve='1'";
        
        if ($type_sort == "1") // ТОП за месяц по рейтингу
        {
            $where[] = "date >= '$this_month' - INTERVAL 1 MONTH AND date < '$this_month'";
            $order = "ORDER BY rating DESC";
        }
        elseif ($type_sort == "2") // ТОП за месяц по просмотрам
        {
            $where[] = "date >= '$this_month' - INTERVAL 1 MONTH AND date < '$this_month'";
            $order = "ORDER BY news_read DESC";
        }
        elseif ($type_sort == "3") // ТОП за месяц по комментариям
        {
            $where[] = "date >= '$this_month' - INTERVAL 1 MONTH AND date < '$this_month'";
            $order = "ORDER BY comm_num DESC";
        }
        elseif ($type_sort == "4") // ТОП за всё время по рейтингу
        {
            $order = "ORDER BY rating DESC";
        }
        elseif ($type_sort == "5") // ТОП за всё время по просмотрам
        {
            $order = "ORDER BY news_read DESC";
        }
        elseif ($type_sort == "6") // ТОП за всё время по комментариям
        {
            $order = "ORDER BY comm_num DESC";
        }
        elseif ($type_sort == "7") // случайные статьи
        {
            $order = "ORDER BY RAND() DESC";
        }
        
        $where = implode (" AND ", $where);
        $DB->prefix = DLE_PREFIX;
        $DB->select( "id, title, date, alt_name, category, flag, short_story", "post", $where, $order." LIMIT 0,".$num );
        
    	while ( $row = $DB->get_row() )
        {
    		$row['date'] = strtotime( $row['date'] );
    		$row['category'] = intval( $row['category'] );
                        
    		if( $cache_config['dle_allow_alt_url']['conf_value'] )
            {
    			if( $row['flag'] and $cache_config['dle_seo_type']['conf_value'] )
                {
    				if( $row['category'] and $cache_config['dle_seo_type']['conf_value'] == 2 )					
    					$full_link = $cache_config['general_site_dle']['conf_value'] . get_dle_url( $row['category'] ) . "/" . $row['id'] . "-" . $row['alt_name'] . ".html";
    				else
                        $full_link = $cache_config['general_site_dle']['conf_value'] . $row['id'] . "-" . $row['alt_name'] . ".html";			
    			}
                else
                    $full_link = $cache_config['general_site_dle']['conf_value'] . date( 'Y/m/d/', $row['date'] ) . $row['alt_name'] . ".html";		
    		}
            else
    			$full_link = $cache_config['general_site_dle']['conf_value'] . "index.php?newsid=" . $row['id'];
    		
    		if( utf8_strlen( $row['title'] ) > 55 )
                $title = utf8_strlen( $row['title'], 0, 55 ) . " ...";
    		else
                $title = $row['title'];
    		
    		$link = "<a href=\"" . $full_link . "\">" . stripslashes( $title ) . "</a>";
            
            if ($cache_config['blockmod_dle_topnews_pic']['conf_value'])
    		{
                preg_match("#<img src=[\"'](\S+?)['\"].+?>#i", $row['short_story'], $image);
                
                $image = $image[1];
                                
                if ($image != "")
                {
                    /*
                    // вывод новости с увеличением картинки.
                    $link = "<a href=\"".$image."\" onclick=\"return hs.expand(this)\" title=\"Увеличить картинку\"><img src=\"".$image."\" width=\"40\" height=\"40\" class='lb_img' alt=\"новость\" /></a>
							".$link." <span>".formatdate($row['date'])."</span>";
                    */
                    
                    $link = "<a href=\"".$full_link."\" title=\"".htmlspecialchars(stripslashes($title))."\"><img src=\"".$image."\" width=\"40\" height=\"40\" class='lb_img' alt=\"новость\" /></a>
							".$link." <span>".formatdate($row['date'])."</span>";
                
                }
                else
                    $link .= "<span>".formatdate($row['date'])."</span>";
    		}
            else
                $link .= "<span>".formatdate($row['date'])."</span>";

            $cache_dle_news .= "<li>" . $link . "</li>";
    	}
    	
    	$DB->free();
    
        if ($type_sort != 7)
            $cache->take("dle_news_".$type_sort, $cache_dle_news, "dle_modules");
    }
    
    $tpl->tags( '{dle_news}', $cache_dle_news );
}
else
    $tpl->tags( '{dle_news}', "" );

?>