Embed G2 in custom PHP website?

geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Sun, 2004-11-28 22:37

First of all: thanks for an awesome product. I'm mindboggled by the amount of work put into this project, and even more so by the fact that it's all absolutely free. You are next on my "FOSS projects to donate to"-list.

Having just recently (like, today) begun following the developers' discussion on G2 (which, by the way is looking way beyond alpha-stage), I've not yet grasped the programming structure of G2. So pardon me, if the answer to this is obvious.

I've installed from CVS (8 hrs ago) and everything went without a hitch. I'm on Debian Sarge, Apache 1.3 and PHP 4.9. Now I'm looking to do what I did with Gallery 1.x [1], which is embed it into my own PHP-based website. It ought to be rather simple, since I have a generic header and footer that could be just wrapped around G2's code, but I'm confused about which approach is The Proper One (tm). I've looked at global.tpl.local but am not sure if Smarty will let me include PHP (seems to treat it somehow). Do I need to create my own theme? Or is there another template that I have to look at?

I realize that G2 has some CSS and other stuff that needs to be there, and most of global.tpl is obviously crucial, but how do I go about inserting my own <HEAD>-stuff with an include()-statement?

Thanks for any pointers you can give me.

G.

[1] See my post here and guide here

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sun, 2004-11-28 23:27

http://smarty.php.net/
I think you can use {php} to add raw php.. check the docs.

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Mon, 2004-11-29 00:50

Oh .. I suddenly see the force in utilizing Smarty for templating G2. Good job! :-)

Never fiddled with Smarty before, though, and after getting most of what I outlined above working, I still have one issue:
G2's default <HEAD> (in global.tpl) contains a Smarty-statement:

    {* Content that G2 wants to display inside the <head> element *}
    {include file="gallery:`$main.viewHeadFile`" l10Domain=$main.viewL10Domain}

which cannot be ignored. However, in order to get my own PHP header and it's contents snuck in before G2, I've replaced the entire <HEAD> in global.tpl.local with an include-statement to my own header. So the call-sequence is something like (G2 <- Smarty <- global.tpl.local <- include PHP header <- back to global.tpl.local).

According to the the Smarty docs, I have access to the Smarty-object thru the PHP variable $this in my header, but how can I make Smarty evaluate that one line and throw it into the cache along with the rest of the template? The Smarty docs don't seem to mention this.

My plan for my PHP header was something like:

<head>
  <some html here>
<?php
    if (isset($this)) { // we're using Smarty, so we're in G2
      $this->eval('{include file="gallery:`$main.viewHeadFile`" l10Domain=$main.viewL10Domain}');
    }
?>
</head>
<body>
  <more html>

Except that there's no such function in Smarty.

Does someone with more Smarty-knowledge than me (ie. 0+) have a clue on this?

Thanks,
G.

 
baschny
baschny's picture

Joined: 2003-01-04
Posts: 328
Posted: Tue, 2004-11-30 01:40

geddeth, I would use a more "standard" way of embeding G2 in your PHP application. Instead of using Smarty as a glue, you should use PHP and our embed.php script that is specifically designed for this kind of task.

This will make sure that the Gallery Framework is loaded (which includes a proper working Smarty) and you don't have to worry about (almost) anyhing.

Your PHP code is for example called "photo_album.php" and will look somehow like:

<?php
  // initialize the G2 framework
  require(dirname(__FILE__) . "/gallery2/embed.php");
  GalleryEmbed::init(array(
        'embedUri' => 'photo_album.php',
        'relativeG2Path' => 'gallery2/',
  ));
  list ($ret, $g2data) = GalleryEmbed::handleRequest();
  if ($g2data['isDone']) {
    exit; // G2 has already sent output (redirect or binary data)
  }
?>
<head>
  <some html here> 
  <?php echo $g2data['headHtml']; ?>
</head>
<body>
  <more html>
  <?php echo $g2data['bodyHtml']; ?>
  <more html>
</body>

You see, after calling handleRequest() you will have the headHtml and bodyHtml content in this array for your usage. Just place it where you want them to appear.

Anyway, this is documented in docs/EMBEDDING, you can read much more details there. Please note that the whole embeding API is still a work in progress and might change. Maybe you find other useful stuff you want added there so your input on that is also welcome.

Cheers,
Ernesto

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Mon, 2004-12-06 18:39

