Here the modified code:
- Code: Select all
function getArticles($categories, $config, $amount) {
// mainframe
$sql_where = '';
$tag_join = '';
//
if($categories) {
$j = 0;
// getting categories ItemIDs
foreach ($categories as $item) {
//Additional Categories for K2
$sql_where .= ($j != 0) ? ' OR content.catid = '.$item->ID.' OR ca.catid ='.$item->ID : ' content.catid = '.$item->ID.' OR ca.catid ='.$item->ID;
//Additional Categories for K2
$j++;
}
}
// Arrays for content
$content_id = array();
$content_alias = array();
$content_cid = array();
$content_title = array();
$content_text = array();
$content_date = array();
$content_date_publish = array();
$content_author = array();
$content_cat_name = array();
$content_cat_alias = array();
$content_hits = array();
$content_email = array();
$content_authorid = array();
$content_rating_sum = array();
$content_rating_count = array();
$content_plugins = array();
$news_amount = 0;
// Initializing standard Joomla classes and SQL necessary variables
$db = JFactory::getDBO();
$user = JFactory::getUser();
$aid = max ($user->getAuthorisedViewLevels());
$date = JFactory::getDate("now", $config);
$now = $date->toMySQL();
$nullDate = $db->getNullDate();
// Overwrite SQL query when user set IDs manually
if($config == 'k2_articles' && $config != ''){
// initializing variables
$sql_where = '';
$ids = explode(',', $config);
//
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 == 'k2_tags' && $config != ''){
// 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;
}
//Additional Categories for K2
$tag_join .=' LEFT JOIN #__k2_additional_categories AS ca ON content.id = ca.itemID ';
// if some data are available
if(count($categories) > 0){
// when showing only frontpage articles is disabled
$featured_con = ($config == 0) ? (($config == 0) ? ' AND content.featured = 0 ' : '' ) : ' AND content.featured = 1 ';
$since_con = '';
if($config !== '') $since_con = ' AND content.created >= ' . $db->Quote($config);
// Ordering string
$order_options = '';
// When sort value is random
if($config == 'random') {
$order_options = ' RAND() ';
} else { // when sort value is different than random
if($config != 'fordering') $order_options = ' content.'.$config.' '.$config.' ';
else $order_options = ' content.featured_ordering '.$config.' ';
}
if($config != 'all_k2_articles') {
$sql_where = ' AND ( ' . $sql_where . ' ) ';
}
// creating SQL query
$query_news = '
SELECT DISTINCT
cats.name AS cat_name,
cats.alias AS cat_alias,
content.catid AS cat_id,
'.((($config != 2) ? 'users.'.(($config == 1) ? 'username':'name') : 'content.created_by_alias')) .' AS author,
users.email AS author_email,
users.id AS author_id,
'.($config ? 'content.alias' : 'content.title').' AS title,
content.introtext AS text,
content.created AS date,
content.publish_up AS date_publish,
content.id AS ID,
content.alias AS alias,
content.hits AS hits,
content_rating.rating_sum AS rating_sum,
content_rating.rating_count AS rating_count,
content.plugins AS plugins
FROM
#__k2_items AS content
LEFT JOIN
#__k2_categories AS cats
ON cats.id = content.catid
LEFT JOIN
#__users AS users
ON users.id = content.created_by
'.$tag_join.'
LEFT JOIN
#__k2_rating AS content_rating
ON content.id = content_rating.itemID
WHERE
content.trash = 0'.(($config == 0) ? '
AND cats.access <= ' .(int) $aid . '
AND content.access <= '.(int) $aid : '').'
AND content.published = 1
AND cats.published = 1
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.'
'.$featured_con.'
'.$since_con.'
ORDER BY
'.$order_options.'
LIMIT
'.($config).','.$amount.';
';
//echo $query_news;
// run SQL query
$db->setQuery($query_news);
// when exist some results
if($news = $db->loadObjectList()) {
// generating tables of news data
foreach($news as $item) {
$content_id[] = $item->ID; // news IDs
$content_alias[] = $item->alias; // news aliases
$content_cid[] = $item->cat_id; // news CIDs
$content_title[] = $item->title; // news titles
$content_text[] = $item->text; // news text
$content_date[] = $item->date; // news dates
$content_date_publish[] = $item->date_publish; // news dates
$content_author[] = $item->author; // news author
$content_cat_name[] = $item->cat_name; // news category name
$content_cat_alias[] = $item->cat_alias; // news category alias
$content_hits[] = $item->hits; // news hits
$content_email[] = $item->author_email; // news author email
$content_authorid[] = $item->author_id; // news author id
$content_rating_sum[] = $item->rating_sum; // news rating sum
$content_rating_count[] = $item->rating_count; // news rating count
$content_plugins[] = $item->plugins; // news K2Store values
$news_amount++; // news amount
}
}
}
// Returning data in hash table
return array(
"ID" => $content_id,
"alias" => $content_alias,
"CID" => $content_cid,
"title" => $content_title,
"text" => $content_text,
"date" => $content_date,
"date_publish" => $content_date_publish,
"author" => $content_author,
"cat_name" => $content_cat_name,
"cat_alias" => $content_cat_alias,
"hits" => $content_hits,
"email" => $content_email,
"author_id" => $content_authorid,
"rating_sum" => $content_rating_sum,
"rating_count" => $content_rating_count,
"plugins" => $content_plugins,
"news_amount" => $news_amount
);
}
Someone could help me? Do you have an idea about this issue?
Thanks
Paolo