Print Link

About the template and stylesheet - and changing the menu
Post Reply
mitch
Posts: 45
Joined: Mon Oct 03, 2011 1:56 pm

Print Link

Post by mitch » Thu Sep 12, 2013 8:24 am

Hallo,

is it possible if you open a site as printversion (website.com/Unternehmen&print), and you click on a link, you stay in the print version? What I mean is thet the &print will be added automatically to all links in printversion.


Thanks in advance.

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

Re: Print Link

Post by cmb » Thu Sep 12, 2013 12:28 pm

Hi mitch,
mitch wrote:What I mean is thet the &print will be added automatically to all links in printversion.
Basically that is possible--however, adding &print to URLs that are not links to CMSimple pages won't make sense, and may be even wrong. If the latter is not of concern, the solution is rather simple. Just add the following function to cmsimple/userfuncs.php:

Code: Select all

function makePrintLinks($content)
{
    return preg_replace('/<a([^>]+)href="([^"]*)"/', '<a$1href="$2&print"', $content);
}
In cmsimple/cms.php change line 420 (the line number varies between different versions) from:

Code: Select all

    content(), '</body>' . "\n" . '</html>' . "\n";
to:

Code: Select all

    makePrintLinks(content()), '</body>' . "\n" . '</html>' . "\n";
Christoph
Christoph M. Becker – Plugins for CMSimple_XH

mitch
Posts: 45
Joined: Mon Oct 03, 2011 1:56 pm

Re: Print Link

Post by mitch » Fri Sep 13, 2013 7:55 am

cmb wrote:Hi mitch,
mitch wrote:What I mean is thet the &print will be added automatically to all links in printversion.
Basically that is possible--however, adding &print to URLs that are not links to CMSimple pages won't make sense, and may be even wrong. If the latter is not of concern, the solution is rather simple. Just add the following function to cmsimple/userfuncs.php:

Code: Select all

function makePrintLinks($content)
{
    return preg_replace('/<a([^>]+)href="([^"]*)"/', '<a$1href="$2&print"', $content);
} 
In cmsimple/cms.php change line 420 (the line number varies between different versions) from:

Code: Select all

    content(), '</body>' . "\n" . '</html>' . "\n"; 
to:

Code: Select all

    makePrintLinks(content()), '</body>' . "\n" . '</html>' . "\n"; 
Christoph
Thanks a lot for your efficient answer. Can some pages be defined to be excluded?

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

Re: Print Link

Post by cmb » Fri Sep 13, 2013 5:56 pm

mitch wrote:Can some pages be defined to be excluded?
Well, you can split function makePrintLinks() in two parts and treat links to these pages differently:

Code: Select all

function makePrintLink($matches)
{
    $url = $matches[2];
    if (strpos($url, 'http') === 0) {
        // let "external" links alone
    } elseif ($url == '?No_print_view') {
        // let certain links alone
    } else {
        // append "&print" to all others
        $url = $url . '&print';
    }
    return '<a' . $matches[1] . 'href="' . $url . '"';
}

function makePrintLinks($content)
{
    return preg_replace_callback('/<a([^>]+)href="([^"]*)"/', 'makePrintLink', $content);
}
Christoph M. Becker – Plugins for CMSimple_XH

mitch
Posts: 45
Joined: Mon Oct 03, 2011 1:56 pm

Re: Print Link

Post by mitch » Mon Sep 16, 2013 7:38 am

Thaks a lot exactly what I'm searching. I will try that...

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

Re: Print Link

Post by cmb » Mon Sep 16, 2013 9:32 pm

I have suggested to implement a general solution for CMSimple_XH 1.6.
Christoph M. Becker – Plugins for CMSimple_XH

Post Reply