public function getData($params) { if (!class_exists('EasyDiscussModelPosts')) { jimport('joomla.application.component.model'); JLoader::import('posts', DISCUSS_MODELS); } $count = (int) trim($params->get('count', 0)); $db = DiscussHelper::getDBO(); $queryExclude = ''; $excludeCats = DiscussHelper::getPrivateCategories(); if (!empty($excludeCats)) { $queryExclude .= ' AND a.`category_id` NOT IN (' . implode(',', $excludeCats) . ')'; } // posts $query = 'select a.`isresolve`, a.`id`, a.`user_id`, a.`user_type`, a.`poster_name`, a.`title`, a.`id` as `parent_id`, a.category_id,'; $query .= ' (select count(1) from `#__discuss_votes` as b1 where b1.`post_id` = a.`id`) as `VotedCnt`, count( c.id ) as `num_replies`'; $query .= ' from `#__discuss_posts` as a '; $query .= ' left join `#__discuss_posts` as c on a.`id` = c.`parent_id`'; $query .= ' and c.`published` = 1'; $query .= ' inner join `#__discuss_votes` as b on a.`id` = b.`post_id`'; $query .= ' where a.`parent_id` = 0'; $query .= ' and a.`published` = 1'; if (!empty($excludeCats)) { $query .= ' AND a.`category_id` NOT IN (' . implode(',', $excludeCats) . ')'; } $query .= ' group by a.`id`'; // union both posts and replies $query .= ' union '; // replies $query .= ' select c.`isresolve`, a.`id`, a.`user_id`, a.`user_type`, a.`poster_name`, a.`title`, a.`parent_id`, c.category_id, '; $query .= ' count( b.id ) as `VotedCnt`, 0 as `num_replies`'; $query .= ' from `#__discuss_posts` as a'; $query .= ' inner join `#__discuss_posts` as c on a.`parent_id` = c.`id`'; $query .= ' inner join `#__discuss_votes` as b on a.`id` = b.`post_id`'; $query .= ' where a.`published` = 1'; $query .= ' and c.`published` = 1'; if (!empty($excludeCats)) { $query .= ' and c.`category_id` NOT IN (' . implode(',', $excludeCats) . ')'; } $query .= ' group by a.`id`'; // ordring $query .= ' order by VotedCnt desc'; if ($count > 0) { $query .= ' limit ' . $count; } $db->setQuery($query); $posts = $db->loadObjectList(); return $posts; }
public static function getChildCategories(&$result, $params, &$categories, $level = 1) { $db = DiscussHelper::getDBO(); $my = JFactory::getUser(); $mainframe = JFactory::getApplication(); $order = $params->get('order', 'popular'); $sort = $params->get('sort', 'desc'); $count = (int) trim($params->get('count', 0)); $hideEmptyPost = $params->get('hideemptypost', '0'); $queryExclude = ''; $excludeCats = DiscussHelper::getPrivateCategories(); if (!empty($excludeCats)) { $queryExclude .= ' AND a.`id` NOT IN (' . implode(',', $excludeCats) . ')'; } foreach ($result as $row) { $categories[$row->id] = $row; $categories[$row->id]->childs = array(); $query = 'SELECT a.`id`, a.`title`, a.`parent_id`, a.`alias`, a.`avatar`, COUNT(b.`id`) AS `discussioncount`' . ', ' . $db->quote($level) . ' AS level,' . ' ( SELECT COUNT(id) FROM ' . $db->nameQuote('#__discuss_category') . ' WHERE lft < a.lft AND rgt > a.rgt AND a.lft != ' . $db->Quote(0) . ' ) AS depth' . ' FROM ' . $db->nameQuote('#__discuss_category') . ' AS `a`' . ' LEFT JOIN ' . $db->nameQuote('#__discuss_posts') . ' AS b' . ' ON a.`id` = b.`category_id`' . ' AND b.`parent_id` = ' . $db->Quote('0') . ' AND b.`published` = ' . $db->Quote('1'); $query .= ' WHERE a.`published` = 1'; $query .= ' AND a.`parent_id`=' . $db->Quote($row->id); $query .= $queryExclude; if (!$hideEmptyPost) { $query .= ' GROUP BY a.`id`'; } else { $query .= ' GROUP BY a.`id` HAVING (COUNT(b.`id`) > 0)'; } switch ($order) { case 'popular': $orderBy = ' ORDER BY `discussioncount` '; break; case 'alphabet': $orderBy = ' ORDER BY a.`title` '; break; case 'latest': default: $orderBy = ' ORDER BY a.`created` '; break; } $query .= $orderBy . $sort; $db->setQuery($query); $records = $db->loadObjectList(); if ($records) { modEasydiscussCategoriesHelper::getChildCategories($records, $params, $categories[$row->id]->childs, ++$level); } } }
/** * Retrieve similar question based on the keywords * * @access public * @param string $keywords */ public static function getSimilarQuestion($text = '') { if (empty($text)) { return ''; } $config = Discusshelper::getConfig(); if (!$config->get('main_similartopic', 0)) { return ''; } // $text = 'how to configure facebook integration?'; $itemLimit = $config->get('main_similartopic_limit', '5'); $db = DiscussHelper::getDBO(); // remove punctuation from the string. $text = preg_replace("/(?![.=\$'â?])\\p{P}/u", "", $text); //$text = preg_replace("/(?![.=$'â?)\p{P}/u", "", $text); $queryExclude = ''; if (!$config->get('main_similartopic_privatepost', 0)) { $excludeCats = DiscussHelper::getPrivateCategories(); if (!empty($excludeCats)) { $queryExclude .= ' AND a.`category_id` NOT IN (' . implode(',', $excludeCats) . ')'; } } // lets check if db has more than 2 records or not. $query = 'SELECT COUNT(1) FROM `#__discuss_posts` as a'; $query .= ' WHERE a.`published` = ' . $db->Quote('1'); $query .= ' AND a.`parent_id` = ' . $db->Quote('0'); $query .= $queryExclude; $db->setQuery($query); $rCount = $db->loadResult(); if ($rCount <= 2) { // full index search will fail if record has only two. So we do a normal like search. $phrase = 'or'; $words = explode(' ', $text); $wheres = array(); foreach ($words as $word) { $word = $db->Quote('%' . $db->getEscaped($word, true) . '%', false); $wheres2 = array(); $wheres2[] = 'a.title LIKE ' . $word; $wheres2[] = 'a.content LIKE ' . $word; $wheres[] = implode(' OR ', $wheres2); } $whereString = '(' . implode($phrase == 'all' ? ') AND (' : ') OR (', $wheres) . ')'; $query = 'select a.`id`, a.`title`, 0 AS score'; $query .= ' FROM `#__discuss_posts` as a'; $query .= ' WHERE a.`published` = ' . $db->Quote('1'); $query .= ' AND a.`parent_id` = ' . $db->Quote('0'); $query .= ' AND ' . $whereString; $query .= $queryExclude; $query .= ' LIMIT ' . $itemLimit; $db->setQuery($query); $result = $db->loadObjectList(); return $result; } // we know table has more than 3 records. // lets do a full index search. // lets get the tags match the keywords $tagkeywords = explode(' ', $text); for ($i = 0; $i < count($tagkeywords); $i++) { if (JString::strlen($tagkeywords[$i]) > 3) { $tagkeywords[$i] = $tagkeywords[$i] . '*'; } else { $tagkeywords[$i] = $tagkeywords[$i]; } } $tagkeywords = implode(' ', $tagkeywords); $query = 'select `id` FROM `#__discuss_tags`'; $query .= ' WHERE MATCH(`title`) AGAINST (' . $db->Quote($tagkeywords) . ' IN BOOLEAN MODE)'; $db->setQuery($query); $tagResults = $db->loadResultArray(); // now try to get the main topic $query = 'select a.`id`, a.`title`, MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($text) . ' WITH QUERY EXPANSION) AS score'; $query .= ' FROM `#__discuss_posts` as a'; $query .= ' WHERE MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($text) . ' WITH QUERY EXPANSION)'; $query .= ' AND a.`published` = ' . $db->Quote('1'); $query .= ' AND a.`parent_id` = ' . $db->Quote('0'); $query .= $queryExclude; $tagQuery = ''; if (count($tagResults) > 0) { $tagQuery = 'select a.`id`, a.`title`, MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($text) . ' WITH QUERY EXPANSION) AS score'; $tagQuery .= ' FROM `#__discuss_posts` as a'; $tagQuery .= ' INNER JOIN `#__discuss_posts_tags` as b ON a.id = b.post_id'; $tagQuery .= ' WHERE MATCH(a.`title`,a.`content`) AGAINST (' . $db->Quote($text) . ' WITH QUERY EXPANSION)'; $tagQuery .= ' AND a.`published` = ' . $db->Quote('1'); $tagQuery .= ' AND a.`parent_id` = ' . $db->Quote('0'); $tagQuery .= ' AND b.`tag_id` IN (' . implode(',', $tagResults) . ')'; $tagQuery .= $queryExclude; $query = 'SELECT * FROM (' . $query . ' UNION ' . $tagQuery . ') AS x LIMIT ' . $itemLimit; } else { $query .= ' LIMIT ' . $itemLimit; } $db->setQuery($query); $result = $db->loadObjectList(); return $result; }
public function getFeaturedPosts($category = '') { $db = DiscussHelper::getDBO(); $my = JFactory::getUser(); $queryExclude = ''; $excludeCats = array(); // get all private categories id $excludeCats = DiscussHelper::getPrivateCategories(); if (!empty($excludeCats)) { $queryExclude .= ' AND a.`category_id` NOT IN (' . implode(',', $excludeCats) . ')'; } $query = 'SELECT a.* FROM `#__discuss_posts` AS a'; if (!empty($tagId)) { $query .= ' INNER JOIN `#__discuss_posts_tags` AS b ON a.`id` = b.`post_id`'; $query .= ' AND b.`tag_id` = ' . $db->Quote($tagId); } $query .= ' WHERE a.`featured` = ' . $db->Quote('1'); $query .= ' AND a.`parent_id` = ' . $db->Quote('0'); $query .= ' AND a.`published` = ' . $db->Quote('1'); if ($category) { $query .= ' AND a.`category_id`=' . $db->Quote($category); } $query .= $queryExclude; $db->setQuery($query); $result = $db->loadResult(); return $result; }
function getTagCloud($limit = '', $order = 'title', $sort = 'asc', $userId = '') { $db = DiscussHelper::getDBO(); $query = 'select a.`id`, a.`title`, a.`alias`, a.`created`, count(c.`id`) as `post_count`'; $query .= ' from #__discuss_tags as a'; $query .= ' left join #__discuss_posts_tags as b'; $query .= ' on a.`id` = b.`tag_id`'; $query .= ' left join #__discuss_posts as c'; $query .= ' on b.post_id = c.id'; $query .= ' and c.`private`=' . $db->Quote(0); $query .= ' and c.`published` = ' . $db->Quote('1'); $exclude = DiscussHelper::getPrivateCategories(); if (!empty($exclude)) { $query .= ' AND c.`category_id` NOT IN(' . implode(',', $exclude) . ')'; } $query .= ' where a.`published` = ' . $db->Quote('1'); if (!empty($userId)) { $query .= ' AND a.`user_id`=' . $db->Quote($userId); } $query .= ' group by (a.`id`)'; //order switch ($order) { case 'postcount': $query .= ' ORDER BY (post_count)'; break; case 'title': default: $query .= ' ORDER BY (a.`title`)'; } //sort switch ($sort) { case 'asc': $query .= ' asc '; break; case 'desc': default: $query .= ' desc '; } //limit if (!empty($limit)) { $query .= ' LIMIT ' . (int) $limit; } $db->setQuery($query); $result = $db->loadObjectList(); return $result; }
public function canViewReplies() { if (DiscussHelper::isModerator($this->id)) { return true; } $privCats = DiscussHelper::getPrivateCategories(DISCUSS_CATEGORY_ACL_ACTION_VIEWREPLY); $canView = in_array($this->id, $privCats) ? false : true; return $canView; }
public static function getData($params) { $db = DiscussHelper::getDBO(); $count = (int) $params->get('count', 10); $filter = (int) $params->get('filter_option', 0); $state = (int) $params->get('filter_state', 0); $includeSubcat = (bool) $params->get('include_subcategories', 0); $catId = intval($params->get('category', 0)); $tagId = intval($params->get('tags', 0)); $limitQuery = ''; $catQuery = ''; $exclusionQuery = ''; if (!empty($count)) { $limitQuery = 'LIMIT 0,' . $count; } if ($state == 1) { // Unanswered $stateQuery = ' AND a.`isresolve`=' . $db->Quote(0); $stateQuery .= ' AND a.`answered`=' . $db->Quote(0); //Order query $orderBy = 'ORDER BY a.`replied` DESC '; } else { $stateQuery = ''; $orderBy = 'ORDER BY a.`created` DESC '; } if ($filter == 0 || $filter == 1) { if ($filter == 1 && !empty($catId)) { if (!$includeSubcat) { $catQuery = ' AND a.`category_id` = ' . $db->quote($catId) . ' '; } else { $catIds = array($catId); self::appendChildCategories($catId, $catIds); JArrayHelper::toInteger($catIds); $catQuery = ' AND a.`category_id` IN (' . implode(',', $catIds) . ') '; } } $excludedCategories = DiscussHelper::getPrivateCategories(); if (!empty($excludedCategories)) { $exclusionQuery .= ' AND a.`category_id` NOT IN (' . implode(',', $excludedCategories) . ')'; } $query = 'SELECT a.*, (SELECT COUNT(1) FROM `#__discuss_posts` WHERE `parent_id` = a.`id` AND `published`="1") AS `num_replies` FROM ' . $db->nameQuote('#__discuss_posts') . ' AS a ' . 'WHERE a.`published`=' . $db->Quote(1) . ' ' . 'AND a.`parent_id`=' . $db->Quote(0) . ' ' . $catQuery . $exclusionQuery . $stateQuery . $groupByQuery . $orderBy . $limitQuery; } if ($filter == 2) { $query = 'SELECT a.*, (SELECT COUNT(1) FROM `#__discuss_posts` WHERE `parent_id` = a.`id` AND `published`="1") AS `num_replies` ' . ' FROM ' . $db->nameQuote('#__discuss_posts') . ' AS a' . ' LEFT JOIN ' . $db->nameQuote('#__discuss_posts_tags') . ' AS c' . ' ON a.' . $db->nameQuote('id') . '= c.' . $db->nameQuote('post_id') . ' WHERE a.' . $db->nameQuote('published') . '=' . $db->Quote(1) . ' AND a.' . $db->nameQuote('parent_id') . '=' . $db->Quote(0) . ' AND b.' . $db->nameQuote('published') . '=' . $db->Quote(1) . ' AND c.' . $db->nameQuote('tag_id') . '=' . $db->Quote($tagId) . $stateQuery . $groupByQuery . $orderBy . $limitQuery; } if ($filter == 3) { // If featured post + unanswered settings in backend showing no post in the madule // is because featured post considered as answered // this behaviour is respecting to the component's "unanswered tab" $query = 'SELECT a.*, (SELECT COUNT(1) FROM `#__discuss_posts` WHERE `parent_id` = a.`id` AND `published`="1") AS `num_replies` ' . ' FROM ' . $db->nameQuote('#__discuss_posts') . ' AS a' . ' WHERE a.' . $db->nameQuote('published') . '=' . $db->Quote(1) . ' AND a.' . $db->nameQuote('parent_id') . '=' . $db->Quote(0) . ' AND a.' . $db->nameQuote('featured') . '=' . $db->Quote(1) . $stateQuery . $groupByQuery . $orderBy . $limitQuery; } $db->setQuery($query); if (!($result = $db->loadObjectList())) { return false; } $posts = array(); require_once DISCUSS_HELPERS . '/parser.php'; foreach ($result as $row) { $profile = DiscussHelper::getTable('Profile'); $profile->load($row->user_id); $row->profile = $profile; $row->content = EasyDiscussParser::bbcode($row->content); $row->title = DiscussHelper::wordFilter($row->title); $row->content = DiscussHelper::wordFilter($row->content); // Process bbcode $row->content = EasyDiscussParser::bbcode($row->content); $posts[] = $row; } // Append profile objects to the result return $posts; }
public function getCategoryTree($sortParentChild = true) { $db = DiscussHelper::getDBO(); $my = JFactory::getUser(); $config = DiscussHelper::getConfig(); $sortConfig = $config->get('layout_ordering_category', 'latest'); $queryExclude = ''; $excludeCats = array(); // get all private categories id $excludeCats = DiscussHelper::getPrivateCategories(); if (!empty($excludeCats)) { $queryExclude .= ' AND a.`id` NOT IN (' . implode(',', $excludeCats) . ')'; } $query = 'SELECT a.*, COUNT(b.id) -1 as depth'; $query .= ' FROM ' . $db->nameQuote('#__discuss_category') . ' AS a '; $query .= ' INNER JOIN ' . $db->nameQuote('#__discuss_category') . ' AS b'; $query .= ' WHERE a.`published`=' . $db->Quote(DISCUSS_ID_PUBLISHED); $query .= ' AND (a.lft between b.lft and b.rgt)'; $query .= $queryExclude; $query .= ' GROUP BY a.id'; if (!$config->get('layout_show_all_subcategories')) { $query .= ' HAVING ' . $db->nameQuote('depth') . ' = 0'; } switch ($sortConfig) { case 'alphabet': $orderBy = ' ORDER BY a.`title` '; break; case 'ordering': $orderBy = ' ORDER BY a.`lft` '; break; case 'latest': $orderBy = ' ORDER BY a.`created` '; break; default: $orderBy = ' ORDER BY a.`lft` '; break; } $sort = $config->get('layout_sort_category', 'asc'); $query .= $orderBy . $sort; $db->setQuery($query); $rows = $db->loadObjectList(); $total = count($rows); $categories = array(); for ($i = 0; $i < $total; $i++) { $category = DiscussHelper::getTable('Category'); $category->bind($rows[$i]); $category->depth = $rows[$i]->depth; $categories[] = $category; } if ($sortParentChild && ($sortConfig == 'alphabet' || $sortConfig == 'latest')) { $cats = array(); $groups = array(); foreach ($categories as $row) { $cats[$row->parent_id][] = $row; } $this->sortAlpha($groups, $cats, 0); $categories = $groups; } return $categories; }
/** * Method to build the query for the tags * * @access private * @return string */ private function _buildQuery($sort = 'latest', $filter = '', $category = '') { $my = JFactory::getUser(); $config = DiscussHelper::getConfig(); $date = DiscussHelper::getDate(); $db = DiscussHelper::getDBO(); // Get the WHERE and ORDER BY clauses for the query if (empty($this->_parent)) { $parent_id = JRequest::getInt('parent_id', 0); $this->_parent = $parent_id; } $filteractive = empty($filter) ? JRequest::getString('filter', 'allposts') : $filter; $where = ''; $orderby = ''; $queryExclude = ''; $excludeCats = array(); $excludeCats = DiscussHelper::getPrivateCategories(); if (!empty($excludeCats)) { $queryExclude .= ' AND a.`category_id` NOT IN (' . implode(',', $excludeCats) . ')'; } // // Posts $pquery = 'SELECT DATEDIFF(' . $db->Quote($date->toMySQL()) . ', a.`created` ) as `noofdays`, '; $pquery .= ' DATEDIFF(' . $db->Quote($date->toMySQL()) . ', IF(a.`replied` = ' . $db->Quote('0000-00-00 00:00:00') . ', a.`created`, a.`replied`) ) as `daydiff`, '; $pquery .= ' TIMEDIFF(' . $db->Quote($date->toMySQL()) . ', IF(a.`replied` = ' . $db->Quote('0000-00-00 00:00:00') . ', a.`created`, a.`replied`) ) as `timediff`,'; $pquery .= ' ' . $db->Quote('posts') . ' as `itemtype`,'; $pquery .= ' a.`id`, a.`title`, a.`content`, a.`user_id`, a.`category_id`, a.`parent_id`, a.`user_type`, a.`created` AS `created`, a.`poster_name`,'; $pquery .= ' b.`title` AS `category`, a.password, a.`featured` AS `featured`, a.`islock` AS `islock`, a.`isresolve` AS `isresolve`,'; $pquery .= ' IF(a.`replied` = ' . $db->Quote('0000-00-00 00:00:00') . ', a.`created`, a.`replied`) as `lastupdate`'; $pquery .= ' ,a.`legacy`, pt.`suffix` AS post_type_suffix, pt.`title` AS post_type_title'; $pquery .= ' FROM `#__discuss_posts` AS a'; $pquery .= ' LEFT JOIN ' . $db->nameQuote('#__discuss_category') . ' AS b ON a.`category_id`=b.`id`'; $pquery .= ' LEFT JOIN ' . $db->nameQuote('#__discuss_post_types') . ' AS pt ON a.`post_type`= pt.`alias`'; $pquery .= $this->_buildQueryWhere('posts', 'a', $category); $pquery .= $queryExclude; // // Replies $rquery = 'SELECT DATEDIFF(' . $db->Quote($date->toMySQL()) . ', a.`created` ) as `noofdays`, '; $rquery .= ' DATEDIFF(' . $db->Quote($date->toMySQL()) . ', IF(a.`replied` = ' . $db->Quote('0000-00-00 00:00:00') . ', a.`created`, a.`replied`) ) as `daydiff`, '; $rquery .= ' TIMEDIFF(' . $db->Quote($date->toMySQL()) . ', IF(a.`replied` = ' . $db->Quote('0000-00-00 00:00:00') . ', a.`created`, a.`replied`) ) as `timediff`,'; $rquery .= ' ' . $db->Quote('replies') . ' as `itemtype`,'; $rquery .= ' a.`id`, a.`title`, a.`content`, a.`user_id`, a.`category_id`, a.`parent_id`, a.`user_type`,a.`created` AS `created`, a.`poster_name`,'; $rquery .= ' b.`title` AS `category`, a.password, a.`featured` AS `featured`, a.`islock` AS `islock`, a.`isresolve` AS `isresolve`,'; $rquery .= ' IF(a.`replied` = ' . $db->Quote('0000-00-00 00:00:00') . ', a.`created`, a.`replied`) as `lastupdate`'; $rquery .= ' ,a.`legacy`, ' . $db->Quote('') . ' AS `post_type_suffix`, ' . $db->Quote('') . ' AS `post_type_title`'; $rquery .= ' FROM `#__discuss_posts` AS a'; $rquery .= ' LEFT JOIN ' . $db->nameQuote('#__discuss_category') . ' AS b ON a.`category_id`=b.`id`'; $rquery .= $this->_buildQueryWhere('replies', 'a', $category); $rquery .= $queryExclude; // Categories $cquery = 'SELECT 0 as `noofdays`, '; $cquery .= ' 0 as `daydiff`, '; $cquery .= ' ' . $db->Quote('00:00:00') . ' as `timediff`,'; $cquery .= ' ' . $db->Quote('category') . ' as `itemtype`,'; $cquery .= ' a.`id`, a.`title`, a.`description` as `content`, a.`created_by` as `user_id`, a.`id` as `category_id`, 0 as `parent_id`, 0 AS `user_type`, a.`created` AS `created`, 0 as `poster_name`,'; $cquery .= ' a.`title` AS `category`, 0 AS `password`,0 as `featured`, 0 as `islock` , 0 as `isresolve`,'; $cquery .= ' a.`created` as `lastupdate`,'; $cquery .= ' 1 as `legacy`, ' . $db->Quote('') . ' AS `post_type_suffix`, ' . $db->Quote('') . ' AS `post_type_title`'; $cquery .= ' FROM `#__discuss_category` AS a'; $cquery .= $this->_buildQueryWhere('category', 'a', $category); if (!empty($excludeCats)) { $cquery .= ' AND a.`id` NOT IN (' . implode(',', $excludeCats) . ')'; } $query = 'SELECT * FROM ('; $query .= '(' . $pquery . ') UNION (' . $rquery . ') UNION (' . $cquery . ')'; $query .= ') as x'; $query .= ' ORDER BY x.`lastupdate` DESC'; return $query; }