Skocz do zawartości
"Idzie nowe..." - o zmianach i nie tylko ×
Przeniesienie zakupów z IPS Marketplace / Moving bought items from IPS Marketplace ×

Losowe wpisy z blogów - propozycja/prośba


Mdx91
Przejdź do rozwiązania Rozwiązane przez JamLasica,

Rekomendowane odpowiedzi

Miałbym propozycję/prośbę o napisanie hook'a do losowego wyświetlania wpisów na blogach. Wpisy mogłyby być przedstawiane tak jak w hook'u (SOS32) Recent Blog Entries Ticker i wyświetlane losowo po każdym odświeżeniu strony.

 

Przeszukałem MP ale niestety nie znalazłem takiej modyfikacji :)

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

No, a właśnie ten mod tylko zmienione sortowanie z ostatnich wpisów na losowe? :)

 

Pytałem autora o zmianę wyświetlania wpisów po odświeżeniu strony zamiast czasowo (w tym momencie odbywa sie to za pomocą ajax po określonym w ACP czasie) i odpisał że by trzeba było zmienić cały kod i nie wykona tego.

Więc mając to na uwadze zmiana na losowe wpisy i wyświetlanie losowo po odświeżeniu byłaby na tyle duża że trzeba by było przerobić większą część hooka (tak mi się przynajmniej wydaje :D).

 

Do tego nie znam się na js i nie wiem jak to ogarnąć :)

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

  • Manager

Poważnie Ci takie bzdury pisał? Coś nie chce mi się wierzyć.

 

Otwórz plik .xml z paczki i znajdź:

'order'    => 'e.entry_date DESC',

Zamień na:

'order'    => $this->DB->buildRandomOrder(),

Po czym oczywiście ponownie zaimportuj ten plik w ACP.

  • Lubię to 2

intermedia - profesjonalne rozwiązania Invision Power Board

---

Chcesz uzyskać szybko i sprawnie pomoc? Uzupełnij wersję i adres w profilu.

Odnośnik do komentarza
Udostępnij na innych stronach

@UP poniekąd się sprawdziło bo wyświetla losowo wpisy ale z dwóch ostatnich blogów ;)

 

Dodatkowo dałoby radę przerobić kod aby wyświetlał się wpis po odświeżeniu zamiast czasowo? :)

 

 

PS. Trochę źle odczytałem odpowiedź autora (napisał mniej więcej że nie jest to możliwe bez zmiany całego kodu który nie będzie inaczej działał) :)

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

Trochę poza tematem - ORDER BY RAND() w MySQL przy dużych tabelach jest zły i baaaaardzo wolny - poniżej rzeczywisty wynik w przypadku postów i liczby 307690;

Oczywiście blogów będzie trochę mniej, ale będzie też długo.

SELECT * FROM  `posts` ORDER BY RAND( ) LIMIT 10;
Showing rows 0 - 9 ( 10 total, Query took 1.7250 sec)
Najlepszym rozwiązaniem będzie przenieść ten random do PHP-a.

http://jan.kneschke.de/projects/mysql/order-by-rand/

Jeśli ktoś jest zainteresowany dlaczego tak jest - bo MySQL przy ORDER BY RAND() robi tymczasową kopię całej tabeli i dopiero na niej działa.

Edytowane przez JamLasica
  • Lubię to 3
Odnośnik do komentarza
Udostępnij na innych stronach

  • 3 miesiące temu...

Wracam do was z ostatnim pytaniem :)

 

Więc tak: Udało się zmienić sortowanie z ostatnich na losowe oraz przerobić tak by zmieniało się dopiero po odświeżeniu.

 

 

Została jedna sprawa bo hook pobiera tylko ostatnie wpisy (tak ok 15 ostatnich). Coś da radę zrobić by pobierał wpisy wszystkie które kiedykolwiek pojawiły się na blogach? :)

 

Pisałem do Adriano ale niestety nie odpowiada :)

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

Oto kod :)

 

