<?php

#---------------------------------------------#
#                            初期設定
#---------------------------------------------#

//コンテンツ名
list($temp,$contTIT,$temp)=csv_set('sokuho');

$sch_days=array("日","月","火","水","木","金","土");

#-----------------メニュー-------------------#
$TAB_MENU=array("掲載リスト","記事を確認","記事を消す");
$TAB_LINK=array("?ss=sokuho","?ss=sokuho","?ss=sokuho");
$TAB_PLUG=array(",edit,new","view","del,del-v");
$TAB_COM=array("<b>ご掲載中の記事</b><br>・ご掲載中の記事確認、編集<br>・新規にご掲載の投稿","<b>ご掲載中の記事</b><br>・ご掲載中の記事編集","<b>投稿記事の削除</b><br>・掲載中の特定記事の削除");

//画像dir
$UPDIR=log_dir."sokuho/";// アップdir
$HMDIR=log_full."sokuho/";// 表示dir

// ロックファイル
$lockfile=$UPDIR.lock_file;

//タイトル
$mmax=50;

//拡張フォーム専用変換
$Q{'bcm'} = encode_input($Q{'bcm'});

#---------------------------------------------#
#                            SQL設定
#---------------------------------------------#

//契約読みこみ
$CTLOG=ContCTno('sokuho',ID,"",$Q{'editct'});
if(!$CTLOG) error("当コンテンツへのご契約が無いか、ご契約情報を調整中です。 error-CG0001");

//設定
$SET_sc=$SC_set[$CTLOG[0]['ctplug_ct']]['sokuho'];
if(sizeof($CTLOG)>1 || !$SET_sc) $SET_sc=$SC_set['adbox']['sokuho'];
// debug-s kyoh for お店から速報の投稿回数の調整
// $SOKUHO_MAXCNTは、読み込み元から設定していれば反映
if($SOKUHO_MAXCNT){ $SET_sc['limit_log'] = $SOKUHO_MAXCNT; }
// debug-e kyoh for お店から速報の投稿回数の調整

//掲載範囲判定
$limit_day=FutureDay(TODAY,-($SET_sc[limit_day]));
$limit_time=sprintf("%02d",date("H")).sprintf("%02d",date("i"));
$YOBI=date("w", TimeStamp(TODAYS));
$YOBI2=date("w", TimeStamp($limit_day."0000"));//前日
$ct_temp="";foreach($CTLOG as $v) $ct_temp.="ct_log=".$v['no_ct']." or ";
if($ct_temp) $ct_temp=" and (".rtrim($ct_temp," or ").")";

//記事ログ
$CONT_TABLE=$SQL_TABLE;
$CONT_QUERY_CELL=$SQL_QUERY_CELL;
$CONT_QUERY_WHERE=$SQL_QUERY_WHERE;
$CONT_QUERY_KEY=" order by YOBI,temp1_log desc,temp2_log,no_log"; //（order節）

$CONT_QUERY_CELL.=",no_log,gid_log,id_log,ct_log,sys_log,com_log,uptime_log,time_log,data_log,temp1_log,temp2_log,";
$CONT_QUERY_CELL.=" case when temp1_log - ".$YOBI." < 0 then temp1_log + ".$YOBI." else temp1_log - ".$YOBI." end as YOBI";
$CONT_TABLE.=" join ".sql_sokuhotable." on (id_log=id_p and temp1_log is not NULL)";
$CONT_QUERY_WHERE.=$ct_temp;
$CONT_QUERY_WHERE.=" and (sys_log=1 or (sys_log<>1 and uptime_log>=".$limit_day."))";//掲載or削除and保存期間
$CONT_QUERY_WHERE.=" and (uptime_log is NULL or (uptime_log>".$limit_day.") or (uptime_log=".$limit_day." and temp2_log>".$limit_time."))";//周回or日時
if($Q{'editno'}!='') $CONT_QUERY_WHERE.=" and no_log=".$Q{'editno'};


