Changing Album Thumbnail

ralphch

Joined: 2005-06-18
Posts: 27
Posted: Sun, 2005-06-19 21:28

Hi,

I would like to have G2 automatically detect if there's a filename named '_category_image.jpg' in an album folder, and if so, use that to show as the album's thumbnail.

Is this possible? If so, where in the code should I look to make this modification?

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sun, 2005-06-19 21:39

you can change the album thumbnail manually. Isn't that enough?

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Sun, 2005-06-19 22:29

Unfortunately not, because each of my albums already has a _category_image.jpg and I need to be able to set up albums in the fastest way possible, since many are batch uploaded every day. Any help will be really appreciated. Thanks, Ralph.

 
nivekiam
nivekiam's picture

Joined: 2002-12-10
Posts: 16504
Posted: Sun, 2005-06-19 22:43

Well it would be kind of a cool to have the option of selecting an image of a certain name to be a predetermined thumbnail. Of course an option would also need to be added to be able to select what that name is because I'd prefer to use something like _album_thumbnail.jpg. Then there is all of the non-english speaking people who would probably prefer to use their native language to name files.

The way I see it you have 2 options, code it yourself and submit a patch or submit a RFE on sourceforge, http://sourceforge.net/tracker/?atid=357130&group_id=7130&func=browse

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Sun, 2005-06-19 22:52

Thanks for the reply. I'll look into submitting the request at SourceForge. However, I would also be interested in coding this myself, but can't seem to find exactly what script/s I should edit. Any guidance will be appreciated. Thanks, Ralph.

 
nivekiam
nivekiam's picture

Joined: 2002-12-10
Posts: 16504
Posted: Sun, 2005-06-19 22:58

I'm sure a dev will correct me if I'm wrong, but something like this may be better suited to be a standalone module, much like square thumbnails. This way someone can choose to have it enabled or not. This way is someone doesn't want it they don't have to install the module and take what ever minor performance hit it would be for gallery to do this extra check.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Sun, 2005-06-19 23:13

Ok, I would be interested in writing a module for this, as well as for other features in the future. Although I know PHP, I'm finding it quite hard to understand the overall functioning of the system and how I should approach this. Some guidance might help me get on the right path. Thanks, Ralph.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Mon, 2005-06-20 15:45

Hi, I'm trying to do this customization but don't know where to start. I've looked at modules/core/DownloadItem.inc but this doesn't seem to change anything when I make modifications. Could you please guide me as to where I should be looking and what I should be modifying? Thanks, Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-06-20 17:32

ralphch, first i'd write down the user story.
how is it supposed to work?
1. bob creates a new album A
2. bob adds an item called _category_image.jpg to A
3. G2 detects that it should use A as the album highlight for A

correct?

So what you need is a G2 module that listens for GalleryEntity::save events and checks whether the entity's storage flag is newly created and check what type of Entity it is (you want GalleryPhotoItem or something like that) and if it's name is the same is the name for default album highlights.
If all these if conditions apply in your handleEvent function, then you get the parent item (the album) of the newly created item and assign the new item as the highlight of the parent.

you can look at other modules (module.inc) on how to listen for events and how to handle them.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Mon, 2005-06-20 18:01

valiant thanks for the reply! But wouldn't that be setting _category_image.jpg as an item in my album as well? I just want _category_image.jpg to show as that album's thumbnail but not within the album as an item.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Mon, 2005-06-20 21:16

Here's how I've started the functionality section of my module.inc file:

/**
* @see GalleryEntity_core::onSave
*/

function onSave() {

list($ret, $itemtype) = GalleryEntity::getentityType();
if ($ret->isError()) {
return array($ret->wrap(__FILE__, __LINE__), null);
}

}

Is this correct? I'm quite lost since I've read the documentation but it's incomplete and I can't seem to find any information on how to use each of these objects.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-06-20 21:49

ralphch, no, i don't think you wanna do it with $entity->onSave().

$entity->onSave(); is called when an entity is stored in the database.
But that would mean that you have to write / implement your own GalleryPhotoItem class and in this class, you could define your onSave method.

What I meant in my previous post was to listen for GalleryEntity::save events.

See modules/imageblock/module.inc on how to add function registerEventListeners in your module.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Mon, 2005-06-20 23:05

Thanks for the reply. Ok, so here's what I'm doing:

In module.inc

/**
* @see GalleryModule::registerEventListeners()
*/

function registerEventListeners() {

GalleryCoreApi::requireOnce(dirname(__FILE__) . '/classes/SetAlbumThumbnail.class');
$listener = new SetAlbumThumbnail();
GalleryCoreApi::registerEventListener('GalleryEntity::save',$listener,true);

}

And then in SetAlbumThumbnail.class:

class SetAlbumThumbnail /* extends GalleryEventListener */ {

/**
* @see GalleryEventListener::handleEvent
*/

function handleEvent($event) {

switch($event->getEventName()) {
case 'GalleryEntity::save':

// do all the stuff.

}

}


}

However, I don't understand where and how I can check to see if the filaneme is '_category_image.jpg' and, if so, upload it to the album's folder and set it as the album's thumbnail (shouldn't this be written to a mysql table or something?).

Also, what would happen if the user uploads the _category_image.jpg file through FTP? Then none of this would work. So can't I implement a module that adds functionality to the front-end display of thumbnails? Such that before displaying a thumbnail, it checks if the item is an album and if so, looks for '_category_image.jpg'. Otherwise, it defaults to normal functionality.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Mon, 2005-06-20 23:44

the $event contains all the informat, it contains the $enity object so you can ask it what it is (see modules/core/classes/interfaces/GalleryEntity...)

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 00:29

Here's the code, I followed the API documentation, but nothing works.

module.inc

<?php
/*
* $RCSfile: module.inc,v $
*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2005 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
USA.
*/
/**
* @package albumthumb
* @version $Revision: 1.63 $ $Date: 2005/06/20 21:00:00 $
* @author Ralph
*/

/**
* Album Thumbnail module
*
* @package albumthumb
*/
class AlbumThumbModule extends GalleryModule {

    function AlbumThumbModule() {
    global $gallery;

    $this->setId('albumthumb');
    $this->setName('Album Thumbnail');
    $this->setDescription($gallery->i18n('Detects the thumbnail for an album'));
    $this->setVersion('0.9.0');
    $this->setGroup('data', $this->translate('Extra Data'));
    $this->setCallbacks('');
    $this->setRequiredCoreApi(array(5, 8));
    $this->setRequiredModuleApi(array(0, 9));
    }

    /**
     * @see GalleryModule::autoConfigure
     */
    function autoConfigure() {
    /* We don't require any special configuration */
    return array(GalleryStatus::success(), true);
    }

/**
* @see GalleryModule::registerEventListeners()
*/

function registerEventListeners() {

GalleryCoreApi::requireOnce(dirname(__FILE__) . '/classes/SetAlbumThumbnail.class');
$listener = new SetAlbumThumbnail();
GalleryCoreApi::registerEventListener('GalleryEntity::save',$listener,true);

}

}

?>

SetAlbumThumbnail.class

<?php

/*
 * $RCSfile: SetAlbumThumbnail.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 * @version $Revision: 1.36 $ $Date: 2005/06/20 21:00:00 $
 * @package albumthumb
 * @author Ralph
 */

/**
 * A helper class for the AlbumThumb module.
 *
 * @package albumthumb
 * @subpackage Classes
 */

class SetAlbumThumbnail /* extends GalleryEventListener */ {

/**
* @see GalleryEventListener::handleEvent
*/

function handleEvent($event) {

switch($event->getEventName()) {
case 'GalleryEntity::save':
if($event->_pathComponent == '_category_image.jpg') { $event->setserialNumber(4); }
}

}

}

?>

Am I missing something here? Any help will be appreciated.

Thanks,
Ralph[/code]

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-06-21 05:40

if something does work, try to debug it...
either echo stuff and then exit;
or add $gallery->debug($debugMsg . " blabla, bla."); to get debug messages in debug mode.

and please see:
http://gallery.sourceforge.net/wiki.php?page=Development%20Documents
for G2 -> coding style / standards

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 10:59

I just added echo $gallery->debug($debugMsg) but nothing shows up. Is this code correct or am I doing it all incorrectly (this is what it seems like)?

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-06-21 11:13

this was just an example...
you gotta assign some text to $debugMsg of course...

you can learn a lot by just looking at other code in G2.

this debug stuff is just the G2 way to debug, instead of print / echo

global $gallery;
$gallery->debug('reached line so and so and and var x as value ' . $x);

of course you need then to activate buffered debug mode in config.php

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 11:34

Ok thanks, I just enabled the Debug module and added the following to my eventHandle($event) function in SetAlbumThumbnail.class

global $gallery;
$gallery->debug('The filename is' . $event->_pathComponent);

