Recent Topics

1 Jan 27, 2012 00:31    

My b2evolution Version: 4.0.5

I am trying to use random post plugin, but there's a catch, if the blog is aggregated, it displays "no posts" error since the aggregated blog doesnt have any posts or categories.

Can we add an "ALL" option or sth or get aggregated blog id's for the selected aggr. blog and apply that selection ?


<?php
/**
 * This file implements the Random redirect plugin for {@link http://b2evolution.net/ b2evolution}.
 *
 * @copyright (c) 2009 by Tilman Blumenbach - {@link http://ax86.net}
 * @license GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
 * @package plugins_ax86
 * @author Tilman Blumenbach <tilman (at) ax86 (dot) net>
 */
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );

/**
 * Random redirect plugin
 *
 * Allows you to link to a random post on your blog.
 * 
 * @package plugins_ax86
 */
 load_class( '/items/model/_itemlistlight.class.php', 'ItemListLight' ); 
class random_redirect_plugin extends Plugin
{
	var $name;
	var $code = 'ax86_RandRedir';
	var $priority = 1; // Redirect as early as possible
	var $version = '1.0';
	var $author = 'Tilman Blumenbach';
	var $help_url = '';
	var $short_desc;
	var $long_desc;
	var $number_of_installs = 1;
	var $group = 'util';

	function PluginInit( & $params )
	{
		$this->name = $this->T_( 'Random redirect' );
		$this->short_desc = $this->T_( 'Allows you to link to a random '
									  .'post on your blog.' );
		$this->long_desc = $this->T_( 'Use an URL like "http://yourblog.com/blog1.php?random". '
									 .'You can also specify parameters '
									 .'like "rand_types" and "cat".' );
	}

	function GetDependencies()
	{
		return array(
			'requires' => array(
				'app_min' => '2.4.0', // for ordering by RAND()
			)
		);
	}

	function GetExtraEvents()
	{
		return array(
			'display_random_link' => 'Display a link to a random blog post.',
			'get_random_url' => 'Get an URL to a random blog post.',
		);
	}

	function get_random_url( & $params )
	{
		global $Blog;

		$params = array_merge( array(
			'Blog' => & $Blog,
			'url_params' => 'cat=5&amp;rand_cat_focus=wide',
		), $params );

		$url_params = 'random';
		if( ! empty( $params['url_params'] ) )
		{
			$url_params .= '&'.$params['url_params'];
		}

		return url_add_param( $params['Blog']->gen_blogurl(), $url_params );
	}

	function display_random_link( & $params )
	{
		$params = array_merge( array(
			'linktext' => $this->T_( 'random' ),
		), $params );

		echo '<a href="', $this->get_random_url( $params ), '">';
		echo $params['linktext'], '</a>';
	}

	function BeforeBlogDisplay( & $params )
	{
		global $Blog, $app_version;

		if( ! isset( $_GET['random'] ) )
		{	// Random post is not requested:
			return;
		}

		$ItemListLight = & new ItemListLight( $Blog, NULL, NULL, 1,
												'ItemCacheLight', 'rand_' );

		// Set default filters:
		$ItemListLight->set_default_filters( array(
			'cat_focus' => 'main',
			'types' => '1', // Posts only
		) );

		// Load other filters from the request:
		$ItemListLight->load_from_Request( false );

		/* Set RANDom order.
		 * This is dirty, but necessary since the param RegExp does not
		 * allow "RAND"?!
		 */
		if( version_compare( $app_version, '3.0.0-alpha', '<' ) )
		{	// b2evo 2.4.x
			$ItemListLight->filters['order'] = 'RAND';
		}
		else
		{	// b2evo 3.x
			$ItemListLight->filters['orderby'] = 'RAND';
		}

		// Run query:
		$ItemListLight->query();

		if( ! $ItemListLight->get_num_rows() )
		{
			debug_die( 'No post in ItemListLight! Parameters too restrictive?' ); // EXITS
		}

		//debug_die( var_export( $ItemListLight->filters, true ) );
		// Get item and redirect to single (permanent) URL:
		$an_Item = & $ItemListLight->get_by_idx( 0 );
		header_redirect( $an_Item->get_single_url( 'auto', '', '&' ) ); // EXITS
	}
}
?>


Btw i dont know why but i had to load ItemListLight class seperately.


Form is loading...