//$CONT_QUERY_WHERE.=" and (uptime_log is NULL or (uptime_log>".$limit_day." and temp2_log<=".$limit_time.") or (uptime_log=".$limit_day." and temp2_log>".$limit_time."))";//周回or日時
//$CONT_QUERY_WHERE.=" and sys_log=1 and ((uptime_log is NULL and (temp1_log=".$YOBI." or (temp1_log=".$YOBI2." and temp2_log>".$limit_time."))) or (uptime_log>=".$limit_day." and temp2_log<=".$limit_time."))";//未完成

#---------------------------------------------#
#                         　メイン画面
#---------------------------------------------#

//PLUGFILE(デフォルトプラグ,割込プラグ)
$plug=$SPLUG;
if($Q{'plug'}) $plug=$SPLUG."-".$Q{'plug'};
$PLUGFILE=adm_dir.plug_carry_adm(QSS,$plug);
if($PLUGFILE) if(file_exists($PLUGFILE)) require_once($PLUGFILE);


#---------------------------------------------#
#                       	記事表示
#---------------------------------------------#
function time_menu($TEMPLOG,$NO,$editno=''){

	global $SC_set,$UPDIR,$HMDIR,$CG,$sch_days;
	$SET_sc=$SC_set['adbox']['sokuho'];

	$n=1;
	for($week=-1;$week<6;++$week){
		$tdat=FutureDay(TODAY,$week);
        // debug-s kyoh
//        echo TODAY;
//        echo "<br><br>";
//        var_dump($week);
//        echo "<br><br>";
//        var_dump($tdat);
//        echo "<br><br>";
        // debug-e kyoh

		$YOBI=date("w", TimeStamp($tdat."0000"));
		$max=$SET_sc['limit_log'];
		foreach($TEMPLOG as $value){
			$vdat=substr($value['uptime_log'],0, 8);
			if($editno && $YOBI==$value['temp1_log']) return $tdat;
			if($vdat==='' && $vdat!=$tdat || $YOBI!=$value['temp1_log']) continue;
			if(!$value['sys_log']){ $max--;$n++;continue; };
			$max--;
			$n++;
		}
		if($week<0) continue;
		for(;$max;--$max){
			if($n==$NO && $editno=='') return $tdat;
			$n++;
		}
	}
	error("不明な処理が含まれています。 error-***");//不正処理排除
}

/**
 * 投稿の順番で、該当する日付を取得
 * @param $no
 * @return false|string
 */
function time_menu_no($no)
{
    global $SET_sc;

    if(!$SET_sc['limit_log']){ return date('Ymd'); }

    $quotient = (int)($no / $SET_sc['limit_log']);

    if(!$quotient){ return date('Ymd'); }

    if(!($no % $SET_sc['limit_log'])){ $quotient--; }
    return date('Ymd', strtotime(strval($quotient) . " day" ));
}
#---------------------------------------------#
#                       不要データ、ファイル削除
#---------------------------------------------#
function del_other_data($DEL_TABLE,$LIMIT){
	
	if($DEL_TABLE==sql_maintable) return;
	if($DEL_TABLE==sql_memtable) return;
	if($DEL_TABLE==sql_setttable) return;
	if(!$LIMIT) return;

	//存在しないIDを抽出
	$query="SELECT no_log FROM ".$DEL_TABLE." WHERE (uptime_log is not NULL and uptime_log<".$LIMIT.")";
	$DATALOG=loadsqldata($query,"","");
	if(!$DATALOG) return;

	//該当IDを削除
	$query=array();
	foreach($DATALOG as $key => $value){
		$newname=sprintf("%05d",($value['no_log']));
		Img_Contents_Del('sokuho',$newname);
		$query[] = "DELETE FROM ".$DEL_TABLE." WHERE no_log='".$value['no_log']."'";
	}

	//SQL書き換え
	if($query) savesqlfile($query);
	
}

?>