the-art-of-joomla-archive /modules/code/trunk/modules/mod_artof_articles/helper.php

Language PHP Lines 230
MD5 Hash 271a8667f50b2df3677fb86e6a06602c Estimated Cost $3,300 (why?)
Repository https://bitbucket.org/eddieajau/the-art-of-joomla-archive View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?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;
	}
}
Back to Top