<?php

include_once('./../../../include/cp_header.php');
define( '_MYMENU_CONSTANT_IN_MODINFO' , '_MI_SLIDER_NAME' ) ;

// branch for altsys
if( defined( 'XOOPS_TRUST_PATH' ) && ! empty( $_GET['lib'] ) ) {
	$mydirname = basename( dirname( dirname( __FILE__ ) ) ) ;
	$mydirpath = dirname( dirname( __FILE__ ) ) ;

	// common libs (eg. altsys)
	$lib = preg_replace( '/[^a-zA-Z0-9_-]/' , '' , $_GET['lib'] ) ;
	$page = preg_replace( '/[^a-zA-Z0-9_-]/' , '' , @$_GET['page'] ) ;

	if( file_exists( XOOPS_TRUST_PATH.'/libs/'.$lib.'/'.$page.'.php' ) ) {
		include XOOPS_TRUST_PATH.'/libs/'.$lib.'/'.$page.'.php' ;
	} else if( file_exists( XOOPS_TRUST_PATH.'/libs/'.$lib.'/index.php' ) ) {
		include XOOPS_TRUST_PATH.'/libs/'.$lib.'/index.php' ;
	} else {
		die( 'wrong request' ) ;
	}
	exit ;
}

//icms_cp_header();
//include( './mymenu.php' ) ;

// postdata get
if(isset($_POST) AND is_array($_POST)){
	foreach($_POST AS $key=>$str){
		$post[$key] = htmlspecialchars($str , ENT_QUOTES , "UTF-8");
		$hidden_data .= "<input type='hidden' name='" . $key . "' value='" . $post[$key] . "'>";
	}
} else {
	icms_cp_header();
	include( './mymenu.php' ) ;
	echo "Data Error";
	icms_cp_footer();
	return; //POSTされていなければ停止
}

$genzai = date('Y-m-d H:i:s');
$newfile = "";

if (is_uploaded_file($_FILES["upfile"]["tmp_name"])) {
		// 画像ファイル情報をゲット
		$ufile_oname = $_FILES["upfile"]["name"];
		$ufile_mine  = $_FILES["upfile"]["type"];
		$ufile_size  = $_FILES["upfile"]["size"];
		$ufile_tmp   = $_FILES["upfile"]["tmp_name"];
		$size        = getimagesize("$ufile_tmp");
		$img_w = $size[0];
		$img_h = $size[1];

		$myext = substr($ufile_oname,-3);
		$myext = strtolower($myext);
		if($myext == "ebp")
			$myext = "webp";

		$path = XOOPS_ROOT_PATH . "/uploads/slider";
		$path0 = "/uploads/slider/";
    if(!file_exists($path)){
      mkdir($path,0777) ;
    }
		if($myext=='jpg' || $myext=='peg' || $myext=='png' ||  $myext=='gif' ||  $myext=='webp' ){
	    //縦か横かでタイプ分け
      if($size[0] > $size[1]){
        $img_Type = 1;
		  }else{
	    	$img_Type = 2;
    	}
    	$Size_0 = $size[0]; //オリジナルサイズ　横
		  $Size_1 = $size[1]; //オリジナルサイズ　縦
			$fileN = time().'_'.mt_rand(5, 15);
			$new_upload_file  = $path . "/" . $fileN . "." . $myext; 		//usual
			$real_file = $fileN.'.'.$myext;
			move_uploaded_file($ufile_tmp,$new_upload_file);

		}
		$newfile = $path0 . $real_file;
	}



if($post['mode'] == "upd"){
	$sql = "update`" . $xoopsDB->prefix("slider_data") . "` set ";
	$sql .= "`name` = '" . $post['name'] . "',";
	$sql .= "`msg1` = '" . $post['msg1'] . "',";
	$sql .= "`msg2` = '" . $post['msg2'] . "',";
	if($post['start_date'])
		$sql .= "`start_date` = '" . $post['start_date'] . "',";
	else
		$sql .= "`start_date` = NULL,";

	if($post['end_date'])
		$sql .= "`end_date` = '" . $post['end_date'] . "',";
	else
		$sql .= "`end_date` = NULL,";
	$sql .= "`update_time` = '" . $genzai . "'";
	if($newfile)
		$sql .= ", `imgfile` = '" . $newfile . "' ";
	$sql .= " where ID='" . $post['myid'] . "';";
} else {
	$sql = "insert into `" . $xoopsDB->prefix("slider_data") . "` (uid,name,imgfile,msg1,msg2,start_date,end_date,update_time) values (";
	$sql .= "'" . icms::$user->uid() . "',";
	$sql .= "'" . $post['name'] . "',";
	$sql .= "'" . $newfile . "',";
	$sql .= "'" . $post['msg1'] . "',";
	$sql .= "'" . $post['msg2'] . "',";
	if($post['start_date'])
		$sql .= "'" . $post['start_date'] . "',";
	else
		$sql .= "NULL,";
	if($post['end_date'])
		$sql .= "'" . $post['end_date'] . "',";
	else
		$sql .= "NULL,";
	$sql .= "'" . $genzai . "');";
}

$result = icms::$xoopsDB->queryF($sql);

redirect_header('index.php', 3, "データベースを更新しました");