Whenever I upload a file using the Add Item interface on the front-end, this debug message doesn't show up anywhere on the page. My best guess is that I'm going wrong about this whole thing and the handleEvent function isn't even being called whenever I upload a new item.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-06-21 11:45

this has nothing to do with the debug module.
i'm talking about config.php, the file. open it and change the debug mode by changing the text in the appropriate line.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 17:21

I set config.php to debug mode ($gallery->setDebug('buffered')) and it's still not showing the debug msg I'm sending from SetAlbumThumbnail.class. So this module is clearly not working, not even being called whenever a new item is added. Any ideas?

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-06-21 17:47

ralphch, have you installed + activated the module in site admin .> modules?
the first step is get it just install / uninstall, activate.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 18:24

Yeah, I installed and activated it, but it doesn't work.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-06-21 18:31

well, then use immediate debug mode. in immediate debug mode, you should see the debug messages if they exist. in some cases, you see debug messages in immediate but not in buffered debug mode.

if you don't get your debug messages, then the code line is never reached.
part of debugging is then finding out why it doesn't do what you expect, so you'll have to add debug messages in other places.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 19:14

valiant thanks for the help. Even when debugging there's no indication that the module is being loaded when an item is saved. Therefore, I recoded the whole thing into the following:

module.inc

class AlbumThumbModule extends GalleryModule {

    function AlbumThumbModule() {
    global $gallery;

    $this->setId('albumthumb');
    $this->setName('Album Thumbnail');
    $this->setDescription($gallery->i18n('Detects the thumbnail for an album'));
    $this->setVersion('0.9.0');
    $this->setGroup('data', $this->translate('Extra Data'));
    $this->setCallbacks('');
    $this->setRequiredCoreApi(array(5, 8));
    $this->setRequiredModuleApi(array(0, 9));
    }

/*
* @see GalleryModule::performFactoryRegistrations()
*/

function performFactoryRegistrations() {

	$ret = GalleryCoreApi::registerFactoryImplementation('ItemAddOption',
	    'SetAlbumThumbnail', 'SetAlbumThumbnail',
	    'modules/albumthumb/SetAlbumThumbnail.inc', 'albumthumb', null);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	return GalleryStatus::success();

}

    /**
     * @see GalleryModule::autoConfigure
     */
    function autoConfigure() {
    /* We don't require any special configuration */
    return array(GalleryStatus::success(), true);
    }

}

SetAlbumThumbnail.inc

class SetAlbumThumbnail extends ItemAddOption {

/*
* @see ItemAddOption::isAppropriate
*/

function isAppropriate() {
return array(GalleryStatus::success(), true);
}

/*
* @see ItemAddOption::handleRequestAfterAdd
*/

function handleRequestAfterAdd($form, $items) {
global $gallery;

$errors = array();
$warnings = array();

$itemIds = array();

foreach ($items as $item) {
$itemIds[] = $item->getId();
}

list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($itemIds);
if ($ret->isError()) {
return array($ret->wrap(__FILE__, __LINE__), null, null);
}

for ($i = 0; $i < count($items); $i++) {

$warnings[$i] = array();
$itemId = $items[$i]->getId();

$mustSave = false;
$items[$i]->setSummary('Test summary'); // REPLACE THIS FOR DETECT ALBUM THUMBNAIL CODE AND SET AS HIGHLIGHT
$mustSave = true;

$gallery->debug('The items filename is ' . $items[$i]->getFilename());

	    if ($mustSave) {
		$ret = $items[$i]->save();
		if ($ret->isError()) {
		    GalleryCoreApi::releaseLocks($lockId);
		    return array($ret->wrap(__FILE__, __LINE__), null, null);
		}
	}

}

$ret = GalleryCoreApi::releaseLocks($lockId);
if($ret->isError()) {
return array($ret->wrap(__FILE__,__LINE__), null, null);
}

return array(GalleryStatus::success(), $errors, $warnings);

}

}

Is this approach correct? Now the module is loaded whenever I add a new item but whenever saving a new item the following error displays:

Your change cannot be completed because somebody else has made a conflicting change to the same item. Use the back button in your browser to go back to the page you were on, then reload that page and try your change again.
Go back and try again

Alternatively, you can return to the main Gallery page and resume browsing.

