Integration of Gallery2 in a Stand-Alone-Import-Program (ERROR_MISSING_OBJECT) GalleryUser

kloepfer

Joined: 2007-08-09
Posts: 5
Posted: Thu, 2007-08-09 16:08

Hello.

My task is the import of Album- and Picture-data into an existing Gallary2-System.
This Gallery2-System is runnig as a Slave being controlled by Joomla and the existing bridge to Gallery2.

I have to migrate about 5500 Pictures residing in about 500 albums (including description, title, and keywords for both the pictures and the albums) from about 40 users from an external database to this embeded Gallery2-System.

For the external database there was no documentation, but the structure was simple so writing a programm that runs through the data of each user, checking the albums and getting the pictures was (more or less) a straight forward job. So what I have now is a program that runs through my 'Source'-Database and has two entry-Points: One entry point when the Album changes, and One entry for each Picture.

My first aproach was to reverse engineer the gallery2 data-structure of the MySql database and writing directly to the database. (bad idea, as I know now). I ended up having to fill 16 tables, spend about 30 hours, succeeded for the Albums but failed for the Pictures.

So now I am having a try with the API:

GalleryCoreApi::addItemToAlbum(
  $fileName, $itemName, $title, $summary, $description, $mimeType, $albumId)
GalleryCoreApi::createAlbum(
  $parentAlbumId, $name, $title, $summary, $description, $keywords)

These functions sound like they are exactly what I need.
I have all the nesessary variables for the function-calls.

My Problem ist, that I do not quite understand why I get an error message when I try to switch the user from the admin user to the user, with the user-id in the g2_User table.
The user is a working user in gallery2 and I can do everything if I use the webfrontend.
The error Message I get is:

Error (ERROR_MISSING_OBJECT) : 2147 GalleryUser

    * in modules/core/classes/helpers/GalleryEntityHelper_simple.class at line 124 (gallerycoreapi::error)
    * in modules/core/classes/GalleryCoreApi.class at line 2290 (galleryentityhelper_simple::loadentitybyexternalid)
    * in modules/core/classes/GalleryEmbed.class at line 215 (gallerycoreapi::loadentitybyexternalid)
    * in modules/core/classes/GalleryEmbed.class at line 120 (galleryembed::checkactiveuser)
    * in /usr/www/users/photop2/tkl_import/wrapper_main.php at line 56 (galleryembed::init)
    * in /usr/www/users/photop2/tkl_import/wrapper_main.php at line 18

What can I do?
What can I check?

Thomas

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2007-08-10 12:09

are you maybe confusing externalIds with entityIds?

Most GalleryEmbed:: methods expect an externalId. A G2 userId is a G2 entityId. Each user has 2 ids when you're embedding G2 in your own application. The id of the user in your own application (the externalId) and the id of the user in G2's database (the entityId). If you're using GalleryEmbed::init or GalleryEmbed::checkActiveUser etc, then you need to provide the externalId, not the entityId.

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
kloepfer

Joined: 2007-08-09
Posts: 5
Posted: Fri, 2007-08-10 14:06

Hi,

upps - you are right!

I didn't use the external Id. This was my mistake.
I knew about the two id's but I didn't know GalleryEmbed
needed the external id.

Thanks a lot.

Thomas

 
kloepfer

Joined: 2007-08-09
Posts: 5
Posted: Sat, 2007-08-11 09:42

Now I ran into another problem while tying to add an album
one level below the user album. The steps I take are:
- lookup the g2 id
- lookup the g2 useralbum itemId
- check that this user has permissions on this album

=> error! no permissions! why?

I use:

GalleryCoreApi::loadEntityByExternalId($user_id_cms, 'GalleryUser'); // gives me $user
GalleryCoreApi::getPluginParameter('module', 'useralbum', 'albumId', $user->getId()) // -> gives me $itemId=2148
GalleryCoreApi::assertHasItemPermission($itemId, 'core.addAlbumItem')

The error messeage I get is:

GalleryCoreApi::assertHasItemPermission: (2148)
Error (ERROR_PERMISSION_DENIED) : user id: 2147 doesn't have permission: core.addAlbumItem for item id

If I continue creating the album anyway with:

GalleryCoreApi::acquireReadLock($itemId)
GalleryCoreApi::createAlbum($albumId, 'Album_test', 'Album Title', 'Album Summery', 'Album Description', 'Album Keywords')
GalleryCoreApi::releaseLocks($lockIds)

The folder for the album gets created at the right spot in the servers fileystem, but I do not see any new album in the webfrontend.
Eaven when I am logged in with the user-id I used in the creation process.

Do I have to use a admin user to creat the Album an change the permissions afterwards?
Do I have to do some kind of "transaction-complete"-command so that the Album gets displayed?

 
kloepfer

Joined: 2007-08-09
Posts: 5
Posted: Sat, 2007-08-11 13:57

Found it by myself (seaching the forum http://gallery.menalto.com/node/66951 )
The funiction-call to tell gallery to realy execute the commands and write to the database was missing.
With this function-call:

GalleryEmbed::done()

I can see the album from the webfrontend.

I still don't understand why the user has no rights to his own "root"-Album (see) last post.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2007-08-11 17:27
if ( $_G2B_CONF['user_albums'])// is my cms allowing useralbums?
	{
		G2B_G2_init(); // my cms init function
		//check to see if useralbum module is available
		list ($ret, $modules) = GalleryCoreApi::fetchPluginStatus('module');
		if ($ret) 
		{
			print "checking module:" . $ret->getAsHtml();
		}
		if($modules['useralbum']['active'] && $modules['useralbum']['available']){
			$username = $_USER['username']; //my cms user
			list ($ret, $user) = GalleryCoreApi::fetchUserByUserName($username);
    			if ( !$ret)
    			{
               			 list ($ret, $albumId) = GalleryCoreApi::getPluginParameter('module', 'useralbum', 'albumId', $user->getId());
					if($albumId){
                					return array( 'My Gallery', $_CONF['site_url'].'/'.$_G2B_CONF['public_dir'].'/index.php?g2_itemId='.$albumId, 0 );// return my cms formatted menu item
					}else{
						global $gallery;
						$urlGenerator =& $gallery->getUrlGenerator();
						$theUrl = $urlGenerator->generateUrl(
						array('controller' => 'useralbum.UserAlbum'),
						array('forceFullUrl' => true, 'forceSessionId' => true, 'htmlEntities' => false));
						return array( 'My Gallery', $theUrl , 0 ); // return my cms formatted menu item
					}
           		}
		}

	}

Gallery will automatically create the useralbum using the controller.

-s
FlashYourWeb and Your Gallery

 
kloepfer

Joined: 2007-08-09
Posts: 5
Posted: Sat, 2007-08-11 18:52

Generating the 'Root' User-Albun is not my task.
I know gallary (or the joomla bridge to gallery) is doing this automatically.
But I want to import a bunch of albums with photos per user from a external database.
Here I have to do the import especially if I want the picture, title, description, summary and keywords to be importet as well.

But thanks for the code. I'll have a look at it so this will help me developping myown code.