<?php
// ------------------------------------------------------------------------- //
//                      IMGTag for TacmicCMS                                 ////                        <https://impresscms.jp/>                           //
// ------------------------------------------------------------------------- //


include("admin_header.php");
//include_once(XOOPS_ROOT_PATH."/class/xoopstree.php");
include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");
include_once(XOOPS_ROOT_PATH."/include/xoopscodes.php");

$myts = icms_core_Textsanitizer::getInstance();
$cattree = new icms_view_Tree($table_cat , "cid" , "pid");

// GPCS vars
$submitter = empty($_POST['submitter']) ? $my_uid : intval($_POST['submitter']);
if(isset($_POST['cid'])) $cid = intval($_POST['cid']);
else if(isset($_GET['cid'])) $cid = intval($_GET['cid']);
else $cid = 0 ;
$dir4edit = isset($_POST['dir']) ? $myts->makeTboxData4Edit($_POST['dir']) : '' ;
$title4edit = isset($_POST['title']) ? $myts->makeTboxData4Edit($_POST['title']) : '' ;
$desc4edit = isset($_POST['desc']) ? $myts->makeTareaData4Edit($_POST['desc']) : '' ;
$shareimg = @$_POST["shareimg"];
if ($shareimg != 1) $shareimg = 0;


// reject Not Admin
if(! $isadmin) {
	redirect_header($mod_url.'/' , 2 , _MD_D3IMGTAG_MUSTREGFIRST);
	exit;
}

// check Categories exist
$result = $xoopsDB->query("SELECT count(cid) as count FROM $table_cat");
list($count) = $xoopsDB->fetchRow($result);
if($count < 1) {
	redirect_header(XOOPS_URL."/modules/$mydirname/admin/index.php", 2, _MD_D3IMGTAG_MUSTADDCATFIRST);
	exit();
}

if(isset($_POST['submit']) && $_POST['submit'] != "") {
	ob_start() ;

	// Check Directory
	$dir = $myts->stripSlashesGPC( $_POST['dir'] ) ;
	if( empty( $dir ) || ! is_dir( $dir ) ) {
		if( ord( $dir ) != 0x2f ) $dir = "/$dir" ;
		$prefix = XOOPS_ROOT_PATH ;
		while( strlen( $prefix ) > 0 ) {
			if( is_dir( "$prefix$dir" ) ) {
				$dir = "$prefix$dir" ;
				break ;
			}
			$prefix = substr( $prefix , 0 , strrpos( $prefix , '/' ) ) ;
		}
		if( ! is_dir( $dir ) ) {
			redirect_header( 'index.php?page=batch' , 3 , _MD_D3IMGTAG_MES_INVALIDDIRECTORY . "<br />$dir4edit" ) ;
			exit ;
		}
	}
	if( substr( $dir , -1 ) == '/' ) $dir = substr( $dir , 0 , -1 ) ;

	$title4save = $myts->makeTboxData4Save( $_POST['title'] ) ;
	$desc4save = $myts->makeTareaData4Save( $_POST['desc'] ) ;

	$date = strtotime( $_POST['post_date'] ) ;
	if( $date == -1 ) $date = time() ;

	$dir_h = opendir( $dir ) ;
	if( $dir_h === false ) {
		redirect_header( 'index.php?page=batch' , 3 , _MD_D3IMGTAG_MES_INVALIDDIRECTORY . "<br />$dir4edit" ) ;
		exit ;
	}
	// get all file_names from the directory.
	$file_names = array() ;
	while( $file_name = readdir( $dir_h ) ) {
		$file_names[] = $file_name ;
	}
	sort( $file_names ) ;

	$filecount = 1 ;
	foreach( $file_names as $file_name ) {

		// Skip '.' , '..' and hidden file
		if( substr( $file_name , 0 , 1 ) == '.' ) continue ;

		$ext = substr( strrchr( $file_name , '.' ) , 1 ) ;
		$ext2 = "webp";
		$node = substr( $file_name , 0 , - strlen( $ext ) - 1 ) ;
		$file_path = "$dir/$node.$ext" ;

		$title = empty( $_POST['title'] ) ? addslashes( $node ) : "$title4save $filecount" ;

		if( is_readable( $file_path ) && in_array( strtolower( $ext ) , $array_allowed_exts ) ) {
			$lid = $xoopsDB->genId( $table_photos."_lid_seq" ) ;
			if( in_array( strtolower( $ext ) , $d3imgtag_normal_exts ) ) {
				list( $w , $h ) = getimagesize( $file_path ) ;
			} else {
				list( $w , $h ) = array( 0 , 0 ) ;
			}
			$fileName = createFileName($d3imgtag_minfile, $d3imgtag_maxfile, $d3imgtag_filerule);	//@d3imgtag
			$fileSize = filesize($file_path);	//@d3imgtag
			$sql = "INSERT INTO $table_photos SET lid='$lid', img='$fileName', cid='$cid', title='$title', ext='$ext2', share='$shareimg', size='$fileSize', res_x='$w', res_y='$h', submitter='$submitter', status=1, date='$date'" ;
			$xoopsDB->query( $sql ) or die( "DB error: INSERT photos table." ) ;
			if( $lid == 0 ) {
				$lid = $xoopsDB->getInsertId() ;
			}
			print " &nbsp; <a href='../index.php?page=photo&lid=$lid' target='_blank'>$file_path</a>\n" ;
			copy( $file_path , "$photos_dir/$lid.$ext" ) ;
			d3imgtag_modify_photo("$photos_dir/$lid.$ext" , "$photos_dir/$fileName.$ext2") ;

			//d3imgtag_create_thumb( "$photos_dir/$lid.$ext" , $fileName , $ext ) ;
			d3imgtag_create_thumb( "$photos_dir/$fileName.$ext2" , $fileName , $ext2 ) ;
			d3imgtag_create_preview("$photos_dir/$fileName.$ext2" , $fileName , $ext2);
			$xoopsDB->query( "INSERT INTO $table_text SET lid='$lid', description='$desc4save'" ) ;
			echo _AM_D3IMGTAG_MB_FINISHED."<br />\n" ;

			$filecount ++ ;
		}
	}
	closedir( $dir_h ) ;

	if( $filecount <= 1 ) {
		echo "<p>$dir4edit : "._MD_D3IMGTAG_MES_BATCHNONE."</p>" ;
	} else {
		printf( "<p>"._MD_D3IMGTAG_MES_BATCHDONE."</p>" , $filecount - 1 ) ;
	}

	$result_str = ob_get_contents() ;
	ob_end_clean() ;
}