[long]
class latestBlogPostsTicker
{
public $registry;
public $member;

public function __construct()
{
$this->registry = ipsRegistry::instance();
$this->DB = $this->registry->DB();
$this->settings =& $this->registry->fetchSettings();
$this->request =& $this->registry->fetchRequest();
$this->member = $this->registry->member();
$this->memberData =& $this->registry->member()->fetchMemberData();
$this->lang = $this->registry->getClass('class_localization');
$this->cache = $this->registry->cache();
$this->caches =& $this->registry->cache()->fetchCaches();

if ( ! $this->registry->isClassLoaded('blogFunctions') )
{
$classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'blog' ) . '/sources/classes/blogFunctions.php', 'blogFunctions', 'blog' );
$this->registry->setClass('blogFunctions', new $classToLoad($this->registry));
}

$this->blogFunctions = $this->registry->getClass('blogFunctions');

if ( ! defined( 'BLOG_LIBS' ) )
{
define( 'BLOG_LIBS', IPS_ROOT_PATH . 'applications_addon/ips/blog/sources/lib/' );
define( 'BLOG_PATH', IPS_ROOT_PATH . 'applications_addon/ips/blog/' );
}

/* Build the permissions */
$this->memberData = $this->blogFunctions->buildPerms( $this->memberData );

//$this->registry->class_localization->loadLanguageFile( array( 'public_blog' ), 'blog' );
}

public function getOutput()
{
if ( $this->settings['blog_online'] == 0 )
{
return;
}

if ( $this->settings['latestBlogPostsTicker_local'] == 2 )
{
return;
}

if ( ! isset( $this->caches['blog_stats'] ) )
{
$this->caches['blog_stats'] = $this->cache->getCache('blog_stats');
}

if( ! $this->memberData['g_blog_settings'] )
{
return '';
}

if( ! is_array( $this->memberData['g_blog_settings'] ) )
{
$this->memberData['g_blog_settings'] = unserialize( $this->memberData['g_blog_settings'] );
}

if ( in_array( $this->memberData['member_group_id'], explode( ',', $this->settings['latestBlogPostsTicker_grupos'] ) ) AND $this->memberData['g_blog_settings']['g_blog_allowview'] )
{
$entries = array();

if ( defined( 'BLOG_RENTRIES_BLOG_ID' ) )
{
$_where = "b.blog_id IN (" . BLOG_RENTRIES_BLOG_ID . ") AND e.entry_status !='draft'";
}
else
{
$_where = "e.entry_id IN(" . implode( ",", array_keys( $this->caches['blog_stats']['recent_entries'] ) ) . ") AND e.entry_status !='draft'";
}

$_where .= " AND ( ( b.blog_owner_only=1 AND b.member_id=" . intval( $this->memberData['member_id'] ) . " ) OR b.blog_owner_only=0 ) AND ( b.blog_authorized_users LIKE '%," . intval( $this->memberData['member_id'] ) . ",%' OR b.blog_authorized_users IS NULL )";

$_where .= $this->settings['latestBlogPostsTicker_blog'] == 'ALL' ? '' : " AND b.blog_id = ".$this->settings['latestBlogPostsTicker_blog'];

/* Recent entries */
if ( count( $this->caches['blog_stats']['recent_entries'] ) )
{
$this->DB->build( array('select' => 'e.entry_id, e.entry_last_update, e.entry_name, e.blog_id, e.entry_name_seo, e.entry_author_id, e.entry_date',
'from' => array('blog_entries' => 'e' ),
'where' => $_where,
'order' => 'RAND()',
'limit' => array( 0, $this->settings['latestBlogPostsTicker_nr'] ),
'add_join' => array( array( 'select' => 'b.blog_name, b.blog_seo_name',
'from' => array( 'blog_blogs' => 'b' ),
'where' => 'b.blog_id=e.blog_id',
'type' => 'left' ) ) ) );

$this->DB->execute();

while( $entry = $this->DB->fetch() )
{
$entries[ $entry['entry_id'] ] = $entry;
$mids[ $entry['entry_author_id'] ] = $entry['entry_author_id'];
}
}

if ( count( $mids ) )
{
$members = IPSMember::load( $mids, 'all' );

if ( count( $members ) )
{
foreach( $entries as $cid => $cdata )
{
if ( $cdata['entry_author_id'] and isset( $members[ $cdata['entry_author_id'] ] ) )
{
$entries[ $cid ] = array_merge( $entries[ $cid ], $members[ $cdata['entry_author_id'] ] );
}
}
}
}

if( count( $entries ) > 0 )
{
if( is_array( $entries ) )
{
foreach( $entries as $eid => $entry )
{
$entry = IPSMember::buildDisplayData( $entry );
$entry['_entry_date'] = $this->registry->getClass('class_localization')->getDate( $entry['entry_date'], 'SHORT2' );

$entry['_lastRead'] = $this->registry->classItemMarking->fetchTimeLastMarked( array( 'blogID' => $entry['blog_id'], 'itemID' => $entry['entry_id'] ), 'blog' );

if( $entry['entry_last_update'] > $entry['_lastRead'] )
{
$entry['newpost'] = true;
}
else
{
$entry['newpost'] = false;
}

$entries[ $eid ] = $entry;
}

return $this->registry->output->getTemplate( 'boards' )->latestBlogPostsTicker( $entries );
}
}
else
{
return '';
}
}
}
}]]>[/long]

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