baschny, thanks a lot for that information. I'm sorry for the late reply, I somehow got unwatched for this topic.

Anyway, I'll have a look at it straight away and report what happens. I forgot to mention in my first post, that I had actually seen the EMBEDDING document, but found it kinda bloated for my needs (and hard to figure out, actually). But your approach is much more understandable, I'm really happy that you thought of this scenario as well, so it's so easy to embed.

I'll be in touch.

Thanks,
G.

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Mon, 2004-12-06 22:39

Okay, it's installed from latest CVS. It was quite easy to figure out, and the method is very flexible and fits nicely into my own code.

One thing, though - the $g2data['headHtml']-variable which fits into the HTML header contains a <TITLE>-tag. This seems out of place, since one will often have the title of one's website here. It would be more useful, if the $g2data-array contained a string with the Gallery name along with the albums (and sub-albums) you were currently in. That way, one could use it anywhere on the current page, even in the <TITLE>, if one preferred that (which I do). So it would be, e.g.

<HEAD><TITLE>MyWebSite :: <?php echo $g2data['naviString']; ?></TITLE></HEAD>
or something similar. Might need some config option for e.g. a path seperator.

Another thing is that I can't figure out how to implement the Gallery login procedure. I don't have seperate user logins, but I do have a few Gallery users (and admin, of course). It seems like I have to build the login form myself. Why not have an option that includes the default Gallery login-link in $g2data['bodyHtml'] for cases where one prefers not to synchronize Gallery users with the embedding application's?

Otherwise, kudos for the way the Gallery output is simply available to be used in this way - it can hardly be any simpler.

Thanks,
G.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2004-12-07 04:08

yup, we're aware of the title thing but haven't yet figured out a solution.
if you're not going to sync users then do you want to use the G2 login view? the embed class assumes the embedding app will take care of logins, so the G2 login is disabled by default, but you can turn it back on if you like.. you can see where it gets turned off in GalleryEmbed::init.. just do GalleryCapabilities::set('login', true) after your init call but before handleRequest..

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Wed, 2004-12-08 12:12

mindless: Thats exactly what I wanted, thanks. Apart from the <TITLE>-bit, I've achieved everything I wanted, and more.

Is the GalleryCapabilities documented anywhere (apart from source)?

G.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2004-12-08 16:01
Quote:
Is the GalleryCapabilities documented anywhere (apart from source)?

no, not yet.. eventually this should be in docs/EMBEDDING.
for the title thing, for now you can do some regular expressions on the returned header content to pull out the title..

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Wed, 2004-12-08 20:37

Yes, I did something to that effect for the time being. Thanks.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2004-12-08 23:10

We should definitely add some methods to embed.php like
getPageTitle()
getCSSLinks()
getJavascript()

even if they just contain some regexp to extract the strings from the head HTML and return it.
I mean, I have written the regexp, geddeth, walkah for drupal, etc. and they all do the same.
IMO the regexp I have isn't that bad, we could just move it to embed.php.

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Thu, 2004-12-09 20:44

Today's changes to embed.php (in CVS) seem to alter the way all this works. The EMBEDDING doc doesn't seem to reflect or, or else I'm not reading it right.

Now, it seems $g2data is just NULL. Here's the essentials of my code:

<?php

  // initialize the G2 framework
  require_once(dirname(__FILE__) . "/embed.php");
  $ret = GalleryEmbed::init(array(
                                  'embedUri' => 'index.php',
                                  'relativeG2Path' => './',
                                  ));
  if ($ret->isError()) {
    echo $ret->getAsHtml();
    exit;
  }

  GalleryCapabilities::set('login', true);

  list ($ret, $g2data) = GalleryEmbed::handleRequest();
  if ($g2data['isDone']) {
    exit; // G2 has already sent output (redirect or binary data)
  }

/** my stuff here **/

  echo $g2data['bodyHtml'];

?>

Can anyone explain?

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Fri, 2004-12-10 00:15

check the CMS Integration topic in this forum.. I posted all about it.
and docs/EMBEDDING is updated to match the changes.

(Edit: so if you see anything that seems wrong in the docs, let me know!)

 
geddeth
geddeth's picture

Joined: 2004-05-16
Posts: 52
Posted: Sat, 2004-12-11 21:03

Hmm .. odd. I didn't change anything in my code, but after updating from CVS, all is back to normal. Weird.