/modules/code/trunk/modules/mod_artof_articles/helper.php
PHP | 229 lines | 136 code | 27 blank | 66 comment | 16 complexity | 271a8667f50b2df3677fb86e6a06602c MD5 | raw file
- <?php
- /**
- * @version $Id:combolayout.php 2547 2007-11-10 04:37:15Z masterchief $
- * @package Artof.Modules
- * @subpackage mod_artof_articles
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
- * @author Andrew Eddie <andrew.eddie@newlifeinit.com>
- * @link http://www.theartofjoomla.com/extensions/mod-artof-articles.html
- */
- // No direct access.
- defined('JPATH_BASE') or die();
- require_once JPATH_SITE.'/components/com_content/helpers/route.php';
- /**
- * @package Artof.Modules
- * @subpackage mod_artof_articles
- * @since 1.0.0
- */
- class modArtofArticlesHelper
- {
- /**
- * Get a list of the articles for the given filter parameters.
- *
- * @param JObject $params The filter parameters.
- *
- * @return array A list of articles.
- * @since 1.0.0
- */
- public static function getList(&$params)
- {
- $db = JFactory::getDBO();
- $user = JFactory::getUser();
- $userId = (int) $user->get('id');
- $count = (int) $params->get('count', 5);
- $catId = trim($params->get('catid'));
- $aid = $user->get('aid', 0);
- $config = JComponentHelper::getParams('com_content');
- $access = !$config->get('show_noauth');
- $nullDate = $db->getNullDate();
- $date = JFactory::getDate();
- $now = $date->toMySQL();
- $where = 'a.state = 1'
- . ' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).')'
- . ' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).')';
- // Ordering
- switch ($params->get('ordering'))
- {
- default:
- case 'o_asc':
- $ordering = 'a.ordering ASC';
- break;
- case 'o_dsc':
- $ordering = 'a.ordering DESC';
- break;
- case 'm_dsc':
- $ordering = 'a.modified DESC, a.created DESC';
- break;
- case 'c_dsc':
- $ordering = 'a.created DESC';
- break;
- }
- if ($catId) {
- $ids = explode(',', $catId);
- JArrayHelper::toInteger($ids);
- $catCondition = ' AND (cc.id=' . implode(' OR cc.id=', $ids) . ')';
- }
- // Content Items only
- $db->setQuery(
- 'SELECT a.id, a.title, a.alias, a.created_by_alias, a.access, a.sectionid, ' .
- ($params->get('show_intro') ? 'a.introtext, ' : '').
- ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
- ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
- ' CASE WHEN CHAR_LENGTH(a.created_by_alias) THEN a.created_by_alias ELSE u.name END as author'.
- ' FROM #__content AS a' .
- ' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
- ' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
- ' INNER JOIN #__users AS u ON u.id = a.created_by' .
- ' WHERE '. $where .' AND s.id > 0' .
- ($access ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
- ($catId ? $catCondition : '').
- ' AND s.published = 1' .
- ' AND cc.published = 1' .
- ' ORDER BY '. $ordering,
- 0, $count
- );
- $rows = $db->loadObjectList();
- if ($error = $db->getErrorMsg()) {
- JError::raiseWarning(500, $error);
- return;
- }
- foreach ($rows as &$row)
- {
- if($row->access <= $aid) {
- $row->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));
- }
- else {
- $row->link = JRoute::_('index.php?option=com_user&view=login');
- }
- if ($params->get('show_intro')) {
- $row->text = JHtml::_('content.prepare', $row->introtext);
- }
- else {
- $row->text = '';
- }
- }
- return $rows;
- }
- /**
- * Get the route for the Viewall link.
- *
- * @param JObject The module parameters
- *
- * @return string The route for the link.
- * @since 1.0.0
- */
- public static function getViewallRoute(&$params)
- {
- $categoryId = (int) $params->get('catid');
- $db = JFactory::getDbo();
- $db->setQuery(
- 'SELECT section' .
- ' FROM #__categories' .
- ' WHERE id = '.$categoryId
- );
- $sectionId = (int) $db->loadResult();
- if ($error = $db->getErrorMsg()) {
- JError::raiseError(500, $error);
- return false;
- }
- else {
- return ContentHelperRoute::getCategoryRoute($categoryId, $sectionId);
- }
- }
- /**
- * Gets a thumbnail from the first image in the article.
- *
- * @param string $text The source text.
- * @param boolean $autoThumb True to create a sized thumbnail, false to use the native image.
- * @param int $width The width of the thumbnail.
- * @param int $height The height of the thumbnail.
- * @param string $alt The alt text for the image.
- * @param string $align The align.
- * @param string $title The title for the image.
- *
- * @return string
- */
- public static function getThumb(&$text, $autoThumb = false, $width = null, $height = null, $alt = null, $align = null, $title = null)
- {
- $result = '';
- // lets get the first image out of the article and use it for the main image
- preg_match('@<img[^>]*src=\"([^\"]*)\"[^>]*>@Usi', $text, $matches);
- $images = (count($matches)) ? $matches : array ();
- if (count($images)) {
- // did we find an image?
- if ($image = $images[1]) {
- $attribs = ($align) ? ' align="'.$align.'"' : '';
- if ($autoThumb) {
- require_once dirname(__FILE__).'/image.php';
- //and ($thumb = $this->makeThumb($image, $width, $height))) {
- // $image = '<img src="'.$thumb.'" alt="'.$row->title.'"'.$align.' />';
- $src = JXHTMLMedia::thumb($image, $width, $height);
- }
- else {
- $src = $image;
- }
- $attribs .= ($width) ? ' width="'.(int) $width.'"' : '';
- $attribs .= ($height) ? ' height="'.(int) $height.'"' : '';
- $result = '<img src="'.$image.'" alt="'.$alt.'"'.$attribs.' />';
- // Remove the image from the text.
- $text = str_replace($images[0], '', $text);
- }
- }
- return $result;
- }
- /**
- * Truncates text blocks over the specified character limit. The
- * behavior will not truncate an individual word, it will find the first
- * space that is within the limit and truncate at that point. This
- * method is UTF-8 safe.
- *
- * @param string $text The text to truncate.
- * @param int $length The maximum length of the text.
- *
- * @return string The truncated text.
- * @since 1.0.0
- */
- public static function truncate($text, $length = 0, $append = '...')
- {
- // Truncate the item text if it is too long.
- if ($length > 0 && JString::strlen($text) > $length) {
- // Find the first space within the allowed length.
- $tmp = JString::substr($text, 0, $length);
- $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' '));
- // If we don't have 3 characters of room, go to the second space within the limit.
- if (JString::strlen($tmp) >= $length - 3) {
- $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' '));
- }
- $text = $tmp.$append;
- }
- return $text;
- }
- }