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
Posts: 32509
you can change the album thumbnail manually. Isn't that enough?
Posts: 27
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.
Posts: 16504
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
Posts: 27
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.
Posts: 16504
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.
Posts: 27
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.
Posts: 27
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
Posts: 32509
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.
Posts: 27
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.
Posts: 27
Here's how I've started the functionality section of my module.inc file:
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.
Posts: 32509
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.
Posts: 27
Thanks for the reply. Ok, so here's what I'm doing:
In module.inc
And then in SetAlbumThumbnail.class:
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.
Posts: 32509
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...)
Posts: 27
Here's the code, I followed the API documentation, but nothing works.
module.inc
SetAlbumThumbnail.class
Am I missing something here? Any help will be appreciated.
Thanks,
Ralph[/code]
Posts: 32509
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
Posts: 27
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
Posts: 32509
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
Posts: 27
Ok thanks, I just enabled the Debug module and added the following to my eventHandle($event) function in SetAlbumThumbnail.class
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.
Posts: 32509
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.
Posts: 27
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
Posts: 32509
ralphch, have you installed + activated the module in site admin .> modules?
the first step is get it just install / uninstall, activate.
Posts: 27
Yeah, I installed and activated it, but it doesn't work.
Posts: 32509
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.
Posts: 27
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
SetAlbumThumbnail.inc
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:
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
Posts: 32509
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.
Posts: 27
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
Posts: 32509
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.
Posts: 27
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:
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
Posts: 27
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.
Posts: 8601
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.
Posts: 27
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.
Posts: 32509
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)
Posts: 27
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
SetAlbumThumbnail.inc
And the error I'm getting:
Thanks,
Ralph
Posts: 32509
ah, i found the bug, but it's in your code ;)
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
Posts: 27
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:
But it returns another error:
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
Posts: 32509
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/
Posts: 27
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
Posts: 27
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
Posts: 32509
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.
Posts: 27
valiant thanks a lot for the help! The module's finished; I've posted it in the G2 General Development forum.
Regards,
Ralph
Posts: 394
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)