Back to the Gallery
Error Detail -
Error (ERROR_OBSOLETE_DATA) : UPDATE g2_Entity SET g_modificationTimestamp=?,g_serialNumber=? WHERE g_id=? AND g_serialNumber=? (1119388590|2|459|1)

    * in modules/core/classes/GalleryStorage/DatabaseStorage.class at line 586 (gallerystatus::error)
    * in modules/core/classes/GalleryStorage.class at line 133 (mysqldatabasestorage::saveentity)
    * in modules/core/classes/GalleryEntity.class at line 293 (gallerystorage::saveentity)
    * in modules/core/classes/GalleryItem.class at line 409 (galleryfilesystementity::save)
    * in modules/albumthumb/SetAlbumThumbnail.inc at line 81 (galleryphotoitem::save)
    * in modules/core/ItemAdd.inc at line 142 (setalbumthumbnail::handlerequestafteradd)
    * in main.php at line 182 (itemaddcontroller::handlerequest)
    * in main.php at line 86
    * in main.php at line 77

And the summary doesn't get set to the new item. Ideally, this should be setting the 'Test summary' to all new items, right? I'm just doing this to test the functionality of the module, but would later like to change that so that it checks the item's filename, and if it matches '_category_thumbnail.jpg', then sets it as the parent's (album's) highlight. Any ideas will be appreciated.

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2005-06-21 22:06

the problem with the ItemAddOptions is that not all ItemAddOptions are executed when adding items with GalleryRemote or the upload applet. The ItemAddOptions that need form vars, e.g. a checkbox, only work in the other Item add methods.

but yes, this is a good alternative to GalleryEntity::save events.

$mustSave: better use $item->isModified() but $item->save() does this check by itself. so it's safe to call $item->save();

@code:
don't know if that's correct. i'd have to compare it to the other itemadd options. i always copy as much code as possible to minimize typos etc.

@error:
maybe you need to call $item->refresh();
before items[$i]->setSummary(..);

and i'd rename $lockId to $lockIds because it's an array of lock IDs.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Tue, 2005-06-21 22:34

Thanks for the reply. I've checked the code for typos, but there's none, so the issue must be somewhere else. I'm comparing it to the functionality of the Exif module. Any other ideas on why this error is showing up? setSummary doesn't go through (I tried $items[$i]->refresh() but it still doesn't work). Also, do you know where I can find the specific documentation on how to read the filename and highlight it?

Thanks again,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2005-06-22 00:05

please dive a little into the gallery2 code, of course i can always try to find the stuff for you and post it but i hope you can do more and more on your own ;)

@setting the highlight
i'd check the randomHighlight module on how this is done.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Wed, 2005-06-22 02:11

I've tried debugging the whole thing, and nothing appears as an error; I also cleared all cache, and it's still got the same error. I'm doing exactly the same operation as the Exif module, and still mine fails. I've also looked at the GalleryItem.class line where it then fails:

/* Save myself */
$ret = parent::save();
if($ret->isError()) {
return $ret->wrap(__FILE__,__LINE__);
}

But can't understand what this 'Save myself' means. I'm lost as to where to look in the code. I've read the documentation and, according to that, I'm locking and refreshing items correctly before saving.

Thanks,
Ralph

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Thu, 2005-06-23 02:38

I've been testing this and I still can't find what the problem is. The functionality is exactly the same as with any other module that extends ItemAddOption. If anyone has an idea of where the problem might be, please let me know. Could this be a bug in G2? Thanks, Ralph.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Thu, 2005-06-23 22:49

i wonder if some other module's ItemAddOption saves the item and doesn't refresh it, so when you get it the $items list has out-of-date items.. try deactivating exif module and trying again, or do list ($ret, $item) = $item->refresh() before changing each item.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Thu, 2005-06-23 23:24

I disabled the Exif module and my module works now. However, I need to have both enabled, and refreshing ($item->refresh) before making changes doesn't work. Any ideas? This looks like a bug, since I can't have many modules that use ItemAddOption. Thanks, Ralph.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-06-24 07:19

1. acquirewritelocks
2. refresh
3. setitemsummary
4. save
5. releaselocks

implement this code, it *should* work.
if it doesn't please post the code and make sure the error you get is in the save call (take a look at the line numbers of the stack trace)

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Fri, 2005-06-24 12:22

Here's the module's code. As you'll notice, I'm doing everything as it should be done and it still fails. It only works if I disable the Exif module, but I need this enabled as well. So I'm guessing this is a bug in G2.

module.inc

<?php
/*
* $RCSfile: module.inc,v $
*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2005 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
USA.
*/
/**
* @package albumthumb
* @version $Revision: 1.63 $ $Date: 2005/06/20 21:00:00 $
* @author Ralph
*/

/**
* Album Thumbnail module
*
* @package albumthumb
*/
class AlbumThumbModule extends GalleryModule {

    function AlbumThumbModule() {
    global $gallery;

    $this->setId('albumthumb');
    $this->setName('Album Thumbnail');
    $this->setDescription($gallery->i18n('Detects the thumbnail for an album'));
    $this->setVersion('0.9.0');
    $this->setGroup('data', $this->translate('Extra Data'));
    $this->setCallbacks('');
    $this->setRequiredCoreApi(array(5, 8));
    $this->setRequiredModuleApi(array(0, 9));
    }

/*
* @see GalleryModule::performFactoryRegistrations()
*/

function performFactoryRegistrations() {

	$ret = GalleryCoreApi::registerFactoryImplementation('ItemAddOption',
	    'SetAlbumThumbnail', 'SetAlbumThumbnail',
	    'modules/albumthumb/SetAlbumThumbnail.inc', 'albumthumb', null);
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}

	return GalleryStatus::success();

}

    /**
     * @see GalleryModule::autoConfigure
     */
    function autoConfigure() {
    /* We don't require any special configuration */
    return array(GalleryStatus::success(), true);
    }

}

