<?php
if(!defined('IN_MYMPS')) exit('Forbidden');
require MYMPS_INC.'/upfile.fun.php';

!in_array($ac,array('upload','list','edit','delete')) && $ac = 'list';
$mymps_image = '';

if(submit_check('album_submit')){
	
	$name_file = 'album_up';
	
	if($ac == 'upload'){
		$album_maxpost = $group['album_maxpost'];
		$cur_album=mymps_count("member_album"," WHERE userid = '$s_uid'");
		if($cur_album >= $album_maxpost && $album_maxpost > 0){
			write_msg('','?m=album&type=corp&error=49');
			exit;
		}
		if(empty($title)) write_msg('','?m=album&type=corp&ac=upload&error=28');
		if ($_FILES[$name_file]['name']){
			check_upimage($name_file);
			$destination = "/album/".date('Ymd')."/";
			$mymps_image = start_upload($name_file,$destination,$mymps_global['cfg_upimg_watermark'],$mymps_mymps['cfg_memberalbum_limit']['width'],$mymps_mymps['cfg_memberalbum_limit']['height']);
			$sql = "INSERT INTO `{$db_mymps}member_album` (id,title,path,prepath,pubtime,userid)
					Values('','$title','$mymps_image[0]','$mymps_image[1]','$timestamp','$s_uid')";
			$db->query($sql);
			unset($destination,$mymps_image);
		}else{
			write_msg('','?m=album&type=corp&ac=upload&error=29');
		}
		
		write_msg('','?m=album&type=corp&ac=upload&success=12');
		
	} elseif($ac == 'edit') {
		
		if(empty($id)) write_msg('','?m=album&type=corp&ac=list&error=1');
		if(empty($title)) write_msg('','?m=album&type=corp&ac=edit&error=28&id='.$id);
		
		if ($_FILES[$name_file]['name']){
			check_upimage($name_file);
			$destination="/album/".date('Ymd')."/";
			$mymps_image=start_upload($name_file,$destination,1,$mymps_mymps['cfg_memberalbum_limit']['width'],$mymps_mymps['cfg_memberalbum_limit']['height'],$path,$prepath);
			$path = $mymps_image[0];
			$prepath = $mymps_image[1];
			unset($destination,$mymps_image);
		}
		
		$db->query("UPDATE `{$db_mymps}member_album` SET title = '$title',path = '$path', prepath = '$prepath',pubtime = '$timestamp' $where AND id = '$id'");
		write_msg('','?m=album&type=corp&ac=edit&success=8&id='.$id);
		
	}
	
} else {
	
	if($ac == 'upload'){
		$album_maxpost = $group['album_maxpost'];
		$cur_album=mymps_count("member_album"," WHERE userid = '$s_uid'");
		if($cur_album >= $album_maxpost && $album_maxpost > 0){
			write_msg('','?m=album&type=corp&error=49');
			exit;
		}
	} elseif($ac == 'list') {
		$sql 		= "SELECT * FROM {$db_mymps}member_album $where ORDER BY id DESC";
		$rows_num 	= mymps_count("member_album",$where);
		$param		= setParam(array('m','type','ac'));
		$album  	= page1($sql);
	} elseif($ac == 'edit') {
		if(empty($id)) write_msg('','?m=album&type=corp&error=1');
		$edit = $db->getRow("SELECT id,title,path,prepath FROM `{$db_mymps}member_album` $where AND id  = '$id'");
		if(empty($edit['id'])) write_msg('','?m=album&type=corp&ac=list&error=27');
	} elseif($ac == 'delete') {
		if(empty($id)) write_msg('','?m=album&type=corp&error=1');
		if(!$row = $db->getRow("SELECT path,prepath FROM `{$db_mymps}member_album` $where AND id  = '$id'")){
			write_msg('','?m=album&type=corp&ac=list&error=27');
		} else {
			delete_mayi_img($row['path']);
			delete_mayi_img($row['prepath']);
			$res = $db->query("DELETE FROM `{$db_mymps}member_album` $where AND id  = '$id'");
		}
		write_msg('','?m=album&type=corp&ac=list&success=8');
	}
	$location = location('corp');
	include mayi_tpl('album_'.(in_array($ac,array('upload','edit')) ? 'upload' : $ac));
	unset($album);
}
?>