Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package Related News
- * @version 2.5
- * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
- * @copyright (c) 2012 YouTech Company. All Rights Reserved.
- * @author YouTech Company http://www.smartaddons.com
- *
- */
- defined('_JEXEC') or die;
- $com_path = JPATH_SITE.'/components/com_content/';
- require_once $com_path.'router.php';
- require_once $com_path.'helpers/route.php';
- JModelLegacy::addIncludePath($com_path . '/models', 'ContentModel');
- include_once dirname(__FILE__).'/helper_base.php';
- class RelatedNews extends BaseHelper{
- public static function getList(&$_params)
- {
- $db = JFactory::getDbo();
- // Get an instance of the generic articles model
- $articles = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
- // Set application parameters in model
- $articles->setState(
- 'list.select',
- 'a.id, a.title, a.alias, a.introtext, a.fulltext, ' .
- 'a.checked_out, a.checked_out_time, ' .
- 'a.catid, a.created, a.created_by, a.created_by_alias, ' .
- // use created if modified is 0
- 'CASE WHEN a.modified = ' . $db->q($db->getNullDate()) . ' 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 = ' . $db->q($db->getNullDate()) . ' 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.xreference, a.featured'
- );
- $app = JFactory::getApplication();
- $appParams = $app->getParams();
- $articles->setState('params', $appParams);
- // Set the filters based on the module params
- $articles->setState('list.start', 0);
- $articles->setState('list.limit', (int) $_params->get('count', 0)+1);
- $articles->setState('filter.published', 1);
- // Access filter
- $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
- $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
- $articles->setState('filter.access', $access);
- // Category filter
- $catids = $_params->get('depends');
- if ($catids != null) {
- if ($_params->get('show_child_category_articles', 0) && (int) $_params->get('levels', 0) > 0) {
- // Get an instance of the generic categories model
- $categories = JModelLegacy::getInstance('Categories', 'ContentModel', array('ignore_request' => true));
- $categories->setState('params', $appParams);
- $levels = $_params->get('levels', 1) ? $_params->get('levels', 1) : 9999;
- $categories->setState('filter.get_children', $levels);
- $categories->setState('filter.published', 1);
- $categories->setState('filter.access', $access);
- $additional_catids = array();
- foreach($catids as $catid)
- {
- $categories->setState('filter.parentId', $catid);
- $recursive = true;
- $items = $categories->getItems($recursive);
- if ($items)
- {
- foreach($items as $category)
- {
- $condition = (($category->level - $categories->getParent()->level) <= $levels);
- if ($condition) {
- $additional_catids[] = $category->id;
- }
- }
- }
- }
- $catids = array_unique(array_merge($catids, $additional_catids));
- }
- $articles->setState('filter.category_id', $catids);
- // Ordering
- $articles->setState('list.ordering', $_params->get('article_ordering', 'a.ordering'));
- $articles->setState('list.direction', $_params->get('article_ordering_direction', 'ASC'));
- // // New Parameters
- $articles->setState('filter.featured', $_params->get('show_front', 'show'));
- // Filter by language
- $articles->setState('filter.language', $app->getLanguageFilter());
- $items = $articles->getItems();
- //var_dump($items); die("ancnc");
- //$show_introtext = $_params->get('show_introtext', 0);
- //$introtext_limit = $_params->get('introtext_limit', 100);
- $title_limit = $_params->get('item_title_max_characs', 20);
- // Find current Article ID if on an article page
- $option = $app->input->get('option');
- $view = $app->input->get('view');
- if ($option === 'com_content' && $view === 'article') {
- $active_article_id = $app->input->getInt('id');
- }
- else {
- $active_article_id = 0;
- }
- // Prepare data for display using display options
- foreach ($items as &$item)
- {
- $item->slug = $item->id.':'.$item->alias;
- $item->catslug = $item->catid ? $item->catid .':'.$item->category_alias : $item->catid;
- if ($access || in_array($item->access, $authorised))
- {
- // We know that user has the privilege to view the article
- $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
- }
- else
- {
- $app = JFactory::getApplication();
- $menu = $app->getMenu();
- $menuitems = $menu->getItems('link', 'index.php?option=com_users&view=login');
- if (isset($menuitems[0]))
- {
- $Itemid = $menuitems[0]->id;
- }
- elseif ($app->input->getInt('Itemid') > 0)
- {
- // Use Itemid from requesting page only if there is no existing menu
- $Itemid = $app->input->getInt('Itemid');
- }
- $item->link = JRoute::_('index.php?option=com_users&view=login&Itemid='.$Itemid);
- }
- // Used for styling the active article
- $item->active = $item->id == $active_article_id ? 'active' : '';
- $item->title = BaseHelper::truncate($item->title, $title_limit);
- //if ($show_introtext) {
- // $item->introtext = JHtml::_('content.prepare', $item->introtext, '', 'plg_content_relatednews.content');
- // self::getImages($item, $_params);
- // $item->introtext = self::_cleanIntrotext($item->introtext);
- //} else {
- $item->introtext = JHtml::_('content.prepare', $item->introtext, '', 'plg_content_relatednews.content');
- BaseHelper::getArticleImages($item, $_params);
- //}
- //$item->displayIntrotext = $show_introtext ? self::truncate($item->introtext, $introtext_limit) : '';
- //$item->displayReadmore = $item->alternative_readmore;
- }
- return $items;
- }
- }
- public static function groupBy($list, $fieldName, $article_grouping_direction, $fieldNameToKeep = null)
- {
- $grouped = array();
- if (!is_array($list)) {
- if ($list == '') {
- return $grouped;
- }
- $list = array($list);
- }
- foreach($list as $key => $item)
- {
- if (!isset($grouped[$item->$fieldName])) {
- $grouped[$item->$fieldName] = array();
- }
- if (is_null($fieldNameToKeep)) {
- $grouped[$item->$fieldName][$key] = $item;
- }
- else {
- $grouped[$item->$fieldName][$key] = $item->$fieldNameToKeep;
- }
- unset($list[$key]);
- }
- $article_grouping_direction($grouped);
- return $grouped;
- }
- public static function groupByDate($list, $type = 'year', $article_grouping_direction, $month_year_format = 'F Y')
- {
- $grouped = array();
- if (!is_array($list)) {
- if ($list == '') {
- return $grouped;
- }
- $list = array($list);
- }
- foreach($list as $key => $item)
- {
- switch($type)
- {
- case 'month_year':
- $month_year = JString::substr($item->created, 0, 7);
- if (!isset($grouped[$month_year])) {
- $grouped[$month_year] = array();
- }
- $grouped[$month_year][$key] = $item;
- break;
- case 'year':
- default:
- $year = JString::substr($item->created, 0, 4);
- if (!isset($grouped[$year])) {
- $grouped[$year] = array();
- }
- $grouped[$year][$key] = $item;
- break;
- }
- unset($list[$key]);
- }
- $article_grouping_direction($grouped);
- if ($type === 'month_year') {
- foreach($grouped as $group => $items)
- {
- $date = new JDate($group);
- $formatted_group = $date->format($month_year_format);
- $grouped[$formatted_group] = $items;
- unset($grouped[$group]);
- }
- }
- return $grouped;
- }
- }
- <field name="article_ordering" type="list" default="a.title"
- label="Article Field to Order By"
- description="Select which field you would like Articles to be ordered by. Featured Ordering should only be used when Filtering Option for Featured Articles is set to 'Only'.">
- <option value="a.metakey">Article Kewords Order</option>
- <option value="a.ordering">Article Manager Order</option>
- <option value="fp.ordering">Featured Articles Order</option>
- <option value="a.hits">Hits</option>
- <option value="a.title">Title</option>
- <option value="a.id">ID</option>
- <option value="a.alias">Alias</option>
- <option value="a.created">Created Date</option>
- <option value="modified">Modified Date</option>
- <option value="publish_up">Start Publishing Date</option>
- <option value="a.publish_down">Finish Publishing Date</option>
- </field>
- <?php
- /**
- * @package Related News
- * @version 2.5
- * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
- * @copyright (c) 2012 YouTech Company. All Rights Reserved.
- * @author YouTech Company http://www.smartaddons.com
- *
- */
- defined('_JEXEC') or die;
- require_once JPATH_SITE.'/libraries/joomla/filesystem/folder.php';
- JLoader::register('ImageHelper', dirname(__FILE__).'/helper_image.php');
- if (!class_exists('BaseHelper')){
- abstract class BaseHelper{
- /**
- *
- * @param string $introtext
- * @return string
- */
- public function _cleanText($text){
- $text = str_replace('<p>', ' ', $text);
- $text = str_replace('</p>', ' ', $text);
- $text = strip_tags($text, '<a><em><strong>');
- $text = trim($text);
- return $text;
- }
- /**
- * Parse and build target attribute for links.
- * @param string $value (_self, _blank, _windowopen, _modal)
- */
- public static function parseTarget($value='_self'){
- $target = '';
- switch($value){
- default:
- case '0':
- case '_self':
- break;
- case '1':
- case '_blank':
- $target = "target="_blank"";
- break;
- case '2':
- case '_windowopen':
- $target = "onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,false');return false;"";
- break;
- case '3':
- case '_modal':
- $target = "";
- break;
- }
- return $target;
- }
- /**
- * Truncate string by $length
- * @param string $string
- * @param int $length
- * @param string $etc
- * @return string
- */
- public static function truncate($string, $length, $etc='...'){
- return defined('MB_OVERLOAD_STRING')
- ? self::_mb_truncate($string, $length, $etc)
- : self::_truncate($string, $length, $etc);
- }
- /**
- * Truncate string if it's size over $length
- * @param string $string
- * @param int $length
- * @param string $etc
- * @return string
- */
- private static function _truncate($string, $length, $etc='...'){
- if ($length>0 && $length<strlen($string)){
- $buffer = '';
- $buffer_length = 0;
- $parts = preg_split('/(<[^>]*>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
- $self_closing_tag = split(',', 'area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed');
- $open = array();
- foreach($parts as $i => $s){
- if( false===strpos($s, '<') ){
- $s_length = strlen($s);
- if ($buffer_length + $s_length < $length){
- $buffer .= $s;
- $buffer_length += $s_length;
- } else if ($buffer_length + $s_length == $length) {
- if ( !empty($etc) ){
- $buffer .= ($s[$s_length - 1]==' ') ? $etc : " $etc";
- }
- break;
- } else {
- $words = preg_split('/([^s]*)/', $s, - 1, PREG_SPLIT_DELIM_CAPTURE);
- $space_end = false;
- foreach ($words as $w){
- if ($w_length = strlen($w)){
- if ($buffer_length + $w_length < $length){
- $buffer .= $w;
- $buffer_length += $w_length;
- $space_end = (trim($w) == '');
- } else {
- if ( !empty($etc) ){
- $more = $space_end ? $etc : " $etc";
- $buffer .= $more;
- $buffer_length += strlen($more);
- }
- break;
- }
- }
- }
- break;
- }
- } else {
- preg_match('/^<([/]?s?)([a-zA-Z0-9]+)s?[^>]*>$/', $s, $m);
- //$tagclose = isset($m[1]) && trim($m[1])=='/';
- if (empty($m[1]) && isset($m[2]) && !in_array($m[2], $self_closing_tag)){
- array_push($open, $m[2]);
- } else if (trim($m[1])=='/') {
- $tag = array_pop($open);
- if ($tag != $m[2]){
- // uncomment to to check invalid html string.
- // die('invalid close tag: '. $s);
- }
- }
- $buffer .= $s;
- }
- }
- // close tag openned.
- while(count($open)>0){
- $tag = array_pop($open);
- $buffer .= "</$tag>";
- }
- return $buffer;
- }
- return $string;
- }
- /**
- * Truncate mutibyte string if it's size over $length
- * @param string $string
- * @param int $length
- * @param string $etc
- * @return string
- */
- private static function _mb_truncate($string, $length, $etc='...'){
- $encoding = mb_detect_encoding($string);
- if ($length>0 && $length<mb_strlen($string, $encoding)){
- $buffer = '';
- $buffer_length = 0;
- $parts = preg_split('/(<[^>]*>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
- $self_closing_tag = explode(',', 'area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed');
- $open = array();
- foreach($parts as $i => $s){
- if (false === mb_strpos($s, '<')){
- $s_length = mb_strlen($s, $encoding);
- if ($buffer_length + $s_length < $length){
- $buffer .= $s;
- $buffer_length += $s_length;
- } else if ($buffer_length + $s_length == $length) {
- if ( !empty($etc) ){
- $buffer .= ($s[$s_length - 1]==' ') ? $etc : " $etc";
- }
- break;
- } else {
- $words = preg_split('/([^s]*)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE);
- $space_end = false;
- foreach ($words as $w){
- if ($w_length = mb_strlen($w, $encoding)){
- if ($buffer_length + $w_length < $length){
- $buffer .= $w;
- $buffer_length += $w_length;
- $space_end = (trim($w) == '');
- } else {
- if ( !empty($etc) ){
- $more = $space_end ? $etc : " $etc";
- $buffer .= $more;
- $buffer_length += mb_strlen($more);
- }
- break;
- }
- }
- }
- break;
- }
- } else {
- preg_match('/^<([/]?s?)([a-zA-Z0-9]+)s?[^>]*>$/', $s, $m);
- //$tagclose = isset($m[1]) && trim($m[1])=='/';
- if (empty($m[1]) && isset($m[2]) && !in_array($m[2], $self_closing_tag)){
- array_push($open, $m[2]);
- } else if (trim($m[1])=='/') {
- $tag = array_pop($open);
- if ($tag != $m[2]){
- // uncomment to to check invalid html string.
- // die('invalid close tag: '. $s);
- }
- }
- $buffer .= $s;
- }
- }
- // close tag openned.
- while(count($open)>0){
- $tag = array_pop($open);
- $buffer .= "</$tag>";
- }
- return $buffer;
- }
- return $string;
- }
- public static $image_article_cache = array();
- public static function getArticleImage($item, $_params, $ctype='article'){
- $images = &self::getArticleImages($item, $_params, $ctype);
- return is_array($images) && count($images) ? $images[0] : null;
- }
- public static function getArticleImages($item, $_params, $ctype='article'){
- $hash = md5( serialize(array($_params, $ctype)) );
- if ( !isset(self::$image_article_cache[$hash][$item->id]) ){
- $defaults = array(
- 'external' => 1,
- 'image_intro' => 1,
- 'inline_introtext' => 1,
- 'image_fulltext' => 1,
- 'inline_fulltext' => 1
- );
- $images_path = array();
- $priority = preg_split('/[s|,|;]/', $_params->get('imgcfg_order', 'external, imagE_intro,inline_introtext,image_fulltext,inline_fulltext'), -1, PREG_SPLIT_NO_EMPTY);
- if ( count($priority) > 0 ){
- $priority = array_map('strtolower', $priority);
- $mark = array();
- for($i=0; $i<count($priority); $i++){
- $type = $priority[$i];
- if ( array_key_exists($type, $defaults) )
- unset($defaults[ $type ]);
- if ( $_params->get('imgcfg_from_'.$type, 1) )
- $mark[ $type ] = 1;
- }
- }
- foreach($defaults as $type => $val){
- if ( $_params->get('imgcfg_from_'.$type, 1) )
- $mark[ $type ] = 1;
- }
- if ( count($mark) > 0 ){
- // prepare data.
- $images_data = null;
- if (array_key_exists('image_intro', $mark) || array_key_exists('image_fulltext', $mark)){
- $images_data = json_decode($item->images, true);
- }
- foreach($mark as $type => $true){
- switch ($type){
- case 'image_intro':
- case 'image_fulltext':
- if ( isset($images_data) && isset($images_data[$type]) && !empty($images_data[$type])){
- $image = array(
- 'src' => $images_data[$type]
- );
- if (array_key_exists($type.'_alt', $images_data)){
- $image['alt'] = $images_data[$type.'_alt'];
- }
- if (array_key_exists($type.'_caption', $images_data)){
- /* $image['class'] = 'caption'; */
- $image['title'] = $images_data[$type.'_caption'];
- }
- array_push($images_path, $image);
- }
- break;
- case 'inline_introtext':
- $text = $item->introtext;
- case 'inline_fulltext':
- if ($type == 'inline_fulltext'){
- $text = $item->fulltext;
- }
- $inline_images = self::getInlineImages($text);
- for ($i=0; $i<count($inline_images); $i++){
- array_push($images_path, $inline_images[$i]);
- }
- break;
- case 'external':
- $exf = $_params->get('imgcfg_external_url', '/images');
- preg_match_all('/{([a-zA-Z0-9_]+)}/', $exf, $m);
- if ( count($m)==2 && count($m[0])>0 ){
- $compat = 1;
- foreach ($m[1] as $property){
- !property_exists($item, $property) && ($compat=0);
- }
- if ($compat){
- $replace = array();
- foreach ($m[1] as $property){
- $replace[] = is_null($item->$property) ? '' : $item->$property;
- }
- $exf = str_replace($m[0], $replace, $exf);
- }
- }
- $files = self::getExternalImages($exf);
- for ($i=0; $i<count($files); $i++){
- array_push($images_path, array('src'=>$files[$i]));
- }
- break;
- default:
- break;
- }
- }
- }
- if ( count($images_path) == 0 && $_params->get('imgcfg_placeholder', 1)==1){
- $images_path[] = array('src'=> $_params->get('imgcfg_placeholder_path', null), 'class'=>'placeholder');
- }
- self::$image_article_cache[$hash][$item->id] = $images_path;
- }
- return self::$image_article_cache[$hash][$item->id];
- }
- public static $image_category_cache = array();
- public static function getCategoryImage($item, $_params, $ctype='category'){
- $images = &self::getCategoryImages($item, $_params, $ctype);
- return is_array($images) && count($images) ? $images[0] : null;
- }
- public static function getCategoryImages($item, $_params, $ctype='category'){
- $hash = md5( serialize(array($_params, $ctype)) );
- if ( !isset(self::$image_category_cache[$hash][$item->id]) ){
- $defaults = array(
- 'external' => 1,
- 'params' => 1,
- 'description' => 1
- );
- $images_path = array();
- $priority = preg_split('/[s|,|;]/', $_params->get('imgcfg_order', 'external, params, description'), -1, PREG_SPLIT_NO_EMPTY);
- if ( count($priority) > 0 ){
- $priority = array_map('strtolower', $priority);
- $mark = array();
- for($i=0; $i<count($priority); $i++){
- $type = $priority[$i];
- if ( array_key_exists($type, $defaults) )
- unset($defaults[ $type ]);
- if ( $_params->get('imgcfg_from_'.$type, 1) )
- $mark[ $type ] = 1;
- }
- }
- foreach($defaults as $type => $val){
- if ( $_params->get('imgcfg_from_'.$type, 1) )
- $mark[ $type ] = 1;
- }
- if ( count($mark) > 0 ){
- $cparams = null;
- if (array_key_exists('params', $mark)){
- $cparams = new JRegistry;
- $cparams->loadString($item->params);
- }
- foreach($mark as $type => $true){
- switch ($type){
- case 'params':
- if ( $cparams instanceof JRegistry && $cparams->get('image') ){
- $image = array(
- 'src' => $cparams->get('image')
- );
- array_push($images_path, $image);
- }
- break;
- case 'description':
- $inline_images = self::getInlineImages($item->description);
- for ($i=0; $i<count($inline_images); $i++){
- array_push($images_path, $inline_images[$i]);
- }
- break;
- case 'external':
- $exf = $_params->get('imgcfg_external_url', '/images');
- preg_match_all('/{([a-zA-Z0-9_]+)}/', $exf, $m);
- if ( count($m)==2 && count($m[0])>0 ){
- $compat = 1;
- foreach ($m[1] as $property){
- !property_exists($item, $property) && ($compat=0);
- }
- if ($compat){
- $replace = array();
- foreach ($m[1] as $property){
- $replace[] = is_null($item->$property) ? '' : $item->$property;
- }
- $exf = str_replace($m[0], $replace, $exf);
- }
- }
- $files = self::getExternalImages($exf);
- for ($i=0; $i<count($files); $i++){
- array_push($images_path, array('src'=>$files[$i]));
- }
- break;
- default:
- break;
- }
- }
- }
- if ( count($images_path) == 0 && $_params->get('imgcfg_placeholder', 1)==1){
- $images_path[] = array('src'=> $_params->get('imgcfg_placeholder_path', null), 'class'=>'placeholder');
- }
- self::$image_category_cache[$hash][$item->id] = $images_path;
- }
- return self::$image_category_cache[$hash][$item->id];
- }
- /**
- *
- * @param string $text
- * @return string:
- */
- public static function getInlineImages($text){
- $images = array();
- $searchTags = array(
- 'img' => '/<img[^>]+>/i',
- 'input' => '/<input[^>]+types?=s?"image"[^>]+>/i'
- );
- foreach ($searchTags as $tag => $regex){
- preg_match_all($regex, $text, $m);
- if ( is_array($m) && isset($m[0]) && count($m[0])){
- foreach ($m[0] as $htmltag){
- $tmp = JUtility::parseAttributes($htmltag);
- if ( isset($tmp['src']) ){
- if ($tag == 'input'){
- array_push( $images, array('src' => $tmp['src']) );
- } else {
- array_push( $images, $tmp );
- }
- }
- }
- }
- }
- return $images;
- }
- /**
- *
- * @param string $path
- * @return multitype:multitype:unknown |Ambigous <multitype:, boolean, multitype:unknown multitype:unknown >
- */
- public static function getExternalImages($path){
- $files = array();
- $ps = JString::parse_url($path);
- if ( array_key_exists('path', $ps) && !empty($ps['path']) ){
- $isHttp = isset($ps['scheme']) && in_array($ps['scheme'], array('http', 'https'));
- if (!$isHttp || JURI::isInternal($path)){
- // image on server
- $path = $ps['path'];
- } else {
- $files[] = array( 'src' => $path );
- return $files;
- }
- }
- if (is_file($path)){
- $files[] = $path;
- } else if (is_dir($path)){
- $files = JFolder::files($path, '.jpg|.png|.gif', false, true);
- } else {
- $ext = substr($path, -4);
- $search = substr($path, 0, -4);
- $lext = strtolower($ext);
- if ( is_dir($search) && in_array($lext, array('.jpg', '.png', '.gif')) ){
- $files = JFolder::files($search, $ext, false, true);
- }
- }
- return $files;
- }
- public static function imageTag($image, $options=array()){
- return ImageHelper::init($image, $options)->tag();
- }
- public static function getImageHelper($image, $options=array()){
- return ImageHelper::init($image, $options);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement