<?php
include '../env.php';
require_once(LIB.'common.inc.php');
require_once(LIB.'Post.class.php');
$post = new Post();

if (!is_login()) {
	_a_log_fail('favorite', 'create', 'invalid auth');
	echo json_encode(FALSE);
	die;
}

$log_ret = _a_log2('favorite', 'create', array(
	'detail' => serialize($_POST),
	'target_key' => @$_POST['name']
));

if ($log_ret) {
	$ret = $post->addFavorite(
		empty($_POST['name']) ? date('Y/m/d H:i:s') : $_POST['name'],
		@$_POST['item_ids']
	);
	_a_log_write($log_ret, array(
		'success' => $ret === FALSE ? '0' : '1',
		'target_id' => $ret === FALSE ? NULL : $ret
	));

	if ($ret === FALSE) {
		echo json_encode($ret);
	} else {
		echo json_encode($post->readFavoriteCache($ret));
	}
} else {
	echo json_encode(FALSE);
}
?>