API question

stephen

Joined: 2002-11-20
Posts: 17
Posted: Tue, 2008-04-29 00:26

Hi,

Can someone tell me what does the opposite of this?

http://gallery.menalto.com/apidoc/GalleryCore/Classes/GalleryCoreApi.html#methodfetchItemIdByPath

I mean, given ItemID, returns the path to the item? I can't seem to find it... maybe I'm just blind. Thanks!

 
jettyrat
jettyrat's picture

Joined: 2005-12-30
Posts: 32
Posted: Wed, 2008-04-30 19:02

In the phpbb3 integration I have the ability to use a Gallery image as a forum avatar. In order for phpbb to convert the image to an avatar it must know where the source image is. Don't know if there is a better way to do this, but here is an excerpt of what I use and I think will do what you want.

$path = '';
$first_pass = true;

do
{
	list ($error, $items) = GalleryCoreApi::loadEntitiesById(array($itemId));

	$path_component = $items[0]->getPathComponent();
	$itemId = $items[0]->getParentId();

	if (!empty($path_component))
	{
		if ($first_pass)
		{
			$path = $path_component;
			$first_pass = false;
		}
		else
		{
			$path = $path_component . DIRECTORY_SEPARATOR . $path;
		}
	}
}
while (!empty($path_component));

$source = $gallery->getConfig('data.gallery.base') . 'albums' . DIRECTORY_SEPARATOR . $path;
 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Wed, 2008-04-30 19:16

GalleryItem is a subclass of GalleryFileSystemEntity which has a method fetchPath():

http://gallery.menalto.com/apidoc/GalleryCore/Classes/GalleryFileSystemEntity.html#methodfetchPath

So you should probably do something like


list ($ret, $myItemArray) = GalleryCoreApi::loadEntitiesById($myId,'GalleryItem');
if ($ret) {
   ....
}
list ($ret, $myItemPath) = $myItemArray[0]->fetchPath();
if ($ret) {
   ....
}

 
jettyrat
jettyrat's picture

Joined: 2005-12-30
Posts: 32
Posted: Wed, 2008-04-30 22:17

Nice...thanks Alec!

Only thing I might add is that you get an array back and the path is contained in the second element, so the actual path would be in $myItemPath[1] in your example.

Thanks again...much better way to do it.

 
stephen

Joined: 2002-11-20
Posts: 17
Posted: Wed, 2008-04-30 22:57

Thanks for the ideas. I haven't actually written my code yet, so I can't test to see if this will work. I asked the question to see if what I wanted to do was possible. So, for the mean time, thanks!

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Thu, 2008-05-01 13:09
Quote:
Only thing I might add is that you get an array back and the path is contained in the second element, so the actual path would be in $myItemPath[1] in your example.

I don't think so. According to the docs (and also according to the code itself) the function returns

an array of (GalleryStatus status, string a path)

In my code the status is $ret, and $myItemPath is the string part. No requirement for further array dereferencing, and $myItemPath[0] would be incorrect.

Stephen: if you use this code then you should be aware (the docs make it clear) that path is only guaranteed to remain valid if the relevant locks are held.

 
jettyrat
jettyrat's picture

Joined: 2005-12-30
Posts: 32
Posted: Thu, 2008-05-01 14:29

Hmm, I read the docs yesterday and was expecting a string, but was returned an array instead with element [0] empty and element [1] containing the path. I test it again today and am returned a string just as the docs say. Guess I did something wrong before :-/

 
stephen

Joined: 2002-11-20
Posts: 17
Posted: Thu, 2008-05-01 15:27

I should be more explicit I think...

Eventually, I would like to run my script from the command line, i.e. 'php myscript.php', where it accepts the itemID for a media file (jpg, gif, etc..) as STDIN, and outputs the full system file path as STDOUT (or at least a path I can easily convert to a full path).

I can make a php file that uses require_once('./embed.php') etc... like in numerous embedding examples, and I get my gallery page just fine. I'm a bit confused as to where to put alecmyers code in. I can browse my gallery (without rewrites on) and navigate to a photo page, and then inspect the main image on that page. In the url I have 'g2_itemId=39' so I assume that image's ID is 39. If I put something like this in my file:

list ($ret, $myItemArray) = GalleryCoreApi::loadEntitiesById(39,'GalleryItem');
if ($ret) {
    echo 'Bollocks';
    exit;
}
list ($ret, $myItemPath) = $myItemArray[0]->fetchPath();
if ($ret) {
    echo 'Double Bollocks';
    exit;
}

I get 'Bollocks' back if I run through the web. I've tried putting it before and after the ::handleRequest, and there's no difference. If I run from the command line, I get nothing.

So I guess I have two questions now:
- What am I doing wrong? Is it not as simple as the code snippet you gave me?
- If I get this working through the web, how will I get it to work on the command line? Will I have to resort to using curl http://localhost/...?

Thanks for any additional help you can give me.

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Thu, 2008-05-01 15:38

"Bollocks" ... love it.

use

print $ret->getAsHtml();

instead of the bollocks lines - that'll be less fun, but more informative.

As for getting it to run from the command line - I'm not sure exactly. But one difficulty you may face is that you don't have any of the cookie/session information set up for you - Gallery code is quite tight on that kind of thing for security reasons - any you may fail on that score.

 
stephen

Joined: 2002-11-20
Posts: 17
Posted: Thu, 2008-05-01 20:12

alecmyers,

here's what I get from the code in my previous post using getAsHtml() :

Error (ERROR_MISSING_OBJECT) : Entity with id [39] is a [GalleryDerivativeImage] and does not extend the required entity type [GalleryItem].

    * in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 129 (GalleryCoreApi::error)
    * in modules/core/classes/GalleryCoreApi.class at line 2347 (GalleryEntityHelper_simple::loadEntitiesById)
    * in cli.php at line 23 (GalleryCoreApi::loadEntitiesById)

So I put 'GalleryDerivativeImage' in place of 'GalleryItem' into loadEntitiesById, and then I get this error (the (...) is an edit by me):

Fatal error: Cannot use object of type GalleryDerivativeImage as array in (...)/cli.php on line 30

where line 30 is list ($ret, $myItemPath) = $myItemArray[0]->fetchPath();.

I then changed line 30 to list ($ret, $myItemPath) = $myItemArray->fetchPath();, and now it works! So in summary, this is a working code snippet, where of course for general items 39 gets replaced by $myID or whatever:

list ($ret, $myItemArray) = GalleryCoreApi::loadEntitiesById(39,'GalleryDerivativeImage');
if ($ret) {
    print $ret->getAsHtml();
    exit;
}
list ($ret, $myItemPath) = $myItemArray->fetchPath();
if ($ret) {
    print $ret->getAsHtml();
    exit;
}

echo $myItemPath;

So with respect to the command line stuff, you're probably right that it's a cookie issue. I thought I saw something somewhere about running embedded gallery2 without cookies, but it might be simpler to just accept I'll have to use curl, which I can make work, but it doesn't feel as elegant.

Thanks for help!

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Thu, 2008-05-01 20:43

Great that you got it working! Once comment though:

Quote:
I put 'GalleryDerivativeImage' in place of 'GalleryItem' into loadEntitiesById

I think that will fail if you try to get the path of a GalleryImage (i.e. an original image, not a derivative) - should you ever require to do so. If you want to cover all bases, use 'GalleryEntity', which I think is the base class for all such things.