?>

SetAlbumThumbnail.inc

<?php
/*
 * $RCSfile: SetAlbumThumbnail.inc,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 * @version $Revision: 1.9 $ $Date: 2005/04/22 21:56:28 $
 * @package albumthumb
 * @subpackage UserInterface
 * @author Ralph
 */

/**
 *
 * @package albumthumb
 * @subpackage UserInterface
 */
class SetAlbumThumbnail extends ItemAddOption {

/*
* @see ItemAddOption::isAppropriate
*/

function isAppropriate() {
return array(GalleryStatus::success(), true);
}

/*
* @see ItemAddOption::handleRequestAfterAdd
*/

function handleRequestAfterAdd($form, $items) {

$errors = array();
$warnings = array();

$itemIds = array();

foreach ($items as $item) {
$itemIds[] = $item->getId();
}

list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($itemIds);
if ($ret->isError()) {
return array($ret->wrap(__FILE__, __LINE__), null, null);
}

foreach ($items as $item) {
list($ret,$item) = $item->refresh();
if($ret->isError()) {
return array($ret->wrap(__FILE__,__LINE__));
}

for ($i = 0; $i < count($items); $i++) {

$warnings[$i] = array();
$itemId = $items[$i]->getId();

$testsummary = "This is a test summary";

// REPLACE FOR MAKE HIGHLIGHT CODE.

$mustSave = false;
$items[$i]->setSummary($testsummary);
$mustSave = true;

	    if ($mustSave) {
		$ret = $items[$i]->save();
		if ($ret->isError()) {
		    GalleryCoreApi::releaseLocks($lockId);
		    return array($ret->wrap(__FILE__, __LINE__), null, null);
		}
	}

}

$ret = GalleryCoreApi::releaseLocks($lockId);
if($ret->isError()) {
return array($ret->wrap(__FILE__,__LINE__), null, null);
}

return array(GalleryStatus::success(), $errors, $warnings);

}

}

?>

And the error I'm getting:

Error (ERROR_OBSOLETE_DATA) : UPDATE g2_Entity SET g_modificationTimestamp=?,g_serialNumber=? WHERE g_id=? AND g_serialNumber=? (1119615394|2|534|1)

