Prev and Next

Permalink
I have a bunch of sub nav pages for "products" in c5 to display 1 product per page. is there a javascript to go to the next page (product) in the same nav level for prev and next buttons?

I hope that makes sense...

bryanlewis
 
Tony replied on at Permalink Reply
Tony
Something like this should really be made into a block, but here's some rough code to get you started

first, add something like this to the page type controller:

function view(){
      $this->set( 'nextCollection', $this->getNextCollection() );
      $this->set( 'previousCollection', $this->getPreviousCollection() );
   }
   function getNextCollection(){
      global $c; 
      if(!$this->otherCollectionsLoaded) $this->loadOtherCollections();
      foreach($this->otherCollections as $photoCollection){      
         if(!$firstCollection) $firstCollection=$photoCollection; 
         if( $collectionFound ) return $photoCollection;
         if( $photoCollection->cID == $c->cID ) $collectionFound=1; 
      }
      return $firstCollection;
   }
   function getPreviousCollection(){



And then add something like this to your page type:

<div id="sectionPaging" style=""> 
      <?
      $nextCollectionURL=$nextCollection->getCollectionPath();
      if( !strlen($nextCollectionURL) ) $nextCollectionURL='?cID='.$nextCollection->cID;
      $prevCollectionURL=$previousCollection->getCollectionPath();
      if( !strlen($prevCollectionURL) ) $prevCollectionURL='?cID='.$previousCollection->cID;      
      ?>
      <div style="float:right">
         <a href="<?=View::url( $nextCollectionURL )?>"><? /*$nextCollection->getCollectionName()*/ ?>Next Entry &raquo;</a>
      </div>
      <a href="<?=View::url( $prevCollectionURL )?>">&laquo; Previous</a> 
   </div>
bryanlewis replied on at Permalink Reply
bryanlewis
Yea a block for something like that would be great! Would it be hard to make? I'm starting to try and make my own blocks. Entry level obviously but if something like this isn't too hard it might be a good one to start with.
Tony replied on at Permalink Reply
Tony
give it a try
bryanlewis replied on at Permalink Reply
bryanlewis
I'm getting a Fatal error: Call to a member function getCollectionPath() on a non-object

on this line...

$nextCollectionURL=$nextCollection->getCollectionPath();

any ideas?
Tony replied on at Permalink Reply
Tony
that code I posted was intended to work with a page type and a page type controller. if you're trying to set it up as a block, it'll take some modifications.
bryanlewis replied on at Permalink Reply
bryanlewis
I'm trying to use the page type and the page type controller. I'm getting that error on the page type. which would be my left_sidebar.php in my theme correct?
Tony replied on at Permalink Reply
Tony
maybe there isn't a sibling page there yet? You should just test that you've got a valid collection object before you run that method on it.
bryanlewis replied on at Permalink Reply
bryanlewis
I don't think I'm advanced enough for it. Are there docs on Sibling pages? Do you mean Children's pages? I do have children pages set up.
Tony replied on at Permalink Reply
Tony
by siblings, I just mean other pages on the same level, the one's that share the same parent.
bryanlewis replied on at Permalink Reply 1 Attachment
bryanlewis
Attached is a sitemap. Just to make sure I'm doing it right. Note products one and two. There will be like 50 products for people to scroll through. you think that too much for this type of a thing?
bryanlewis replied on at Permalink Reply
bryanlewis
If there is a work around for something like this or an alternative (something easier). I'm all ears.
bryanlewis replied on at Permalink Reply
bryanlewis
I'm confused as to why its not working. Any advise would be great!
jgarcia replied on at Permalink Reply
jgarcia
bryan - which page on this site map is using the page type that you added this code to?
bryanlewis replied on at Permalink Reply
bryanlewis
All the interior pages are left_sidebar.php but the ones I'm trying to do this with are product_one and product_two
jgarcia replied on at Permalink Reply
jgarcia
just to test something, can you create a product_three page on the same level, and then view the product_two page and see if you still get the error.

it may be that since product_one has no previous page and product_two has no next page that is unable to call the getCollectionPath function on that object. That is essentially what the error you posted is saying - that $nextCollectionURL is not actually a collection object.
bryanlewis replied on at Permalink Reply
bryanlewis
I'm getting the same error as i was before. One the same line of code.
bryanlewis replied on at Permalink Reply
bryanlewis
<div id="sectionPaging" style=""> 
      <?
      $nextCollectionURL=$nextCollection->getCollectionPath();
      if( !strlen($nextCollectionURL) ) $nextCollectionURL='?cID='.$nextCollection->cID;
      $prevCollectionURL=$previousCollection->getCollectionPath();
      if( !strlen($prevCollectionURL) ) $prevCollectionURL='?cID='.$previousCollection->cID;      
      ?>
      <div style="float:right">
         <a href="<?=View::url( $nextCollectionURL )?>"><? /*$nextCollection->getCollectionName()*/ ?>Next Entry È</a>
      </div>
      <a href="<?=View::url( $prevCollectionURL )?>">Ç Previous</a> 
   </div>
jgarcia replied on at Permalink Reply
jgarcia
where did you put the code for the view function? it sounds like $nextCollectionURL might not even be getting defined.
bryanlewis replied on at Permalink Reply
bryanlewis
Under the root folder there is a folder called "controllers". Then I made a new folder called "page_types" and created a file called "left_sidebar.php"

The code in this file is the following
<?php
function view(){
      $this->set( 'nextCollection', $this->getNextCollection() );
      $this->set( 'previousCollection', $this->getPreviousCollection() );
   }
   function getNextCollection(){
      global $c; 
      if(!$this->otherCollectionsLoaded) $this->loadOtherCollections();
      foreach($this->otherCollections as $photoCollection){      
         if(!$firstCollection) $firstCollection=$photoCollection; 
         if( $collectionFound ) return $photoCollection;
         if( $photoCollection->cID == $c->cID ) $collectionFound=1; 
      }
      return $firstCollection;
   }


Then I went into my theme directory and in the left_sidebar.php I added this code.

<div id="sectionPaging" style=""> 
        <?
        $nextCollectionURL=$nextCollection->getCollectionPath();
        if( !strlen($nextCollectionURL) ) $nextCollectionURL='?cID='.$nextCollection->cID;
        $prevCollectionURL=$previousCollection->getCollectionPath();
        if( !strlen($prevCollectionURL) ) $prevCollectionURL='?cID='.$previousCollection->cID;        
        ?>
        <div style="float:right">
            <a href="<?=View::url( $nextCollectionURL )?>"><? /*$nextCollection->getCollectionName()*/ ?>Next Entry &raquo;</a>
        </div>
        <a href="<?=View::url( $prevCollectionURL )?>">&laquo; Previous</a> 
    </div>


I think this is what Tony was telling me to do. Am I doing things right so far? I've never actually worked in the controllers folder before so I'm not for sure if I did that process right.

This is something I found in the source code...

<b>Fatal error</b>: Call to a member function getCollectionPath() on a non-object in <b>/var/www/vhosts/site.com/subdomains/spark/httpdocs/themes/themename/left_sidebar.php</b>
jgarcia replied on at Permalink Reply
jgarcia
do you have the right class name for the page type controller? i believe it would have to look like this:
class LeftSidebarPageTypeController extends Controller
jgarcia replied on at Permalink Reply
jgarcia
oh, and the "loadOtherCollections" function needs this at the beginning:
Loader::model('page_list');


in order to create the PageList object.
bryanlewis replied on at Permalink Reply
bryanlewis
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
class LeftSidebarPageTypeController extends Controller
function view(){
      $this->set( 'nextCollection', $this->getNextCollection() );
      $this->set( 'previousCollection', $this->getPreviousCollection() );
   }
   function getNextCollection(){
      global $c; 
      if(!$this->otherCollectionsLoaded) $this->loadOtherCollections();
      foreach($this->otherCollections as $photoCollection){      
         if(!$firstCollection) $firstCollection=$photoCollection; 
         if( $collectionFound ) return $photoCollection;
         if( $photoCollection->cID == $c->cID ) $collectionFound=1; 
      }
jgarcia replied on at Permalink Reply
jgarcia
yeah, that looks right to me, but don't forget the opening "{" for the class
bryanlewis replied on at Permalink Reply
bryanlewis
Its working now nicely. just need to style it! Thanks so much for the help I really appreciate it!

p.s.Did you get my e-mail?
enque replied on at Permalink Reply
enque
Thanks for this.
I've got this working fine but my question is how do we get it to work on child pages as it seems only to be going through the main pages and not the children. Or am I missing something.
Tony replied on at Permalink Reply
Tony
this example should navigate between all siblings, or pages that are on the same level. ie. those pages with the same parent page.
bryanlewis replied on at Permalink Reply
bryanlewis
I'm hoping to make a block of this sometime soon. We will see how that goes.
bryanlewis replied on at Permalink Reply
bryanlewis
I've made this into a block and it's working great. I'll post it to the marketplace sometime soon.
moth replied on at Permalink Reply
moth
I'm struggling with the same function. Could you share your block?
bryanlewis replied on at Permalink Reply
bryanlewis
I plan on submitting it to the market place soon. If you need it before why don't you e-mail me. I'd be happy to help out.
Tony replied on at Permalink Reply
Tony
sorry bryan, i beat you too it ;-)

the next previous block is now live here:
http://www.concrete5.org/marketplace/addons/next-previous/...
bryanlewis replied on at Permalink Reply
bryanlewis
Yea I noticed that. It's okay though, your version is much more in depth I'm sure. Mine was more so just practice to get better at making blocks and learning PHP. Any tips for that Tony?
zoinks replied on at Permalink Reply
Tony, you are awesome! Thank you for figuring this out AND giving it away for free. This is the kind of core element stuff that is really going to make C5 much more competitive with stuff like CMS Made Simple from a non-expert developer point of view (ie. my point of view).

Question: Is there a way to write this functionality into a specific template without having to Add the block to each individual page? For example, I made 2 pages and I stuck the Prev Next block on page 1, but not on page 2 yet:
http://php.22graphics.com/Concrete5/index.php?cID=74...
Both pages use the same template. I'd like to hard-code the Prev Next code in the template so that every time the user selects that type of template, it's already got the Prev Next buttons on the bottom. Otherwise, every time they add a page, they're going to have to add this, too. It would be better for the end user if it was just already there.
Tony replied on at Permalink Reply
Tony
cool, glad you dig it. here's an example of hardcoding the next previous block into a page type:

$bt = BlockType::getByHandle('tony_next_previous');
$bt->controller->nextLabel = 'Next Collection';
$bt->controller->previousLabel = 'Previous';
$bt->controller->showArrows =1;
$bt->controller->loopSequence=1;
//$bt->render('templates/footer_bar'); 
$bt->render('view');
zoinks replied on at Permalink Reply
Cool! Tried it, it works! Only a few lines of code, too. Thank you again!

Is there a specific code for "exclude system pages" (when hard-coding into template)?

EDIT: I think I got it! Looked through controller.php and found "excludeSystemPages" so it's probably something like
$bt->controller->excludeSystemPages=1;
…right?

I guess I need to set up a Parent/Child relationship (?) because on the last post page, the Next button brings me to the Members page rather than looping back to the beginning.
See:http://php.22graphics.com/Concrete5/index.php?cID=78...
bryanlewis replied on at Permalink Reply
bryanlewis
C5 should include this block in the core. It's really a great add on.
zoinks replied on at Permalink Reply
Bryan, have you been using the add-on with great success? Do you know how to limit it to only a certain section of pages? I'm thinking I need a Parent/Child relationship between the pages, but not sure if that's the case or how to go about it.
bryanlewis replied on at Permalink Best Answer Reply
bryanlewis
Yea I've used it before and it worked great. I'm not for sure if its possible to have a parent/child relationship the way you want it to.
zoinks replied on at Permalink Reply
Perhaps "Parent/Child" is the wrong terminology... Not sure what you'd call it, though. I just basically want to cycle through a Portfolio section and not jump any other sections.
Tony replied on at Permalink Reply
Tony
try adding this line before the render:

$bt->controller->excludeSystemPages=1;

btw, you can normally figure out what the configuration variables are named by looking at the blocks db.xml file.