<?php
if( ! defined( 'DATALIFEENGINE' ) ) {die( "Hacking attempt!" );}

if( $allow_active_news ) {
	
	$i = $cstart;
	$news_found = FALSE;
	
	$tpl->load_template( 'shortstory.tpl' );

	$sql_result = $db->query( $sql_select );
	while ( $row = $db->get_row( $sql_result ) ) {

		$news_found = TRUE;
		$attachments[] = $row['id'];
        $tpl->set('{id}', $row['id']);
		$row['date'] = strtotime( $row['date'] );
		$tpl->set('{date-month}',$r[(int)date("m",$row['date'])-1]);
		$tpl->set('{date-day}',date("d",$row['date']));
		
		$i ++;
		if( ! $row['category'] ) { $my_cat = "---"; $my_cat_link = "---";		
		} else {$my_cat = array (); $my_cat_link = array (); $cat_list = explode( ',', $row['category'] );
			if( count( $cat_list ) == 1 ) {
				$my_cat[] = $cat_info[$cat_list[0]]['name'];
				$my_cat_link = get_categories( $cat_list[0] );
			} else {
				foreach ( $cat_list as $element ) {
					if( $element ) {
						$my_cat[] = $cat_info[$element]['name'];
						$my_cat_link[] = "<a href=\"" . $config['http_home_url'] . get_url( $element ) . "/\">{$cat_info[$element]['name']}</a>";
					}}$my_cat_link = implode( ', ', $my_cat_link );
			}$my_cat = implode( ', ', $my_cat );
		}$row['category'] = intval( $row['category'] );

//Действие с новостью при рейтинге
if ( $row['rating'] <= '-5' and $row['allow_main']){
	$db->query( "UPDATE " . PREFIX . "_post SET allow_main = '0' WHERE id = '".$row['id']."'"); 
	$cache->clear( 'news_' );
} elseif($row['rating'] >= '5' and $row['allow_main']!='1'){
	$db->query( "UPDATE " . PREFIX . "_post SET allow_main = '1' WHERE id = '".$row['id']."'"); 
	$cache->clear( 'news_' );
}
//!Действие с новостью при рейтинге

		$news_find = array ('{comments-num}' => $row['comm_num'],  '{views}' => $row['news_read'], '{category}' => $my_cat, '{link-category}' => $my_cat_link, '{news-id}' => $row['id']);
		$tpl->set( '', $news_find );

		$tpl->copy_template = preg_replace ( "#\{date=(.+?)\}#ie", "langdate('\\1', '{$row['date']}')", $tpl->copy_template );

		if( $row['tags'] ) {
			$tpl->set( '[tags]', "" );
			$tpl->set( '[/tags]', "" );
			$tags = array ();
			$row['tags'] = explode( ",", $row['tags'] );
			foreach ( $row['tags'] as $value ) {
				$value = trim( $value );
				$tags[] = "<a href=\"" . $config['http_home_url'] . "tags/" . urlencode( $value ) . "/\">" . $value . "</a>";
			}$tpl->set( '{tags}', implode( ", ", $tags ) );
		} else {
			$tpl->set_block( "'\\[tags\\](.*?)\\[/tags\\]'si", "" );
			$tpl->set( '{tags}', "" );
		}
		
        if( $row['allow_rate'] ) {$tpl->set( '{rating}', ShortRating( $row['id'], $row['rating'], $user_group[$member_id['user_group']]['allow_rating'] ) );
        }else $tpl->set( '{rating}', "0" );

        $tpl->set( '[day-news]', "<a href=\"".$config['http_home_url'] . date( 'Y/m/d/', $row['date'])."\" >" );
      	$tpl->set( '[/day-news]', "</a>" );

/*        if ($user_group[$user_color[$row['autor']]['user_group']]['colour']){
   			$group_span = $user_group[$user_color[$row['autor']]['user_group']]['colour'];
            $user =  "<font color={$group_span}>".$row['autor']."</font>";
   		}else{*/
            $user = $row['autor'];
   		//}

        $go_page = $config['http_home_url'] . "user/" . urlencode( $row['autor'] ) . "/";
        $tpl->set( '{login}', $row['autor'] );
        $tpl->set( '{author}', "<a href=\"" . $go_page . "\">" .$user. "</a>" );

        $tpl->set( '[profile]', "<a href=\"" . $go_page . "\">" );
        $tpl->set( '[/profile]', "</a>" );

		if( $allow_userinfo and ! $row['approve'] and ($member_id['name'] == $row['autor'] and ! $user_group[$member_id['user_group']]['allow_all_edit']) ) {
			$tpl->set( '[edit]', "<a href=\"" . $config['http_home_url'] . "index.php?do=addnews&id=" . $row['id'] . "\" >" );
			$tpl->set( '[/edit]', "</a>" );
		} elseif( $is_logged and (($member_id['name'] == $row['autor'] and $user_group[$member_id['user_group']]['allow_edit']) or $user_group[$member_id['user_group']]['allow_all_edit']) ) {
			$_SESSION['referrer'] = $_SERVER['REQUEST_URI'];
			$tpl->set( '[edit]', "<a href=\"/control.php?mod=editnews&action=editnews&id=$row[id]\" target=\"_blank\">" );
			$tpl->set( '[/edit]', "</a>" );
		} else $tpl->set_block( "'\\[edit\\](.*?)\\[/edit\\]'si", "" );

		if( $row['remote_addr'] != "" ) {
            $full_link = $config['http_home_url'] . "out.php?url=" . rawurlencode( base64_encode( $row['remote_addr'] ) );
            $tpl->set( '[full-link]', "<a href=\"" . $full_link . "\" target=\"_blank\">" );
			$tpl->set( '[/full-link]', "</a>" );
        } else {
            $full_link = $config['http_home_url'] . $row['id'] . "-" . $row['alt_name'] . ".html";
            $tpl->set( '[full-link]', "<a href=\"" . $full_link . "\">" );
			$tpl->set( '[/full-link]', "</a>" );
        }
		
		$tpl->set( '{full-link}', $full_link );
		include SYSTEM_DIR . '/modules/newsimg.php';
        
		if( $row['allow_comm'] ) {
			$comm_link = $config['http_home_url'] . $row['id'] . "-" . $row['alt_name'] . ".html";
			$tpl->set( '[com-link]', "Комментарии: <a href=\"" . $comm_link . "#comment\">" );
			$tpl->set( '[/com-link]', "</a> | " );
		} else $tpl->set_block( "'\\[com-link\\](.*?)\\[/com-link\\]'si", "" );
		
		if( $is_logged ) {
			$fav_arr = explode( ',', $member_id['favorites'] );
			if( ! in_array( $row['id'], $fav_arr ) or $config['allow_cache'] == "yes" ) $tpl->set( '{favorites}', "<a id=\"fav-id-" . $row['id'] . "\" href=\"$PHP_SELF?do=favorites&amp;doaction=add&amp;id=" . $row['id'] . "\"><img src=\"" . $config['http_home_url'] . "templates/{$config['skin']}/images/add_fav.png\" onclick=\"doFavorites('" . $row['id'] . "', 'plus'); return false;\" title=\"" . $lang['news_addfav'] . "\" style=\"vertical-align: middle;border: none;\" alt=\"\" /></a>" );
			else $tpl->set( '{favorites}', "<a id=\"fav-id-" . $row['id'] . "\" href=\"$PHP_SELF?do=favorites&amp;doaction=del&amp;id=" . $row['id'] . "\"><img src=\"" . $config['http_home_url'] . "templates/{$config['skin']}/images/del_fav.png\" onclick=\"doFavorites('" . $row['id'] . "', 'minus'); return false;\" title=\"" . $lang['news_minfav'] . "\" style=\"vertical-align: middle;border: none;\" alt=\"\" /></a>" );
		} else $tpl->set( '{favorites}', "" );
		
		if( $allow_userinfo and ! $row['approve'] ) {$tpl->set( '{approve}', $lang['approve'] );
		} else $tpl->set( '{approve}', "" );
			
		if ($smartphone_detected) {
				if (!$config['allow_smart_format']) {
						$row['short_story'] = strip_tags( $row['short_story'], '<p><br><a>' );
				} else {
					if ( !$config['allow_smart_images'] ) {
						$row['short_story'] = preg_replace( "#<!--TBegin-->(.+?)<!--TEnd-->#is", "", $row['short_story'] );
    					$row['short_story'] = preg_replace( "#<img(.+?)>#is", "", $row['short_story'] );
					}
					if ( !$config['allow_smart_video'] ) {
						$row['short_story'] = preg_replace( "#<!--dle_video_begin(.+?)<!--dle_video_end-->#is", "", $row['short_story'] );
						$row['short_story'] = preg_replace( "#<!--dle_audio_begin(.+?)<!--dle_audio_end-->#is", "", $row['short_story'] );
			}}}

            if( strlen( $row['title']) > 70 ) $title = substr( stripslashes( $row['title'] ), 0, 70)."..."; else $title = stripslashes( $row['title'] ); $tpl->set( '{title}', $title );
		$tpl->set( '{short-story}', stripslashes( "<div id='news-id-" . $row['id'] . "'>" . $row['short_story'] . "</div>" ) );

		$tpl->compile( 'content' );

		if( $user_group[$member_id['user_group']]['allow_hide'] ) $tpl->result['content'] = preg_replace( "'\[hide\](.*?)\[/hide\]'si", "\\1", $tpl->result['content']);
		else $tpl->result['content'] = preg_replace ( "'\[hide\](.*?)\[/hide\]'si", "<div class=\"quote\">" . $lang['news_regus'] . "</div>", $tpl->result['content'] );
        $tpl->result['content'] = preg_replace ( "'\[group=(.*?)\](.*?)\[/group\]'sie",'group_hide($1,"\\2")', $tpl->result['content']);
	}
	
	$tpl->clear();

    if( $i >= $config['news_number'] AND !isset( $view_template )){
        $count_all = $db->super_query( $sql_count );
        $count_all = $count_all['count'];
    } else $count_all = 0;
	
	if( $do == "" ) $do = $subaction;
	if( $do == "" and $year ) $do = "date";
	$ban_short = array ();
	unset( $ban_short );
	
	if( ! $news_found and $allow_userinfo and $member_id['name'] == $user and $user_group[$member_id['user_group']]['allow_adds'] ) {
		$tpl->load_template( 'info.tpl' );
		$tpl->set( '{error}', $lang['mod_list_f'] );
		$tpl->set( '{title}', $lang['all_info'] );
		$tpl->compile( 'content' );
		$tpl->clear();
	} elseif( ! $news_found and ! $allow_userinfo and $do != '' and $do != 'favorites' and $view_template != 'rss' ) {
		if ( $newsmodule ) @header( "HTTP/1.0 404 Not Found" );
        msgbox( $lang['all_err_1'], $lang['news_err_27'] );
	} elseif( ! $news_found and $catalog != "" ) {
		if ( $newsmodule ) @header( "HTTP/1.0 404 Not Found" );
        msgbox( $lang['all_err_1'], $lang['news_err_27'] );
	} elseif( ! $news_found and $do == 'favorites' ) {
		if ( $member_id['favorites'] AND !$count_all) $db->query( "UPDATE " . USERPREFIX . "_users SET favorites='' WHERE user_id = '{$member_id['user_id']}'" );
        if (!$count_all) msgbox( $lang['all_info'], $lang['fav_notfound'] ); else msgbox( $lang['all_info'], $lang['fav_notfound_1'] );
	}
	
	//####################################################################################################################
	//         Навигация по новостям
	//####################################################################################################################
	if( ! isset( $view_template ) and $count_all ) {
		$tpl->load_template( 'navigation.tpl' );
		
		$no_prev = false;
		$no_next = false;
		
		if( isset( $cstart ) and $cstart != "" and $cstart > 0 ) {
			$prev = $cstart / $config['news_number'];
			
				$prev_page = $url_page . "/page/" . $prev . "/";
				$tpl->set_block( "'\[prev-link\](.*?)\[/prev-link\]'si", "<a href=\"" . $prev_page . "\">\\1</a>" );
		
		} else {
			$tpl->set_block( "'\[prev-link\](.*?)\[/prev-link\]'si", "<span>\\1</span>" );
			$no_prev = TRUE;
		}
		
		if( $config['news_number'] ) {
			if( $count_all > $config['news_number'] ) {
				$enpages_count = @ceil( $count_all / $config['news_number'] );
				$pages = "";
				$cstart = ($cstart / $config['news_number']) + 1;
				if( $enpages_count <= 10 ) {
					
					for($j = 1; $j <= $enpages_count; $j ++) {
						
						if( $j != $cstart ) {
							$pages .= "<a href=\"" . $url_page . "/page/" . $j . "/\">$j</a> ";
						} else {
							$pages .= "<span>$j</span> ";
						}
					}
				} else {
					$start = 1;
					$end = 10;
					$nav_prefix = "<span class=\"nav_ext\">{$lang['nav_trennen']}</span> ";

					if( $cstart > 0 ) {
						
						if( $cstart > 6 ) {
							
							$start = $cstart - 4;
							$end = $start + 8;
							
							if( $end >= $enpages_count ) {
								$start = $enpages_count - 9;
								$end = $enpages_count - 1;
								$nav_prefix = "";
							} else
								$nav_prefix = "<span class=\"nav_ext\">{$lang['nav_trennen']}</span> ";
						
						}
					
					}

					if( $start >= 2 ) {

						$pages .= "<a href=\"" . $url_page . "/page/1/\">1</a> <span class=\"nav_ext\">{$lang['nav_trennen']}</span> ";
					
					}
					
					for($j = $start; $j <= $end; $j ++) {
						
						if( $j != $cstart ) {

							$pages .= "<a href=\"" . $url_page . "/page/" . $j . "/\">$j</a> ";
						
						} else {
							
							$pages .= "<span>$j</span> ";
						}
					
					}
					
					if( $cstart != $enpages_count ) {

						$pages .= $nav_prefix . "<a href=\"" . $url_page . "/page/{$enpages_count}/\">{$enpages_count}</a>";
					
					} else
						$pages .= "<span>{$enpages_count}</span> ";
				
				}
			
			}
			$tpl->set( '{pages}', $pages );
		}
		
		//----------------------------------
		// Next link
		//----------------------------------
		if( $config['news_number'] and $config['news_number'] < $count_all and $i < $count_all ) {
			$next_page = $i / $config['news_number'] + 1;

				$next = $url_page . '/page/' . $next_page . '/';
				$tpl->set_block( "'\[next-link\](.*?)\[/next-link\]'si", "<a href=\"" . $next . "\">\\1</a>" );
		
		} else {
			$tpl->set_block( "'\[next-link\](.*?)\[/next-link\]'si", "<span>\\1</span>" );
			$no_next = TRUE;
		}

if (!$no_prev or !$no_next){
    //$tpl->compile('navpages');
    $tpl->compile('content');
}

$tpl->clear();
}

//$tpl->result['content'] = $tpl->result['navpages'] . $tpl->result['content'] . $tpl->result['navpages'];

}
?>