echo "c1";

// Make form objects
$form = new  icms_form_Theme(_MD_D3IMGTAG_PHOTOBATCHUPLOAD , "batchupload" , "index.php?page=batch");

$title_text = new icms_form_elements_Text("" , "title" , 50 , 255 , $title4edit);
$title_tray = new icms_form_elements_Tray(_AM_D3IMGTAG_TH_TITLE , '<br /><br />');


$title_tray->addElement($title_text);
$title_tray->addElement(new icms_form_elements_Label("" , _MD_D3IMGTAG_BATCHBLANK));

$cat = new icms_form_elements_Select(_AM_D3IMGTAG_TH_CATEGORIES , "cid" , $cid);
$tree = $cattree->getChildTreeArray(0 , 'title');
foreach($tree as $leaf) {
	$leaf['prefix'] = substr($leaf['prefix'], 0, -1);
	$leaf['prefix'] = str_replace(".", "--", $leaf['prefix']);
	$cat->addOption($leaf['cid'] , $leaf['prefix'] . $leaf['title']);
}
if ($d3imgtag_enableshare) {
	$share = new icms_form_elements_Checkbox(_MD_D3IMGTAG_SHAREIMG, "shareimg");
	$share->addOption(1, _MD_D3IMGTAG_SHAREIMGDESC);
}

echo "c2";

$submitter_select = new icms_form_elements_select_User(_AM_D3IMGTAG_TH_SUBMITTER, 'submitter', false, $submitter);

$date_text = new icms_form_elements_Text(_AM_D3IMGTAG_TH_DATE, 'post_date', 20, 20, formatTimestamp(time(), _MD_D3IMGTAG_DTFMT_YMDHI));

$dir_tray = new icms_form_elements_Tray(_MD_D3IMGTAG_TEXT_DIRECTORY , '<br /><br />');


$dir_text = new icms_form_elements_Text(_MD_D3IMGTAG_PHOTOPATH , "dir", 50, 255 , $dir4edit);
$dir_tray->addElement($dir_text);
$dir_tray->addElement(new icms_form_elements_Label(_MD_D3IMGTAG_DESC_PHOTOPATH));
$desc_tarea = new icms_form_elements_Dhtmltextarea(_AM_D3IMGTAG_TH_DESCRIPTION , 'desc' , $desc4edit , 10 , 50);
$submit_button = new icms_form_elements_Button('' , "submit" , _SUBMIT , 'submit');


// Render forms
icms_cp_header();
include dirname(__FILE__).'/mymenu.php';

echo "<h3 style='text-align:left;'>".sprintf(_AM_D3IMGTAG_H3_FMT_BATCHREGISTER, $xoopsModule->name())."</h3>\n";
echo "<h5 style='text-align:left;color:#FF0000;'>"._AM_D3IMGTAG_H3_FMT_BATCHREGISTERNOTICE."</h5>\n";
d3imgtag_opentable();
$form->addElement($title_tray);
$form->addElement($desc_tarea);
$form->addElement($share);
$form->addElement($cat);
$form->addElement($dir_tray);
$form->addElement($submitter_select);
$form->addElement($date_text);
$form->addElement($submit_button);
$form->setRequired($dir_text);
$form->display();
d3imgtag_closetable();

if( isset( $result_str ) ) {
	echo "<br />\n" ;
	echo $result_str ;
}

icms_cp_footer();

?>
