Skip to content

Commit

Permalink
#311 게시글을 휴지통으로 이동시키거나 삭제할 때 발생하던 에러 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
xharpenParksuhyeon committed Aug 20, 2021
1 parent c613701 commit 1a9590c
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/Plugin/Intercepts/ReplyIntercepts.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Xpressengine\Plugins\Board\{
BoardPermissionHandler,
Components\Modules\BoardModule,
ConfigHandler,
Handler as BoardHandler,
Plugin as BoardPlugin,
IdentifyManager,
Expand Down Expand Up @@ -165,48 +164,55 @@ public static function interceptValidateUpdated()
*/
public static function interceptProtectDeleted()
{
$function = function ($function, Board $item, ConfigEntity $config, IdentifyManager $identifyManager) {
/** @var Board $parentBoard */
$parentBoard = null;
$function = function ($function, Board $item, ConfigEntity $config) {
$replyConfig = ReplyConfigHandler::make()->getByBoardConfig($item->instance_id);
$isManager = app(BoardPermissionHandler::class)->checkManageAction($item->instance_id);

if ($replyConfig !== null) {
if ($parentBoard = $item->findParentDoc()) {
// set redirect url
$routeAction = request()->route()->getAction();

if (Arr::get($routeAction, 'module') === BoardModule::getId() && in_array(Arr::get($routeAction, 'as'), ['destroy', 'trash'])) {
request()->merge([
'redirect_url' => app(BoardUrlHandler::class)->getShow($parentBoard, request()->query->all()),
'redirect_message' => xe_trans('board::deletedReply')
]);
}

if ($replyConfig !== null && $item->hasParentDoc()) {
$parentBoard = Board::with('data')->findOrFail($item->parent_id);

// set redirect url
$routeAction = request()->route()->getAction();

if (Arr::get($routeAction, 'module') === BoardModule::getId() && Arr::get($routeAction, 'as') === 'destroy') {
request()->merge([
'redirect_url' => app(BoardUrlHandler::class)->getShow($parentBoard, request()->query->all()),
'redirect_message' => xe_trans('board::deletedReply')
]);
}
}
// 채택된 질문을 삭제하는 경우..
if ($item->isAdopted($parentBoard)) {
if ($isManager === false) {
throw new CanNotDeletedAdoptedException; // 채택된 글은 삭제할 수 없습니다.
}

if ($replyConfig !== null && app(BoardPermissionHandler::class)->checkManageAction($item->instance_id) === false) {
if ($replyConfig->get('protectDeleted', false) && $item->existsReplies()) {
throw new CanNotDeleteHasReplyException; // 답글이 적힌 글은 삭제할 수 없습니다.
$parentBoard->getAttribute('data')->adopt_id = null;
$parentBoard->getAttribute('data')->adopt_at = null;
$parentBoard->getAttribute('data')->save();
}
}

if ($parentBoard !== null && $item->isAdopted($parentBoard)) {
throw new CanNotDeletedAdoptedException; // 채택된 글은 삭제할 수 없습니다.
else if ($item->existsReplies() === true) {
if ($replyConfig->get('protectDeleted', false) === true && $isManager === false) {
throw new CanNotDeleteHasReplyException; // 답글이 적힌 글은 삭제할 수 없습니다.
}
}
}

if ($parentBoard !== null && $item->isAdopted($parentBoard)) {
$parentBoard->getAttribute('data')->adopt_id = null;
$parentBoard->getAttribute('data')->adopt_at = null;
$parentBoard->getAttribute('data')->save();
}

return $function($item, $config, $identifyManager);
return $function($item, $config);
};

// destroy
// trash
intercept(
sprintf('%s@trash', BoardHandler::class),
sprintf('%s::reply__trashed', BoardPlugin::getId()),
$function
);

// remove
intercept(
sprintf('%s@destroy', BoardService::class),
sprintf('%s::reply__protectDestroyed', BoardPlugin::getId()),
sprintf('%s@remove', BoardHandler::class),
sprintf('%s::reply__removed', BoardPlugin::getId()),
$function
);
}
Expand Down

0 comments on commit 1a9590c

Please sign in to comment.