<?
// 圈子创建第2步，提交介绍和封面图片
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_circle_edit2.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$circleId = ParamUtil::getRequestNumber("circleId", 0);
$comment = ParamUtil::getRequestString("comment");

//清理圈子相册数据
$clear = ParamUtil::getRequestBoolean("clear");
$deleteImages = ParamUtil::getRequestString("deleteImages");

//参数验证
if(empty($unionId) || empty($circleId)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//判断用户是否已经存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseOK($result);
}

$userMst = $userList[0];

//圈子是否存在
$param = array();
$param['id'] = $circleId;
$param['owner_id'] = $userMst->id;
$param['delete_flg'] = false;
$circleDatList = CircleDat::getList($param,'id','desc', 0, 1);
if(empty($circleDatList)) {
	$result["message"] = "数据错误！";
	responseOK($result);
}

$circleDat = $circleDatList[0];

//带有comment的时候
if(!empty($comment)) {
	$circleDat->comment = $comment;
	$circleDat->save();
}

//处理图片

//处理上传图片【封面】
if(!empty($_FILES['frontImage']['tmp_name'])){
	$aliHandler = new AliUploadHandler();
	$upload_file = FileUploadUtil::getScalarFile(@$_FILES['frontImage']);
	$tmp_file_name = date("Ymd") . "_" . trim(basename($upload_file->name));//保存的名字自定义，这里如果图片的名字原来的是aa.jp，保存成20200220_aa.jpg
	
	$url = $aliHandler->uploadImg($tmp_file_name, $upload_file->tmp_name);
	ErrorLogger::doOutput("Compass...ajax_circle_edit2.php....url1=" . $url, 0);
	//这里的url就是http://compass-dev.oss-cn-beijing.aliyuncs.com/20200220_aa.jpg
	$oldFrontImage = $circleDat->front_image;
	$circleDat->front_image = $url;
	$circleDat->save();
	
	//删除oss的图片
	$aliHandler->delete($oldFrontImage);
}

//处理上传图片【图片】
if(!empty($_FILES['image']['tmp_name'])){
	$aliHandler = new AliUploadHandler();
	$upload_file = FileUploadUtil::getScalarFile(@$_FILES['image']);
	$tmp_file_name = date("Ymd") . "_" . trim(basename($upload_file->name));//保存的名字自定义，这里如果图片的名字原来的是aa.jp，保存成20200220_aa.jpg
	
	ErrorLogger::doOutput("Compass...ajax_circle_edit2.php....tmp_file_name==".$_FILES['image']['size'], 0);
	
	$url = $aliHandler->uploadImg($tmp_file_name, $upload_file->tmp_name);
	//这里的url就是http://compass-dev.oss-cn-beijing.aliyuncs.com/20200220_aa.jpg
	$circleDat = CircleDat::getById($circleId);
	if(empty($circleDat->images)) {
	  $circleDat->images = $url;
	} else {
	  $circleDat->images = $circleDat->images . "|" . $url;
	}
	$result["url"] = $url;
	$circleDat->save();
}

//清理相册已经删除的图片
if($clear==true && !empty($deleteImages) && !empty($circleDat->images)) {
	$aliHandler = new AliUploadHandler();
	$images = "|" . $circleDat->images;
	//竖线分割为数组
	$deleteImageArray = explode("|",$deleteImages);
	if(!empty($deleteImageArray)) {
		foreach($deleteImageArray as $deleteImage) {
			ErrorLogger::doOutput("Compass...ajax_circle_edit2.php....images==" . $images, 0);
			$images = str_replace("|".$deleteImage,"",$images);
			//删除oss的图片
			$aliHandler->delete($deleteImage);
			
		}
	}
	//更新回数据库
	if(empty($images)) {
		$circleDat->images = "";
	} else {
		$circleDat->images = substr($images, 1);
	}
	$circleDat->save();
}


ErrorLogger::doOutput("Compass...ajax_circle_edit2.php....End.", 0);

$result["message"] = "提交成功！";
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}

?>