How to output all pages in one long page?

General questions about CMSimple
Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: How to output all pages in one long page?

Post by Korvell » Wed Aug 09, 2017 4:56 pm

OK - got it. Changed the line in funtions.php - added the section to userfuncs.php - added <?php echo printlink();?> to the template (below content for test) - but no difference from normal print view, just the current page shows...

Notice: The print icon in the top blue line calls my own version of the print function - was the only way I could figure out to show the Font Awesome print icon and make it clickable to print...

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to output all pages in one long page?

Post by cmb » Wed Aug 09, 2017 5:08 pm

Korvell wrote:but no difference from normal print view, just the current page shows...
Hm, https://sk.egmont-hs.dk/?1.-H%C3%B8jsko ... orie&print appears to be fine (i.e. as expected).

I'm not sure, though, if you want to always display all pages in the print view.
Christoph M. Becker – Plugins for CMSimple_XH

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: How to output all pages in one long page?

Post by Korvell » Wed Aug 09, 2017 5:22 pm

Wow - that does work indeed - although your link only shows sektion 1. But if I simply put in a direkt link for "http://sk.egmont-hs.dk/index.php?print" it shows the whole site from the top level. But now I get an error 404 at the top - the rest of the site shows up nicely. So close now...

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to output all pages in one long page?

Post by cmb » Wed Aug 09, 2017 5:42 pm

Korvell wrote:But if I simply put in a direkt link for "http://sk.egmont-hs.dk/index.php?print" it shows the whole site from the top level. But now I get an error 404 at the top - the rest of the site shows up nicely.
Well, that was pretty surprising for me! The cause is, that ?print is regarded as page name, but as no such page exists, a 404 is printed by content() but the submenu shows all pages. Compare that with https://sk.egmont-hs.dk/index.php?&print, which shows the expected behavior.

Anyhow, if you want to always show all pages in the print view, just use the following in cmsimple/userfuncs.php:

Code: Select all

<?php

class ExpandedSubmenu
{
    private $pages;

    public function __construct()
    {
        $this->pages = new XH_Pages;
    }

    public function render()
    {
        global $print, $s;

        $html = '';
        $old_s = $s;
        foreach ($this->pages->toplevels() as $page) {
            if (!$this->pages->isHidden($page)) {
                $html .= $this->renderPage($page);
            }
        }
        $s = $old_s;
        return $html;
    }

    private function renderPage($index)
    {
        global $c, $s;

        $s = $index;
        $html = XH_convertPrintUrls(evaluate_scripting($c[$index]));
        foreach ($this->pages->children($index) as $page) {
            $html .= $this->renderPage($page);
        }
        return $html;
    }
}

function submenu_print()
{
    return (new ExpandedSubmenu)->render();
    
}
And remove change this line to:

Code: Select all

submenu_print(), '</body>', "\n", '</html>', "\n";
And use ?&print for the print link!
Last edited by cmb on Wed Aug 09, 2017 9:05 pm, edited 1 time in total.
Reason: Fixed code wrt. hidden pages
Christoph M. Becker – Plugins for CMSimple_XH

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: How to output all pages in one long page?

Post by Korvell » Wed Aug 09, 2017 6:03 pm

Actually - I would like to have both options: Print current page and print all pages ;) That was my initially thoughts when I looked for that internal CMSimple command from old ages...

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: How to output all pages in one long page?

Post by Korvell » Wed Aug 09, 2017 6:17 pm

OK - changed the userfuncs.php and now the printout looks better - but still got an issue: the first page shows twice ;)

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: How to output all pages in one long page?

Post by Korvell » Wed Aug 09, 2017 8:31 pm

Update: Seems there might have been a cache problem because now the print works well - no double page 1. But - and I hate to say it - there's still an issue - hidden page titles (the H1 ones) shows up. Guess this is a wink to maybe switch to 1.7.0 eventually.

Thank You all, especially Christoph. Always a pleasure to witness the expertise in this forum.

PS: If it's not too much trouble - any suggestion how to have two print functions - one for all and one for current page?

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to output all pages in one long page?

Post by cmb » Wed Aug 09, 2017 9:15 pm

Korvell wrote:Update: Seems there might have been a cache problem because now the print works well - no double page 1
There may be a difference when logged in as admin or not. If the first page is duplicated, check the modifications in functions.php; the output of content() or $content should be removed.
Korvell wrote:But - and I hate to say it - there's still an issue - hidden page titles (the H1 ones) shows up.
Oh, indeed, I've overlooked that. I have fixed the code above now (only the userfuncs.php part has to be updated).
Korvell wrote:Guess this is a wink to maybe switch to 1.7.0 eventually.
If all necessary plugins are compatible with XH 1.7.0, I'd suggest to upgrade as soon as possible.
Korvell wrote:PS: If it's not too much trouble - any suggestion how to have two print functions - one for all and one for current page?
Do you want two different functions, or just one function which works differently for the first page only (i.e. show all pages for the first page, but only subpages for all other pages)?
Christoph M. Becker – Plugins for CMSimple_XH

