File: /home/confeduphaar/backip-old-files/modules/mod_latestnewsenhanced/helpers/helper_standard.php
<?php
/**
* @copyright Copyright (C) 2011 Simplify Your Web, Inc. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE.txt
*/
// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Access\Access;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\Registry\Registry;
use Joomla\CMS\Helper\TagsHelper;
require_once (JPATH_SITE.'/components/com_content/helpers/route.php');
require_once (dirname(__FILE__).'/helper.php');
jimport('syw.tags', JPATH_LIBRARIES);
jimport('syw.cache', JPATH_LIBRARIES);
jimport('syw.text', JPATH_LIBRARIES);
jimport('syw.utilities', JPATH_LIBRARIES);
// B/C
class modLatestNewsEnhancedExtendedHelperStandard extends modLatestNewsEnhancedHelperStandard { }
class modLatestNewsEnhancedHelperStandard
{
/**
*
* @param unknown $params
* @param unknown $items
* @throws Exception
* @return array of categories (id, description, article count)
*/
static function getCategoryList($params, $items)
{
$categories = array();
// get all categories and how many articles are in them
foreach ($items as $item) {
if (array_key_exists($item->catid, $categories)) {
$categories[$item->catid]++;
} else {
$categories[$item->catid] = 1;
}
}
if ($params->get('show_cat_description', 0)) { // need description
$categories_string = implode(',', array_keys($categories));
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->select($db->quoteName('description'));
$query->from($db->quoteName('#__categories'));
$query->where($db->quoteName('id').' IN ('.$categories_string.')');
$db->setQuery($query);
try {
$categories_list = $db->loadObjectList('id');
} catch (\DatabaseExceptionExecuting $e) {
Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
return null;
}
foreach ($categories_list as $category) {
$category->count = $categories[$category->id];
}
} else {
$categories_list = array();
foreach ($categories as $key => $value) {
$categories_list[$key] = (Object) array('id' => $key, 'count' => $value);
}
}
return $categories_list;
}
static function getList($params, $module)
{
$db = Factory::getDbo();
$app = Factory::getApplication();
$user = Factory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$nullDate = $db->quote($db->getNullDate());
$nowDate = $db->quote(Factory::getDate()->toSql());
$jinput = $app->input;
$option = $jinput->get('option');
$view = $jinput->get('view');
if (!$params->get('show_on_item_page', 1)) {
if ($option === 'com_content' && ($view === 'article' || $view === 'form')) {
return null;
}
}
$query = $db->getQuery(true);
$item_on_page_id = '';
$item_on_page_tagids = array();
$item_on_page_keys = array();
$related = (string) $params->get('related', '0'); // 0: no, 1: keywords, 2: tags articles only, 3: tags any content
if ($related == '1') { // related by keyword
if ($option === 'com_content' && $view === 'article') {
$temp = $jinput->getString('id');
$temp = explode(':', $temp);
$item_on_page_id = $temp[0];
}
if ($item_on_page_id) {
$query->select($db->quoteName('metakey'));
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('id').' = '.$item_on_page_id);
$db->setQuery($query);
try {
$result = $db->loadResult();
} catch (\DatabaseExceptionExecuting $e) {
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
return null;
}
$result = trim($result);
if (empty($result)) {
return array(); // won't find a related article if no key is present
}
$keys = explode(',', $result);
// assemble any non-blank word(s)
foreach ($keys as $key) {
$key = trim($key);
if ($key) {
$item_on_page_keys[] = $key;
}
}
if (empty($item_on_page_keys)) {
return array();
}
$query->clear();
} else {
return null; // no result (was not on article page)
}
} else if ($related == '2' || $related == '3') { // related by tag
$get_the_tags = false;
if ($related == '2' && $option === 'com_content' && $view === 'article') {
$get_the_tags = true;
} else if ($related == '3') { // no restriction on the type of content
$get_the_tags = true;
if ($option === 'com_trombinoscopeextended' && $view === 'contact') { // because tags are recorded with com_contact
$option = 'com_contact';
}
}
if ($get_the_tags) {
$temp = $jinput->getString('id');
$temp = explode(':', $temp);
$item_on_page_id = $temp[0];
if ($item_on_page_id) {
$helper_tags = new TagsHelper();
$tag_objects = $helper_tags->getItemTags($option.'.'.$view, $item_on_page_id); // array of tag objects
foreach ($tag_objects as $tag_object) {
$item_on_page_tagids[] = $tag_object->tag_id;
}
}
if (empty($item_on_page_tagids)) {
return array(); // no result because no tag found for the object on the page
}
} else {
return null; // no result (was not on article page)
}
}
// START OF DATABASE QUERY
$fulltext_query = 'a.fulltext, ';
$subquery1 = ' CASE WHEN ';
$subquery1 .= $query->charLength('a.alias');
$subquery1 .= ' THEN ';
$a_id = $query->castAsChar('a.id');
$subquery1 .= $query->concatenate(array($a_id, 'a.alias'), ':');
$subquery1 .= ' ELSE ';
$subquery1 .= $a_id.' END AS slug';
$subquery2 = ' CASE WHEN ';
$subquery2 .= $query->charLength('c.alias');
$subquery2 .= ' THEN ';
$c_id = $query->castAsChar('c.id');
$subquery2 .= $query->concatenate(array($c_id, 'c.alias'), ':');
$subquery2 .= ' ELSE ';
$subquery2 .= $c_id.' END AS cat_slug';
$query->select('DISTINCT a.id, a.title, a.alias, a.introtext, '.$fulltext_query.
'CASE WHEN a.fulltext IS NULL OR a.fulltext = \'\' THEN 0 ELSE 1 END AS fulltexthascontent, '.
'a.checked_out, a.checked_out_time, '.
'a.catid, a.created, a.created_by, a.created_by_alias, '.
'a.state, '.
// Use created if modified is 0
'CASE WHEN a.modified = '.$nullDate.' THEN a.created ELSE a.modified END as modified, '.
'a.modified_by, uam.name as modified_by_name,'.
// Use created if publish_up is 0
'CASE WHEN a.publish_up = '.$nullDate.' THEN a.created ELSE a.publish_up END as publish_up, '.
'a.publish_down, a.images, a.urls, a.attribs, a.metadata, a.metakey, a.metadesc, a.access, a.hits, a.featured, a.language');
$query->select($subquery1);
$query->select($subquery2);
$query->from('#__content AS a');
// join over the categories
$query->select('c.title AS category_title, c.path AS category_route, c.access AS category_access, c.alias AS category_alias');
$query->join('LEFT', '#__categories AS c ON c.id = a.catid');
// join over the users for the author and modified_by names
switch ($params->get('show_a', 'alias')) {
case 'full': $query->select("ua.name AS author"); break;
case 'user': $query->select("ua.username AS author"); break;
default: $query->select("CASE WHEN a.created_by_alias > ' ' THEN a.created_by_alias ELSE ua.name END AS author");
}
$query->select("ua.email AS author_email");
$query->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
$query->join('LEFT', '#__users AS uam ON uam.id = a.modified_by');
// join over the categories to get parent category titles
$query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias');
$query->join('LEFT', '#__categories as parent ON parent.id = c.parent_id');
$query->where('c.published = 1');
// join on voting table
$query->select('ROUND(v.rating_sum / v.rating_count, 1) AS rating, v.rating_count as rating_count');
$query->join('LEFT', '#__content_rating AS v ON a.id = v.content_id');
// access filter
//$access = !JComponentHelper::getParams('com_content')->get('show_noauth'); // for links only
$authorised = Access::getAuthorisedViewLevels(Factory::getUser()->get('id'));
$show_unauthorized_items = false; // do not allow to show unauthorized items in the free version
if (!$show_unauthorized_items) { // show authorized items only
$query->where('a.access IN ('.$groups.')');
$query->where('c.access IN ('.$groups.')');
}
// filter by start and end dates
$postdate = $params->get('post_d', 'published');
if ($postdate != 'fin_pen' && $postdate != 'pending') {
$query->where('(a.publish_up = '.$nullDate.' OR a.publish_up <= '.$nowDate.')');
}
if ($postdate == 'pending') {
$query->where('a.publish_up > ' . $nowDate);
}
$query->where('(a.publish_down = '.$nullDate.' OR a.publish_down >= '.$nowDate.')');
// filter by date range
switch ($postdate)
{
case 'created' : $dateField = 'a.created'; break;
case 'modified' : $dateField = 'a.modified'; break;
case 'finished' : case 'fin_pen' : /*case 'pending' :*/ $dateField = 'a.publish_down'; break;
default: $dateField = 'a.publish_up';
}
$query->select($db->quoteName($dateField, 'date'));
switch ($params->get('use_range', 0))
{
case 1: // relative
$range_from = $params->get('range_from', 'now'); // now, day, week, month, year
$spread_from = $params->get('spread_from', 1);
$range_to = $params->get('range_to', 'week');
$spread_to = $params->get('spread_to', 1);
if ($range_from == 'now') {
$query->where($dateField.' <= '.$nowDate);
} else {
// if ($dateField != 'a.publish_down') {
// $query->where($dateField.' <= DATE_SUB('.$nowDate.', INTERVAL '.$spread_from.' '.$range_from.')');
// } else {
// $query->where($dateField.' <= DATE_ADD('.$nowDate.', INTERVAL '.$spread_from.' '.$range_from.')');
// }
if ($postdate == 'finished' || $postdate == 'fin_pen' || $postdate == 'pending') {
$query->where($dateField.' <= DATE_ADD('.$nowDate.', INTERVAL '.$spread_from.' '.$range_from.')');
} else {
$query->where($dateField.' <= DATE_SUB('.$nowDate.', INTERVAL '.$spread_from.' '.$range_from.')');
}
}
if ($range_to == 'now') {
$query->where($dateField.' >= '.$nowDate);
} else {
// if ($dateField != 'a.publish_down') {
// $query->where($dateField.' >= DATE_SUB('.$nowDate.', INTERVAL '.$spread_to.' '.$range_to.')');
// } else {
// $query->where($dateField.' >= DATE_ADD('.$nowDate.', INTERVAL '.$spread_to.' '.$range_to.')');
// }
if ($postdate == 'finished' || $postdate == 'fin_pen' || $postdate == 'pending') {
$query->where($dateField.' >= DATE_ADD('.$nowDate.', INTERVAL '.$spread_to.' '.$range_to.')');
} else {
$query->where($dateField.' >= DATE_SUB('.$nowDate.', INTERVAL '.$spread_to.' '.$range_to.')');
}
}
break;
case 2: // range
$startDateRange = $db->quote($params->get('start_date_range', $db->getNullDate()));
$endDateRange = $db->quote($params->get('end_date_range', $db->getNullDate()));
$query->where('('.$dateField.' >= '.$startDateRange.' AND '.$dateField.' <= '.$endDateRange.')');
break;
}
// category filter
$categories_array = $params->get('catid', array());
$array_of_category_values = array_count_values($categories_array);
if (isset($array_of_category_values['all']) && $array_of_category_values['all'] > 0) { // 'all' was selected therefore no filtering
// take everything, so no category selection
} else {
if (isset($array_of_category_values['auto']) && $array_of_category_values['auto'] > 0) { // 'auto' was selected
$categories_array = array();
if ($option === 'com_content') {
switch($view)
{
case 'categories':
$categories_array[] = $jinput->getInt('id'); // id is the top-level category (can be 0 if 'root' has been selected)
break;
case 'category':
$categories_array[] = $jinput->getInt('id');
break;
case 'article':
//if ($params->get('show_on_item_page', 1)) { // useless since test before
$article_id = $jinput->getInt('id');
$catid = $jinput->getInt('catid');
if (!$catid) {
// Get an instance of the generic article model
$article = BaseDatabaseModel::getInstance('Article', 'ContentModel', array('ignore_request' => true));
$article->setState('params', $app->getParams());
$article->setState('filter.published', 1);
$article->setState('article.id', (int) $article_id);
$item = $article->getItem();
$categories_array[] = $item->catid;
} else {
$categories_array[] = $catid;
}
//}
break;
}
}
if (empty($categories_array)) {
return null; // no result if not in the category page
}
}
if (!empty($categories_array)) {
$categories_ids_array = array();
foreach ($categories_array as $category_id) {
$categories_ids_array[$category_id] = array($category_id);
}
// sub-category inclusion
$get_sub_categories = $params->get('includesubcategories', 'no');
if ($get_sub_categories != 'no') {
$levels = $params->get('levelsubcategories', 1);
if (!$show_unauthorized_items) {
$categories_object = Categories::getInstance('Content');
} else {
$categories_object = Categories::getInstance('Content', array('access' => false));
}
foreach ($categories_array as $category_id) {
$category_object = $categories_object->get($category_id); // if category unpublished, unset
if (isset($category_object) && $category_object->hasChildren()) {
$sub_categories_array = $category_object->getChildren(true); // get all levels recursively
foreach ($sub_categories_array as $subcategory_object) {
$condition = ($get_sub_categories == 'all' || ($subcategory_object->level - $category_object->level) <= $levels);
if ($condition) {
//$categories_array[] = $subcategory_object->id;
$categories_ids_array[$category_id][] = $subcategory_object->id;
}
}
}
}
//$categories_array = array_unique($categories_array);
$final_categories_array = array();
foreach ($categories_array as $category_id) {
$final_categories_array = array_merge($final_categories_array, $categories_ids_array[$category_id]);
}
$categories_array = array_unique($final_categories_array);
}
$test_type = $params->get('cat_inex', 1) ? 'IN' : 'NOT IN';
$query->where('a.catid '.$test_type.' ('.implode(',', $categories_array).')');
}
}
// metakeys filter
$metakeys = array();
$keys = explode(',', $params->get('keys', ''));
// assemble any non-blank word(s)
foreach ($keys as $key) {
$key = trim($key);
if ($key) {
$metakeys[] = $key;
}
}
if (!empty($item_on_page_keys)) {
if (!empty($metakeys)) { // if none of the tags we filter are in the content item on the page, return nothing
$keys_in_common = array_intersect($item_on_page_keys, $metakeys);
if (empty($keys_in_common)) {
return array();
}
$metakeys = $keys_in_common;
} else {
$metakeys = $item_on_page_keys;
}
}
if (!empty($metakeys)) {
$concat_string = $query->concatenate(array('","', ' REPLACE(a.metakey, ", ", ",")', ' ","')); // remove single space after commas in keywords
$query->where('('.$concat_string.' LIKE "%'.implode('%" OR '.$concat_string.' LIKE "%', $metakeys).'%")');
}
// tags filter
$tags = $params->get('tags', array());
if (!empty($tags)) {
// if all selected, get all available tags
$array_of_tag_values = array_count_values($tags);
if (isset($array_of_tag_values['all']) && $array_of_tag_values['all'] > 0) { // 'all' was selected
$tags = array();
$tag_objects = SYWTags::getTags('com_content.article');
if ($tag_objects !== false) {
foreach ($tag_objects as $tag_object) {
$tags[] = $tag_object->id;
}
}
if (empty($tags) && $params->get('tags_inex', 1)) { // won't return any article if no article has been associated to any tag (when include tags only)
return array();
}
} else if ($params->get('include_tag_children', 0)) { // get tag children
$tagTreeArray = array();
$helper_tags = new TagsHelper();
foreach ($tags as $tag) {
$helper_tags->getTagTreeArray($tag, $tagTreeArray);
}
$tags = array_unique(array_merge($tags, $tagTreeArray));
}
}
if (!empty($item_on_page_tagids)) {
if (!empty($tags)) { // if none of the tags we filter are in the content item on the page, return nothing
// take the tags common to the item on the page and the module selected tags
$tags_in_common = array_intersect($item_on_page_tagids, $tags);
if (empty($tags_in_common)) {
return array();
}
if ($params->get('tags_match', 'any') == 'all') {
if (count($tags_in_common) != count($tags)) {
return array();
}
}
$tags = $tags_in_common;
} else {
$tags = $item_on_page_tagids;
}
// Note: does not work if 'exclude' tags, which is normal
}
if (!empty($tags)) {
$tags_to_match = implode(',', $tags);
$query->select('COUNT(tags.id) AS tags_count');
$query->join('INNER', $db->quoteName('#__contentitem_tag_map', 'm').' ON '.$db->quoteName('m.content_item_id').' = '.$db->quoteName('a.id').' AND '.$db->quoteName('m.type_alias').' = '.$db->quote('com_content.article'));
$query->join('INNER', $db->quoteName('#__tags', 'tags').' ON '.$db->quoteName('m.tag_id').' = '.$db->quoteName('tags.id'));
$query->where($db->quoteName('tags.access').' IN ('.$groups.')');
$query->where($db->quoteName('tags.published').' = 1');
// keep all items with tags to be handled outside the query (when exclude all)
if (!$params->get('tags_inex', 1) && $params->get('tags_match', 'any') == 'all') {
// keep all tags
} else {
$test_type = $params->get('tags_inex', 1) ? 'IN' : 'NOT IN';
$query->where($db->quoteName('tags.id').' '.$test_type.' ('.$tags_to_match.')');
}
if (!$params->get('tags_inex', 1) && $params->get('tags_match', 'any') == 'all') {
// handled outside the query
} else {
if (!$params->get('tags_inex', 1)) { // EXCLUDE TAGS
$query->select('tags_per_items.tag_count_per_item');
// subquery gets all the tags for all items
$subquery = 'SELECT mm.content_item_id AS content_id, COUNT(tt.id) AS tag_count_per_item FROM #__contentitem_tag_map AS mm INNER JOIN #__tags AS tt ON mm.tag_id = tt.id WHERE tt.access IN ('.$groups.') AND tt.published = 1 AND mm.type_alias = \'com_content.article\' GROUP BY content_id';
$query->join('INNER', '(' . $subquery . ') AS tags_per_items ON tags_per_items.content_id = a.id');
// we keep items that have the same amount of tags before and after removals
$query->having('COUNT('.$db->quoteName('tags.id').') = '.$db->quoteName('tags_per_items.tag_count_per_item'));
} else { // INCLUDE TAGS
if ($params->get('tags_match', 'any') == 'all') {
$query->having('COUNT('.$db->quoteName('tags.id').') = '.count($tags));
}
}
}
$query->group($db->quoteName('a.id'));
}
// user filter
$include = $params->get('author_inex', 1);
$authors_array = $params->get('created_by', array());
// old parameter - backward compatibility
$old_authors = $params->get('user_id', '');
if ($old_authors) {
switch ($old_authors)
{
case 'by_me': $include = true; $authors_array[] = 'auto'; break;
case 'not_me': $include = false; $authors_array[] = 'auto'; break;
case 'all': default: $authors_array[] = 'all';
}
}
$array_of_authors_values = array_count_values($authors_array);
if (isset($array_of_authors_values['all']) && $array_of_authors_values['all'] > 0) { // 'all' was selected
if ($params->get('allow_edit', 0)) {
if ($user->authorise('core.edit', 'com_content')) {
// logged user can see everyone's unpublished articles
$query->where('a.state IN (0, 1)');
} else {
// logged user can see his own unpublished articles only
$query->where('(a.state = 1) OR (a.state = 0 AND a.created_by = ' . (int) $user->get('id') . ')');
}
} else {
$query->where('a.state = 1');
}
} else if (isset($array_of_authors_values['auto']) && $array_of_authors_values['auto'] > 0) { // 'auto' was selected
$test_type = $include ? '=' : '<>';
$query->where('a.created_by ' .$test_type.' '.(int) $user->get('id'));
if ($include && $params->get('allow_edit', 0)) {
$query->where('a.state IN (0, 1)'); // show all articles for the logged author, published or not
} else {
$query->where('a.state = 1');
}
} else {
$authors = implode(',', $authors_array);
if ($authors) {
$test_type = $include ? 'IN' : 'NOT IN';
$query->where('a.created_by '.$test_type.' ('.$authors.')');
}
if ($params->get('allow_edit', 0)) {
if ($user->authorise('core.edit', 'com_content')) {
// logged user can see everyone's unpublished articles
$query->where('a.state IN (0, 1)');
} else {
if ($include) {
if (in_array($user->get('id'), $authors_array)) {
// the user is part of the selected authors
// logged user can see his own unpublished articles only
$query->where('(a.state = 1) OR (a.state = 0 AND a.created_by = ' . (int) $user->get('id') . ')');
} else {
$query->where('a.state = 1');
}
} else {
if (!in_array($user->get('id'), $authors_array)) {
// the user is not part of the discarded authors but may not be an author
// logged user can see his own unpublished articles only
$query->where('(a.state = 1) OR (a.state = 0 AND a.created_by = ' . (int) $user->get('id') . ')');
} else {
$query->where('a.state = 1');
}
}
}
} else {
$query->where('a.state = 1');
}
// if ($include && $params->get('allow_edit', 0) && count($authors_array) == 1 && $authors_array[0] == (int) $user->get('id')) {
// $query->where('a.state IN (0, 1)'); // show all articles for the logged author, published or not
// } else {
// $query->where('a.state = 1');
// }
}
// language filter
if ($params->get('filter_lang', 1) && $app->getLanguageFilter()) {
$query->where('a.language IN ('.$db->quote(Factory::getLanguage()->getTag()).','.$db->quote('*').')');
}
$ordering = '';
// author order
switch ($params->get('author_order', ''))
{
case 'selec_asc': $ordering .= "author ASC,"; break;
case 'selec_dsc': $ordering .= "author DESC,"; break;
}
// featured switch
$featured = false;
$featured_only = false;
switch ($params->get('show_f', 3))
{
case '1': // only
$featured = true;
$featured_only = true;
$query->where('a.featured = 1');
if ($params->get('order') == 'o_asc' || $params->get('order') == 'o_dsc') {
$query->join('LEFT', '#__content_frontpage AS fp ON fp.content_id = a.id');
}
break;
case '0': // hide
$query->where('a.featured = 0');
break;
case '2': // first the featured ones
$featured = true;
if ($params->get('order') == 'o_asc' || $params->get('order') == 'o_dsc') {
$query->join('LEFT', '#__content_frontpage AS fp ON fp.content_id = a.id');
}
$ordering .= 'a.featured DESC,';
break;
default: // no discrimination between featured/unfeatured items
$featured = true;
if ($params->get('order') == 'o_asc' || $params->get('order') == 'o_dsc') {
$query->join('LEFT', '#__content_frontpage AS fp ON fp.content_id = a.id');
}
break;
}
// category order
if (!$featured_only) {
switch ($params->get('cat_order', ''))
{
case 'o_asc': $ordering .= 'c.lft ASC,'; break;
case 'o_dsc': $ordering .= 'c.lft DESC,'; break;
case 't_asc': $ordering .= 'c.title ASC,'; break;
case 't_dsc': $ordering .= 'c.title DESC,'; break;
}
}
// general ordering
switch ($params->get('order'))
{
case 'o_asc': if ($featured) { $ordering .= 'CASE WHEN (a.featured = 1) THEN fp.ordering ELSE a.ordering END ASC'; } else { $ordering .= 'a.ordering ASC'; } break;
case 'o_dsc': if ($featured) { $ordering .= 'CASE WHEN (a.featured = 1) THEN fp.ordering ELSE a.ordering END DESC'; } else { $ordering .= 'a.ordering DESC'; } break;
case 'p_asc': $ordering .= 'a.publish_up ASC'; break;
case 'p_dsc': $ordering .= 'a.publish_up DESC'; break;
case 'f_asc': $ordering .= 'CASE WHEN (a.publish_down = '.$nullDate.') THEN a.publish_up ELSE a.publish_down END ASC'; break;
case 'f_dsc': $ordering .= 'CASE WHEN (a.publish_down = '.$nullDate.') THEN a.publish_up ELSE a.publish_down END DESC'; break;
case 'm_asc': $ordering .= 'a.modified ASC, a.created ASC'; break;
case 'm_dsc': $ordering .= 'a.modified DESC, a.created DESC'; break;
case 'c_asc': $ordering .= 'a.created ASC'; break;
case 'c_dsc': $ordering .= 'a.created DESC'; break;
case 'mc_asc': $ordering .= 'CASE WHEN (a.modified = '.$nullDate.') THEN a.created ELSE a.modified END ASC'; break;
case 'mc_dsc': $ordering .= 'CASE WHEN (a.modified = '.$nullDate.') THEN a.created ELSE a.modified END DESC'; break;
case 'random': $ordering .= 'rand()'; break;
case 'hit': $ordering .= 'a.hits DESC'; break;
case 'title_asc': $ordering .= 'a.title ASC'; break;
case 'title_dsc': $ordering .= 'a.title DESC'; break;
default: $ordering .= 'a.publish_up DESC'; break;
}
$query->order($ordering);
// include only
$articles_to_include = trim($params->get('in', ''));
if (!empty($articles_to_include)) {
$query->where('a.id IN ('.$articles_to_include.')');
}
// exclude
$articles_to_exclude = array_filter(explode(",", trim($params->get('ex', ''))));
$item_on_page_id = '';
if ($params->get('ex_current_item', 0) && $option === 'com_content' && $view === 'article') {
$temp = $jinput->getString('id');
$temp = explode(':', $temp);
$item_on_page_id = $temp[0];
}
if ($item_on_page_id) { // do not show the current article in the list
$articles_to_exclude[] = $item_on_page_id;
}
if (!empty($articles_to_exclude)) {
$query->where('a.id NOT IN ('.implode(",", $articles_to_exclude).')');
}
// launch query
$count = trim($params->get('count', ''));
$startat = $params->get('startat', 1);
if ($startat < 1) {
$startat = 1;
}
if (!empty($tags) && !$params->get('tags_inex', 1) && $params->get('tags_match', 'any') == 'all') {
$db->setQuery($query);
} else if (!empty($count) && $params->get('count_for', 'articles') == 'articles') {
$db->setQuery($query, $startat - 1, intval($count));
} else {
$db->setQuery($query);
}
try {
$items = $db->loadObjectList();
} catch (\DatabaseExceptionExecuting $e) {
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
return null;
}
// END OF DATABASE QUERY
if (empty($items)) {
return array();
}
// ITEM DATA MODIFICATIONS AND ADDITIONS
$still_need_to_slice_count = false;
// exclude all
if (!empty($tags) && !$params->get('tags_inex', 1) && $params->get('tags_match', 'any') == 'all') {
$total_tags = count($tags);
foreach ($items as $key => &$item) {
if (!isset($item->tags)) {
$item->tags = self::getItemTags($item->id);
}
if (self::getItemTagsCountIn($item->tags, $tags) == $total_tags) {
unset($items[$key]);
}
}
$still_need_to_slice_count = true;
}
// restrict articles per author or category
// drawback : forces grouping per author or category
$count_for = $params->get('count_for', 'articles');
if ($count_for == 'catid' || $count_for == 'author') {
$grouped = array();
$pass = array();
$ordered_items = array();
if ($count_for == 'catid' && isset($categories_ids_array) && count($categories_ids_array) > 1) {
$array_keys = array_keys($categories_ids_array);
$last_key = array_pop($array_keys);
foreach ($categories_ids_array as $category_id => $categories_id_array) {
$grouped[$category_id] = array();
$pass[$category_id] = array();
foreach ($items as $key => $item) {
if (in_array($item->catid, $categories_id_array)) {
if (count($pass[$category_id]) < ($startat - 1)) {
$pass[$category_id][] = $item->id;
} else {
if ($count) {
if (count($grouped[$category_id]) < intval($count)) {
$grouped[$category_id][] = $item->catid;
$ordered_items[$key] = $item;
} else {
if ($category_id == $last_key) {
break; // only break on the last category
}
}
} else {
$ordered_items[$key] = $item;
}
}
unset($items[$key]);
}
}
}
ksort($ordered_items); // the ordre may have been lost
} else {
foreach ($items as $key => $item) {
if (!isset($grouped[$item->$count_for])) {
$grouped[$item->$count_for] = array();
$pass[$item->$count_for] = array();
}
if (count($pass[$item->$count_for]) < ($startat - 1)) {
$pass[$item->$count_for][] = $item->id;
} else {
if ($count) {
if (count($grouped[$item->$count_for]) < intval($count)) {
$grouped[$item->$count_for][] = $item->id;
$ordered_items[$key] = $item;
}
} else {
$ordered_items[$key] = $item;
}
}
}
}
$items = $ordered_items;
}
// limit to count
if ($still_need_to_slice_count) {
if (!empty($count)) {
$items = array_slice($items, $startat - 1, intval($count));
} else {
$items = array_slice($items, $startat - 1);
}
}
// parameters for all
$head_type = $params->get('head_type', 'none');
$image_types = array('image', 'imageintro', 'imagefull', 'allimagesasc', 'allimagesdesc');
$show_image = false;
if (in_array($head_type, $image_types)) {
$show_image = true;
$crop_picture = $params->get('crop_pic', 0);
$allow_remote = $params->get('allow_remote', true);
$maintain_height = $params->get('maintain_height', 0);
$head_width = $params->get('head_w', 64);
$head_height = $params->get('head_h', 64);
$border_width = $params->get('border_w', 0);
$head_width = $head_width - $border_width * 2;
$head_height = $head_height - $border_width * 2;
$filter = $params->get('filter', 'none');
$quality_jpg = $params->get('quality_jpg', 100);
$quality_png = $params->get('quality_png', 0);
$quality_webp = $params->get('quality_webp', 80);
if ($quality_jpg > 100) {
$quality_jpg = 100;
}
if ($quality_jpg < 0) {
$quality_jpg = 0;
}
if ($quality_png > 9) {
$quality_png = 9;
}
if ($quality_png < 0) {
$quality_png = 0;
}
if ($quality_webp > 100) {
$quality_webp = 100;
}
if ($quality_webp < 0) {
$quality_webp = 0;
}
$image_qualities = array('jpg' => $quality_jpg, 'png' => $quality_png, 'webp' => $quality_webp);
$clear_cache = $params->get('clear_cache', 0);
if ($params->get('site_mode', 'adv') == 'dev') {
$clear_cache = 1;
} else if ($params->get('site_mode', 'adv') == 'prod') {
$clear_cache = 0;
}
$subdirectory = 'thumbnails/lne';
if ($params->get('thumb_path', 'images') == 'cache') {
$subdirectory = 'mod_latestnewsenhanced';
}
$tmp_path = SYWCache::getTmpPath($params->get('thumb_path', 'images'), $subdirectory);
$default_picture = trim($params->get('default_pic', ''));
if ($clear_cache) {
modLatestNewsEnhancedHelper::clearThumbnails($module->id, $tmp_path);
}
}
$text_type = $params->get('text', 'intro');
$letter_count = trim($params->get('l_count'));
$truncate_last_word = $params->get('trunc_l_w', 0);
$keep_tags = $params->get('keep_tags');
$strip_tags = $params->get('strip_tags', 1);
$always_show_readmore = $params->get('readmore_always_show', true);
$trigger_OnContentPrepare = $params->get('trigger_events', false);
$force_one_line = $params->get('force_one_line', false);
$title_letter_count = trim($params->get('letter_count_title', ''));
$title_truncate_last_word = $params->get('trunc_l_w_title', 0);
//$show_date = $params->get('show_d', 'date');
$link_to = $params->get('link_to', 'item');
switch ($params->get('link_target', 'default')) {
case 'same': $link_target = ''; break;
case 'new': $link_target = 1; break;
case 'modal': $link_target = 3; break;
case 'popup': $link_target = 2; break;
default: $link_target = 'default';
}
$when_no_date = $params->get('when_no_date', 0);
$items_with_no_date = array();
foreach ($items as $key => &$item) {
// date
if ($item->date == $db->getNullDate() || $item->date == null) {
if ($when_no_date == 0) {
unset($items[$key]);
continue;
}
$item->date = '';
}
// category link
if (!$show_unauthorized_items || in_array($item->category_access, $authorised)) {
$item->catlink = Route::_(ContentHelperRoute::getCategoryRoute($item->cat_slug, $item->language));
$item->category_authorized = true;
} else {
$catlink = new Uri(Route::_('index.php?option=com_users&view=login', false));
$category = Categories::getInstance('Content', array('access' => false))->get($item->catid);
$catlink->setVar('return', base64_encode(ContentHelperRoute::getCategoryRoute($category, $item->language)));
$item->catlink = $catlink;
$item->category_authorized = false;
}
// item edit link
if ($params->get('allow_edit', 0)) {
$edit = false;
if ($user->authorise('core.edit', 'com_content.article.' . $item->id)) {
$edit = true;
} else if ($user->get('id') && $user->authorise('core.edit.own', 'com_content.article.' . $item->id)) {
if ($user->get('id') == $item->created_by) {
$edit = true;
}
}
if ($edit) {
$item->link_edit = ContentHelperRoute::getArticleRoute($item->slug, $item->cat_slug, $item->language);
$item->link_edit .= '&task=article.edit&a_id=' . $item->id . '&return=' . base64_encode(Uri::getInstance());
$item->link_edit = Route::_($item->link_edit);
}
}
// item link
$item->link = '';
if ($item->state == 1) {
//$item->linktarget = '';
$item->isinternal = true;
$item->linktitle = $item->title;
$link_string = ContentHelperRoute::getArticleRoute($item->slug, $item->cat_slug, $item->language);
$forced_itemid = intval($params->get('force_itemid', ''));
if ($forced_itemid > 0) {
if (Multilanguage::isEnabled()) {
$currentLanguage = Factory::getLanguage()->getTag();
$langAssociations = Associations::getAssociations('com_menus', '#__menu', 'com_menus.item', $forced_itemid, 'id', '', '');
foreach ($langAssociations as $langAssociation) {
if ($langAssociation->language == $currentLanguage) {
$forced_itemid = $langAssociation->id;
break;
}
}
}
if (strpos($link_string, 'Itemid') === false) {
$link_string .= '&Itemid=' . $forced_itemid;
} else {
$link_string = preg_replace('#Itemid=([0-9]*)#', 'Itemid=' . $forced_itemid, $link_string);
}
}
if ($item->category_authorized && (!$show_unauthorized_items || in_array($item->access, $authorised))) {
if ($link_target !== 'default') {
$item->linktarget = $link_target;
} else {
$item->linktarget = '';
}
// strange: no Itemid search in ContentHelperRoute::getArticleRoute starting in Joomla 3.7
$item->link = Route::_($link_string);
$item->authorized = true; // we know that user has the privilege to view the article
} else {
$link = new Uri(Route::_('index.php?option=com_users&view=login', false));
//$link->setVar('return', base64_encode(JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->cat_slug, $item->language), false)));
// returns /MyWork_3_4/index.php/latest-news/13-ipsum/9-phosfluorescently-engage-worldwide-methodologies-with-web-enabled-technology-5
// does not work
if ($item->category_authorized) {
$link->setVar('return', base64_encode($link_string));
} else {
$link_string = self::getUnauthorizedArticleRoute($item->slug, $item->cat_slug, $item->language);
if ($forced_itemid > 0) {
if (strpos($link_string, 'Itemid') === false) {
$link_string .= '&Itemid=' . $forced_itemid;
} else {
$link_string = preg_replace('#Itemid=([0-9]*)#', 'Itemid=' . $forced_itemid, $link_string);
}
}
$link->setVar('return', base64_encode($link_string));
}
// works
// returns 'index.php/latest-news/13-ipsum/9-phosfluorescently-engage-worldwide-methodologies-with-web-enabled-technology-5';
$item->link = $link;
$item->linktarget = ''; // cannot open in modal window in this case - too many cases where it might fail bacause the login form opens first
$item->authorized = false;
}
}
// rating (to avoid call to rating plugin, use $item->vote)
if (isset($item->rating)) {
$item->vote = $item->rating; // to avoid calls to rating plugin
$item->vote_count = $item->rating_count;
unset($item->rating);
unset($item->rating_count);
} else {
$item->vote = '';
$item->vote_count = 0;
}
// tags
if (!isset($item->tags)) {
$item->tags = self::getItemTags($item->id);
}
// thumbnail image creation
$item->imagetag = '';
$item->error = array();
if ($show_image) {
// Convert the images field to an array
$registry = new Registry();
$registry->loadString($item->images);
$images_array = $registry->toArray();
$thumbnails_exist = false;
$filename = '';
if (!$clear_cache && $params->get('create_thumb', 1)) {
$thumbnails_exist_tmp = modLatestNewsEnhancedHelper::thumbnailExists($module->id, $item->id, $tmp_path);
if ($thumbnails_exist_tmp != false) {
$filename = $thumbnails_exist_tmp;
$thumbnails_exist = true;
}
}
if (!$thumbnails_exist) {
// thumbnail(s) do not exist
$imagesrc = '';
if ($head_type == 'imageintro') {
if ($images_array) {
$imagesrc = trim($images_array['image_intro']);
}
} else if ($head_type == 'imagefull') {
if ($images_array) {
$imagesrc = trim($images_array['image_fulltext']);
}
} else if ($head_type == 'image') {
if (isset($item->fulltext)) {
$imagesrc = modLatestNewsEnhancedHelper::getImageSrcFromContent($item->introtext, $item->fulltext);
} else {
$imagesrc = modLatestNewsEnhancedHelper::getImageSrcFromContent($item->introtext);
}
} else if ($head_type == 'allimagesasc') {
if (isset($item->fulltext)) {
$imagesrc = modLatestNewsEnhancedHelper::getImageSrcFromContent($item->introtext, $item->fulltext);
} else {
$imagesrc = modLatestNewsEnhancedHelper::getImageSrcFromContent($item->introtext);
}
// if images not found, look into intro and full article
if (empty($imagesrc)) {
if ($images_array) {
$imagesrc = trim($images_array['image_intro']);
if (empty($imagesrc)) {
$imagesrc = trim($images_array['image_fulltext']);
}
}
}
} else if ($head_type == 'allimagesdesc') {
// look into image intro and full first
if ($images_array) {
$imagesrc = trim($images_array['image_intro']);
if (empty($imagesrc)) {
$imagesrc = trim($images_array['image_fulltext']);
}
}
// if image full article not found, look into the article
if (empty($imagesrc)) {
if (isset($item->fulltext)) {
$imagesrc = modLatestNewsEnhancedHelper::getImageSrcFromContent($item->introtext, $item->fulltext);
} else {
$imagesrc = modLatestNewsEnhancedHelper::getImageSrcFromContent($item->introtext);
}
}
}
// last resort, use default image if it exists
$used_default_image = false;
if (empty($imagesrc)) {
if ($default_picture) {
$imagesrc = $default_picture;
$used_default_image = true;
}
}
if ($imagesrc) { // found an image
if (!$params->get('create_thumb', 1)) {
$filename = $imagesrc;
} else {
$result_array = modLatestNewsEnhancedHelper::getImageFromSrc($module->id, $item->id, $imagesrc, $tmp_path, $head_width, $head_height, $crop_picture, $image_qualities, $filter, false, $allow_remote);
if (!empty($result_array[0])) {
$filename = $result_array[0];
}
if (!empty($result_array[1])) {
// if error for the file found, try and use the default image instead
if (!$used_default_image && $default_picture) { // if the default image was the one chosen, no use to retry
$result_array = modLatestNewsEnhancedHelper::getImageFromSrc($module->id, $item->id, $default_picture, $tmp_path, $head_width, $head_height, $crop_picture, $image_qualities, $filter, false, $allow_remote);
if (!empty($result_array[0])) {
$filename = $result_array[0];
}
if (!empty($result_array[1])) {
$item->error[] = $result_array[1];
}
} else {
$item->error[] = $result_array[1];
}
}
}
}
if ($filename && empty($item->error)) {
$thumbnails_exist = true;
}
}
if ($filename) {
// $extra_styling = '';
// if ($thumbnails_exist) {
// thumbnails have been created
// if (!$crop_picture && $maintain_height) {
// $imagesize = @getimagesize($filename); // @ to avoid warnings
// if ($imagesize !== FALSE) {
// $imageheight = $imagesize[1];
// $top = intval(($head_height - $imageheight) / 2); // to center the image, when no cropping
// $extra_styling = ' style="position: relative; top: '.$top.'px"';
// }
// }
// $filename = Uri::base(true).'/'.$filename;
// }
$img_attributes = array();
if ($crop_picture) {
$img_attributes = array('width' => $head_width, 'height' => $head_height);
}
//$item->imagetag = '<img alt="'.$item->title.'" src="'.$filename.'"'.$extra_styling.' />';
$item->imagetag = SYWUtilities::getImageElement($filename, $item->title, $img_attributes, true);
}
}
// ago
if ($item->date) {
$details = modLatestNewsEnhancedHelper::date_to_counter($item->date, ($postdate == 'finished' || $postdate == 'fin_pen' || $postdate == 'pending') ? true : false);
$item->nbr_seconds = intval($details['secs']);
$item->nbr_minutes = intval($details['mins']);
$item->nbr_hours = intval($details['hours']);
$item->nbr_days = intval($details['days']);
$item->nbr_months = intval($details['months']);
$item->nbr_years = intval($details['years']);
}
// calendar shows a custom field of type 'calendar'
if ($head_type == 'calendar') {
$item->calendar_date = $item->date;
}
// title
if (!$force_one_line) {
if (strlen($title_letter_count) > 0) {
$item->title = SYWText::getText($item->title, 'txt', (int)$title_letter_count, true, '', true, $title_truncate_last_word);
}
}
// text
$item->text = '';
$number_of_letters = -1;
if ($letter_count != '') {
$number_of_letters = (int)($letter_count);
}
$beacon = '';
if (!$always_show_readmore) {
$beacon = '^';
}
switch ($text_type)
{
case 'intrometa': $use_intro = (trim($item->introtext) != '') ? true : false; break;
case 'metaintro': $use_intro = (trim($item->metadesc) != '') ? false : true; break;
case 'meta': $use_intro = false; break;
default: case 'intro': $use_intro = true;
}
if ($use_intro) { // use intro text
$item->text = $item->introtext;
if ($item->text) {
if ($trigger_OnContentPrepare) { // will trigger events from plugins
//$item->text = JHtml::_('content.prepare', $item->text);
PluginHelper::importPlugin('content');
JEventDispatcher::getInstance()->trigger('onContentPrepare', array('com_content.article', &$item, &$item->params, 0));
}
$item->text = SYWText::getText($item->text.$beacon, 'html', $number_of_letters, $strip_tags, trim($keep_tags), true, $truncate_last_word);
}
} else { // use meta text
$item->text = SYWText::getText($item->metadesc.$beacon, 'txt', $number_of_letters, false, '', true, $truncate_last_word);
}
// the text won't be cropped if the ^ character is still present after processing (hopefully no ^ at the end of the text)
$item->cropped = true;
if (!$always_show_readmore) {
$text_length = strlen($item->text);
$item->text = rtrim($item->text, "^");
if (strlen($item->text) < $text_length && !$item->fulltexthascontent) {
$item->cropped = false;
}
}
// re-order items with no dates
if (empty($item->date) && ($when_no_date == 1 || $when_no_date == 2)) {
$items_with_no_date[] = $item;
unset($items[$key]);
}
}
if ($when_no_date == 1) {
return array_merge($items_with_no_date, $items);
} else if ($when_no_date == 2) {
return array_merge($items, $items_with_no_date);
}
return $items;
}
// rewrite of article route in the case where the category is not authorized
// Joomla should have handled the category node like in the category route code
protected static function getUnauthorizedArticleRoute($id, $catid = 0, $language = 0)
{
$needles = array(
'article' => array((int) $id)
);
// Create the link
$link = 'index.php?option=com_content&view=article&id=' . $id;
if ((int) $catid > 1)
{
$categories = Categories::getInstance('Content', array('access' => false)); // important!
$category = $categories->get((int) $catid);
if ($category)
{
$needles['category'] = array_reverse($category->getPath());
$needles['categories'] = $needles['category'];
$link .= '&catid=' . $catid;
}
}
if ($language && $language != "*" && Multilanguage::isEnabled())
{
$link .= '&lang=' . $language;
$needles['language'] = $language;
}
if ($item = self::_findItem($needles))
{
$link .= '&Itemid=' . $item;
}
return $link;
}
protected static $lookup = array();
protected static function _findItem($needles = null)
{
$app = Factory::getApplication();
$menus = $app->getMenu('site');
$language = isset($needles['language']) ? $needles['language'] : '*';
// Prepare the reverse lookup array.
if (!isset(self::$lookup[$language]))
{
self::$lookup[$language] = array();
$component = ComponentHelper::getComponent('com_content');
$attributes = array('component_id');
$values = array($component->id);
if ($language != '*')
{
$attributes[] = 'language';
$values[] = array($needles['language'], '*');
}
$items = $menus->getItems($attributes, $values);
foreach ($items as $item)
{
if (isset($item->query) && isset($item->query['view']))
{
$view = $item->query['view'];
if (!isset(self::$lookup[$language][$view]))
{
self::$lookup[$language][$view] = array();
}
if (isset($item->query['id']))
{
/**
* Here it will become a bit tricky
* language != * can override existing entries
* language == * cannot override existing entries
*/
if (!isset(self::$lookup[$language][$view][$item->query['id']]) || $item->language != '*')
{
self::$lookup[$language][$view][$item->query['id']] = $item->id;
}
}
}
}
}
if ($needles)
{
foreach ($needles as $view => $ids)
{
if (isset(self::$lookup[$language][$view]))
{
foreach ($ids as $id)
{
if (isset(self::$lookup[$language][$view][(int) $id]))
{
return self::$lookup[$language][$view][(int) $id];
}
}
}
}
}
// Check if the active menuitem matches the requested language
$active = $menus->getActive();
if ($active
&& $active->component == 'com_content'
&& ($language == '*' || in_array($active->language, array('*', $language)) || !Multilanguage::isEnabled()))
{
return $active->id;
}
// If not found, return language specific home link
$default = $menus->getDefault($language);
return !empty($default->id) ? $default->id : null;
}
protected static function getItemTags($id)
{
$helper_tags = new TagsHelper();
$tags_array = $helper_tags->getItemTags('com_content.article', $id); // array of tag objects
if (count($tags_array) > 0) {
return $tags_array;
}
return array();
}
protected static function getItemTagsCountIn($item_tags, $tags)
{
if (empty($item_tags)) {
return 0;
}
$count_tags_in_item = 0;
foreach ($item_tags as $tag) {
if (in_array($tag->id, $tags)) {
$count_tags_in_item++;
}
}
return $count_tags_in_item;
}
}
?>