505 lines
16 KiB
PHP
505 lines
16 KiB
PHP
|
|
<?php
|
||
|
|
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Collection 动作处理器
|
||
|
|
*/
|
||
|
|
class Collection_Action extends Typecho_Widget implements Widget_Interface_Do
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 数据库连接
|
||
|
|
*/
|
||
|
|
private $db;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 构造函数
|
||
|
|
*/
|
||
|
|
public function __construct($request, $response, $params = NULL)
|
||
|
|
{
|
||
|
|
parent::__construct($request, $response, $params);
|
||
|
|
$this->db = Collection_Plugin::getDbConnection();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 动作入口
|
||
|
|
*/
|
||
|
|
public function action()
|
||
|
|
{
|
||
|
|
// 检查管理员权限
|
||
|
|
$user = Typecho_Widget::widget('Widget_User');
|
||
|
|
if (!$user->hasLogin() || !$user->pass('administrator', true)) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '无权限访问'
|
||
|
|
));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 确保返回JSON格式
|
||
|
|
$this->response->setContentType('application/json');
|
||
|
|
|
||
|
|
$do = $this->request->get('do');
|
||
|
|
if (!$do) {
|
||
|
|
$do = $this->request->isPost() ? $this->request->get('do') : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
switch ($do) {
|
||
|
|
case 'add':
|
||
|
|
$this->add();
|
||
|
|
break;
|
||
|
|
case 'update':
|
||
|
|
$this->update();
|
||
|
|
break;
|
||
|
|
case 'delete':
|
||
|
|
$this->delete();
|
||
|
|
break;
|
||
|
|
case 'get':
|
||
|
|
$this->get();
|
||
|
|
break;
|
||
|
|
case 'getAll':
|
||
|
|
$this->getAll();
|
||
|
|
break;
|
||
|
|
case 'getArticleInfo':
|
||
|
|
$this->getArticleInfo();
|
||
|
|
break;
|
||
|
|
case 'getCommentInfo':
|
||
|
|
$this->getCommentInfo();
|
||
|
|
break;
|
||
|
|
case 'getUserInfo':
|
||
|
|
$this->getUserInfo();
|
||
|
|
break;
|
||
|
|
case 'getRelatedArticlesInfo':
|
||
|
|
$this->getRelatedArticlesInfo();
|
||
|
|
break;
|
||
|
|
case 'getRelatedCommentsInfo':
|
||
|
|
$this->getRelatedCommentsInfo();
|
||
|
|
break;
|
||
|
|
case 'getRelatedUsersInfo':
|
||
|
|
$this->getRelatedUsersInfo();
|
||
|
|
break;
|
||
|
|
case 'batchDelete':
|
||
|
|
$this->batchDelete();
|
||
|
|
break;
|
||
|
|
case 'test':
|
||
|
|
$this->test();
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '无效的操作'
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 测试方法
|
||
|
|
*/
|
||
|
|
public function test()
|
||
|
|
{
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Collection插件Action工作正常',
|
||
|
|
'timestamp' => time()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 添加合集
|
||
|
|
*/
|
||
|
|
public function add()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$name = $this->request->get('name');
|
||
|
|
$description = $this->request->get('description');
|
||
|
|
$related_items = $this->request->get('related_items');
|
||
|
|
$sort_order = $this->request->get('sort_order', 'created_desc');
|
||
|
|
$collection_type = $this->request->get('collection_type', 'article');
|
||
|
|
|
||
|
|
if (empty($name)) {
|
||
|
|
throw new Exception('合集名称不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查名称是否已存在
|
||
|
|
$stmt = $this->db->prepare("SELECT COUNT(*) FROM plugin_collection WHERE name = ? AND collection_type = ? AND is_active = 1");
|
||
|
|
$stmt->execute(array($name, $collection_type));
|
||
|
|
$count = $stmt->fetchColumn();
|
||
|
|
|
||
|
|
if ($count > 0) {
|
||
|
|
throw new Exception('合集名称已存在');
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = $this->db->prepare("INSERT INTO plugin_collection (name, description, related_items, sort_order, collection_type) VALUES (?, ?, ?, ?, ?)");
|
||
|
|
$stmt->execute(array($name, $description, $related_items, $sort_order, $collection_type));
|
||
|
|
|
||
|
|
$id = $this->db->lastInsertId();
|
||
|
|
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'message' => '合集添加成功',
|
||
|
|
'data' => array('id' => $id)
|
||
|
|
));
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '添加失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 更新合集
|
||
|
|
*/
|
||
|
|
public function update()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $this->request->get('id');
|
||
|
|
$name = $this->request->get('name');
|
||
|
|
$description = $this->request->get('description');
|
||
|
|
$related_items = $this->request->get('related_items');
|
||
|
|
$sort_order = $this->request->get('sort_order', 'created_desc');
|
||
|
|
$collection_type = $this->request->get('collection_type', 'article');
|
||
|
|
|
||
|
|
if (empty($id) || empty($name)) {
|
||
|
|
throw new Exception('ID和合集名称不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查名称是否已存在(排除自身)
|
||
|
|
$stmt = $this->db->prepare("SELECT COUNT(*) FROM plugin_collection WHERE name = ? AND collection_type = ? AND id != ? AND is_active = 1");
|
||
|
|
$stmt->execute(array($name, $collection_type, $id));
|
||
|
|
$count = $stmt->fetchColumn();
|
||
|
|
|
||
|
|
if ($count > 0) {
|
||
|
|
throw new Exception('合集名称已存在');
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = $this->db->prepare("UPDATE plugin_collection SET name = ?, description = ?, related_items = ?, sort_order = ?, collection_type = ? WHERE id = ?");
|
||
|
|
$result = $stmt->execute(array($name, $description, $related_items, $sort_order, $collection_type, $id));
|
||
|
|
|
||
|
|
if ($result) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'message' => '合集更新成功'
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('合集不存在或更新失败');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '更新失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 删除合集
|
||
|
|
*/
|
||
|
|
public function delete()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $this->request->get('id');
|
||
|
|
|
||
|
|
if (empty($id)) {
|
||
|
|
throw new Exception('ID不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = $this->db->prepare("UPDATE plugin_collection SET is_active = 0 WHERE id = ?");
|
||
|
|
$result = $stmt->execute(array($id));
|
||
|
|
|
||
|
|
if ($result) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'message' => '合集删除成功'
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('合集不存在或删除失败');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '删除失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 批量删除合集
|
||
|
|
*/
|
||
|
|
public function batchDelete()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$ids = $this->request->filter('int')->getArray('collection');
|
||
|
|
|
||
|
|
if (empty($ids)) {
|
||
|
|
$rawIds = $this->request->get('collection');
|
||
|
|
if (!empty($rawIds)) {
|
||
|
|
$ids = is_array($rawIds) ? $rawIds : array($rawIds);
|
||
|
|
$ids = array_map('intval', $ids);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (empty($ids) || !is_array($ids)) {
|
||
|
|
throw new Exception('请选择要删除的合集');
|
||
|
|
}
|
||
|
|
|
||
|
|
$ids = array_filter($ids, function($id) {
|
||
|
|
return is_numeric($id) && $id > 0;
|
||
|
|
});
|
||
|
|
|
||
|
|
if (empty($ids)) {
|
||
|
|
throw new Exception('无效的合集ID');
|
||
|
|
}
|
||
|
|
|
||
|
|
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||
|
|
$stmt = $this->db->prepare("UPDATE plugin_collection SET is_active = 0 WHERE id IN ($placeholders)");
|
||
|
|
$result = $stmt->execute($ids);
|
||
|
|
|
||
|
|
if ($result) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'message' => '合集批量删除成功'
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('删除失败');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '批量删除失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取单个合集
|
||
|
|
*/
|
||
|
|
public function get()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$id = $this->request->get('id');
|
||
|
|
|
||
|
|
if (empty($id)) {
|
||
|
|
throw new Exception('ID不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$stmt = $this->db->prepare("SELECT * FROM plugin_collection WHERE id = ? AND is_active = 1");
|
||
|
|
$stmt->execute(array($id));
|
||
|
|
$collection = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
|
|
|
||
|
|
if ($collection) {
|
||
|
|
// 获取关联内容信息
|
||
|
|
if (!empty($collection['related_items'])) {
|
||
|
|
if ($collection['collection_type'] == 'comment') {
|
||
|
|
$collection['related_items_info'] = Collection_Plugin::getRelatedCommentsInfo($collection['related_items']);
|
||
|
|
} elseif ($collection['collection_type'] == 'user') {
|
||
|
|
$collection['related_items_info'] = Collection_Plugin::getRelatedUsersInfo($collection['related_items']);
|
||
|
|
} else {
|
||
|
|
$collection['related_items_info'] = Collection_Plugin::getRelatedArticlesInfo($collection['related_items']);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $collection
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('合集不存在');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '查询失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取所有合集
|
||
|
|
*/
|
||
|
|
public function getAll()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$stmt = $this->db->query("SELECT * FROM plugin_collection WHERE is_active = 1 ORDER BY collection_type, created_at DESC");
|
||
|
|
$collections = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||
|
|
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $collections
|
||
|
|
));
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '查询失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取文章信息
|
||
|
|
*/
|
||
|
|
public function getArticleInfo()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$article_cid = $this->request->get('article_cid');
|
||
|
|
|
||
|
|
if (empty($article_cid)) {
|
||
|
|
throw new Exception('文章CID不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$articleInfo = Collection_Plugin::getArticleInfo($article_cid);
|
||
|
|
|
||
|
|
if (!empty($articleInfo['title'])) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $articleInfo
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('文章不存在');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '获取文章信息失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取评论信息
|
||
|
|
*/
|
||
|
|
public function getCommentInfo()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$comment_coid = $this->request->get('comment_coid');
|
||
|
|
|
||
|
|
if (empty($comment_coid)) {
|
||
|
|
throw new Exception('评论COID不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$commentInfo = Collection_Plugin::getCommentInfo($comment_coid);
|
||
|
|
|
||
|
|
if (!empty($commentInfo['author'])) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $commentInfo
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('评论不存在');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '获取评论信息失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取用户信息
|
||
|
|
*/
|
||
|
|
public function getUserInfo()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$user_uid = $this->request->get('user_uid');
|
||
|
|
|
||
|
|
if (empty($user_uid)) {
|
||
|
|
throw new Exception('用户UID不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$userInfo = Collection_Plugin::getUserInfo($user_uid);
|
||
|
|
|
||
|
|
if (!empty($userInfo['name'])) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $userInfo
|
||
|
|
));
|
||
|
|
} else {
|
||
|
|
throw new Exception('用户不存在');
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '获取用户信息失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取关联文章信息
|
||
|
|
*/
|
||
|
|
public function getRelatedArticlesInfo()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$related_articles = $this->request->get('related_articles');
|
||
|
|
|
||
|
|
if (empty($related_articles)) {
|
||
|
|
throw new Exception('关联文章不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = Collection_Plugin::getRelatedArticlesInfo($related_articles);
|
||
|
|
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $result
|
||
|
|
));
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '获取关联文章信息失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取关联评论信息
|
||
|
|
*/
|
||
|
|
public function getRelatedCommentsInfo()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$related_comments = $this->request->get('related_comments');
|
||
|
|
|
||
|
|
if (empty($related_comments)) {
|
||
|
|
throw new Exception('关联评论不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = Collection_Plugin::getRelatedCommentsInfo($related_comments);
|
||
|
|
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $result
|
||
|
|
));
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '获取关联评论信息失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取关联用户信息
|
||
|
|
*/
|
||
|
|
public function getRelatedUsersInfo()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$related_users = $this->request->get('related_users');
|
||
|
|
|
||
|
|
if (empty($related_users)) {
|
||
|
|
throw new Exception('关联用户不能为空');
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = Collection_Plugin::getRelatedUsersInfo($related_users);
|
||
|
|
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => true,
|
||
|
|
'data' => $result
|
||
|
|
));
|
||
|
|
} catch (Exception $e) {
|
||
|
|
$this->response->throwJson(array(
|
||
|
|
'success' => false,
|
||
|
|
'message' => '获取关联用户信息失败: ' . $e->getMessage()
|
||
|
|
));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|