<?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>." );
}

class LB_posts
{
	PUBLIC $query = "";

	function Data_out($template, $template_save, $topic = "", $attachment = true, $adt = true, $other_counter = false, $topic_nav = false, $ajax_post = false, $numbering = true)
	{
        global $tpl, $DB, $cache_config, $member_id, $cache_group, $cache_forums, $i, $j, $onl_limit, $link_on_post, $lang_message, $cache;
                
        if ($attachment)
            $attachment_post = array();
            
        $adt_i = 0;
        
        if (is_array($topic))
        {
            $topic_fid = $topic['forum_id'];
            $topic_status = $topic['status'];
            $topic_mido = $topic['member_id_open'];
        }
        else
        {
            $topic_fid = 0;
            $topic_status = "";
            $topic_mido = 0;
        }

        $tpl->load_template ( $template );
        
        while ( $row = $DB->get_row($this->query) )
        {
            if (!$other_counter)
                $i ++;
            else
                $j ++;
            
            if ($attachment)
            {
                if ($row['attachments'])
                    $attachment_post[] = $row['pid'];
            }
            
            if ($adt)
            {
                $tpl->copy_template .= topics_adtblock( $adt_i, $cache_config['topic_post_page']['conf_value'] );
                $adt_i ++;
            }
            
            if (isset($row['forum_id']))
                $topic_fid = $row['forum_id'];

            if (isset($row['status']))
                $topic_status = $row['status'];
                
            if (isset($row['member_id_open']))
                $topic_mido = $row['member_id_open'];
            
    		$tpl->copy_template .= "<div id=\"post-id_jq-".$row['pid']."\"></div>";
            
            if(forum_options_topics_mas($topic_fid, $row['topic_id'], "check"))
                $tpl->tags_blocks("moder_mass");
            else
                $tpl->tags_blocks("moder_mass", false);
            
            $tpl->tags('{pid}', $row['pid']);
            $tpl->tags('{link_on_post}', $link_on_post."#post".$row['pid']);
            
            $tpl->tags_blocks("full_link", $topic_nav);
            if ($topic_nav)
            {
                $tpl->tags('{forum}', $cache_forums[$topic_fid]['title']);
                $tpl->tags('{forum_link}', forum_link($topic_fid));
                $tpl->tags('{topic}', $row['title']);
                $tpl->tags('{topic_link}', topic_link($row['topic_id']));
            }
            
            if ($row['post_member_id'] == 0 OR !$row['user_id'])
            {
                $tpl->tags_blocks("guest_post");
                $tpl->tags_blocks("member_post", false);
                $tpl->tags('{member_name}', $row['post_member_name']);
                $tpl->tags('{member_group}', member_group(5));
                $tpl->tags('{member_avatar}', member_avatar());
                $tpl->tags('{member_id}', 0);
                $tpl->tags_blocks("online", false);
                $tpl->tags_blocks("offline", false);
            }
            else
            {
                $tpl->tags_blocks("member_post");
                $tpl->tags_blocks("guest_post", false);
                $tpl->tags('{member_name}', $row['name']);
                $tpl->tags('{member_group}', member_group($row['user_group'], $row['banned']));
                $tpl->tags_blocks("online", member_online($row['mo_id'], $row['mo_date'], $onl_limit));
                $tpl->tags_blocks("offline", member_online($row['mo_id'], $row['mo_date'], $onl_limit), true);
                $tpl->tags('{member_avatar}', member_avatar($row['foto']));
                $tpl->tags('{profile_link}', profile_link($row['name'], $row['user_id']));
                $tpl->tags('{pm_link}', pm_member($row['name'], $row['user_id']));
                $tpl->tags('{topics_link}', member_topics_link($row['name'], $row['user_id']));
                $tpl->tags('{posts_link}', member_posts_link($row['name'], $row['user_id']));
                $tpl->tags('{member_posts}', $row['posts_num']);
                $tpl->tags('{member_id}', $row['user_id']);
            }
        
            $tpl->tags('{member_group_icon}', member_group_icon($row['user_group']));
            
            if (!$cache_group[$row['user_group']]['g_signature'] OR !$row['post_member_id'] OR ($cache_group[$row['user_group']]['g_signature'] AND !$row['signature']))
            {
                $tpl->tags('{signature}', "");
                $tpl->tags_blocks("signature", false);
            }
            else
            {
                $tpl->tags('{signature}', $row['signature']);
                $tpl->tags_blocks("signature");
            }
    
            if (member_rank($row['posts_num']))
            {
                $rank = member_rank($row['posts_num']);
                $tpl->tags('{ranks_starts}', $rank[0]);
                $tpl->tags('{ranks_title}', $rank[1]);
                $tpl->tags_blocks("ranks");
            }
            else
                $tpl->tags_blocks("ranks", false);
            
            if (!$row['edit_member_id'])
                $tpl->tags_blocks("last_edit_post", false);
            else
            {
                if ($row['edit_reason'])
                {
                    $tpl->tags('{edit_reason}', $row['edit_reason']);
                    $tpl->tags_blocks("edit_reason");
                }
                else
                    $tpl->tags_blocks("edit_reason", false);
                    
                $tpl->tags('{edit_date}', formatdate($row['edit_date']));
                $tpl->tags('{edit_member_link}', profile_link($row['edit_member_name'], $row['edit_member_id']));
                $tpl->tags('{edit_member}', $row['edit_member_name']);
                $tpl->tags_blocks("last_edit_post");
            }
               
            $tpl->tags_blocks("hide_post_moder", forum_options_topics($topic_fid, "hidetopic"));
            if(forum_options_topics($topic_fid, "hidetopic"))
            {                
                $tpl->tags('{post_showhide_link}', post_edit_link($row['pid'], "showhide", intval($_GET['page'])));
                
                if ($row['hide'])
                {
                    $tpl->tags_blocks("hide_post");
                    $tpl->tags_blocks("un_hide_post", false);
                }
                else
                {
                    $tpl->tags_blocks("hide_post", false);
                    $tpl->tags_blocks("un_hide_post");
                }
            }
            else
            {                
                if (forum_options_topics($topic_fid, "hideshow") AND $row['hide'])
                {
                    $tpl->tags_blocks("hide_post");
                    $tpl->tags_blocks("un_hide_post", false);
                }
                else
                {
                    $tpl->tags_blocks("hide_post", false);
                    $tpl->tags_blocks("un_hide_post");
                }
            }
            
            $tpl->tags_blocks("fixed_post_moder", forum_options_topics($topic_fid, "fixedpost"));
            if(forum_options_topics($topic_fid, "fixedpost"))
            {                
                $tpl->tags('{post_unfixed_link}', post_edit_link($row['pid'], "unfixed", intval($_GET['page'])));
                $tpl->tags('{post_fixed_link}', post_edit_link($row['pid'], "fixed", intval($_GET['page'])));
                
                if ($row['fixed'])
                {
                    $tpl->tags_blocks("unfixed_post");
                    $tpl->tags_blocks("fixed_post", false);
                }
                else
                {
                    $tpl->tags_blocks("unfixed_post", false);
                    $tpl->tags_blocks("fixed_post");
                }
            }
                
            if(!$row['new_topic'] AND (forum_options_topics($topic_fid, "delpost") OR ($member_id['user_id'] == $row['post_member_id'] AND group_permission("local_delpost"))))
            {
                $tpl->tags_blocks("del_post");
                $tpl->tags('{post_del_link}', post_edit_link($row['pid'], "delete", intval($_GET['page'])));
            }
            else
                $tpl->tags_blocks("del_post", false);
                
            $double_click = "";
            if(forum_options_topics($topic_fid, "changepost") OR ($member_id['user_id'] == $row['post_member_id'] AND group_permission("local_changepost")))
            {
                $tpl->tags_blocks("edit_post");
                $tpl->tags('{post_edit_link}', post_edit_link($row['pid'], "edit", intval($_GET['page'])));
                $double_click = "ondblClick=\"EditPost('".$row['pid']."');return false;\"";
            }
            else
                $tpl->tags_blocks("edit_post", false);
                
                
            $tpl->tags_blocks("moder_warning", $row['moder_reason']);
            if($row['moder_reason'])
            {
                $tpl->tags('{moder_reason}', $row['moder_reason']);
                $tpl->tags('{moder_member_name}', $row['moder_member_name']);
                $tpl->tags('{moder_member_link}', profile_link($row['moder_member_name'], $row['moder_member_id']));
                $tpl->tags('{moder_date}', formatdate($row['moder_date']));
            }
            
            if (!$numbering)
                $tpl->tags('{post_id}', "--");
            elseif ($row['fixed'] AND $other_counter)
                $tpl->tags('{post_id}', $j);
            else
                $tpl->tags('{post_id}', $i);
                
            $tpl->tags('{post_date}', formatdate($row['post_date']));
            $tpl->tags('{post_date2}', date("d.m.Y, H:i", $row['post_date']));
                    
            $row['text'] = hide_in_post($row['text'], $row['post_member_id']);
                
            $tpl->tags('{post_text}', "<div id=\"post-id-".$row['pid']."\" ".$double_click.">".$row['text']."</div>");
            
            $tpl->tags('{reply_link}', reply_link($row['topic_id'], $row['pid']));
            
            $tpl->tags_blocks("ip", forum_moderation());
            if (forum_moderation())
            { 
                if ($cache_config['security_adminip']['conf_value'] AND $row['user_group'] == "1")
                    $tpl->tags('{ip}', $lang_message['ip_hide']);
                else
                    $tpl->tags('{ip}', "<a href=\"".$cache_config['general_site']['conf_value']."control_center/?do=users&op=tools&ip=".$row['ip']."\" target=\"blank\">".$row['ip']."</a>");
            }
                
            if ($topic_status == "closed" AND !forum_options_topics("0", "reply_close") AND !$cache_group[$member_id['user_group']]['g_reply_close'])
                $tpl->tags_blocks("answer_but", false);
            elseif(!forum_permission($topic_fid, "answer_theme"))
                $tpl->tags_blocks("answer_but", false);
            elseif (!member_publ_access(1))
           	    $tpl->tags_blocks("answer_but", false);
            elseif($cache_config['basket_on']['conf_value'] AND $cache_config['basket_fid']['conf_value'] == $topic_fid)
                $tpl->tags_blocks("answer_but", false);
            elseif (!$cache_group[$member_id['user_group']]['g_reply_topic'] AND $topic_mido != $member_id['user_id'] AND $logged)
           	    $tpl->tags_blocks("answer_but", false);
            else
                $tpl->tags_blocks("answer_but");

            $tpl->compile($template_save);
        
        }

        if ($attachment)
        {        
            if (strpos($tpl->result[$template_save], "[attachment=") !== false)
            {                
                $tpl->result[$template_save] = show_attach ($tpl->result[$template_save], $attachment_post);
            }
        } 
           
        $DB->free();    
        $tpl->clear();
        
        $this->clear();
        
        if (!$ajax_post)
        {
$small_img = <<<SCRIPT

    <script type="text/javascript">
    $(window).load(function(){
        Resize_img();
    });
    </script>
SCRIPT;
        }
        else
        {
$small_img = <<<SCRIPT

    <script type="text/javascript">Resize_img();</script>
SCRIPT;
        }
        $tpl->result[$template_save] .= $small_img;

	}

	function clear()
	{
        unset($this->query);
		$this->query = "";
	}
}
?>