Korvell
Posts: 93
Joined: Thu May 22, 2008 10:33 pm

Re: How to output all pages in one long page?

Post by Korvell » Thu Aug 10, 2017 9:00 am

Thanks. Now it works perfectly - I indeed had forgotten to remove "$content" from the line in functions.php.

My wish for the function is two separate functions; one that takes current page only and one that takes all. I think it would be confusing for the visitor if the print button would work differently depending on what pages he was displaying. I do understand that would require two buttons/links - one for each option - on the page.
I agree that in this current project that probably no one wishes to just print the first page, but I could vision other projects of mine to have much more on the first page that you would only want to print that specific page and not all. So - two functions . Or maybe it could actually be just one with optional parameter call of what to print?

Thanks so much again for all the help.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to output all pages in one long page?

Post by cmb » Thu Aug 10, 2017 12:17 pm

Korvell wrote:Or maybe it could actually be just one with optional parameter call of what to print?
Okay, makes sense. I've also noticed an issue with XH 1.7 where changing XH_builtinTemplate() as described above would make login impossible. Furthermore, it doesn't make sense to show this "printlink" on special pages (such as ?sitemap). So I've reworked the code; the following has to be put into cmsimple/userfuncs.php:

Code: Select all

<?php

class XPrint
{
    private $pages;

    public function __construct()
    {
        $this->pages = new XH_Pages;
    }

    public function dispatch($all, $text)
    {
        global $s;

        if ($s < 0) {
            return;
        }
        if (isset($_GET['xprint'])) {
            $this->render($_GET['xprint'] === 'all');
        } else {
            return a($s, '&xprint=' . ($all ? 'all' : 'sub')) . $text . '</a>';
        }
    }

    private function render($all)
    {
        global $sl, $cf, $_XH_csrfProtection;

        while (ob_get_level()) {
            ob_end_clean();
        }
        if ($cf['xhtml']['endtags'] == 'true') {
            echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"',
                ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', "\n",
                '<html xmlns="http://www.w3.org/1999/xhtml"',
                (strlen($sl) == 2 ? " lang=\"$sl\" xml:lang=\"$sl\"" : ''), '>', "\n";
        } else {
            echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"',
                ' "http://www.w3.org/TR/html4/loose.dtd">', "\n", '<html',
                (strlen($sl) == 2 ? " lang=\"$sl\"" : ''), '>', "\n";
        }
        $content = XH_convertPrintUrls($this->renderContent($all));
        echo '<head>', "\n" . head(),
            tag('meta name="robots" content="noindex"'), "\n",
            '</head>', "\n", '<body class="', $bodyClass,'"', onload(), '>', "\n",
            $content, '</body>', "\n", '</html>', "\n";
        if (isset($_XH_csrfProtection)) {
            $_XH_csrfProtection->store();
        }
        exit;
    }

    private function renderContent($all)
    {
        global $print, $s;

        $html = '';
        $old_s = $s;
        if ($all) {
            foreach ($this->pages->toplevels() as $page) {
                if (!$this->pages->isHidden($page)) {
                    $html .= $this->renderPage($page);
                }
            }
        } else {
            $html .= $this->renderPage($s);
            foreach ($this->pages->children($s) as $page) {
                $html .= $this->renderPage($page);
            }
        }
        $s = $old_s;
        return $html;
    }

    private function renderPage($index)
    {
        global $c, $s;

        $s = $index;
        $html = XH_convertPrintUrls(evaluate_scripting($c[$index]));
        foreach ($this->pages->children($index) as $page) {
            $html .= $this->renderPage($page);
        }
        return $html;
    }
}

function xprint($all, $text)
{
    return (new XPrint)->dispatch($all, $text);
}
No modifications to cmsimple/functions.php have to be made! Then you can put one of the following template functions (or both) in the template:

Code: Select all

<?php echo xprint(true, 'print all pages')?>
<?php echo xprint(false, 'print page with subpages')?>
You can freely choose the text (second parameter), and even use HTML to insert a image, for example. Basically, xprint() works like printlink(). If you need that for multilingual sites, you can use one of the template texts in the language file, and call:

Code: Select all

<?php echo xprint(true, $tx['template']['text1']?>
Last edited by cmb on Fri Aug 11, 2017 9:55 am, edited 1 time in total.
Reason: Fix wrong example calls
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply