<?
//========================================
// dehead(config/function/lib) Include
//========================================
include $_SERVER['DOCUMENT_ROOT']."/front/include/dehead.php";

//========================================
// Class Connect
//========================================
$Db = new Db(); // 데이터 베이스 클래스

//========================================
// Data Process
//========================================
$rowCount = $pageInfo['epilogueA']['rowCount']; // 한 페이지에 보여질 게시물 수
$startNum = ($page - 1) * $rowCount; // limit 시작 위치
$param = array('no' => PARAMETER_NO, 'show' => PARAMETER_Y, 'del' => PARAMETER_N, 'sort' => "A");

//========================================
// Search Query
//========================================
if($st){
    $param['tag'] = $Db->likeify($st);
    $searchINS .= " AND tag LIKE :tag";
}

if($sk){
    if($sf){
        $param['keyword'] = $Db->likeify($sk);
    } else {
        $param = array_merge($param, array(
            'keyword_title' => $Db->likeify($sk),
            'keyword_content' => $Db->likeify($sk)
        ));
    }
    $searchINS .= ($sf) ? "AND $sf LIKE :keyword" : "AND (title LIKE :keyword_title AND content LIKE :keyword_content)";
}

if($sb){
    $param['best'] = $sb;
    $searchINS .= " AND best = :best";
}


if($sb == "" || $sb == "N"){
    $bestParam = array_merge($param, array('best' => PARAMETER_Y));
    $best = $Db->query("SELECT no, store, insert_date, name, title, content, hit FROM $TABLE_EPILOGUE WHERE no > :no AND d_show = :show AND del = :del AND sort = :sort AND best = :best $searchINS ORDER BY insert_date DESC LIMIT 3", $bestParam);

    foreach ($best as $key => $val){
        $strTmp .= $val['no'].",";
    }
    $noStr = ($best) ? "AND no NOT IN (".substr($strTmp, 0, -1).")" : "";
} else {
    $noStr = "";
}

$totalCount = $Db->single("SELECT COUNT(*) FROM $TABLE_EPILOGUE WHERE no > :no AND d_show = :show AND del = :del AND sort = :sort $noStr $searchINS", $param);
$totalPages = ceil($totalCount / $rowCount);

$param = array_merge($param, array('startNum' => $startNum, 'rowCount' => $rowCount));
$list = $Db->query("SELECT no, best, store, insert_date, name, title,  content, hit FROM $TABLE_EPILOGUE WHERE no > :no AND d_show = :show AND del = :del AND sort = :sort $noStr $searchINS ORDER BY insert_date DESC LIMIT :startNum, :rowCount", $param);
$Db->CloseConnection;

$addPath = "&no=$no&sc=$sc&sd=$sd&st=$st&sf=$sf&sk=$sk&sb=$sb";
?>

<?
if($sb == "" || $sb == "N"){
    foreach($best as $key => $val){
        $no = $val['no'];
        $store = $storeNameArr[$val['store']];
        $storeColor = StoreColorClass($val['store']);
        $name = Utf8SubstrReplace(stripslashes($val['name']), "o");
        $title = stripslashes($val['title']);
        $content = stripslashes($val['content']);
        preg_match("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $content, $contImg);
        $imgPath = (is_file($_SERVER['DOCUMENT_ROOT'].$contImg['1'])) ? $contImg['1'] : PATH_IMAGES."/epilogue/exp1.jpg";
        $hit = $val['hit'];
        $insert_date = DateConv($val['insert_date'], 7);
		
		//첫 이미지
							$content_img = explode("src=\"",$content);
							$content_img = explode("\"",$content_img[1]);
							$content_img = $content_img[0];
							//$imgPath = $content_img;
							
							$ext = array_pop(explode(".",strtolower($content_img)));
							$save_path = "../../uploads/thumb/".$no.".".$ext;
							if(!is_file($save_path)){
								
								if(!preg_match('#http#',$content_img)){
									$content_img = "https://www.ssdream.co.kr".$content_img;
								}
								if($ext == 'jpg' || $ext == 'jpeg'){
								
									$ori_path = $content_img;
									$ori_img = imagecreatefromjpeg($ori_path);
									$info_img = getimagesize($ori_path);
									$new_img = imagecreatetruecolor(100,100);
									imagecopyresampled($new_img,$ori_img,0,0,0,0,100,100,$info_img[0],$info_img[1]);
									
									imagejpeg($new_img,$save_path);
								}else if($ext == 'png'){
									
									$ori_path = $content_img;
									$ori_img = imagecreatefrompng($ori_path);
									$info_img = getimagesize($ori_path);
									$new_img = imagecreatetruecolor(100,100);
									imagecopyresampled($new_img,$ori_img,0,0,0,0,100,100,$info_img[0],$info_img[1]);
									
									imagepng($new_img,$save_path);
									
								}else if($ext == 'gif'){
									
									$ori_path = $content_img;
									$ori_img = imagecreatefromgif($ori_path);
									$info_img = getimagesize($ori_path);
									$new_img = imagecreatetruecolor(100,100);
									imagecopyresampled($new_img,$ori_img,0,0,0,0,100,100,$info_img[0],$info_img[1]);
									
									imagegif($new_img,$save_path);
									
								}
								
							}
						
							$imgPath = "/uploads/thumb/".$no.".".$ext;
							
							$relate = $Db->row("SELECT self_view,real_view,beaf,youtube FROM 2019_epilogue_relate WHERE self_view = '".$no."'",array());
?>
<style>
.ico_box{
	position:absolute; margin-left:230px;
}
.ico_box .ico{
	width:38px;
	height:38px;
}
@media screen and (max-width:640px){
	.ico_box{
		position:absolute; margin-left:120px;
	}
	.ico_box .ico{
		width:25px;
		height:25px;
	}	
}
</style>
    <li class="best">
        <a href="#<?=$no?>">
            <span class="img_box">
                <img src="<?=$imgPath?>" alt="문서" />
                <span class="cover"></span>
                <span class="best">BEST</span>
            </span>
            <strong class="txt_box">
				<div class="ico_box">
				<? if($relate['youtube']){?>
				<img src="./img/ico_video.png" class="ico">
				<?}?>
				<? if($relate['real_view']){?>
				<img src="./img/ico_real.png" class="ico">
				<?}?>
				<? if($relate['beaf']){?>
				<img src="./img/ico_beaf.png" class="ico">
				<?}?>
				</div>
                <span class="branch <?=$storeColor?>"><?=$store?> 자필후기</span>
                <span class="name"><?=$title?></span>
            </strong>
            <span class="date_box">
                <span><em>작성자</em> <?=$name?></span>
                <span><em>등록일</em> <?=$insert_date?></span>
                <span><em>조회</em> <?=$hit?></span>
            </span>
        </a>
    </li>
<?
    }
}

if($totalCount > 0){
    foreach($list as $key => $val){
        $no = $val['no'];
        $best = ($val['best'] === "Y" && $sb === "Y") ? "best" : "";
        $store = $storeNameArr[$val['store']];
        $storeColor = StoreColorClass($val['store']);
        $name = Utf8SubstrReplace(stripslashes($val['name']), "o");
        $title = stripslashes($val['title']);
        $content = stripslashes($val['content']);
        preg_match("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $content, $contImg);
        $imgPath = (is_file($_SERVER['DOCUMENT_ROOT'].$contImg['1'])) ? $contImg['1'] : PATH_IMAGES."/epilogue/exp1.jpg";
        $hit = $val['hit'];
        $insert_date = DateConv($val['insert_date'], 7);
		
		//첫 이미지
							$content_img = explode("src=\"",$content);
							$content_img = explode("\"",$content_img[1]);
							$content_img = $content_img[0];
							//$imgPath = $content_img;
							
							$ext = array_pop(explode(".",strtolower($content_img)));
							$save_path = "../../uploads/thumb/".$no.".".$ext;
							if(!is_file($save_path)){
								
								if(!preg_match('#http#',$content_img)){
									$content_img = "https://www.ssdream.co.kr".$content_img;
								}
								if($ext == 'jpg' || $ext == 'jpeg'){
								
									$ori_path = $content_img;
									$ori_img = imagecreatefromjpeg($ori_path);
									$info_img = getimagesize($ori_path);
									$new_img = imagecreatetruecolor(100,100);
									imagecopyresampled($new_img,$ori_img,0,0,0,0,100,100,$info_img[0],$info_img[1]);
									
									imagejpeg($new_img,$save_path);
								}else if($ext == 'png'){
									
									$ori_path = $content_img;
									$ori_img = imagecreatefrompng($ori_path);
									$info_img = getimagesize($ori_path);
									$new_img = imagecreatetruecolor(100,100);
									imagecopyresampled($new_img,$ori_img,0,0,0,0,100,100,$info_img[0],$info_img[1]);
									
									imagepng($new_img,$save_path);
									
								}else if($ext == 'gif'){
									
									$ori_path = $content_img;
									$ori_img = imagecreatefromgif($ori_path);
									$info_img = getimagesize($ori_path);
									$new_img = imagecreatetruecolor(100,100);
									imagecopyresampled($new_img,$ori_img,0,0,0,0,100,100,$info_img[0],$info_img[1]);
									
									imagegif($new_img,$save_path);
									
								}
								
							}
						
							$imgPath = "/uploads/thumb/".$no.".".$ext;
							
							$relate = $Db->row("SELECT self_view,real_view,beaf,youtube FROM 2019_epilogue_relate WHERE self_view = '".$no."'",array());
?>
    <li class="<?=$best?>" <? if($key == 0){ ?>totalcount="<?=$totalCount?>"<? } ?>>
        <a href="#<?=$no?>">
            <span class="img_box">
                <img src="<?=$imgPath?>" alt="문서" />
                <span class="cover"></span>
                <span class="best"></span>
            </span>
            <strong class="txt_box">
				<div class="ico_box">
				<? if($relate['youtube']){?>
				<img src="./img/ico_video.png" class="ico">
				<?}?>
				<? if($relate['real_view']){?>
				<img src="./img/ico_real.png" class="ico">
				<?}?>
				<? if($relate['beaf']){?>
				<img src="./img/ico_beaf.png" class="ico">
				<?}?>
				</div>
                <span class="branch <?=$storeColor?>"><?=$store?> 자필후기</span>
                <span class="name"><?=$title?></span>
            </strong>
            <span class="date_box">
                <span><em>작성자</em> <?=$name?></span>
                <span><em>등록일</em> <?=$insert_date?></span>
                <span><em>조회</em> <?=$hit?></span>
            </span>
        </a>
    </li>
<?
    }
}
?>
<div class="pagination_wrap"><? if($totalPages > 0) Pageing($totalPages, $blockCount, $page, $addPath,"#boardList"); ?></div>