Esempio n. 1
0
 /**
  * Get array of blogs defined by parameters
  *
  * @param	$type			str
  * @param	$typeId			int
  * @param	$sort			str
  * @param	$max			int
  * @param	$published		str
  * @param	$search			bool
  * @param	$frontpage		bool
  * @param	$excludeBlogs	array
  * @param	$pending		bool
  * @param	$dashboard		bool
  * @param	$protected		bool
  * @param	$excludeCats	array
  * @param	$includeCats	array
  *
  */
 public function getBlogsBy($type, $typeId = 0, $sort = '', $max = 0, $published = EBLOG_FILTER_PUBLISHED, $search = false, $frontpage = false, $excludeBlogs = array(), $pending = false, $dashboard = false, $protected = true, $excludeCats = array(), $includeCats = array(), $postType = null, $limitType = 'listlength', $pinFeatured = true)
 {
     $db = EB::db();
     $my = JFactory::getUser();
     $config = EB::config();
     $queryPagination = false;
     $queryWhere = '';
     $queryOrder = '';
     $queryLimit = '';
     $queryWhere = '';
     $queryExclude = '';
     $queryExcludePending = '';
     $queryExcludePrivateJSGrp = '';
     // use in generating category access sql
     $catAccess = array();
     // Get excluded categories
     $excludeCats = !empty($excludeCats) ? $excludeCats : array();
     // Determines if the user is viewing a blogger mode menu item
     $isBloggerMode = EasyBlogRouter::isBloggerMode();
     // What is this for?
     $teamBlogIds = '';
     // What?
     $customOrdering = '';
     if (!empty($sort) && is_array($sort)) {
         $customOrdering = isset($sort[1]) ? $sort[1] : '';
         $sort = isset($sort[0]) ? $sort[0] : '';
     }
     // Sorting options
     $sort = empty($sort) ? $config->get('layout_postorder', 'latest') : $sort;
     $isJSGrpPluginInstalled = false;
     $isJSGrpPluginInstalled = JPluginHelper::isEnabled('system', 'groupeasyblog');
     $isEventPluginInstalled = JPluginHelper::isEnabled('system', 'eventeasyblog');
     $isJSInstalled = false;
     // need to check if the site installed jomsocial.
     $file = JPATH_ROOT . '/components/com_community/libraries/core.php';
     $exists = JFile::exists($file);
     if ($exists) {
         $isJSInstalled = true;
     }
     $includeJSGrp = $type != 'teamblog' && !$dashboard && $isJSGrpPluginInstalled && $isJSInstalled ? true : false;
     $includeJSEvent = $type != 'teamblog' && !$dashboard && $isEventPluginInstalled && $isJSInstalled ? true : false;
     $jsEventPostIds = '';
     $jsGrpPostIds = '';
     // contribution type sql
     $contributor = EB::contributor();
     $contributeSQL = ' AND ( (a.`source_type` = ' . $db->Quote(EASYBLOG_POST_SOURCE_SITEWIDE) . ') ';
     if ($config->get('main_includeteamblogpost') || $type == 'teamblog' || $dashboard) {
         $contributeSQL .= $contributor::genAccessSQL(EASYBLOG_POST_SOURCE_TEAM, 'a');
     }
     if ($includeJSEvent) {
         $contributeSQL .= $contributor::genAccessSQL(EASYBLOG_POST_SOURCE_JOMSOCIAL_EVENT, 'a');
     }
     if ($includeJSGrp) {
         $contributeSQL .= $contributor::genAccessSQL(EASYBLOG_POST_SOURCE_JOMSOCIAL_GROUP, 'a');
     }
     // Only process the contribution sql for EasySocial if EasySocial really exists.
     if ($type != 'teamblog' && !$dashboard && EB::easysocial()->exists()) {
         $contributeSQL .= $contributor::genAccessSQL(EASYBLOG_POST_SOURCE_EASYSOCIAL_GROUP, 'a');
         $contributeSQL .= $contributor::genAccessSQL(EASYBLOG_POST_SOURCE_EASYSOCIAL_EVENT, 'a');
     }
     $contributeSQL .= ')';
     //get teamblogs id.
     $query = '';
     //check if the request come with statastic or not.
     $statType = JRequest::getString('stat', '');
     $statId = '';
     if (!empty($statType)) {
         $statId = $statType == 'tag' ? JRequest::getString('tagid', '') : JRequest::getString('catid', '');
     }
     if (!empty($excludeBlogs)) {
         $queryExclude .= ' AND a.`id` NOT IN (';
         for ($i = 0; $i < count($excludeBlogs); $i++) {
             $queryExclude .= $db->Quote($excludeBlogs[$i]);
             if (next($excludeBlogs) !== false) {
                 $queryExclude .= ',';
             }
         }
         $queryExclude .= ')';
     }
     // Exclude postings from specific categories
     if (!empty($excludeCats)) {
         $catAccess['exclude'] = $excludeCats;
     }
     $queryInclude = '';
     // Respect inclusion categories
     if (!empty($includeCats)) {
         $catAccess['include'] = $includeCats;
     }
     switch ($published) {
         case EBLOG_FILTER_PENDING:
             $queryWhere = ' WHERE a.`published` = ' . $db->Quote(EASYBLOG_POST_PENDING);
             break;
         case EBLOG_FILTER_ALL:
             $queryWhere = ' WHERE (a.`published` = 1 OR a.`published`=0 OR a.`published`=2 OR a.`published`=3) ';
             $queryWhere .= ' AND a.' . $db->quoteName('state') . '=' . $db->Quote(EASYBLOG_POST_NORMAL);
             break;
         case EBLOG_FILTER_SCHEDULE:
             $queryWhere = ' WHERE a.' . $db->quoteName('published') . '=' . $db->Quote(EASYBLOG_POST_SCHEDULED);
             break;
         case EBLOG_FILTER_UNPUBLISHED:
             $queryWhere = ' WHERE a.' . $db->quoteName('published') . '=' . $db->Quote(EASYBLOG_POST_UNPUBLISHED);
             break;
         case EBLOG_FILTER_DRAFT:
             $queryWhere = ' WHERE a.' . $db->quoteName('published') . '=' . $db->Quote(EASYBLOG_POST_DRAFT);
             break;
         case EBLOG_FILTER_PUBLISHED:
         default:
             $queryWhere = ' WHERE a.' . $db->quoteName('published') . '=' . $db->Quote(EASYBLOG_POST_PUBLISHED);
             $queryWhere .= ' AND a.' . $db->quoteName('state') . '=' . $db->Quote(EASYBLOG_POST_NORMAL);
             break;
     }
     //do not list out protected blog in rss
     if (JRequest::getCmd('format', '') == 'feed') {
         if ($config->get('main_password_protect', true)) {
             $queryWhere .= ' AND a.`blogpassword`="" ';
         }
     }
     // Blog privacy setting
     // @integrations: jomsocial privacy
     $file = JPATH_ROOT . '/components/com_community/libraries/core.php';
     $easysocial = EasyBlogHelper::getHelper('EasySocial');
     if ($config->get('integrations_easysocial_privacy') && $easysocial->exists() && !EasyBlogHelper::isSiteAdmin() && $type != 'teamblog' && !$dashboard) {
         $esPrivacyQuery = $easysocial->buildPrivacyQuery('a');
         $queryWhere .= $esPrivacyQuery;
     } else {
         if ($config->get('main_jomsocial_privacy') && JFile::exists($file) && !EasyBlogHelper::isSiteAdmin() && $type != 'teamblog' && !$dashboard) {
             require_once $file;
             $my = JFactory::getUser();
             $jsFriends = CFactory::getModel('Friends');
             $friends = $jsFriends->getFriendIds($my->id);
             array_push($friends, $my->id);
             // Insert query here.
             $queryWhere .= ' AND (';
             $queryWhere .= ' (a.`access`= 0 ) OR';
             $queryWhere .= ' ( (a.`access` = 20) AND (' . $db->Quote($my->id) . ' > 0 ) ) OR';
             if (empty($friends)) {
                 $queryWhere .= ' ( (a.`access` = 30) AND ( 1 = 2 ) ) OR';
             } else {
                 $queryWhere .= ' ( (a.`access` = 30) AND ( a.' . $db->nameQuote('created_by') . ' IN (' . implode(',', $friends) . ') ) ) OR';
             }
             $queryWhere .= ' ( (a.`access` = 40) AND ( a.' . $db->nameQuote('created_by') . '=' . $my->id . ') )';
             $queryWhere .= ' )';
         } else {
             if ($my->id == 0) {
                 $queryWhere .= ' AND a.`access` = ' . $db->Quote(BLOG_PRIVACY_PUBLIC);
             }
         }
     }
     if ($isBloggerMode !== false) {
         $queryWhere .= ' AND a.`created_by` = ' . $db->Quote($isBloggerMode);
     }
     $contentId = '';
     $isIdArray = false;
     if (is_array($typeId)) {
         if (count($typeId) > 1) {
             for ($i = 0; $i < count($typeId); $i++) {
                 if ($typeId[$i]) {
                     $contentId .= $typeId[$i];
                     if ($i + 1 < count($typeId)) {
                         $contentId .= ',';
                     }
                 }
             }
             $isIdArray = true;
         } else {
             if (!empty($typeId)) {
                 $contentId = $typeId[0];
             }
         }
     } else {
         $contentId = $typeId;
     }
     if ($contentId) {
         switch ($type) {
             case 'category':
                 $catAccess['type'] = $typeId;
                 if ($isBloggerMode === false) {
                     $catBloggerId = EasyBlogHelper::getCategoryMenuBloggerId();
                     if (!empty($catBloggerId)) {
                         $queryWhere .= ' AND a.`created_by` = ' . $db->Quote($catBloggerId);
                     }
                 }
                 break;
             case 'blogger':
                 $queryWhere .= $isIdArray ? ' AND a.`created_by` IN (' . $contentId . ')' : ' AND a.`created_by` = ' . $db->Quote($contentId);
                 break;
             case 'teamblog':
                 $queryWhere .= ' AND (a.source_type = ' . $db->Quote(EASYBLOG_POST_SOURCE_TEAM);
                 $queryWhere .= $isIdArray ? ' AND a.source_id IN (' . $contentId . ')' : ' AND a.`source_id` = ' . $db->Quote($contentId);
                 $queryWhere .= ')';
                 break;
             default:
                 break;
         }
     }
     // @rule: Filter for `source` column type.
     if (!is_null($postType)) {
         switch ($postType) {
             case 'microblog':
                 $queryWhere .= ' AND a.`posttype` != ' . $db->Quote('');
                 break;
             case 'posts':
                 $queryWhere .= ' AND a.`posttype` = ' . $db->Quote('');
                 break;
             case 'quote':
                 $queryWhere .= ' AND a.`posttype` = ' . $db->Quote('quote');
                 break;
             case 'link':
                 $queryWhere .= ' AND a.`posttype` = ' . $db->Quote('link');
                 break;
             case 'photo':
                 $queryWhere .= ' AND a.`posttype` = ' . $db->Quote('photo');
                 break;
             case 'video':
                 $queryWhere .= ' AND a.`posttype` = ' . $db->Quote('video');
                 break;
             case 'twitter':
                 $queryWhere .= ' AND a.`posttype` = ' . $db->Quote('twitter');
                 break;
         }
     }
     if ($type == 'blogger' || $type == 'teamblog') {
         if (!empty($statType)) {
             if ($statType == 'category') {
                 $catAccess['statType'] = $statId;
             } else {
                 $queryWhere .= ' AND t.`tag_id` = ' . $db->Quote($statId);
             }
         }
     }
     if ($search) {
         $queryWhere .= ' AND a.`title` LIKE ' . $db->Quote('%' . $search . '%');
     }
     if ($frontpage) {
         $queryWhere .= ' AND a.`frontpage` = ' . $db->Quote('1');
     }
     // @rule: When language filter is enabled, we need to detect the appropriate contents
     $filterLanguage = JFactory::getApplication()->getLanguageFilter();
     if ($filterLanguage) {
         $queryWhere .= EBR::getLanguageQuery('AND', 'a.language');
     }
     if ($protected == false) {
         $queryWhere .= ' AND a.`blogpassword` = ""';
     }
     // category access here
     $catLib = EB::category();
     $catAccessSQL = $catLib->genAccessSQL('a.`id`', $catAccess);
     $queryWhere .= ' AND (' . $catAccessSQL . ')';
     // get the default sorting.
     $defaultSorting = $customOrdering ? $customOrdering : $config->get('layout_postsort', 'desc');
     $queryOrder = ' ORDER BY ';
     $sortableItems = array('latest', 'published', 'popular', 'active', 'alphabet', 'modified', 'random');
     if ($frontpage && $pinFeatured) {
         $queryOrder .= ' f.`created` DESC ,';
     }
     switch ($sort) {
         case 'latest':
             $queryOrder .= ' a.`created` ' . $defaultSorting;
             break;
         case 'published':
             $queryOrder .= ' a.`publish_up` ' . $defaultSorting;
             break;
         case 'popular':
             $queryOrder .= ' a.`hits` ' . $defaultSorting;
             break;
         case 'active':
             $queryOrder .= ' a.`publish_down` ' . $defaultSorting;
             break;
         case 'alphabet':
             $queryOrder .= ' a.`title` ' . $defaultSorting;
             break;
         case 'modified':
             $queryOrder .= ' a.`modified` ' . $defaultSorting;
             break;
         case 'random':
             $queryOrder .= ' `random_id` ';
             break;
         default:
             break;
     }
     if ($max > 0) {
         $queryLimit = ' LIMIT ' . $max;
     } else {
         //set frontpage list length if it is detected to be the frontpage
         $view = JRequest::getCmd('view', '');
         $limit = EB::call('Pagination', 'getLimit', array($limitType));
         $limitstart = JRequest::getInt('limitstart', 0, 'REQUEST');
         // In case limit has been changed, adjust it
         $limitstart = (int) ($limit != 0 ? floor($limitstart / $limit) * $limit : 0);
         if ($limitstart < 0) {
             $limitstart = 0;
         }
         $queryLimit = ' LIMIT ' . $limitstart . ',' . $limit;
         $queryPagination = true;
     }
     if ($queryPagination) {
         $query = 'SELECT COUNT(1) FROM `#__easyblog_post` AS a';
         if (($type == 'blogger' || $type == 'teamblog') && $statType == 'tag') {
             $query .= ' LEFT JOIN `#__easyblog_post_tag` AS t ON a.id = t.post_id';
         }
         $query .= $queryWhere;
         $query .= $contributeSQL;
         $query .= $queryExclude;
         $query .= $queryInclude;
         // echo $query;exit;
         $db->setQuery($query);
         $this->_total = $db->loadResult();
         $this->_pagination = EB::pagination($this->_total, $limitstart, $limit);
     }
     $query = 'SELECT a.`id` AS key1, a.*';
     $query .= ', ifnull(f.`id`, 0) as `featured`';
     if ($sort == 'random') {
         $query .= ', floor( 1 + rand() * rd.`rid` ) as `random_id`';
     }
     $query .= ' FROM `#__easyblog_post` AS a';
     // if ($frontpage && $pinFeatured) {
     $query .= ' LEFT JOIN `#__easyblog_featured` AS f';
     $query .= ' 	ON a.`id` = f.`content_id` AND f.`type` = ' . $db->Quote('post');
     // }
     if (($type == 'blogger' || $type == 'teamblog') && $statType == 'tag') {
         $query .= ' LEFT JOIN `#__easyblog_post_tag` AS t ON a.`id` = t.`post_id`';
         $query .= ' AND t.`tag_id` = ' . $db->Quote($statId);
     }
     if ($sort == 'random') {
         $query .= ', (select max(tmp.`id`) - 1 as `rid` from `#__easyblog_post` as tmp ) as rd';
     }
     $query .= $queryWhere;
     $query .= $contributeSQL;
     $query .= $queryExclude;
     $query .= $queryInclude;
     $query .= $queryOrder;
     $query .= $queryLimit;
     // // Debugging
     // echo str_ireplace( '#__' , 'jos_' , $query );
     // exit;
     $db->setQuery($query);
     if ($db->getErrorNum() > 0) {
         JError::raiseError($db->getErrorNum(), $db->getErrorMsg() . $db->stderr());
     }
     $result = $db->loadObjectList();
     return $result;
 }
