Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

pagination problem


Reply

1190 Views

Avatar
roterl

17 May 2009 at 7:56am Community Member, 44 Posts

hi
I'm tring to make pagination that will be set from the template instead of the code. so the theme designer will be able to select how much items to show per page.
so for example in the ArticlesHolder it will be possible to do somehting like this:

<% control NewsArticles(10) %>

for 10 items per page.
This is the code:

ArticleHolder.php:

<?php

/**
* Defines the ArticleHolder page type
*/
class ArticleHolder extends Page {
   static $db = array(
   );
   static $has_one = array(
   );
   
   static $allowed_children = array('ArticlePage');
   static $icon = "themes/tutorial/images/treeicons/news";

}

class ArticleHolder_Controller extends Page_Controller {

   function init() {
      RSSFeed::linkToFeed($this->Link() . "rss");
      parent::init();
   }

   function rss() {
      $rss = new RSSFeed($this->Children(), $this->Link(), "The coolest news around");
      $rss->outputToBrowser();
   }

   /**
    * Get the latest 2 news articles, allowing us to get more
    *
    * @return DataObjectSet|false The DataObjectSet of the news articles you're looking at or boolean false if there aren't any news articles
    */
   function NewsArticles($itemsPerPage = 10) {
      if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) {
         $_GET['start'] = 0;
      }

      $start = (int)$_GET['start'];
      $doSet = DataObject::get(
      $callerClass = "ArticlePage",
      $filter = "`ParentID` = '".$this->ID."'",
      $sort = "Date DESC",
      $join = "",
      $limit = "{$start},{$itemsPerPage}"
      );
      
      return $doSet ? $doSet : false;
   }

}

?>

ArticleHolder.ss:

.....
               <ul id="NewsList" >
                  <% control NewsArticles(5) %>
                     <div id="item" class="$EvenOdd">
                     <div class="newsTitle"><a href="$Link" title="<% _t('READ_MORE_ABOUT',"Read more about") %> &quot;{$Title}&quot;">$Title</a></div>
                     <div class="newsDate"><% _t('PUBLISH_DATE',"Publish at:") %> $Date.Nice <% _t('PUBLISHER',"by") %> $Author</div>
                     <!--
                     <li class="newsAuthor">$Author</li>
                     <li class="newsSummary">$Content.FirstParagraph</li>
                     <li class="newsReadMore"><a href="$Link" title="<% _t('READ_MORE_ON',"Read more on") %> &quot;{$Title}&quot;"><% _t('READ_MORE',"Read more") %> &gt;&gt;</a></li>
                      -->
                     </div>
                  <% end_control %>
            
                  <% if NewsArticles.MoreThanOnePage %>
                     <p>
                     <% if NewsArticles.PrevLink %>
                        <a href="$NewsArticles.PrevLink">&lt;&lt; <% _t('PREV',"Prev") %></a> |
                     <% end_if %>
            
                     <% control NewsArticles.Pages %>
                        <% if CurrentBool %>
                           <strong>$PageNum</strong>
                        <% else %>
                           <a href="$Link" title="><% _t('GO_TO_PAGE',"Go to page") %> $PageNum">$PageNum</a>
                        <% end_if %>
                     <% end_control %>
            
                     <% if NewsArticles.NextLink %>
                        | <a href="$NewsArticles.NextLink"><% _t('NEXT',"Next") %> &gt;&gt;</a>
                     <% end_if %>
                     </p>
                  <% end_if %>
               </ul>
.....

The problem is the the NewsArticles function called twice. once at <% control NewsArticles(5) %> (which get the right results) and another time at <% if NewsArticles.MoreThanOnePage %> which return the wrong results.
This mess up the pagination.

any ideas?

thanks,
Rotem.