    * in modules/core/classes/GalleryStorage/DatabaseStorage.class at line 586 (gallerystatus::error)
    * in modules/core/classes/GalleryStorage.class at line 133 (mysqldatabasestorage::saveentity)
    * in modules/core/classes/GalleryEntity.class at line 293 (gallerystorage::saveentity)
    * in modules/core/classes/GalleryItem.class at line 409 (galleryfilesystementity::save)
    * in modules/albumthumb/SetAlbumThumbnail.inc at line 87 (galleryphotoitem::save)
    * in modules/core/ItemAdd.inc at line 142 (setalbumthumbnail::handlerequestafteradd)
    * in main.php at line 182 (itemaddcontroller::handlerequest)
    * in main.php at line 86
    * in main.php at line 77

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-06-24 13:01

ah, i found the bug, but it's in your code ;)

foreach ($items as $item) {
list($ret,$item) = $item->refresh();
if($ret->isError()) {
return array($ret->wrap(__FILE__,__LINE__));
}

here, you work with copies of the items.
the items in $items are still obsolete.

fix: do the refresh in the main loop:
- and don't forget to release the locks! the exif module does the stuff with releasing the locks not that good. you should release them whenever you have acquired locks and before any return statement.
- and have always the same number of return values. e.g. array(GalleryStatus::success(), null, null) in this case, you shouldn't return one time an array of 3 entries and then an array of 1 entry
- $mustSave is not necessary. $items[$i]->save() does the same logic internally

for ($i = 0; $i < count($items); $i++) {
list($ret,$items[$i]) = $items[$i]->refresh();
if($ret->isError()) {
GalleryCoreApi::releaseLocks($lockId);
return array($ret->wrap(__FILE__,__LINE__), null, null);
}

$warnings[$i] = array();
$itemId = $items[$i]->getId();

$testsummary = "This is a test summary";

// REPLACE FOR MAKE HIGHLIGHT CODE.

$items[$i]->setSummary($testsummary);

      $ret = $items[$i]->save();
      if ($ret->isError()) {
          GalleryCoreApi::releaseLocks($lockId);
          return array($ret->wrap(__FILE__, __LINE__), null, null);
      }
} 
 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Fri, 2005-06-24 15:40

valiant thanks a million! That fixed it.
So now I'm trying to get it to highlight the item as the parent's thumbnail instead. Here's the code:

list($ret, $success) = GalleryCoreApi::setThumbnailFromItem($form['itemId'], $itemId);
if($ret->isError()) {
return array($ret->wrap(__FILE__,__LINE__), null, null);
}

But it returns another error:

Security Violation

The action you attempted is not permitted.

Error Detail -
Error (ERROR_BAD_PARAMETER)

    * in modules/core/classes/GalleryChildEntity.class at line 83 (gallerystatus::error)
    * in modules/core/classes/GalleryDerivative.class at line 238 (gallerychildentity::create)
    * in modules/core/classes/GalleryDerivativeImage.class at line 144 (galleryderivative::create)
    * in modules/core/classes/helpers/GalleryItemHelper_medium.class at line 697 (galleryderivativeimage::create)
    * in modules/core/classes/GalleryCoreApi.class at line 1518 (galleryitemhelper_medium::setthumbnailfromitem)
    * in modules/albumthumb/SetAlbumThumbnail.inc at line 81 (gallerycoreapi::setthumbnailfromitem)
    * in modules/core/ItemAdd.inc at line 142 (setalbumthumbnail::handlerequestafteradd)
    * in main.php at line 182 (itemaddcontroller::handlerequest)
    * in main.php at line 86
    * in main.php at line 77

Any ideas on how I can highlight the item. I've looked into the RandomHighlight module but this approaches it in a completely different manner. Also, how can I get the item's filename? Does something like $items[$i]->getFilename() exist? So that I can detect if it's named '_category_image.jpg' for example.

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-06-24 16:10

sorry, don't have time to assist you in every step.

ERROR_BAD_PARAMETER -> obviously you don't use a function properly, it expects other parameters. we have documented every function with
@param ...
@param ...
@return ...

so it should be clear what parameters it expects.
if you think you're using the correct parameters, do a var_dump(...); exit; for the variable you're passing to verify that you're doing the correct thing.

there's a html version of the API documentation in the dev documents.
http://gallery.menalto.com/modules/GalleryAPI/apidoc/

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Sat, 2005-06-25 13:54

valiant thanks for the help! I think I'm getting a better understanding of G2 now. I'll be posting the module once it's done.

Thanks,
Ralph

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Wed, 2005-06-29 10:44

Hi, just wondering if there's a function so that once I set the album thumbnail, I can set its permissions so that it can only be viewed, in the album, by the Site Administrator group. I haven't found any function in the API that works in the way of: removeAllItemPermissions except for Site Administrators. Any ideas will be appreciated.

Thanks,
Ralph

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2005-06-29 14:10

removeAllItemPermissions($itemId) followed by addGroupPermission(...);
i'd search the g2 code for examples of both functions and copy'n'paste the examples, then adjust it to your needs.

 
ralphch

Joined: 2005-06-18
Posts: 27
Posted: Thu, 2005-06-30 01:26

valiant thanks a lot for the help! The module's finished; I've posted it in the G2 General Development forum.

Regards,
Ralph

 
dwdallam

Joined: 2006-11-19
Posts: 394
Posted: Mon, 2007-02-05 04:13

Just code it so there is a default name, so that any image uploaded will be automatically used as teh albums thumbnail--no specific language needed.

Filename: _xxx_.extension (xxx=numerical)