Esempio n. 1
0
    static function getArticles($categories, $config, $amount)
    {
        //
        $sql_where = '';
        $tag_join = '';
        //
        if ($categories) {
            // getting categories ItemIDs
            for ($j = 0; $j < count($categories); $j++) {
                $sql_where .= $j != 0 ? ' OR content.catid = ' . $categories[$j] : ' content.catid = ' . $categories[$j];
            }
        }
        // Overwrite SQL query when user set IDs manually
        if ($config['data_source'] == 'k2_articles' && $config['k2_articles'] != '') {
            // initializing variables
            $sql_where = '';
            $ids = explode(',', $config['k2_articles']);
            //
            for ($i = 0; $i < count($ids); $i++) {
                // linking string with content IDs
                $sql_where .= $i != 0 ? ' OR content.id = ' . $ids[$i] : ' content.id = ' . $ids[$i];
            }
        }
        // Overwrite SQL query when user specified tags
        if ($config['data_source'] == 'k2_tags' && $config['k2_tags'] != '') {
            // initializing variables
            $sql_where = '';
            $tag_join = ' LEFT JOIN #__k2_tags_xref AS tx ON content.id = tx.itemID LEFT JOIN #__k2_tags AS t ON t.id = tx.tagID ';
            // getting tag
            $sql_where .= ' t.id = ' . $config['k2_tags'];
        }
        // Overwrite SQL query when user specified authors
        if ($config['data_source'] == 'k2_authors' && $config['k2_authors'] != '') {
            // initializing variables
            $sql_where = '';
            if (!is_array($config['k2_authors'])) {
                $ids = explode(',', $config['k2_authors']);
            } else {
                $ids = $config['k2_authors'];
            }
            //
            for ($i = 0; $i < count($ids); $i++) {
                // linking string with content IDs
                $sql_where .= $i != 0 ? ' OR content.created_by = ' . $ids[$i] : ' content.created_by = ' . $ids[$i];
            }
        }
        // Arrays for content
        $content = array();
        $news_amount = 0;
        // Initializing standard Joomla classes and SQL necessary variables
        $db = JFactory::getDBO();
        $access_con = '';
        $user = JFactory::getUser();
        if ($config['news_unauthorized'] == '0') {
            $access_con = ' AND content.access IN (' . implode(',', $user->getAuthorisedViewLevels()) . ') ';
        }
        if ($config['time_offset'] == 0) {
            $date = JFactory::getDate(date('Y-m-d H:i:s', strtotime('now')));
        } else {
            $date = JFactory::getDate($config['time_offset'] . ' hour ' . date('Y-m-d H:i:s', strtotime('now')));
        }
        //$date = JFactory::getDate("now", $config['time_offset']);
        $now = $date->toSql(true);
        $nullDate = $db->getNullDate();
        // if some data are available
        // when showing only frontpage articles is disabled
        $frontpage_con = '';
        if ($config['only_featured'] == 0 && $config['news_featured'] == 0) {
            $frontpage_con = ' AND content.featured = 0 ';
        } else {
            if ($config['only_featured'] == 1) {
                $frontpage_con = ' AND content.featured = 1';
            }
        }
        $since_con = '';
        //
        if ($config['news_since'] !== '') {
            $since_con = ' AND content.created >= ' . $db->Quote($config['news_since']);
        }
        //
        if ($config['news_since'] == '' && $config['news_in'] != '') {
            $since_con = ' AND content.created >= ' . $db->Quote(strftime('%Y-%m-%d 00:00:00', time() - $config['news_in'] * 24 * 60 * 60));
        }
        // current article hiding
        $current_con = '';
        if ($config['hide_current_k2_article'] == '1' && JRequest::getCmd('option') == 'com_k2' && JRequest::getCmd('view') == 'item' && JRequest::getVar('id') != '') {
            $id = JRequest::getVar('id');
            // filter the alias from ID
            if (stripos($id, ':') !== FALSE) {
                $id = explode(':', $id);
                $id = $id[0];
            }
            // create the condition
            $current_con = ' AND (content.id != ' . $id . ') ';
        }
        // Ordering string
        $order_options = '';
        $rating_join = '';
        // When sort value is random
        if ($config['news_sort_value'] == 'random' || $config['news_sort_value'] == 'user') {
            $order_options = ' RAND() ';
        } else {
            if ($config['news_sort_value'] == 'rating') {
                $order_options = ' (content_rating.rating_sum / content_rating.rating_count) ' . $config['news_sort_order'];
                $rating_join = 'LEFT JOIN #__k2_rating AS content_rating ON content_rating.itemID = content.id';
            } else {
                // when sort value is different than random
                $order_options = ' content.' . $config['news_sort_value'] . ' ' . $config['news_sort_order'] . ' ';
            }
        }
        // language filters
        $lang_filter = '';
        if (JFactory::getApplication()->getLanguageFilter()) {
            $lang_filter = ' AND content.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ') ';
        }
        if ($config['data_source'] != 'k2_all' && $sql_where != '') {
            $sql_where = ' AND ( ' . $sql_where . ' ) ';
        }
        // one article per page - helper variables
        $article_id_query = 'content.id AS id';
        $one_article_query = '';
        if ($config['one_article_per_category'] && $config['data_source'] == 'k2_authors') {
            $article_id_query = 'MAX(content.id) AS id, content.created_by AS author';
            $one_article_query = ' GROUP BY content.created_by ';
        } elseif ($config['one_article_per_category']) {
            $article_id_query = 'MAX(content.id) AS id, content.catid AS cid';
            $one_article_query = ' GROUP BY content.catid ';
        }
        // creating SQL query
        $query_news = '
		SELECT
			' . $article_id_query . '				
		FROM 
			#__k2_items AS content 
			' . $rating_join . '
			' . $tag_join . '
		WHERE 
			content.published = 1 AND content.trash = 0
                ' . $access_con . '   
		 		AND ( content.publish_up = ' . $db->Quote($nullDate) . ' OR content.publish_up <= ' . $db->Quote($now) . ' )
				AND ( content.publish_down = ' . $db->Quote($nullDate) . ' OR content.publish_down >= ' . $db->Quote($now) . ' )
			' . $sql_where . '
			' . $lang_filter . '
			' . $frontpage_con . ' 
			' . $since_con . '
			' . $current_con . '
		
		' . $one_article_query . '	
		
		ORDER BY 
			' . $order_options . '
		LIMIT
			' . $config['offset'] . ',' . $amount . ';
		';
        // run SQL query
        $db->setQuery($query_news);
        // when exist some results
        if ($news = $db->loadAssocList()) {
            // generating tables of news data
            foreach ($news as $item) {
                $content[] = $item;
                // store item in array
                $news_amount++;
                // news amount
            }
        }
        // generate SQL WHERE condition
        $second_sql_where = '';
        for ($i = 0; $i < count($content); $i++) {
            $second_sql_where .= ($i != 0 ? ' OR ' : '') . ' content.id = ' . $content[$i]['id'];
        }
        if ($second_sql_where != '') {
            $second_sql_where = ' AND (' . $second_sql_where . ')';
        }
        // second SQL query to get rest of the data and avoid the DISTINCT
        $second_query_news = '
		SELECT
			content.id AS id,
			content.alias AS alias,
			' . ($config['use_title_alias'] ? 'content.alias' : 'content.title') . ' AS title, 
			content.' . $config['com_k2_text_type'] . ' AS text,
			content.' . ($config['date_publish'] == 0 ? 'created' : ($config['date_publish'] == 1 ? 'publish_up' : 'publish_down')) . ' AS date, 
			content.publish_up AS date_publish,
			content.hits AS hits,
			content.featured AS frontpage,
			content.access AS access,
			content.catid AS cid,
			content.video AS video,
			content.plugins AS plugins,
			categories.name AS catname, 
			categories.image AS cat_image,
			categories.alias AS cat_alias,
			users.email AS author_email,
			content.created_by_alias AS author_alias,
			' . $config['username'] . ' AS author_username,
			content.created_by AS author_id,
			content_rating.rating_sum AS rating_sum,
			content_rating.rating_count AS rating_count		
		FROM 
			#__k2_items AS content 
			LEFT JOIN 
				#__k2_categories AS categories 
				ON categories.id = content.catid 
			LEFT JOIN 
				#__users AS users 
				ON users.id = content.created_by 			
			LEFT JOIN 
				#__k2_rating AS content_rating 
				ON content_rating.itemID = content.id
		WHERE 
			1=1
			' . $second_sql_where . '
		ORDER BY 
			' . $order_options . '
		';
        // run the query
        $db->setQuery($second_query_news);
        // when exist some results
        if ($news2 = $db->loadAssocList()) {
            // load URL overrides
            $url_overrides = false;
            if (isset($config['url_overrides']) && $config['url_overrides'] == '1') {
                $override_file = JPATH_SITE . '/modules/mod_news_pro_gk5/url_overrides.json';
                if (JFile::exists($override_file)) {
                    $override_content = file_get_contents($override_file);
                    if ($override_content && $override_content != '') {
                        $url_overrides = json_decode($override_content, true);
                    }
                }
            }
            // create the iid array
            $content_id = array();
            // create the content IDs array
            foreach ($content as $item) {
                array_push($content_id, $item['id']);
            }
            // generating tables of news data
            foreach ($news2 as $item) {
                $pos = array_search($item['id'], $content_id);
                if ($url_overrides && is_array($url_overrides) && count($url_overrides) > 0 && isset($url_overrides['com_k2'])) {
                    if (isset($url_overrides['com_k2'][$item['id']])) {
                        $item['overrided_url'] = $url_overrides['com_k2'][$item['id']];
                    }
                }
                // merge the new data to the array of items data
                if (isset($content[$pos]) && is_array($content[$pos])) {
                    $content[$pos] = array_merge($content[$pos], (array) $item);
                }
            }
        }
        // load comments
        if (stripos($config['info_format'], '%COMMENTS') !== FALSE || stripos($config['info2_format'], '%COMMENTS') !== FALSE) {
            $content = NSP_GK5_com_k2_Model::getComments($content, $config);
        }
        // load extra fields
        if (isset($config['k2_get_extra_fields']) && $config['k2_get_extra_fields'] == 1) {
            $content = NSP_GK5_com_k2_Model::getExtraFields($content, $config);
        }
        // load tags
        if (stripos($config['info_format'], '%TAGS') !== FALSE || stripos($config['info2_format'], '%TAGS') !== FALSE) {
            $content = NSP_GK5_com_k2_Model::getTags($content, $config);
        }
        // Reorder items if necessary
        if ($config['news_sort_value'] == 'user' && $config['data_source'] == 'k2_articles' && $config['k2_articles'] != '') {
            $new_content = array();
            $ids = explode(',', $config['k2_articles']);
            $query_ids = array();
            if (count($content)) {
                foreach ($content as $key => $item) {
                    $query_ids[$item['id']] = $key;
                }
                foreach ($ids as $id) {
                    if (isset($query_ids[$id])) {
                        array_push($new_content, $content[$query_ids[$id]]);
                    }
                }
                $content = $new_content;
            }
        }
        // the content array
        return $content;
    }
