From 38485607524e17ae8487b7b7548c6728e67a10ca Mon Sep 17 00:00:00 2001 From: phibel Date: Sun, 30 Sep 2018 23:17:58 +0200 Subject: [PATCH] ENH allow user to delete his own post, if it is the last post in the thread --- e107_plugins/forum/forum_class.php | 41 +++++++++++++++++ e107_plugins/forum/forum_viewtopic.php | 46 +++++++++++-------- .../shortcodes/batch/view_shortcodes.php | 11 +++++ 3 files changed, 80 insertions(+), 18 deletions(-) diff --git a/e107_plugins/forum/forum_class.php b/e107_plugins/forum/forum_class.php index 1af36d6c5..7fa410d2e 100644 --- a/e107_plugins/forum/forum_class.php +++ b/e107_plugins/forum/forum_class.php @@ -368,6 +368,47 @@ class e107forum } + /** + * Allow a user to delete their own post, if it is the last post in the thread. + */ + function usersLastPostDeletion() + { + $ret = array('hide' => false, 'msg' => LAN_FORUM_7008, 'status' => 'error'); + $actionAllowed = false; + + if (isset($_POST['post']) && is_numeric($_POST['post'])) + { + $postId = intval($_POST['post']); + $sql = e107::getDb(); + $query = "SELECT fp.post_user + FROM #forum_post AS fp + WHERE fp.post_id = ". $postId; + if ($sql->gen($query) > 0) + { + $row = $sql->fetch(); + if (USERID == $row['post_user']) $actionAllowed = true; + } + } + + if ($actionAllowed && $_POST['action'] == 'deletepost') + { + if ($this->postDelete($postId)) + { + $ret['msg'] = ''.LAN_FORUM_8021.' #'.$postId; + $ret['hide'] = true; + $ret['status'] = 'ok'; + } + else + { + $ret['msg'] = "".LAN_FORUM_8021." #".$postId; + $ret['status'] = 'error'; + } + } + echo json_encode($ret); + exit(); + } + + /** * get user ids with moderator permissions for the given $postId * @param $postId id of a forum post diff --git a/e107_plugins/forum/forum_viewtopic.php b/e107_plugins/forum/forum_viewtopic.php index c832b9e4b..57eb63827 100644 --- a/e107_plugins/forum/forum_viewtopic.php +++ b/e107_plugins/forum/forum_viewtopic.php @@ -69,9 +69,27 @@ if(vartrue($_GET['id']) && isset($_GET['dl'])) exit; } +if (isset($_GET['last'])) +{ + $_GET['f'] = 'last'; +} + +if(isset($_GET['f']) && $_GET['f'] == 'post') +{ + $thread->processFunction(); +} + +$thread->init(); + + +/* Check if use has moderator permissions for this thread */ +$moderatorUserIds = $forum->getModeratorUserIdsByThreadId($thread->threadInfo['thread_id']); +define('MODERATOR', (USER && in_array(USERID, $moderatorUserIds))); + + if(e_AJAX_REQUEST) { - if(varset($_POST['action']) == 'quickreply') + if(varset($_POST['action']) == 'quickreply') { $forum->ajaxQuickReply(); } @@ -85,22 +103,12 @@ if(e_AJAX_REQUEST) { $forum->ajaxModerate(); } - + else if(varset($_POST['action']) == 'deletepost') + { + $forum->usersLastPostDeletion(); + } } - -if (isset($_GET['last'])) -{ - $_GET['f'] = 'last'; -} - -if(isset($_GET['f']) && $_GET['f'] == 'post') -{ - $thread->processFunction(); -} - -$thread->init(); - /* if(isset($_POST['track_toggle'])) @@ -145,9 +153,6 @@ define('e_PAGETITLE', strip_tags($tp->toHTML($thread->threadInfo['thread_name'], $forum->modArray = $forum->forumGetMods($thread->threadInfo['forum_moderators']); -/* Check if use has moderator permissions for this thread */ -$moderatorUserIds = $forum->getModeratorUserIdsByThreadId($thread->threadInfo['thread_id']); -define('MODERATOR', (USER && in_array(USERID, $moderatorUserIds))); e107::getScBatch('view', 'forum')->setScVar('forum', $forum); //var_dump(e107::getScBatch('forum', 'forum')); @@ -485,6 +490,8 @@ $i = $thread->page; $sc->wrapper('forum_viewtopic/end'); $forend = $tp->parseTemplate($FORUMEND, true, $sc); +$lastPostDetectionCounter = count($postList); +$sc->setScVar('thisIsTheLastPost', false); foreach ($postList as $c => $postInfo) { @@ -494,6 +501,9 @@ foreach ($postList as $c => $postInfo) } $loop_uid = (int)$postInfo['post_user']; + $lastPostDetectionCounter--; + if ($lastPostDetectionCounter == 0) $sc->setScVar('thisIsTheLastPost', true); + //---- Orphan $tnum???? $tnum = $i; diff --git a/e107_plugins/forum/shortcodes/batch/view_shortcodes.php b/e107_plugins/forum/shortcodes/batch/view_shortcodes.php index d96bde48d..079e497db 100644 --- a/e107_plugins/forum/shortcodes/batch/view_shortcodes.php +++ b/e107_plugins/forum/shortcodes/batch/view_shortcodes.php @@ -859,6 +859,17 @@ } + // Delete own post, if it is the last in the thread + if($this->thisIsTheLastPost && USER && $this->thread->threadInfo['thread_lastuser'] == USERID) + { + /* only show delete button when post is not the initial post of the topic + * AND if this post is the last post in the thread */ + if($this->thread->threadInfo['thread_active'] && empty($this->postInfo['thread_start']) ) + { + $text .= "
  • " . LAN_DELETE . " " . $tp->toGlyph('trash') . "
  • "; + } + } + if($this->forum->checkperm($this->postInfo['post_forum'], 'post')) { $url = e107::url('forum', 'post') . "?f=quote&id=" . $this->postInfo['post_thread'] . "&post=" . $this->postInfo['post_id'];