<?php
/*****************************************/
/* NP_PageBreak                          */
/* ------------------------------------- */
/* by Rodrigo Moraes                     */
/* http://www.tipos.com.br               */
/*****************************************/
// Feedback is welcome here: http://forum.nucleuscms.org/viewtopic.php?t=3995
 
class NP_PageBreak extends NucleusPlugin {
    function getName() { return 'Page Break'; }
    function getAuthor() { return 'Rodrigo Moraes'; }
    function getURL() { return 'http://www.tipos.com.br'; }
    function getVersion() { return '0.9'; }
    function getDescription() {
    return 'NP_PageBreak can break a post in multiple pages and create navigation links between them. Use &lt;%pagebreak(Page Title)%&gt; every time you want a page break in your posts. Useful for long texts, tutorials, presentations etc.'; }
    function supportsFeature($what) {
        switch($what) {
            case 'SqlTablePrefix': return 1;
            default: return 0;
        }
    }
 
    function install() {
        $this->createOption('navstart','Navigation links start tag','text','<div class="pagebreak">');
        $this->createOption('navend','Navigation links end tag','text','</div>');
        $this->createOption('titlestart','Page title start tag','text','<h2>');
        $this->createOption('titleend','Page title end tag','text','</h2>');
        $this->createOption('nextstart','Next page start tag','text','<div class="nextpage">');
        $this->createOption('nextend','Next page end tag','text','</div>');
        $this->createOption('shownumbers','Show numbers before navigation links?','yesno','yes');
        $this->createOption('error','Error message when trying to access a non-existent page','text','This page doesn\'t exist. Use the navigation links to find what you want.');
    }
 
    function getEventList() { return array('PreItem'); }
 
    function event_PreItem(&$data,$page=1) {
        global $pagecount, $pagetitle, $page;
 
        $pagecount = 0;
        if (!getVar('page')) {
            $page = 1;
        }
        else {
            $page=getVar('page');
        }
 
        $z = 0;
        $parts=array('body','more');
        foreach ($parts as $part) {
 
            if (preg_match('/\<%pagebreak\(.*?\)%\>/', $data['item']->$part)) {
 
                preg_match_all('/\<%pagebreak\(.*?\)%\>/',$data['item']->$part,$tags);     
                $text = $data['item']->$part;
                foreach ($tags[0] as $tag) {
 
                    // let's get the page content...
                    $tagpos = strpos($text, $tag);
                    $pages[$z] = substr($text, 0, $tagpos);
 
                    // ... and now the page title
                    $end = strpos(substr($tag, 0), ')%>');
                    if ($pages[0]) {
                        $pagetitle[$z+1] = substr($tag, 12, $end-12);
                    }
                    else {
                        $pagetitle[$z] = substr($tag, 12, $end-12);
                    }
 
                    // finally, let's forget used data and go back to the beginning
                    $tagend = $tagpos + strlen($tag);
                    $text = substr($text, $tagend, strlen($text)-$tagend);
                    $z++;
                } // foreach ($tags[0] as $tag)
 
                // this is the last page content of each item part
                $pages[$z] = substr($text, 0, strlen($text));
                $z++;
                if (!$pages[0]) {
                    $pagearray = $page;
                }
 
                else {
                    $pagearray = $page - 1;
                    $pagetitle[0] = $data['item']->title;
                }
 
                // check if page exists
                if (!$pages[$pagearray]) {
                    $data['item']->$part = $this->getOption('error');
                }
                else {
					// full mode = show all parts
					if (getVar('mode') == 'full') {
						$data['item']->$part = '';
						$n = 0;
						foreach($pages as $pagepart) {
							if (!$pages[0]) {
								if ($z == 1) {
									echo $this->getOption('titlestart');
									echo $pagetitle[$n];
									echo $this->getOption('titleend');
									echo $pagepart;
								$n++;
								}
								$z = 1;
							}
							else { 
								if ($n != 0) {
									echo $this->getOption('titlestart');
									echo $pagetitle[$n];
									echo $this->getOption('titleend');
								}
								echo $pagepart;
								$n++;
							}
						}
					} // end of full mode
					else {
 
						if ($pages[0] && $page == 1 || !$pagetitle[$page-1]) {
							$data['item']->$part = '';
						}
						else {
							$data['item']->$part = $this->getOption('titlestart') . strip_tags($pagetitle[$page-1]) . $this->getOption('titleend');
						}
						if ($pages[0]) {
							$data['item']->$part .= $pages[$pagearray];
						}
						else {
							$strip_br = explode("\r", $pages[$pagearray], 2);
							$data['item']->$part .= $strip_br[1];
						}
					}
                }
                $pagecount = count($pagetitle);
			//} // if (getVar('mode') != 'full')
            } // if (preg_match
        } // foreach ($parts as $part)
    } // event_PreItem
 
    function doTemplateVar(&$item) {
        global $pagecount, $pagetitle, $page;
 
        if ($pagecount > 1) {        
            $params = func_get_args();
            $link = createItemLink($item->itemid);
 
            if ($params[1] == 'links') {
 
            // create navigation if one or more pagebreaks are found
                echo $this->getOption('navstart'),"\n";
                echo "<ul>\n";
                for ($n = 0; $n < $pagecount; $n++) {
                    $z = $n + 1;
                    echo "<li>";
                    if ($this->getOption('shownumbers') == 'yes') {
                        echo $z,". ";
                    }
                    if ($z != $page) {
                        // M.H. change: Check if Fancy URL ist turned on
                        if ($GLOBALS['CONF']['URLMode'] == 'normal')
                        {
                            echo "<a href=\"$link&amp;page=$z\">";
                        } else {
                            echo "<a href=\"$link/?page=$z\">";
                        }
                    }
                    if (trim($pagetitle[$n])) {
                        echo $pagetitle[$n];
                    }
                    else {
                        echo "page $z";
                    }
                    if ($z != $page) {
                        echo "</a>";
                    }
                    echo "</li>\n";
                }
                echo "</ul>\n";
                echo $this->getOption('navend');
            }
 
            if ($params[1] == 'next') {
                $next = $page+1;
                if ($next <= $pagecount) {
                    echo $this->getOption('nextstart');
                    echo "<a href=\"$link&amp;page=".$next."\">";
                    if (trim($pagetitle[$page])) {
                        echo $pagetitle[$page];
                    }
                    else {
                      // M.H. Change: Check Fancy URL
                      if ($GLOBALS['CONF']['URLMode'] == 'normal')
                      {
                        echo "<a href=\"$link&amp;?page=".$next."\">";
                      } else {
                        echo "<a href=\"$link/?page=".$next."\">";
                      }
                    }
                    echo "</a>";
                    echo " &raquo;";
                    echo $this->getOption('nextend');
                }
            }
        }
    } // doTemplateVar
} // class
?> 
np_pagebreak_code.txt · Last modified: 2007/11/25 15:57 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki