get('id');
$jnow = JFactory::getDate();
$now = $db->Quote($jnow->toSql());
$null = $db->Quote($db->getNullDate());
$count = $params->get('count', 4);
$catid = $params->get('catid');
$show_front = $params->get('show_front', 1);
$aid = $user->get('aid', 0);
$content_type = $params->get('content_type', 'joomla');
$ordering = $params->get('itemsOrdering');
$cid = $params->get('category_id', NULL);
$user_id = $params->get('user_id');
$text_length = intval($params->get('preview_count', 200));
$tags_option = $params->get('strip_tags', 'a,i,br,img');
$thumb_size = $params->get('thumb_width', 90);
$show_thumbnails = $params->get('show_thumbnails', 1);
$thumbnail_link = $params->get('thumbnail_link', 1);
$params->set('tabs_count', $count);
// content specific stuff
if ($content_type == 'joomla') {
// start Joomla specific
jimport('joomla.application.component.model');
// Get an instance of the generic articles model
if (class_exists('JModelLegacy')) {
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
} else {
$model = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
}
// Set application parameters in model
$appParams = JFactory::getApplication()->getParams();
$model->setState('params', $appParams);
// Set the filters based on the module params
$model->setState('list.start', 0);
$model->setState('list.limit', $count);
$model->setState('filter.published', 1);
// Access filter
$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
$model->setState('filter.access', $access);
// Category filter
$model->setState('filter.category_id', $catid);
// User filter
$userId = JFactory::getUser()->get('id');
switch ($user_id) {
case 'by_me':
$model->setState('filter.author_id', (int)$userId);
break;
case 'not_me':
$model->setState('filter.author_id', $userId);
$model->setState('filter.author_id.include', false);
break;
case 0:
break;
default:
$model->setState('filter.author_id', $user_id);
break;
}
// Featured switch
switch ($show_front) {
case 1:
$model->setState('filter.featured', 'show');
break;
case 0:
$model->setState('filter.featured', 'hide');
break;
default:
$model->setState('filter.featured', 'only');
break;
}
// ordering
switch ($ordering) {
case 'date' :
$orderby = 'a.created ASC';
break;
case 'rdate' :
$orderby = 'a.created DESC';
break;
case 'alpha' :
$orderby = 'a.title';
break;
case 'ralpha' :
$orderby = 'a.title DESC';
break;
case 'order':
if ($show_front == '2') $orderby = 'a.featured_ordering'; else
$orderby = 'a.ordering';
break;
case 'random' :
$orderby = 'RAND()';
break;
default :
$orderby = 'a.id DESC';
break;
}
$ordering = explode(' ', $orderby);
$model->setState('list.ordering', $ordering[0]);
$model->setState('list.direction', isset($ordering[1]) ? $ordering[1] : null);
$rows = $model->getItems();
// end Joomla specific
} else {
// start K2 specific
require_once(JPATH_SITE . '/components/com_k2/models/itemlist.php');
require_once(JPATH_SITE . '/components/com_k2/helpers/route.php');
require_once(JPATH_SITE . '/components/com_k2/helpers/utilities.php');
//Initialize Variables
$k2_category = $params->get('k2_category', array());
$k2_children = $params->get('k2_children', 0);
$k2_ordering = $params->get('k2_ordering', 'a.title');
$k2_featured = $params->get('k2_featured', 1);
$k2_image_size = $params->get('k2_image_size', 'M');
$query = "SELECT a.*, c.name AS categoryname,c.id AS categoryid, c.alias AS categoryalias, c.params AS categoryparams";
$query .= " FROM #__k2_items as a LEFT JOIN #__k2_categories c ON c.id = a.catid";
$query .= " WHERE a.published = 1 AND a.access IN(" . implode(',', $user->authorisedLevels()) . ") AND a.trash = 0 AND c.published = 1 AND c.access IN(" . implode(',', $user->authorisedLevels()) . ") AND c.trash = 0";
//User Filter
switch ($user_id) {
case 'by_me':
$query .= ' AND (a.created_by = ' . (int)$userId . ' OR a.modified_by = ' . (int)$userId . ')';
break;
case 'not_me':
$query .= ' AND (a.created_by <> ' . (int)$userId . ' AND a.modified_by <> ' . (int)$userId . ')';
break;
}
$query .= " AND ( a.publish_up = " . $null . " OR a.publish_up <= " . $now . " )";
$query .= " AND ( a.publish_down = " . $null . " OR a.publish_down >= " . $now . " )";
if (!is_null($k2_category)) {
if (is_array($k2_category)) {
if ($k2_children) {
require_once (JPATH_SITE . '/components/com_k2/models/itemlist.php');
$categories = K2ModelItemlist::getCategoryTree($k2_category);
$sql = @implode(',', $categories);
$query .= " AND a.catid IN ({$sql})";
} else {
JArrayHelper::toInteger($k2_category);
$query .= " AND a.catid IN(" . implode(',', $k2_category) . ")";
}
} else {
if ($k2_children) {
require_once (JPATH_SITE . '/components/com_k2/models/itemlist.php');
$categories = K2ModelItemlist::getCategoryTree($k2_category);
$sql = @implode(',', $categories);
$query .= " AND a.catid IN ({$sql})";
} else {
$query .= " AND a.catid=" . (int)$k2_category;
}
}
}
if ($k2_featured == '0') $query .= " AND a.featured != 1";
if ($k2_featured == '2') $query .= " AND a.featured = 1";
if ($app->getLanguageFilter()) {
$languageTag = JFactory::getLanguage()->getTag();
$query .= " AND c.language IN (" . $db->Quote($languageTag) . ", " . $db->Quote('*') . ") AND a.language IN (" . $db->Quote($languageTag) . ", " . $db->Quote('*') . ")";
}
// ordering
switch ($k2_ordering) {
case 'date' :
$orderby = 'a.created ASC';
break;
case 'rdate' :
$orderby = 'a.created DESC';
break;
case 'alpha' :
$orderby = 'a.title';
break;
case 'ralpha' :
$orderby = 'a.title DESC';
break;
case 'order':
if ($k2_featured == '2') $orderby = 'a.featured_ordering'; else
$orderby = 'a.ordering';
break;
case 'random' :
$orderby = 'RAND()';
break;
default :
$orderby = 'a.id DESC';
break;
}
$query .= " ORDER BY " . $orderby;
$db->setQuery($query, 0, $count);
$rows = $db->loadObjectList();
}
// Process the prepare content plugins
JPluginHelper::importPlugin('content');
$i = 0;
$lists = array();
foreach ($rows as $row) {
$user = JFactory::getUser();
$dispatcher = JDispatcher::getInstance();
$results = @$dispatcher->trigger('onContentPrepare', array('com_content.article', & $row, & $params, 0));
$text = JHtml::_('content.prepare', $row->introtext);
$lists[$i] = new stdClass();
$lists[$i]->id = $row->id;
$lists[$i]->created = $row->created;
$lists[$i]->modified = $row->modified;
if ($content_type == 'joomla') {
$link = JRoute::_(ContentHelperRoute::getArticleRoute($row->id, $row->catid));
$readmore_register = false;
$images = self::getImages($row->introtext, $thumb_size);
} else {
$link = JRoute::_(K2HelperRoute::getItemRoute($row->id, $row->catid));
$readmore_register = false;
$images = self::getK2Images($row->id, $k2_image_size, $thumb_size);
}
$lists[$i]->link = $link;
$lists[$i]->readmore_register = $readmore_register;
if ($images) {
$lists[$i]->image = $images->image;
$lists[$i]->thumb = $images->thumb;
$lists[$i]->thumbSizes = $images->thumbSizes;
} else {
$lists[$i]->image = '';
$lists[$i]->thumb = '';
$lists[$i]->thumbSizes = '';
}
$lists[$i]->title = htmlspecialchars($row->title);
$thumb = '';
if ($content_type == 'k2') {
if ($show_thumbnails && $images->thumb) {
if ($thumbnail_link) {
$thumb .= '';
}
$thumb .= '';
if ($thumbnail_link) {
$thumb .= '';
}
}
}
$lists[$i]->introtext = $thumb;
$lists[$i]->introtext .= self::prepareContent($text, $text_length, $params->get('show_readmore'), $tags_option);
if ($params->get('show_readmore')) {
ob_start();
?>
readmore_register) :
echo JText::_('Register to read more...'); elseif ($readmore = $params->get('readmore')) :
echo $readmore; else :
echo JText::sprintf('Read more...');
endif; ?>
introtext .= $readmore_html;
}
$i++;
}
return $lists;
}
private static function getK2Images($id, $image_size, $thumb_size = 70)
{
$images = new stdClass();
$images->image = false;
$images->thumb = false;
$images->thumbSizes = array('width' => $thumb_size, 'height' => 'auto');
$current_size = intval($thumb_size);
if (file_exists(JPATH_SITE . '/media/k2/items/cache/' . md5("Image" . $id) . '_' . $image_size . '.jpg')) {
$image_path = 'media/k2/items/cache/' . md5("Image" . $id) . '_' . $image_size . '.jpg';
$images->image = JURI::Root(true) . '/' . $image_path;
// create a thumb filename
$file_div = strrpos($image_path, '.');
$thumb_ext = substr($image_path, $file_div);
$thumb_prev = substr($image_path, 0, $file_div);
$thumb_path = $thumb_prev . "_thumb" . $thumb_ext;
// check to see if this file exists, if so we don't need to create it
if (function_exists("gd_info")) {
// file doens't exist, so create it and save it
if (!class_exists("Thumbnail")) include_once('thumbnail.inc.php');
if (file_exists($thumb_path)) {
$existing_thumb = new Thumbnail($thumb_path);
$current_size = $existing_thumb->getCurrentWidth();
$images->thumbSizes = $existing_thumb->currentDimensions;
}
if (!file_exists($thumb_path) || $current_size != $thumb_size) {
$thumb = new Thumbnail($image_path);
if ($thumb->error) {
echo "ROKSTORIES ERROR: " . $thumb->errmsg . ": " . $image_path;
return false;
}
$thumb->resize($thumb_size);
if (!is_writable(dirname($thumb_path))) {
$thumb->destruct();
return false;
}
$thumb->save($thumb_path);
$images->thumbSizes = $thumb->currentDimensions;
$thumb->destruct();
}
}
$images->thumb = $thumb_path;
}
return $images;
}
private static function getImages($text, $thumb_size = 70)
{
preg_match("/\