Esempio n. 2
0
 function getTotalPostCount($catIds)
 {
     $db = EasyBlogHelper::db();
     //blog privacy setting
     $my = JFactory::getUser();
     $isBloggerMode = EasyBlogRouter::isBloggerMode();
     $categoryId = '';
     $isIdArray = false;
     if (is_array($catIds)) {
         if (count($catIds) > 1) {
             $categoryId = implode(',', $catIds);
             $isIdArray = true;
         } else {
             $categoryId = $catIds[0];
         }
     } else {
         $categoryId = $catIds;
     }
     $query = 'SELECT COUNT(b.`id`) AS `cnt`';
     $query .= ' FROM ' . EasyBlogHelper::getHelper('SQL')->nameQuote('#__easyblog_category') . ' AS `a`';
     $query .= ' LEFT JOIN ' . EasyBlogHelper::getHelper('SQL')->nameQuote('#__easyblog_post') . ' AS b';
     $query .= ' ON a.`id` = b.`category_id`';
     $query .= ' AND b.`published` = ' . $db->Quote('1');
     if ($my->id == 0) {
         $query .= ' AND b.`private` = ' . $db->Quote(BLOG_PRIVACY_PUBLIC);
     }
     if ($isBloggerMode !== false) {
         $query .= ' AND b.`created_by` = ' . $db->Quote($isBloggerMode);
     } else {
         $catBloggerId = EasyBlogHelper::getCategoryMenuBloggerId();
         if (!empty($catBloggerId)) {
             $query .= ' AND b.`created_by` = ' . $db->Quote($catBloggerId);
         }
     }
     $query .= ' WHERE a.`published` = 1';
     $query .= $isIdArray ? ' AND a.`id` IN (' . $categoryId . ')' : ' AND a.`id` = ' . $db->Quote($categoryId);
     if (EasyBlogHelper::getJoomlaVersion() >= '1.6') {
         // @rule: When language filter is enabled, we need to detect the appropriate contents
         $filterLanguage = JFactory::getApplication()->getLanguageFilter();
         if ($filterLanguage) {
             $query .= ' AND (';
             $query .= ' b.`language`=' . $db->Quote(JFactory::getLanguage()->getTag());
             $query .= ' OR b.`language`=' . $db->Quote('');
             $query .= ' OR b.`language`=' . $db->Quote('*');
             $query .= ' )';
         }
     }
     $query .= ' GROUP BY a.`id` HAVING (COUNT(b.`id`) > 0)';
     $db->setQuery($query);
     $result = $db->loadResultArray();
     if (!empty($result)) {
         return array_sum($result);
     } else {
         return '0';
     }
 }
