Replacement tag: next, previous with rollover image

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Post Reply
User avatar
Kosse
Posts: 1066
Joined: Thu 9. Sep 2004, 12:08
Location: Brussels, Belgium
Contact:

Replacement tag: next, previous with rollover image

Post by Kosse »

Hi there,

this is my second "hack" and I'm no php coder so...
If it is useful to you ok, if you can enhance it even cooler. :)
Maybe there are less complicated ways to do this, anyhow here it goes:

I did this mod because I wanted rollover images for the neat next and previous buttons. (I didn't do it for the Up function but it's the same)

In order to do that, you have 4 steps to follow. This is what you should:

# 1. Create 4 images
2 Images for next button (same size, different colors) - Example: I've created back-on.gif and back-off.gif
2 Images for previous button (same size, different colors) - Example: I've created next-on.gif and next-off.gif

# 2. Put the 4 images you created in the /img/button/ folder
Upload you images ;-)

3. Modify the front.inc.php file

Code: Select all

#
#-----[ OPEN ]------------------------------------------
#

inc_front/front.func.inc.php

#
#-----[ FIND ]------------------------------------------
#

function get_index_link_next($linktext, $cat_down=0) {
	// return the link to next article in current ctageory


#
#------[ BEFORE, ADD ]
#

// -------------------------------------------------------------------------
// Use rollover images for next I've called it: [nextim]
// -------------------------------------------------------------------------

function get_index_link_nextim($linktext, $cat_down=0) {
	// return the link to next article in current ctageory
	$a_id = ($GLOBALS['content']['article_id']) ? $GLOBALS['content']['article_id'] : $GLOBALS['aktion'][1];
	$linktext = trim($linktext);
	if(!$linktext) $linktext = 'NEXT';
	$link = '';

	if(count($GLOBALS['content']['articles']) > 1) {

		$c = 0; //temp counter
		foreach($GLOBALS['content']['articles'] as $key => $value) {
			if($c || !$a_id) {
				$link  = '<a href="index.php?id='.$GLOBALS['content']['articles'][$key]['article_cid'].',';
				$link .= $key.',0,0,1,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'next\',\'\',\'/php/img/button/next-on.gif\',0)"><img src="/php/img/button/next.gif" name="next" width="20" height="20" border="0" title="suivant">';
				break;
			}
			if($key == $a_id) $c++;
		}
	}
	
	if($cat_down && !$link) {
		// go cat down or to next cat above
		
		if($GLOBALS['content']['cat_id']) {
			foreach($GLOBALS['content']['struct'] as $key => $value) {
				if($GLOBALS['content']['struct'][$key]['acat_struct'] == $GLOBALS['content']['cat_id']) {
					$link = '<a href="index.php?id='.$key.',0,0,1,0,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'next\',\'\',\'/php/img/button/next-on.gif\',0)"><img src="/php/img/button/next.gif" name="next" width="20" height="20" border="0" title="suivant">';
					break;
				}
			}
		} else {
			$c = 0;
			foreach($GLOBALS['content']['struct'] as $key => $value) {
				if($c) {
					$link = '<a href="index.php?id='.$key.',0,0,1,0,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'next\',\'\',\'/php/img/button/next-on.gif\',0)"><img src="/php/img/button/next.gif" name="next" width="20" height="20" border="0" title="suivant">';
					break;
				}
				$c++;
			}
		}
		
		if(!$link && $GLOBALS['content']['cat_id']) {
			$c=0; 
			$temp_key = array();
			foreach($GLOBALS['content']['struct'] as $key => $value) {
				if($GLOBALS['content']['struct'][$key]['acat_struct'] == $GLOBALS['content']['struct'][ $GLOBALS['content']['cat_id'] ]['acat_struct']) {
					$temp_key[] = $key;
				}
			}
			$count_temp = count($temp_key);
			if($count_temp) {
				$c=0;
				foreach($temp_key as $value) {
					if($value == $GLOBALS['content']['cat_id'] && $c+1 < $count_temp) {
						$link = '<a href="index.php?id='.$temp_key[$c+1].',0,0,1,0,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'next\',\'\',\'/php/img/button/next-on.gif\',0)"><img src="/php/img/button/next.gif" name="next" width="20" height="20" border="0" title="suivant">';
						break;
					}
					$c++;
				}
				if($c == $count_temp && !$link) {
					// back reverese to higher next structure level
					$current_id = $GLOBALS['content']['cat_id'];
					
					while($c=1) {
						$parent_id = $GLOBALS['content']['struct'][ $current_id ]['acat_struct'];
						$parent_struct_id = $GLOBALS['content']['struct'][ $parent_id ]['acat_struct'];
					
						$c=0;
						foreach($GLOBALS['content']['struct'] as $key => $value) {
							if($GLOBALS['content']['struct'][$key]['acat_struct'] == $parent_struct_id) {
								if($c) {
									$link = '<a href="index.php?id='.$key.',0,0,1,0,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'next\',\'\',\'/php/img/button/next-on.gif\',0)"><img src="/php/img/button/next.gif" name="next" width="20" height="20" border="0" title="suivant">';
									break;
								}
								if($key == $parent_id) $c=1;
							}
						}
					
						if(!$parent_struct_id) {
							if(!$parent_id) $link = '';
							break;
						} else {
							$current_id = $parent_id;
						}
						
					}
					
					
				}
			}
		}

	}
	
	return ($link) ? $link.$linktext.'</a>' : $linktext;
}


// ---------------------------------------------------------------------
// Previous with rollover image I've called it: [previm]
// ---------------------------------------------------------------------

function get_index_link_previm($linktext, $cat_up=0) {
	// return the link to next article in current ctageory
	$a_id = ($GLOBALS['content']['article_id']) ? $GLOBALS['content']['article_id'] : $GLOBALS['aktion'][1];
	$linktext = trim($linktext);
	if(!$linktext) $linktext = 'PREV';
	$link = '';
	$c = 0; //temp counter

	if(count($GLOBALS['content']['articles']) > 1 && $a_id) {
		
		foreach($GLOBALS['content']['articles'] as $key => $value) {
			if($key == $a_id && $c) {
				$link  = '<a href="index.php?id='.$prev_cat_id.',';
				$link .= $prev_art_id.',0,0,1,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'prev\',\'\',\'/php/img/button/back-on.gif\',0)"><img src="/php/img/button/back.gif" name="prev" width="20" height="20" border="0" title="précédent">';
				break;
			}
			$c++;
			$prev_cat_id = $GLOBALS['content']['articles'][$key]['article_cid'];
			$prev_art_id = $key;
		}
	}
	if($a_id && $c && !$link) {
		$link = '<a href="index.php?id='.$GLOBALS['content']['cat_id'].',0,0,1,0,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'prev\',\'\',\'/php/img/button/back-on.gif\',0)"><img src="/php/img/button/back.gif" name="prev" width="20" height="20" border="0" title="précédent">';
	}
	
	if($cat_up && !$link) {
		// go cat down or to next cat above
		$temp_key = array();
		foreach($GLOBALS['content']['struct'] as $key => $value) {
			if($GLOBALS['content']['struct'][$key]['acat_struct'] == $GLOBALS['content']['struct'][ $GLOBALS['content']['cat_id'] ]['acat_struct']) {
				$temp_key[] = $key;
			}
		}
		if(count($temp_key) && $GLOBALS['content']['cat_id']) {
			$c = 0;
			foreach($temp_key as $value) {
				if($value == $GLOBALS['content']['cat_id']) {
					$prev_cat_id = (!$c) ? $GLOBALS['content']['struct'][$value]['acat_struct'] : $temp_key[$c-1];
					$link = '<a href="index.php?id='.$prev_cat_id.',0,0,1,0,0" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'prev\',\'\',\'/php/img/button/back-on.gif\',0)"><img src="/php/img/button/back.gif" name="prev" width="20" height="20" border="0" title="précédent">';
					break;
				}
				$c++;
			}
		}
	}
	
	return ($link) ? $link.$linktext.'</a>' : $linktext;
}

// -------------------------------------------------------------
// ---- don't forget to modify content.func.inc 
// ---- if u did, delete this last 2 lines

ADDED:
If you don't want this in french you might want to put something else for
title="précédent" or title="suivant" (title is for the ALT tag, nicer because works both in IE and Firefox). Just replace with your language example in english: title="back" or title="previous" and title="next"

THE "GOOD" SOLUTION:
if u are a php coder is to set the title="whatever" as a var and set it in the language prefs, but I don't know how to do that (or it would take me ages to do it)


# 4. Modify the content.func.inc

Code: Select all


#
#-----[ OPEN ]------------------------------------------
#

inc_front/content.func.inc.php

#
#-----[ FIND ]------------------------------------------
#

// special browse the content links: UP, NEXT, PREVIOUS
// echo get_index_link_up('UP')." | ".get_index_link_prev('PREV',1).' | '.get_index_link_next('NEXT',1);
if( ! ( strpos($content["all"],'{BROWSE:')===false ) ) {
	$content["all"] = preg_replace('/\{BROWSE:UP:(.*?)\}/e','get_index_link_up("$1");',$content["all"]);
	$content["all"] = preg_replace('/\{BROWSE:NEXT:(.*?):(0|1)\}/e','get_index_link_next("$1",$2);',$content["all"]);
	$content["all"] = preg_replace('/\{BROWSE:PREV:(.*?):(0|1)\}/e','get_index_link_prev("$1",$2);',$content["all"]);
}


#
#------[ AFTER, ADD ]
#

// based on NEXT, PREVIOUS	
// tag called: BROWSEIM:NEXT and BROWSEIM:PREV
// ANNOTATION: {BROWSEIM:PREV::1}
// ANNOTATION: {BROWSEIM:NEXT::1}

if( ! ( strpos($content["all"],'{BROWSEIM:')===false ) ) {
	$content["all"] = preg_replace('/\{BROWSEIM:NEXT:(.*?):(0|1)\}/e','get_index_link_nextim("$1",$2);',$content["all"]);
	$content["all"] = preg_replace('/\{BROWSEIM:PREV:(.*?):(0|1)\}/e','get_index_link_previm("$1",$2);',$content["all"]);
}
// -------------------------------------------------------------

# Use of the tags called: BROWSEIM:NEXT and BROWSEIM:PREV for example in your templates
# ANNOTATION: {BROWSEIM:PREV: :1} works the same as the BROWSE tag
# ANNOTATION: {BROWSEIM:NEXT: :1} worksthe same as the BROWSE tag

The really good solution is to put this snippets in 2 separate files and add them to the phpwcms_template/inc_script/frontend_render/first_piece_of_code.php
and phpwcms_template/inc_script/frontend_init/second_piece_of_code.php

But as I said, I'm no php developper and the title="your_text_in_your_language" is still an issue to make it really a nice finished replacement tag.

Anyone wanna give it a try?
Post Reply