Sorki :)

 <hooks_source><![CDATA[<?php

class latestBlogPostsTicker
{
	public $registry;
	public $member;
	
	public function __construct()
	{
		$this->registry   = ipsRegistry::instance();
		$this->DB	    = $this->registry->DB();
		$this->settings =& $this->registry->fetchSettings();
		$this->request  =& $this->registry->fetchRequest();
		$this->member   = $this->registry->member();
		$this->memberData =& $this->registry->member()->fetchMemberData();
		$this->lang		=  $this->registry->getClass('class_localization');
		$this->cache	= $this->registry->cache();
		$this->caches   =& $this->registry->cache()->fetchCaches();

		if ( ! $this->registry->isClassLoaded('blogFunctions') )
		{
			$classToLoad = IPSLib::loadLibrary( IPSLib::getAppDir( 'blog' ) . '/sources/classes/blogFunctions.php', 'blogFunctions', 'blog' );
			$this->registry->setClass('blogFunctions', new $classToLoad($this->registry));
		}

		$this->blogFunctions = $this->registry->getClass('blogFunctions');
		
		if ( ! defined( 'BLOG_LIBS' ) )
		{
			define( 'BLOG_LIBS', IPS_ROOT_PATH . 'applications_addon/ips/blog/sources/lib/' );
			define( 'BLOG_PATH', IPS_ROOT_PATH . 'applications_addon/ips/blog/' );
		}
		
		/* Build the permissions */
		$this->memberData = $this->blogFunctions->buildPerms( $this->memberData );

		//$this->registry->class_localization->loadLanguageFile( array( 'public_blog' ), 'blog' );
	}
	
	public function getOutput()
	{
		if ( $this->settings['blog_online'] == 0 )
	 	{
		 	return;
		}

		if ( $this->settings['latestBlogPostsTicker_local'] == 2 )
		{
			return;
		}

		if ( ! isset( $this->caches['blog_stats'] ) )
		{
			$this->caches['blog_stats'] = $this->cache->getCache('blog_stats');
		}

		if( ! $this->memberData['g_blog_settings'] )
		{
			return '';
		}

		if( ! is_array( $this->memberData['g_blog_settings'] ) )
		{
			$this->memberData['g_blog_settings'] = unserialize( $this->memberData['g_blog_settings'] );
		}

		if ( in_array( $this->memberData['member_group_id'], explode( ',', $this->settings['latestBlogPostsTicker_grupos'] ) ) AND $this->memberData['g_blog_settings']['g_blog_allowview'] ) 
		{			
			$entries = array();
			
			if ( defined( 'BLOG_RENTRIES_BLOG_ID' ) )
			{
				$_where = "b.blog_id IN (" .  BLOG_RENTRIES_BLOG_ID . ") AND e.entry_status !='draft'";
			}
			else
			{
				$_where = "e.entry_id IN(" . implode( ",", array_keys( $this->caches['blog_stats']['recent_entries'] ) ) . ")  AND e.entry_status !='draft'";
			}
			
			$_where .= " AND ( ( b.blog_owner_only=1 AND b.member_id="  . intval( $this->memberData['member_id'] ) . " ) OR b.blog_owner_only=0 ) AND ( b.blog_authorized_users LIKE '%,"  . intval( $this->memberData['member_id'] ) .  ",%' OR b.blog_authorized_users IS NULL )";
			
			$_where .= $this->settings['latestBlogPostsTicker_blog'] == 'ALL' ? '' : " AND b.blog_id =  ".$this->settings['latestBlogPostsTicker_blog'];

			/* Recent entries */
			if ( count( $this->caches['blog_stats']['recent_entries'] ) )
			{
				$this->DB->build( array('select'   => 'e.entry_id, e.entry_last_update, e.entry_name, e.blog_id, e.entry_name_seo, e.entry_author_id, e.entry_date',
										'from'     => array('blog_entries' => 'e' ),
										'where'    => $_where,
										'order'    => 'RAND()',
										'limit'    => array( 0, $this->settings['latestBlogPostsTicker_nr'] ),
										'add_join' => array( array( 'select' => 'b.blog_name, b.blog_seo_name',
																	'from'   => array( 'blog_blogs' => 'b' ),
																	'where'  => 'b.blog_id=e.blog_id',
																	'type'   => 'left' ) ) ) );
								
				$this->DB->execute();
				
				while( $entry = $this->DB->fetch() )
				{
					$entries[ $entry['entry_id'] ]     = $entry;
					$mids[ $entry['entry_author_id'] ] = $entry['entry_author_id'];
				}
			}

			if ( count( $mids ) )
			{
				$members = IPSMember::load( $mids, 'all' );
				
				if ( count( $members ) )
				{
					foreach( $entries as $cid => $cdata )
					{
						if ( $cdata['entry_author_id'] and isset( $members[ $cdata['entry_author_id'] ] ) )
						{
							$entries[ $cid ] = array_merge( $entries[ $cid ], $members[ $cdata['entry_author_id'] ] );
						}
					}
				}
			}
	
			if( count( $entries ) > 0 )
			{
				if( is_array( $entries ) )
				{
					foreach( $entries as $eid => $entry )
					{
						$entry                = IPSMember::buildDisplayData( $entry );
						$entry['_entry_date'] = $this->registry->getClass('class_localization')->getDate( $entry['entry_date'], 'SHORT2' );
						
						$entry['_lastRead'] = $this->registry->classItemMarking->fetchTimeLastMarked( array( 'blogID' => $entry['blog_id'], 'itemID' => $entry['entry_id'] ), 'blog' );
						
						if( $entry['entry_last_update'] > $entry['_lastRead'] )
						{
							$entry['newpost'] = true;
						}
						else
						{
							$entry['newpost'] = false;
						}
						
						$entries[ $eid ] = $entry;
					}
					
					return $this->registry->output->getTemplate( 'boards' )->latestBlogPostsTicker( $entries );
				}
			}
			else
			{
				return '';
			}
		}
	}	
}]]></hooks_source>

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

  • Manager

