[ PHPXref.com ] [ Generated: Sun Jul 20 16:35:25 2008 ] [ bBlog 0.7.6 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/bblog/bBlog_plugins/ -> function.nextprev.php (source)

   1  <?php
   2  
   3  /* 
   4      function.nextprev.php - nextprev plugin
   5  */
   6  function identify_function_nextprev () {
   7  
   8      $help = '
   9      <p>This plugin displays your indexes and archive pages in a list of pages.</p>
  10      <p>To call the plugin, simply do a <code>{nextprev}</code>.  Just
  11      like most of bBlog, this defaults to pages of 20 entries.  If you
  12      want to tweak this, use the <var>num</var> parameter.  For example,
  13      if you want pages of 5 entries, do <code>{nextprev num=5}</code>.</p>
  14      <p>Within your pages, you will need to set the skip parameter to
  15      <var>$current_offset</var>, such as in
  16      <code>{getrecentposts num=5 skip=$current_offset assign=posts home=true}</code>
  17      (For 0.7.3, you need a patch for this and <code>getarchiveposts</code>; see the distribution).</p>
  18      <p>The link is stored within the variable <var>{$gonext}</var> and
  19      <var>{$goprev}</var>; If either parameter is not valid, the link
  20      will be an empty string, hence you will probably want to use this
  21      in an <code>if</code>, such as:</p>
  22      <p><code>
  23      {if $goprev ne ""}<br />
  24      &lt;a href="{$goprev}"&gt;Previous&lt;/a&gt;<br />
  25      {/if}<br />
  26      {if $gonext ne ""}<br />
  27      &lt;a href="{$gonext}"&gt;Next&lt/a&gt;<br />
  28      {/if}<br />
  29      </code></p>
  30      <p>If you want to use the <code>link</code> tag in your header,
  31      simply call the <code>{nextprev}</code> before including your
  32      header, and add the following to your header:</p>
  33      <p><code>
  34      &lt;head&gt;<br />
  35      ...<br />
  36      {if $goprev ne ""}<br />
  37      &lt;link rel="prev" type="text/html" href="{$goprev}" /&gt;<br />
  38      {/if}<br />
  39      {if $gonext ne ""}<br />
  40      &lt;link rel="next" type="text/html" href="{$gonext}" /&gt;<br />
  41      {/if}<br />
  42      </p>
  43      <p>If you want to allow users to select pages, use the
  44      <code>{$goprevpages}</code> and <code>{$gonextpages}</code> in
  45      a <code>foreach</code> loop, such as:</p>
  46      <p> <code>
  47      {foreach from=$goprevpages item=page}<br />
  48         &nbsp;&lt;a href="{$page.url}"&gt;{$page.page}&lt;/a&gt;&amp;nbsp;<br />
  49      {/foreach} <br />
  50      {$current_page}<br />
  51      {foreach from=$gonextpages item=page}<br />
  52         &nbsp;&lt;a href="{$page.url}"&gt;{$page.page}&lt;/a&gt;&amp;nbsp;<br />
  53      {/foreach} <br />
  54      </code></p>
  55      <p>By default, this will show all available pages.  You can set the
  56      <code>max_pages</code> parameter to limit the number of pages.
  57      It will show half the number of pages before the current page and
  58      half of those pages afterwards.</p>
  59      <h3>Parameters</h3>
  60      <p><table border="1">
  61      <tr><th>Parameter</th><th>Description</th></tr>
  62      <tr><td><var>num</var></td><td>Number of entries per page; The
  63      default is 20.</td></tr>
  64      <tr><td><var>max_pages</var></td><td>This is the maximum number
  65      of pages to list in the <var>goprevpages</var> and
  66      <var>gonextpages</var> pages.</td></tr>
  67      <tr><td><var>query</var></td><td>This parameter allows you to
  68      assign more query parameters if desired.</td></tr>
  69      <tr><td><var>link</var></td><td>To add some parameters to the
  70      generated link, use this variable.  This form is
  71      <code>&amp;<var>variable</var>=<var>value</var></code>.</td></tr>
  72      <tr><td><var>posts</var></td><td>Instead of this plugin querying
  73      the database to count entries, it will count these entries
  74      instead.  This invalidates the query parameter.</td></tr>
  75      <tr><td><var>adjust</var></td><td>When <var>posts</var> is used,
  76      this parameter will adjust the array according to the parameters.  If it
  77      is not set, this will need to be adjusted externally, and I am not sure
  78      how.</td></tr>
  79      </table></p>
  80      <h3>Variables Assigned</h3>
  81      <p><table border="1">
  82      <tr><th>Variable</th><th>Description</th></tr>
  83      <tr><td><code>current_offset</code></td><td>This is the current
  84      index of the entry at the top of the page.  In other words, this
  85      is <code><var>current_page</var> * <var>entries per page</var>
  86      </code></td></tr>
  87      <tr><td><code>current_page</code></td><td>This is the index of
  88      the current page (starting at 1).</td></tr>
  89      <tr><td><code>gofirstpage</code><br /><code>golastpage</code></td>
  90      <td>When using the parameter <var>max_pages</var>, this is the
  91      index of the first/last page in the
  92      <var>goprevpages</var>/<var>gonextpages</var>.</td></tr>
  93      <tr><td><code>gonum_pages</code></td><td>This is the total
  94      number of pages that could be displayed.</td></tr>
  95      <tr><td><code>goprevpages</code><br /><code>gonextpages</code></td>
  96      <td>This is an array of pages before and after, respectively, the
  97      <var>current_page</var>.  The array contains the <var>page</var>
  98      (index of) and <var>url</var> (URL to display the page).</td></tr>
  99      <tr><td><code>goprev</code><br /><code>gonext</code></td><td>
 100      This is the link to the previous and next page, respectively.</td></tr>
 101      </table></p>
 102      <p>This is version 0.4.1.  For the latest version, see:
 103      <a href="http://www.eyt.ca/Software">http://www.eyt.ca/Software</a></p>';
 104  
 105       return array (
 106      'name'        => 'nextprev',
 107      'type'        => 'function',
 108      'nicename'    => 'NextPrev',
 109      'description'    => 'Adds a previous/next button on your indexes',
 110      'authors'    => 'Eric Y. Theriault',
 111      'licence'    => 'GPL',
 112      'help'        => $help
 113      );
 114      
 115  }
 116  
 117  function smarty_function_nextprev($params, &$bBlog) {
 118      // Initialize default values...
 119      $skip = 0;
 120      $num = 20;
 121      $max_pages = 0;
 122      $pages_before = 0;
 123  
 124      // Set the num parameter
 125      if ( is_numeric( $params[ 'num' ] ) ) {
 126          $num = $params[ 'num' ];
 127      }
 128  
 129      // Set the max_pages parameter
 130      if ( is_numeric( $params[ 'max_pages' ] ) ) {
 131          $max_pages = $params[ 'max_pages' ];
 132          $pages_before = (int)( $max_pages / 2 );
 133      }
 134  
 135      // Acquire the page skip count; if set, snag it.
 136      $newSkip = $_GET[ "pageskip" ];
 137      if ( $newSkip ) {
 138          $skip = $newSkip;
 139      }
 140      $sectionid = $_GET[ "sectionid" ];
 141      $QuerySection = '';
 142      $ExtraParams = '';
 143      if ( $sectionid ) {
 144          $QuerySection .= " AND sections like '%:$sectionid:%'";
 145          $ExtraParams .= "&sectionid=$sectionid";
 146      }
 147      else {
 148         // This is for the case of Clean URLS
 149         $sectionid = $bBlog->get_template_vars("sectionid");
 150         if ( $sectionid ) {
 151            $QuerySection .= " AND sections like '%:$sectionid:%'";
 152         }
 153      }
 154      $query_params = $params[ 'query' ];
 155      if ( $query_params ) {
 156         $QuerySection .= " AND $query_params";
 157      }
 158      $link_params = $params[ 'link' ];
 159      if ( $link_params ) {
 160         $ExtraParams .= $link_params;
 161      }
 162      $posts = $params[ 'posts' ];
 163  
 164      // Calculate the new offset
 165      $offset = $skip * $num;
 166      $nextoffset = $offset + $num;
 167      $bBlog->assign( "current_offset", $offset );
 168  
 169      // Get number of entries...
 170      if ( $posts ) {
 171         $entryCount = count( $posts );
 172         if ( $params[ 'adjust' ] ) {
 173            $bBlog->assign( 'posts', array_slice( $posts, $offset, $num ) );
 174         }
 175      }
 176      else {
 177         // invariant: Need to query the database and count
 178         $countArray = $bBlog->get_results( "select count(*) as nElements from ".T_POSTS." where status = 'live' $QuerySection;" );
 179         if ( $bBlog->num_rows <= 0 ) {
 180            $entryCount = 0;
 181         } else {
 182            foreach ( $countArray as $cnt ) {
 183                $entryCount = $cnt->nElements;
 184            }
 185         }
 186      }
 187  
 188      // Create the previous pages...
 189      $i = 0;
 190      $current_page = $skip;
 191      if ( $max_pages != 0 ) {
 192         $i = $current_page - $pages_before;
 193         if ( $i < 0 ) {
 194            $i = 0;
 195         }
 196      }
 197      $bBlog->assign( "current_page", $current_page + 1 );
 198      $bBlog->assign( "gofirstpage", $i + 1 );
 199      while ( $i < $current_page ) {
 200         $cp = $i + 1;
 201         $prevpages[] = array( 'page' => $cp, 'url' => $_SERVER["PHP_SELF"] . "?pageskip=$i$ExtraParams" );
 202         ++ $i;
 203      }
 204      $bBlog->assign( "goprevpages", $prevpages );
 205  
 206      // Create the next pages
 207      $i = $current_page + 1;
 208      $numberOfPages = (int) ( $entryCount / $num );
 209      $pages = $numberOfPages;
 210      if ( ($pages * $num) < $entryCount ) {
 211         $pages ++;
 212         $numberOfPages ++;
 213      }
 214      if ( $max_pages != 0 ) {
 215         $pages = $i + $pages_before;
 216         if ( $pages > $numberOfPages ) {
 217            $pages = $numberOfPages;
 218         }
 219      }
 220      $bBlog->assign( "golastpage", $pages );
 221      $bBlog->assign( "gonum_pages", $numberOfPages );
 222      while ( $i < $pages ) {
 223         $nextpages[] = array( 'page' => $i+1, 'url' => $_SERVER["PHP_SELF"] . "?pageskip=$i$ExtraParams" );
 224         ++ $i;
 225      }
 226      $bBlog->assign( "gonextpages", $nextpages );
 227  
 228      // Get the previous count...
 229      if ( $offset == 0 ) {
 230         $previous = 0;
 231         $bBlog->assign( "goprev", "" );
 232      } else {
 233         $previous = $skip - 1;
 234         $bBlog->assign( "goprev", $_SERVER["PHP_SELF"] . "?pageskip=$previous$ExtraParams" );
 235      }
 236  
 237      // Get the next count...
 238      if ( $nextoffset < $entryCount ) {
 239         $next = $skip + 1;
 240         $bBlog->assign( "gonext", $_SERVER["PHP_SELF"] . "?pageskip=$next$ExtraParams" );
 241      } else {
 242         $next = 0;
 243         $bBlog->assign( "gonext", "" );
 244      }
 245  }
 246  ?>
 247  


[ Powered by PHPXref - Served by Debian GNU/Linux ]