Jump to content


Photo

previous - next links


  • Please log in to reply
9 replies to this topic

#1 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 27 January 2008 - 08:36 PM

Hi guys,

I want to use a snippet that will return a previous and a next link on a page.
these are defined by the menuindex
is there a way to get the menuindex from the current document using the api's?
i couldn't find this

maybe there already is someone who wrote a snippet like this?

#2 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 27 January 2008 - 09:29 PM

okay, i found out how to:

this is the snippet i wrote. I think it could use some tweaking though, i'm manually setting the parent id, where i'm sure i can get it as well using the api's, can anyone gimme any pointers?

this is what i have so far:
// usage: [[prevnext]] 

/* define these lines in the site's css-file and adapt where necessary:
   .prev{
   font-size: 8pt;
   text-align:left;
   float:left;
   padding: 0 0 20px 0;
   }

   .next{
   font-size: 8pt;
   text-align:right;
   padding: 0 0 20px 0;
   }
*/

//define menuindex current page
  $currentid = $etomite->documentIdentifier;
  $fields = "menuindex";
  $doc = $etomite->getDocument($currentid, $fields);
$menuindex = $doc['menuindex'];

$id=140; //parent page - it should be possible to get this automatically

$children = $etomite->getActiveChildren($id); 
$menu = "<div class=\"prevnext\">"; 
$childrenCount = count($children);

for($x=0; $x<$childrenCount; $x++) {

  $childid = $children[$x]['id'];

//define menuindex for sibling
  $fields = "menuindex";
  $doc = $etomite->getDocument($childid , $fields);
  $siblingindex = $doc['menuindex'];

  if ($siblingindex == ($menuindex-1)) {
		  $menu .= "<div class=\"prev\"><a href='[~".$children[$x]['id']."~]' title='".$children[$x]['longtitle']."'><<&nbsp;".$children[$x]['pagetitle']."</a></div> ";
  }

  if ($siblingindex == ($menuindex+1)) {
		  $menu .= "<div class=\"next\"><a href='[~".$children[$x]['id']."~]' title='".$children[$x]['longtitle']."'>".$children[$x]['pagetitle']."&nbsp;>></a></div> ";
  }

}

$menu .= "</div>";

return $menu;


#3 Dean

Dean

    Loves Etomite Forums!

  • Admin
  • 4,787 posts

Posted 27 January 2008 - 09:32 PM

If the rest is correct, you should be able to do this:

$id=$currentid;

#4 Cris D.

Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts

Posted 27 January 2008 - 11:02 PM

$id=$etomite->getParent($currentid, $active=1, $fields='id');

$children = $etomite->getActiveChildren($id['id']);

[EDITED] to add the array $id['id'] to correctly retrieve the parent id[/EDIT]

Nice work vampke, thanks.

Edited by Cris D., 27 January 2008 - 11:11 PM.


#5 Cris D.

Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts

Posted 27 January 2008 - 11:26 PM

I also had to add this before the getActiveChildren in case the current document does not have a parent to stop an error if this is placed in a template.
if($parent['id']==""){ return;}

I've implemented it on my etomite site here, thanks again.

#6 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 28 January 2008 - 03:15 PM

$id=$etomite->getParent($currentid, $active=1, $fields='id');

$children = $etomite->getActiveChildren($id['id']);

[EDITED] to add the array $id['id'] to correctly retrieve the parent id[/EDIT]

Nice work vampke, thanks.



thanks for your help, i'm glad you like my snippet :)
care to explain what the difference is between my
$children = $etomite->getActiveChildren($id);
and your
$children = $etomite->getActiveChildren($id['id']);

if I add you code
if($parent['id']==""){ return;}
it always returns nothing, shouldn't it be
if($id==""){ return;}
?

#7 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 28 January 2008 - 03:48 PM

I added some more functionality to this.
The first version would only show a link if the previous page had a menu index of 1 less or the next page had a menu index of 1 more than the current page.
This version will generate previous and next links even if difference between the current page's and the previous or next page's menu index is more than 1

// [[prevnext2]]

//define menuindex current page
  $currentid = $etomite->documentIdentifier;
  $fields = "menuindex";
  $doc = $etomite->getDocument($currentid, $fields);
$menuindex = $doc['menuindex'];

$id=$etomite->getParent($currentid, $active=1, $fields='id'); //thank you Chris D. !

if($id==""){ return;}
$children = $etomite->getActiveChildren($id['id']);

$menu = "<div class=\"prevnext\">";
$childrenCount = count($children);

$next=false; //we need to set a boolean in order to know when to generate the "next" link
$pastFirst=false; //we do not need a previous link on the very first page
$quitLoop=false;
$x=0;

while ($quitloop==false && $x<$childrenCount) {

  $childid = $children[$x]['id'];

//define menuindex for sibling
  $fields = "menuindex";
  $doc = $etomite->getDocument($childid , $fields);
  $siblingindex = $doc['menuindex'];

if ($next) {
		  $menu .= "<div class=\"next\"><a href='[~".$children[$x]['id']."~]' title='".$children[$x]['longtitle']."'>".$children[$x]['pagetitle']."&nbsp;>></a></div> ";
		  $next=false; //we only need 1 next link, not a link to all the next pages
		  $quitLoop=true; //we can quit the loop now, because we have our next link
  }

if ($siblingindex == $menuindex) {
		  $next=true; //if loop is in menuindex, next loop should generate the "next" link
  }

if ($siblingindex == $menuindex && $pastFirst) {
		  $menu .= "<div class=\"prev\"><a href='[~".$prevId."~]' title='".$prevTitle."'>&nbsp;<<&nbsp;".$prevPageTitle ."</a></div> ";
   }

//next loop should start with settings from previous loop
$prevId = $children[$x]['id'];
$prevTitle = $children[$x]['longtitle'];
$prevPageTitle = $children[$x]['pagetitle'];

$pastFirst=true; //we are past the first page, the next one needs a previous link
$x++;

}

$menu .= "</div>";

return $menu;


btw: if using this snippet, menu indices should be unique, otherwise more then 1 previous or next link will be generated on 1 page.

edited: bug on first page
edited2: used while instead of for loop, no need to continue looping when a next link is generated

Edited by vampke, 28 January 2008 - 04:06 PM.


#8 Dean

Dean

    Loves Etomite Forums!

  • Admin
  • 4,787 posts

Posted 28 January 2008 - 04:13 PM

Haven't others already created snippets like it??

http://www.etomite.c...ult_type=topics

#9 vampke

vampke

    Likes Etomite Forums!

  • Member
  • PipPip
  • 174 posts

Posted 28 January 2008 - 06:40 PM

Haven't others already created snippets like it??

http://www.etomite.c...ult_type=topics


thanks for pointing this out NOW lol :)

Edited by vampke, 28 January 2008 - 07:37 PM.


#10 Cris D.

Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts

Posted 28 January 2008 - 09:29 PM

thanks for your help, i'm glad you like my snippet smile.gif
care to explain what the difference is between my

$children = $etomite->getActiveChildren($id);

and your

$children = $etomite->getActiveChildren($id['id']);

That's because I got the id from getParent which returns an array, therefore you need to put the array key i to read the id.

Also

if($parent['id']==""){ return;}

it always returns nothing, shouldn't it be

if($id==""){ return;}


Whith the change I made, $id was the current page and $parent['id'] is the id of the parent. Therefore, if there is no parent (at the top of the folder) show no links otherwise you would start viewing siblings. I was using $parent, where you were not.

It's a bit of a moot point now though... and depends on what type of functionality you require, it's good for me though. ta.

Edited by Cris D., 28 January 2008 - 09:32 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users