Esempio n. 2
0
    static function getArticles($categories, $config, $amount)
    {
        //
        $sql_where = '';
        $tag_join = '';
        //
        if ($categories) {
            // getting categories ItemIDs
            for ($j = 0; $j < count($categories); $j++) {
                $sql_where .= $j != 0 ? ' OR content.catid = ' . $categories[$j] : ' content.catid = ' . $categories[$j];
            }
        }
        // Overwrite SQL query when user set IDs manually
        if ($config['data_source'] == 'k2_articles' && $config['k2_articles'] != '') {
            // initializing variables
            $sql_where = '';
            $ids = explode(',', $config['k2_articles']);
            //
            for ($i = 0; $i < count($ids); $i++) {
                // linking string with content IDs
                $sql_where .= $i != 0 ? ' OR content.id = ' . $ids[$i] : ' content.id = ' . $ids[$i];
            }
        }
        // Overwrite SQL query when user specified tags
        if ($config['data_source'] == 'k2_tags' && $config['k2_tags'] != '') {
            // initializing variables
            $sql_where = '';
            $tag_join = ' LEFT JOIN #__k2_tags_xref AS tx ON content.id = tx.itemID LEFT JOIN #__k2_tags AS t ON t.id = tx.tagID ';
            // getting tag
            $sql_where .= ' t.id = ' . $config['k2_tags'];
        }
        // Overwrite SQL query when user specified authors
        if ($config['data_source'] == 'k2_authors' && $config['k2_authors'] != '') {
            // initializing variables
            $sql_where = '';
            $ids = explode(',', $config['k2_authors']);
            //
            for ($i = 0; $i < count($ids); $i++) {
                // linking string with content IDs
                $sql_where .= $i != 0 ? ' OR content.created_by = ' . $ids[$i] : ' content.created_by = ' . $ids[$i];
            }
        }
        // Arrays for content
        $content = array();
        $news_amount = 0;
        // Initializing standard Joomla classes and SQL necessary variables
        $db = JFactory::getDBO();
        $access_con = '';
        if ($config['news_unauthorized'] == '0') {
            $access_con = ' AND content.access IN (' . implode(',', JFactory::getUser()->authorisedLevels()) . ') ';
        }
        $app = JFactory::getApplication();
        $timezone = $app->getCfg('offset') + $config['time_offset'];
        $date = JFactory::getDate("now", $timezone);
        $now = $date->toSql(true);
        $nullDate = $db->getNullDate();
        // if some data are available
        // when showing only frontpage articles is disabled
        $frontpage_con = '';
        if ($config['only_featured'] == 0 && $config['news_featured'] == 0) {
            $frontpage_con = ' AND content.featured = 0 ';
        } else {
            if ($config['only_featured'] == 1) {
                $frontpage_con = ' AND content.featured = 1';
            }
        }
        $since_con = '';
        //
        if ($config['news_since'] !== '') {
            $since_con = ' AND content.created >= ' . $db->Quote($config['news_since']);
        }
        //
        if ($config['news_since'] == '' && $config['news_in'] != '') {
            $since_con = ' AND content.created >= ' . $db->Quote(strftime('%Y-%m-%d 00:00:00', time() - $config['news_in'] * 24 * 60 * 60));
        }
        // current article hiding
        $current_con = '';
        if ($config['hide_current_k2_article'] == '1' && JRequest::getCmd('option') == 'com_k2' && JRequest::getCmd('view') == 'item' && JRequest::getVar('id') != '') {
            $id = JRequest::getVar('id');
            // filter the alias from ID
            if (stripos($id, ':') !== FALSE) {
                $id = explode(':', $id);
                $id = $id[0];
            }
            // create the condition
            $current_con = ' AND (content.id != ' . $id . ') ';
        }
        // Ordering string
        $order_options = '';
        // When sort value is random
        if ($config['news_sort_value'] == 'random') {
            $order_options = ' RAND() ';
        } else {
            // when sort value is different than random
            $order_options = ' content.' . $config['news_sort_value'] . ' ' . $config['news_sort_order'] . ' ';
        }
        // language filters
        $lang_filter = '';
        if (JFactory::getApplication()->getLanguageFilter()) {
            $lang_filter = ' AND content.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ') ';
        }
        if ($config['data_source'] != 'k2_all') {
            $sql_where = ' AND ( ' . $sql_where . ' ) ';
        }
        // creating SQL query
        $query_news = '
		SELECT
			content.id AS id,
			content.alias AS alias,
			' . ($config['use_title_alias'] ? 'content.alias' : 'content.title') . ' AS title, 
			content.introtext AS text, 
			content.created AS date, 
			content.publish_up AS date_publish,
			content.hits AS hits,
			content.featured AS frontpage				
		FROM 
			#__k2_items AS content 
			' . $tag_join . '
		WHERE 
			content.published = 1 AND content.trash = 0
                ' . $access_con . '   
		 		AND ( content.publish_up = ' . $db->Quote($nullDate) . ' OR content.publish_up <= ' . $db->Quote($now) . ' )
				AND ( content.publish_down = ' . $db->Quote($nullDate) . ' OR content.publish_down >= ' . $db->Quote($now) . ' )
			' . $sql_where . '
			' . $lang_filter . '
			' . $frontpage_con . ' 
			' . $since_con . '
			' . $current_con . '
		ORDER BY 
			' . $order_options . '
		LIMIT
			' . $config['offset'] . ',' . $amount . ';
		';
        // run SQL query
        $db->setQuery($query_news);
        // when exist some results
        if ($news = $db->loadAssocList()) {
            // generating tables of news data
            foreach ($news as $item) {
                $content[] = $item;
                // store item in array
                $news_amount++;
                // news amount
            }
        }
        // generate SQL WHERE condition
        $second_sql_where = '';
        for ($i = 0; $i < count($content); $i++) {
            $second_sql_where .= ($i != 0 ? ' OR ' : '') . ' content.id = ' . $content[$i]['id'];
        }
        // second SQL query to get rest of the data and avoid the DISTINCT
        $second_query_news = '
		SELECT
			content.id AS id,
			content.access AS access,
			content.catid AS cid,
			categories.name AS catname, 
			categories.alias AS cat_alias,
			users.email AS author_email,
			content.created_by_alias AS author_alias,
			' . $config['username'] . ' AS author_username,
			content.created_by AS author_id,
			content_rating.rating_sum AS rating_sum,
			content_rating.rating_count AS rating_count		
		FROM 
			#__k2_items AS content 
			LEFT JOIN 
				#__k2_categories AS categories 
				ON categories.id = content.catid 
			LEFT JOIN 
				#__users AS users 
				ON users.id = content.created_by 			
			LEFT JOIN 
				#__k2_rating AS content_rating 
				ON content_rating.itemID = content.id
		WHERE 
			' . $second_sql_where . '
		ORDER BY 
			' . $order_options . '
		';
        // run the query
        $db->setQuery($second_query_news);
        // when exist some results
        if ($news2 = $db->loadAssocList()) {
            // create the iid array
            $content_id = array();
            // create the content IDs array
            foreach ($content as $item) {
                array_push($content_id, $item['id']);
            }
            // generating tables of news data
            foreach ($news2 as $item) {
                $pos = array_search($item['id'], $content_id);
                // merge the new data to the array of items data
                $content[$pos] = array_merge($content[$pos], (array) $item);
            }
        }
        // load comments
        if (stripos($config['info_format'], '%COMMENTS') !== FALSE || stripos($config['info2_format'], '%COMMENTS') !== FALSE) {
            $content = NSP_GK5_com_k2_Model::getComments($content, $config);
        }
        // load tags
        if (stripos($config['info_format'], '%TAGS') !== FALSE || stripos($config['info2_format'], '%TAGS') !== FALSE) {
            $content = NSP_GK5_com_k2_Model::getTags($content, $config);
        }
        // the content array
        return $content;
    }