Details on the Gallery2/Textpattern Integration

11 November 2009, 00:14

I received a note today asking about how I performed by Gallery2 integration. I spent a few more hours than I would’ve like getting Gallery2 integrated with Textpattern, so maybe I can save you some time. Below is hopefully a useful reference (i.e., some code) to get you started on your integration. As I mentioned earlier, my learning process was guided by the posts here: http://gallery.menalto.com/node/33716

Basically, there are a few PHP blocks of code in the template file. The first PHP block sets all of the configuration parameters for G2. After the page HTML starts, the second block spits out the headHtml headers (invisible G2 overhead includes like CSS and JS). The third block generates the actual visible page content (bodyHtml). You’ll probably want to create a custom template derived from ‘classic’ (see /usr/share/gallery2/themes & read about ‘Editing and Creating Themes’ at http://codex.gallery2.org/Gallery2:Themes) to integrate the look and feel of the G2 output with your site. In the code, you can also see how I include my navigational header.

The TXP template code:

<txp:php>
$g2_Config['path'] = '/usr/share/gallery2' . '/';
$g2_Config['g2Uri'] = '/gallery/';
$g2_Config['embedUri'] = '/photos/index.php?';

require_once( $g2_Config['path'] . 'embed.php');

$ret = GalleryEmbed::init(
	array(	'g2Uri' => $g2_Config['g2Uri'],
		'embedUri' => $g2_Config['embedUri'],
		'fullInit' =>'true')
);

if ($ret) {
	echo( 'Content-type: text/html'."\n\n");
	echo( 'Here is the error message from G2:' );
	echo( $ret->getAsText() );
	return false;
}

GalleryCapabilities::set('login', true);
global $g2moddata;
$g2moddata = GalleryEmbed::handleRequest();

if (!isset($g2moddata['isDone'])) {
	echo( 'Content-type: text/html'."\n\n");
	echo ('isDone is not defined, something very bad must have happened.');
	exit;
}
if ($g2moddata['isDone']) {
	exit; /* uploads module does this too */
	// Gallery 2 has already sent output (redirect or binary data)
}
GalleryEmbed::done();
</txp:php>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<link rel="home" href="<txp:site_url />" />

	<txp:css format="link" />

<txp:php>
global $g2moddata;
if (isset($g2moddata['headHtml'])) {
	list($title, $css, $javascript) =
	  GalleryEmbed::parseHead($g2moddata['headHtml']);

	echo "\t<title><txp:page_title /> - " . $title . "</title>\n";
	foreach ($css as $item) { echo("\t".$item."\n"); }
	foreach ($javascript as $item) { echo("\t".$item."\n"); }
}
</txp:php>
</head>
<body id="<txp:if_section name="default">front<txp:else /><txp:section
/></txp:if_section>">

<txp:output_form form="topnav_grass" />
<txp:php>
global $g2moddata;
if (isset($g2moddata['bodyHtml'])) {
	echo $g2moddata['bodyHtml'];
} else {
	echo( 'Gallery2 produced no output.');
}
</txp:php>

</body>
</html>

Also, I remember having difficulty getting the URL rewriting (using mod_rewrite) to work correctly; I may have hand-edited the .htaccess file in the document root, but can’t recall. This is how a snippet of that file reads today. The key is to understand the two RewriteRule statements mentioned in the comment at the top for index.php (I stick in comments like that to prevent code generators from blowing away things that I need to remember later). We basically just need to catch all of the requests to special gallery paths (like /v/ and /d/) and redirect them to a Textpattern link with G2 arguments (the s= tells TXP which section to render and everything else is for G2).

Note that your Textpattern “gallery” URL path must be different from your direct-access Gallery2 path (i.e., if G2 direct is www.domain.net/gallery, then the Textpattern-accessed G2 must be different). I chose to call my Textpattern-accessed G2 section “Photos” (accessible at www.domain.net/photos). From a change management perspective, this is probably the best I can do — preserve people’s access using old links through /gallery, and make the new, standard URL a more intuitively descriptive name.

# rewrite to index.php (for TXP), call the section, and then pass in the G2
variables
# RewriteRule .
/index.php?s=photos&g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3
  [QSA,L]
# RewriteRule .   /index.php?s=photos&g2_path=%1   [QSA,L]

# BEGIN Url Rewrite section
# (Automatically generated.  Do not edit this section)
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} gallery\_remote2\.php
    RewriteCond %{REQUEST_URI} !/index\.php$
    RewriteRule .   -   [L]

    RewriteCond %{THE_REQUEST} /d/([0-9]+)-([0-9]+)/([^/?]+)(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/index\.php$
    RewriteRule . /index.php?s=photos&g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3   [QSA,L]
    RewriteCond %{THE_REQUEST} /v/([^?]+)(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/index\.php$
    RewriteRule . /index.php?s=photos&g2_view=core.ShowItem&g2_path=%1   [QSA,L]
</IfModule>

# END Url Rewrite section

Good luck.

Chris Horn

,

---

Commenting is closed for this article.

---