intermedia - profesjonalne rozwiązania Invision Power Board

---

Chcesz uzyskać szybko i sprawnie pomoc? Uzupełnij wersję i adres w profilu.

Odnośnik do komentarza
Udostępnij na innych stronach

Poczytałem trochę o tym, pokombinowałem i po setnej próbie zamiast zwykłego błędu wyskoczyło mi to:

string(203) "e.entry_id IN(3,2,1) AND e.entry_status !='draft' AND ( ( b.blog_owner_only=1 AND b.member_id=1 ) OR b.blog_owner_only=0 ) AND ( b.blog_authorized_users LIKE '%,1,%' OR b.blog_authorized_users IS NULL )"

oraz warningi (ale je pomijam)

 

Nie wiem czy o to chodziło więc jak nie to będę próbował dalej :D

admin4YSh2.png


 

Odnośnik do komentarza
Udostępnij na innych stronach

  • Rozwiązanie

Ale ma

$this->caches   =& $this->registry->cache()->fetchCaches();
(...)
else
{
	$_where = "e.entry_id IN(" . implode( ",", array_keys( $this->caches['blog_stats']['recent_entries'] ) ) . ")  AND e.entry_status !='draft'";
}
Ja bym to prawie całe $_where wywalił w tym miejscu i zostawił tylko:

	$_where = "e.entry_status !='draft'";
Edytowane przez JamLasica
  • Lubię to 2
Odnośnik do komentarza
Udostępnij na innych stronach

Gość
Ten temat został zamknięty. Brak możliwości dodania odpowiedzi.
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...

Powiadomienie o plikach cookie

Umieściliśmy na Twoim urządzeniu pliki cookie, aby pomóc Ci usprawnić przeglądanie strony. Możesz dostosować ustawienia plików cookie, w przeciwnym wypadku zakładamy, że wyrażasz na to zgodę.