/** * Delete post / thread * * @return integer */ private function deletePost($post) { $app = JFactory::getApplication(); $CofiHelper = new CofiHelper(); $db =& $this->getDBO(); // 1. get thread id $_threadQuery = "SELECT thread FROM " . $db->nameQuote('#__discussions_messages') . " WHERE id='" . $post . "'"; $db->setQuery($_threadQuery); $_threadId = $db->loadResult(); // 2. get parent id $_parentQuery = "SELECT parent_id FROM " . $db->nameQuote('#__discussions_messages') . " WHERE id='" . $post . "'"; $db->setQuery($_parentQuery); $_parentId = $db->loadResult(); // 3. get category id $_categoryQuery = "SELECT cat_id FROM " . $db->nameQuote('#__discussions_messages') . " WHERE id='" . $post . "'"; $db->setQuery($_categoryQuery); $_categoryId = $db->loadResult(); // 4. get user id $_userQuery = "SELECT user_id FROM " . $db->nameQuote('#__discussions_messages') . " WHERE id='" . $post . "'"; $db->setQuery($_userQuery); $_userId = $db->loadResult(); // Check if thread id = post id, means this is the original post OP // if OP then delete all posts in this thread (delete thread) // if not OP then delete single post if ($_threadId == $post) { // get post ids in this thread $sql = "SELECT id FROM" . $db->nameQuote('#__discussions_messages') . " WHERE thread='" . $_threadId . "'"; $db->setQuery($sql); $_postList = $db->loadAssocList(); // remove images of these posts if (count($_postList)) { foreach ($_postList as $_post) { $p_id = $_post['id']; // remove images $CofiHelper->deleteImagesByPostId($p_id); } $rootDir = JPATH_ROOT; $threadfolder = $rootDir . "/images/discussions/posts/" . $_threadId; if (is_dir($threadfolder)) { rmdir($threadfolder); } } // get users who posted in this thread $sql = "SELECT DISTINCT user_id FROM" . $db->nameQuote('#__discussions_messages') . " WHERE thread='" . $_threadId . "'"; $db->setQuery($sql); $_userList = $db->loadAssocList(); // delete thread ( all posts in it) from db $sql = "DELETE FROM " . $db->nameQuote('#__discussions_messages') . " WHERE thread='" . $_threadId . "'"; $db->setQuery($sql); $db->query(); // now update user stats if (count($_userList)) { foreach ($_userList as $_user) { $u_id = $_user['user_id']; // update user stats $result = $CofiHelper->updateUserStats($u_id); } } } else { // not OP // remove images belonging to this post $CofiHelper->deleteImagesByPostId($post); // delete post from db $sql = "DELETE FROM " . $db->nameQuote('#__discussions_messages') . " WHERE id='" . $post . "'"; $db->setQuery($sql); $db->query(); // change parent id of possible replies to this post $sql = "UPDATE " . $db->nameQuote('#__discussions_messages') . " SET parent_id = '" . $_parentId . "'" . " WHERE parent_id = '" . $post . "'"; $db->setQuery($sql); $result = $db->query(); // update category stats $result = $CofiHelper->updateThreadStats($_threadId); // update user stats $result = $CofiHelper->updateUserStats($_userId); } // update category stats $result = $CofiHelper->updateCategoryStats($_categoryId); // redirect link $redirectLink = JRoute::_("index.php?option=com_discussions&view=index"); $app->redirect($redirectLink, JText::_('COFI_POST_DELETED'), "notice"); return 0; }