Esempio n. 3
0
 /**
  * Get array of blogs defined by parameters
  *
  * @param	$type			str
  * @param	$typeId			int
  * @param	$sort			str
  * @param	$max			int
  * @param	$published		str
  * @param	$search			bool
  * @param	$frontpage		bool
  * @param	$excludeBlogs	array
  * @param	$pending		bool
  * @param	$dashboard		bool
  * @param	$protected		bool
  * @param	$excludeCats	array
  * @param	$includeCats	array
  *
  */
 function getBlogsBy($type, $typeId = 0, $sort = '', $max = 0, $published = EBLOG_FILTER_PUBLISHED, $search = false, $frontpage = false, $excludeBlogs = array(), $pending = false, $dashboard = false, $protected = true, $excludeCats = array(), $includeCats = array(), $postType = null, $limitType = 'listlength', $pinFeatured = true)
 {
     $db = EasyBlogHelper::db();
     $my = JFactory::getUser();
     $config = EasyBlogHelper::getConfig();
     $queryPagination = false;
     $queryWhere = '';
     $queryOrder = '';
     $queryLimit = '';
     $queryWhere = '';
     $queryExclude = '';
     $queryExcludePending = '';
     $queryExcludePrivateJSGrp = '';
     $excludeCats = !empty($excludeCats) ? $excludeCats : array();
     $isBloggerMode = EasyBlogRouter::isBloggerMode();
     $teamBlogIds = '';
     $customOrdering = '';
     if (!empty($sort) && is_array($sort)) {
         $customOrdering = isset($sort[1]) ? $sort[1] : '';
         $sort = isset($sort[0]) ? $sort[0] : '';
     }
     $sort = empty($sort) ? $config->get('layout_postorder', 'latest') : $sort;
     $isJSGrpPluginInstalled = false;
     $isJSGrpPluginInstalled = JPluginHelper::isEnabled('system', 'groupeasyblog');
     $isEventPluginInstalled = JPluginHelper::isEnabled('system', 'eventeasyblog');
     $isJSInstalled = false;
     // need to check if the site installed jomsocial.
     if (JFile::exists(JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php')) {
         $isJSInstalled = true;
     }
     $includeJSGrp = $type != 'teamblog' && !$dashboard && $isJSGrpPluginInstalled && $isJSInstalled ? true : false;
     $includeJSEvent = $type != 'teamblog' && !$dashboard && $isEventPluginInstalled && $isJSInstalled ? true : false;
     $jsEventPostIds = '';
     $jsGrpPostIds = '';
     if ($includeJSEvent) {
         $queryEvent = 'SELECT ' . EasyBlogHelper::getHelper('SQL')->nameQuote('post_id') . ' FROM';
         $queryEvent .= ' ' . EasyBlogHelper::getHelper('SQL')->nameQuote('#__easyblog_external') . ' AS ' . EasyBlogHelper::getHelper('SQL')->nameQuote('a');
         $queryEvent .= ' INNER JOIN' . EasyBlogHelper::getHelper('SQL')->nameQuote('#__community_events') . ' AS ' . EasyBlogHelper::getHelper('SQL')->nameQuote('b');
         $queryEvent .= ' ON ' . EasyBlogHelper::getHelper('SQL')->nameQuote('a') . '.uid = ' . EasyBlogHelper::getHelper('SQL')->nameQuote('b') . '.id';
         $queryEvent .= ' AND ' . EasyBlogHelper::getHelper('SQL')->nameQuote('a') . '.' . EasyBlogHelper::getHelper('SQL')->nameQuote('source') . '=' . $db->Quote('jomsocial.event');
         $queryEvent .= ' WHERE ' . EasyBlogHelper::getHelper('SQL')->nameQuote('b') . '.' . EasyBlogHelper::getHelper('SQL')->nameQuote('permission') . '=' . $db->Quote(0);
         $db->setQuery($queryEvent);
         $jsEventPostIds = $db->loadResultArray();
         if (!empty($excludeBlogs) && !empty($jsEventPostIds)) {
             $jsEventPostIds = array_diff($jsEventPostIds, $excludeBlogs);
         }
     }
     if ($includeJSGrp) {
         $queryJSGrp = 'select `post_id` from `#__easyblog_external_groups` as exg inner join `#__community_groups` as jsg';
         $queryJSGrp .= '      on exg.group_id = jsg.id ';
         $queryJSGrp .= '      where jsg.`approvals` = 0';
         $db->setQuery($queryJSGrp);
         $jsGrpPostIds = $db->loadResultArray();
         if (!empty($excludeBlogs) && !empty($jsGrpPostIds)) {
             $jsGrpPostIds = array_diff($jsGrpPostIds, $excludeBlogs);
         }
     }
     //get teamblogs id.
     $query = '';
     if ($config->get('main_includeteamblogpost') || $dashboard) {
         $teamBlogIds = EasyBlogHelper::getViewableTeamIds();
         if (count($teamBlogIds) > 0) {
             $teamBlogIds = implode(',', $teamBlogIds);
         }
     }
     // get all private categories id
     $excludeCats = array_merge($excludeCats, EasyBlogHelper::getPrivateCategories());
     //check if the request come with statastic or not.
     $statType = JRequest::getString('stat', '');
     $statId = '';
     if ($statType != '') {
         $statId = $statType == 'tag' ? JRequest::getString('tagid', '') : JRequest::getString('catid', '');
     }
     if (!empty($excludeBlogs)) {
         $queryExclude .= ' AND a.`id` NOT IN (';
         for ($i = 0; $i < count($excludeBlogs); $i++) {
             $queryExclude .= $db->Quote($excludeBlogs[$i]);
             if (next($excludeBlogs) !== false) {
                 $queryExclude .= ',';
             }
         }
         $queryExclude .= ')';
     }
     if (!empty($excludeCats)) {
         $queryExclude .= ' AND a.`category_id` NOT IN (';
         for ($i = 0; $i < count($excludeCats); $i++) {
             $queryExclude .= $db->Quote($excludeCats[$i]);
             if (next($excludeCats) !== false) {
                 $queryExclude .= ',';
             }
         }
         $queryExclude .= ')';
     }
     $queryInclude = '';
     // Respect inclusion categories
     if (!empty($includeCats)) {
         $queryInclude = ' AND a.`category_id` IN(';
         if (!is_array($includeCats)) {
             $includeCats = array($includeCats);
         }
         for ($i = 0; $i < count($includeCats); $i++) {
             $queryInclude .= $db->Quote($includeCats[$i]);
             if (next($includeCats) !== false) {
                 $queryInclude .= ',';
             }
         }
         $queryInclude .= ')';
     }
     switch ($published) {
         case EBLOG_FILTER_PENDING:
             $queryWhere = ' WHERE a.`ispending` = 1 AND a.`published` != 3';
             break;
         case EBLOG_FILTER_ALL:
             $queryWhere = ' WHERE (a.`published` = 1 OR a.`published`=0 OR a.`published`=2 OR a.`published`=3) ';
             break;
         case EBLOG_FILTER_SCHEDULE:
             $queryWhere = ' WHERE a.`published` = 2 AND a.`ispending` = ' . $db->Quote('0');
             break;
         case EBLOG_FILTER_UNPUBLISHED:
             $queryWhere = ' WHERE a.`published` = 0 AND a.`ispending` = ' . $db->Quote('0');
             break;
         case EBLOG_FILTER_DRAFT:
             $queryWhere = ' WHERE a.`published` = 3 ';
             break;
         case EBLOG_FILTER_PUBLISHED:
         default:
             $queryWhere = ' WHERE a.`published` = 1 AND a.`ispending` = ' . $db->Quote('0');
             break;
     }
     //do not list out protected blog in rss
     if (JRequest::getCmd('format', '') == 'feed') {
         if ($config->get('main_password_protect', true)) {
             $queryWhere .= ' AND a.`blogpassword`="" ';
         }
     }
     //blog privacy setting
     // @integrations: jomsocial privacy
     $file = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_community' . DIRECTORY_SEPARATOR . 'libraries' . DIRECTORY_SEPARATOR . 'core.php';
     $easysocial = EasyBlogHelper::getHelper('EasySocial');
     if ($config->get('integrations_easysocial_privacy') && $easysocial->exists() && !EasyBlogHelper::isSiteAdmin() && $type != 'teamblog' && !$dashboard) {
         $esPrivacyQuery = $easysocial->buildPrivacyQuery('a');
         $queryWhere .= $esPrivacyQuery;
     } else {
         if ($config->get('main_jomsocial_privacy') && JFile::exists($file) && !EasyBlogHelper::isSiteAdmin() && $type != 'teamblog' && !$dashboard) {
             require_once $file;
             $my = JFactory::getUser();
             $jsFriends = CFactory::getModel('Friends');
             $friends = $jsFriends->getFriendIds($my->id);
             array_push($friends, $my->id);
             // Insert query here.
             $queryWhere .= ' AND (';
             $queryWhere .= ' (a.`private`= 0 ) OR';
             $queryWhere .= ' ( (a.`private` = 20) AND (' . $db->Quote($my->id) . ' > 0 ) ) OR';
             if (empty($friends)) {
                 $queryWhere .= ' ( (a.`private` = 30) AND ( 1 = 2 ) ) OR';
             } else {
                 $queryWhere .= ' ( (a.`private` = 30) AND ( a.' . EasyBlogHelper::getHelper('SQL')->nameQuote('created_by') . ' IN (' . implode(',', $friends) . ') ) ) OR';
             }
             $queryWhere .= ' ( (a.`private` = 40) AND ( a.' . EasyBlogHelper::getHelper('SQL')->nameQuote('created_by') . '=' . $my->id . ') )';
             $queryWhere .= ' )';
         } else {
             if ($my->id == 0) {
                 $queryWhere .= ' AND a.`private` = ' . $db->Quote(BLOG_PRIVACY_PUBLIC);
             }
         }
     }
     if ($isBloggerMode !== false) {
         $queryWhere .= ' AND a.`created_by` = ' . $db->Quote($isBloggerMode);
     }
     $contentId = '';
     $isIdArray = false;
     if (is_array($typeId)) {
         if (count($typeId) > 1) {
             for ($i = 0; $i < count($typeId); $i++) {
                 if ($typeId[$i]) {
                     $contentId .= $typeId[$i];
                     if ($i + 1 < count($typeId)) {
                         $contentId .= ',';
                     }
                 }
             }
             $isIdArray = true;
         } else {
             if (!empty($typeId)) {
                 $contentId = $typeId[0];
             }
         }
     } else {
         $contentId = $typeId;
     }
     if ($contentId) {
         switch ($type) {
             case 'category':
                 $queryWhere .= $isIdArray ? ' AND a.`category_id` IN (' . $contentId . ')' : ' AND a.`category_id` = ' . $db->Quote($contentId);
                 if ($isBloggerMode === false) {
                     $catBloggerId = EasyBlogHelper::getCategoryMenuBloggerId();
                     if (!empty($catBloggerId)) {
                         $queryWhere .= ' AND a.`created_by` = ' . $db->Quote($catBloggerId);
                     }
                 }
                 break;
             case 'blogger':
                 $queryWhere .= $isIdArray ? ' AND a.`created_by` IN (' . $contentId . ')' : ' AND a.`created_by` = ' . $db->Quote($contentId);
                 break;
             case 'teamblog':
                 $queryWhere .= $isIdArray ? ' AND u.`team_id` IN (' . $contentId . ')' : ' AND u.`team_id` = ' . $db->Quote($contentId);
                 break;
             default:
                 break;
         }
     }
     // @rule: Filter for `source` column type.
     if (!is_null($postType)) {
         switch ($postType) {
             case 'microblog':
                 $queryWhere .= ' AND a.`source` != ' . $db->Quote('');
                 break;
             case 'posts':
                 $queryWhere .= ' AND a.`source` = ' . $db->Quote('');
                 break;
         }
     }
     if ($type == 'blogger' || $type == 'teamblog') {
         if (!empty($statType)) {
             if ($statType == 'category') {
                 $queryWhere .= ' AND a.`category_id` = ' . $db->Quote($statId);
             } else {
                 $queryWhere .= ' AND t.`tag_id` = ' . $db->Quote($statId);
             }
         }
     }
     if ($search) {
         $queryWhere .= ' AND a.`title` LIKE ' . $db->Quote('%' . $search . '%');
     }
     if ($frontpage) {
         $queryWhere .= ' AND a.`frontpage` = ' . $db->Quote('1');
     }
     if ($type != 'teamblog' && !$dashboard) {
         $tmpIds = array();
         if ($jsGrpPostIds) {
             $tmpIds = array_merge($jsGrpPostIds, $tmpIds);
         }
         if ($jsEventPostIds) {
             $tmpIds = array_merge($jsEventPostIds, $tmpIds);
         }
         if ($config->get('main_includeteamblogpost') && !empty($teamBlogIds)) {
             if (empty($jsGrpPostIds) && empty($jsEventPostIds)) {
                 $queryWhere .= ' AND (u.team_id IN (' . $teamBlogIds . ') OR a.`issitewide` = ' . $db->Quote('1') . ')';
             } else {
                 $tmpIds = array_unique($tmpIds);
                 $tmpIds = implode(',', $tmpIds);
                 $queryWhere .= ' AND (u.team_id IN (' . $teamBlogIds . ') OR a.id IN (' . $tmpIds . ') OR a.`issitewide` = ' . $db->Quote('1') . ')';
             }
         } else {
             if ($tmpIds) {
                 $tmpIds = array_unique($tmpIds);
                 $tmpIds = implode(',', $tmpIds);
                 $queryWhere .= ' AND ( a.`issitewide` = 1 OR a.id IN (' . $tmpIds . ') )';
             } else {
                 $queryWhere .= ' AND a.`issitewide` = ' . $db->Quote('1');
             }
         }
     }
     // show team postings in the entries if they are the admin
     if ($dashboard && $teamBlogIds && $postType != 'microblog') {
         $teamIds = explode(',', $teamBlogIds);
         $adminTeamIds = array();
         foreach ($teamIds as $teamId) {
             // We need to test if the user has admin access.
             $team = EasyBlogHelper::getTable('TeamBlog');
             $team->load($teamId);
             if ($team->isTeamAdmin($my->id)) {
                 $adminTeamIds[] = $teamId;
             }
         }
         if (!empty($adminTeamIds)) {
             $queryWhere .= ' OR a.`id` IN(';
             $queryWhere .= ' SELECT `post_id` FROM `#__easyblog_team_post` WHERE `team_id` IN(';
             for ($i = 0; $i < count($adminTeamIds); $i++) {
                 $queryWhere .= $db->Quote($adminTeamIds[$i]);
                 if (next($adminTeamIds) !== false) {
                     $queryWhere .= ',';
                 }
             }
             $queryWhere .= ') )';
         }
     }
     if (EasyBlogHelper::getJoomlaVersion() >= '1.6') {
         // @rule: When language filter is enabled, we need to detect the appropriate contents
         $filterLanguage = JFactory::getApplication()->getLanguageFilter();
         if ($filterLanguage) {
             $queryWhere .= ' AND (';
             $queryWhere .= ' a.`language`=' . $db->Quote(JFactory::getLanguage()->getTag());
             $queryWhere .= ' OR a.`language`=' . $db->Quote('');
             $queryWhere .= ' OR a.`language`=' . $db->Quote('*');
             $queryWhere .= ' )';
         }
     }
     if ($protected == false) {
         $queryWhere .= ' AND a.`blogpassword` = ""';
     }
     // get the default sorting.
     $defaultSorting = $customOrdering ? $customOrdering : $config->get('layout_postsort', 'desc');
     $queryOrder = ' ORDER BY ';
     if ($frontpage && $config->get('layout_featured_pin') && $pinFeatured) {
         $queryOrder .= ' f.`created` DESC , ';
     }
     switch ($sort) {
         case 'latest':
             $queryOrder .= ' a.`created` ' . $defaultSorting;
             break;
         case 'published':
             $queryOrder .= ' a.`publish_up` ' . $defaultSorting;
             break;
         case 'popular':
             $queryOrder .= ' a.`hits` ' . $defaultSorting;
             break;
         case 'active':
             $queryOrder .= ' a.`publish_down` ' . $defaultSorting;
             break;
         case 'alphabet':
             $queryOrder .= ' a.`title` ' . $defaultSorting;
             break;
         case 'modified':
             $queryOrder .= ' a.`modified` ' . $defaultSorting;
             break;
         case 'random':
             $queryOrder .= ' `random_id` ';
             break;
         default:
             break;
     }
     if ($max > 0) {
         $queryLimit = ' LIMIT ' . $max;
     } else {
         //set frontpage list length if it is detected to be the frontpage
         $view = JRequest::getCmd('view', '');
         $limit = EasyBlogHelper::getHelper('Pagination')->getLimit($limitType);
         $limitstart = JRequest::getVar('limitstart', 0, 'REQUEST');
         // In case limit has been changed, adjust it
         $limitstart = (int) ($limit != 0 ? floor($limitstart / $limit) * $limit : 0);
         $queryLimit = ' LIMIT ' . $limitstart . ',' . $limit;
         $queryPagination = true;
     }
     $excludePendingPost = '';
     if ($dashboard) {
         $excludePendingPost = ' and not exists ( select `entry_id` from `#__easyblog_drafts` as ed where ed.`entry_id` = a.`id` and pending_approval = 1 )';
     }
     if ($queryPagination) {
         $query = 'SELECT COUNT(1) FROM `#__easyblog_post` AS a';
         if ($type == 'teamblog' || ($config->get('main_includeteamblogpost') || $dashboard) && !empty($teamBlogIds)) {
             $query .= ' LEFT JOIN `#__easyblog_team_post` AS u ON a.id = u.post_id';
         }
         if (($type == 'blogger' || $type == 'teamblog') && $statType == 'tag') {
             $query .= ' LEFT JOIN `#__easyblog_post_tag` AS t ON a.id = t.post_id';
             //$query  .= ' AND t.`tag_id` = ' . $db->Quote($statId);
         }
         $query .= $queryWhere;
         $query .= $queryExclude;
         $query .= $queryInclude;
         $query .= $excludePendingPost;
         $db->setQuery($query);
         $this->_total = $db->loadResult();
         $this->_pagination = EasyBlogHelper::getPagination($this->_total, $limitstart, $limit);
     }
     $query = 'SELECT a.`id` AS key1, a.*, b.`id` as key2, b.`title` as `category`';
     if (($type == 'teamblog' || $config->get('main_includeteamblogpost') || $dashboard) && !empty($teamBlogIds)) {
         $query .= ' ,u.`team_id` ';
     }
     if ($sort == 'random') {
         $query .= ', floor( 1 + rand() * rd.`rid` ) as `random_id`';
     }
     $query .= ' FROM `#__easyblog_post` AS a';
     $query .= ' LEFT JOIN `#__easyblog_category` AS b';
     $query .= ' ON a.category_id = b.id';
     if ($frontpage && $config->get('layout_featured_pin') && $pinFeatured) {
         $query .= ' LEFT JOIN `#__easyblog_featured` AS f';
         $query .= ' 	ON a.`id` = f.`content_id` AND f.`type` = ' . $db->Quote('post');
     }
     if ($type == 'teamblog' || ($config->get('main_includeteamblogpost') || $dashboard) && !empty($teamBlogIds)) {
         $query .= ' LEFT JOIN `#__easyblog_team_post` AS u ON a.id = u.post_id';
     }
     if (($type == 'blogger' || $type == 'teamblog') && $statType == 'tag') {
         $query .= ' LEFT JOIN `#__easyblog_post_tag` AS t ON a.`id` = t.`post_id`';
         $query .= ' AND t.`tag_id` = ' . $db->Quote($statId);
     }
     if ($sort == 'random') {
         $query .= ', (select max(tmp.`id`) - 1 as `rid` from `#__easyblog_post` as tmp ) as rd';
     }
     $query .= $queryWhere;
     $query .= $queryExclude;
     $query .= $queryInclude;
     $query .= $excludePendingPost;
     $query .= $queryOrder;
     $query .= $queryLimit;
     // echo str_ireplace( '#__' , 'jos_' , $query );
     // exit;
     $db->setQuery($query);
     if ($db->getErrorNum() > 0) {
         JError::raiseError($db->getErrorNum(), $db->getErrorMsg() . $db->stderr());
     }
     $result = $db->loadObjectList();
     return $result;
 }