My site has a heavy focus on events.
I have a table of events with Flyer images attached to them.
There is an Event Listing interface that people are constantly adding upcoming events to and then an archive of all past events.
What I would like to do is link each event to an subalbum within my Events Albums. These subalbums should be automatically generated when a user has images to upload for that event once it is past.
Is there a way I could use the Gallery 2 External Mapping mechanism to map to my EventID's?
It seems that would be the best way to ensure that only one album per EventID is created.
Posts: 32509
edit:
yes you can. period
just use GalleryEmbed::addexternalIdMap($eventId, $albumId, 'GalleryAlbumItem');
and use loadEntityByExternalId($eventId, 'GalleryAlbumItem');
Posts: 153
wow. I really love gallery.
Would this create the gallery as well?
GalleryEmbed::addexternalIdMap($eventId, $albumId, 'GalleryAlbumItem');
Posts: 153
GalleryEmbed::addexternalIdMap($eventId, $albumId, 'GalleryAlbumItem');
Sould I make $eventID a string (ie. "Event233") or something to make it more identifiable?
I am anticipating possible conflicts if I were to use this same method for another kind of entity like portfolio galleries.
Or is there some way for loadEntityByExternalId to distinguish?
Posts: 32509
- you can make eventId a string, you don't have to.
- GalleryEmbed::addExternalIdMapEntry(..,..,..) only adds a mapping in the map table
if i were you, i'd create your own function analogous to GalleryEmbed::createUser().
don't create it in GalleryEmbed.class, create it somewhere in your files.
first, you need to insert the events album id, the one where all your new event album will be created in, in the database:
add a row to the database table g2_PluginParameterMap: module, core, id.eventsAlbumId, 0, the id of the album.
alternatively, you can use GalleryCoreApi::setPluginParameter('module', 'core', 'id.eventsAlbumId', $eventsAlbum->getId()); which will do the same, insert a row in this db table.
then you can use loadEntityByExternalId to load the album that corresponds to $eventId.
Posts: 153
what would be the best way to load that $eventsAlbum in order to call this?
GalleryCoreApi::setPluginParameter('module', 'core', 'id.eventsAlbumId', $eventsAlbum->getId());
Posts: 32509
yep, i knew that you'd ask this.
i don't know if your integration is for a single website. if so, i'd use the manual way: find out the entity id of your events album that you already created, then insert the row in the database manually.
if you want to have an installer for your integration or if the events album doesn't exist yet, you can create a new album with the G2 api, then use $album->getId() and use this as the events album id.
Posts: 153
main.php?g2_view=core.ShowItem&g2_itemId=1828
so i guess it's 1828.
I think I should avoid touching the g2_database manually and use the API instead.
I'm not quite sure of what goes where yet.
Posts: 153
As I create these I'll want to make the Origination Date of the album the day the event happened.
I guess there is a way to do that too?
Posts: 32509
if g2 shows the event album with the above url, yes, then 1828 is the correct id.
Posts: 153
sorry. skipped one.
is there a way to hand the createAlbum function the Originatin Date?
or do I need another function to update it after I create it?
Posts: 153
is there a way to hand the createAlbum function the Originatin Date?
or do I need another function to update it after I create it?
Posts: 32509
after the createAlbum() call, you have to:
$album->setOriginationTimestamp($unixtimestamp);
or / and
$album->setCreationTimestamp($unixtimestamp);
$unixtimestamp can be any unix timestamp... see php.net / google on how to generate a unit timestamp from a date etc.
list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($album->getId());
if ($ret->isError()) {
print $ret->getAsHtml();
exit;
}
$ret = $album->save();
if ($ret->isError()) {
GalleryCoreApi::releaseLocks($lockId);
print $ret->getAsHtml();
exit;
}
$ret = GalleryCoreApi::releaseLocks($lockId);
if ($ret->isError()) {
print $ret->getAsHtml();
exit;
}
the important lesson here is:
when ever you want to save a change in G2, you have to call $entity->save(); and you have to call acquireWriteLock before() and releaselocks() after it. that way we make sure, only one person is changing this entity (album) at a time.
Posts: 153
Just to be clear....
Now that I have run:
GalleryCoreApi::setPluginParameter('module', 'core', 'id.eventsAlbumId', 1882);
After I create the gallery as you described and I do this :
ExternalIdMap::addMapEntry(array('externalId' => $eventId,
'entityType' => 'GalleryAlbumItem', 'entityId' => $album->getId()))
How does differentiate between an EventAlbum and a possible new series of externalId's that I can forsee coming later?
Down the road suppose now I have to add Portfolio Albums for special profiles in my CMS.
If some of the PorfolioID's are the same as the EventID's wouldn't that cause a problem?
I'm sure I'm probably missing something.
Posts: 32509
you use now the externalIdMap for two things:
a) map CMS users to G2 users
b) map CMS events to G2 albums
how to differentiate? when loading an album, use loadEntityByExternalId($evenId, 'GalleryAlbumItem');
when loading a user, G2 uses internally use loadEntityByExternalId($cmsUserId, 'GalleryUser');
the key difference is the second argument.
ah i see, you want to introduce a third mapping. This would also be a $id, 'GalleryAlbumItem' mapping, so this would definitely conflict with the eventId mapping.
your options are:
- use another table (your own) to map portfolios with g2 albums
- make sure portfolioIds and eventIds use the same sequence of ids. (there's no event / portfolio with the same id).
Posts: 153
I was just making sure there wasn't some other way.
I think what I might do when i get to the portfolios is just make them start at 250k or something.
It will be a very long time before the events reach that.
Either that or I suppose the Portfolios could use a string as the $porfolioID like "Portfolio333" that's allowed right?
Posts: 32509
ah right, the string solution is a good one.
Posts: 153
What about setting permissions on these albums?
I'll want to make it so that all registered users can upload photos to it but not create subgalleries.
Posts: 153
When trying to create a gallerly like this:
With these args:
It seems to work in that I see no errors.
From the root the album claims to have an item within it.
However when I try to go into my Events parent gallery now I get this full page error:
If I follow the model of checking for User Albums we discussed
(which seems to work fine when the ExternalId is in the table but errors out when it is not.)
I am able to load the gallery for the event I just created. But when I try to follow the link I get the same error above.
These are the properties a print_r shows me for the album..
is there a method in particular I should call to check what's going on?
any ideas?
Posts: 32509
do you check all your $ret values with if ($ret->isError()) { ... ? that's important.
@loadEntityByExternalId($galleryEventIDString,'GalleryAlbumItem') error when there's no map entry:
you can find out if it is a real error or if the event just has no album associated with it yet with
if ($ret->getErrorCode() & ERROR_MISSING_OBJECT) or something like that. i guess an example should be in GalleryEmbed.class somewhere.
a real error is bad, if there's just no mapping yet, you may just create an album at this point...
@error
i'm not quite sure right now, but IMO [_ownerId] => shouldn't be empty. ownerId should be something like '6' or so.
could you please post the code which is used from include embed.php up to the createAlbum call?
Posts: 153
According to what I can the album has been created.
I can @loadEntityByExternalId($galleryEventIDString,'GalleryAlbumItem') without an error.
Hence the above quoted "galleryalbumitem Object"
The above error comes when I try to link directly to the album or even to the upper level Parent Album.
It's as if the album has been created and it seems the map has been made (i did a select on the Map table).
But there is some aspect of it that has not been created yet or some support DB entry that has not been made.
Does this sound possible?
What table is ownerID in? Maybe I could update it manually?
This is my script (so far) for creating Event albums:
Posts: 153
I set the $galleryActiveUser to 6 and created another Event Album.
No errors when I access the album now.
Of course this had the effect of giving it the usermap of User 6 in my database. oops.
This seems to be the cause of the trouble. This seems a really big error for just not having the ownerID set.
I want to make the OwnerID be the GalleryAdmin which i believe has the G2_UserID of 6.
How do I do that as that GalleryUser does not have a Map that I am aware of.
Posts: 32509
ok, there's the error.
you called GalleryEmbed::init with activeUserId = '' which is the anonynmous user.
that's ok, but here comes the first error:
you didn't call ::init with 'fullInit' => true.
see docs/EMBEDDING:
you need to set 'fullInit' => true in the ::init call, unless you call ::handleRequest() directly after it.
second error:
you didn't login as administrator. you shouldn't do things like create an album with the anonymous user.
do it with the real owner. e.g. use ::init with activeUserId = $cmsUserId.
or do it as admin, activeUserId = $cmsAdminId.
but first make sure, your cms admin id is mapped to the g2 admin in the externalIdMap.
third error:
- you forgot to call GalleryEmbed::done(); at the end of the script, which is important. done() commits database transactions.
note:
- you have there some return $ret->wrap(__FILE__, __LINE__); lines. you won't see these errors in your browser. if you want to see the errors, do print $ret->getAsHtml();
Posts: 32509
ownerId is the id of a g2 user.
see table g2_User for g2 users and the g_Id which is the ownerId in the other table.
Posts: 153
Now we're cooking!
Thanks alot!
I look forward to the full docs on all this one day.
Hopefully these threads will help decide what sort of stuff should be going in.
Ok. maybe a silly question but how do i map my cmsAdminId to the g2AdminId ?
Also I can't find the place to change the OwnerID's of the galleries that I created with no owner.
(Gallery won't let me browse to them to change as admin)
Posts: 32509
either manually or...
i don't have the source code available right now...
pseudo code:
1. get id of cms admin user
2. get id of g2 admin user
3. call GallerytEmbed::addExternalIdMapEntry
details for step 2:
- ::init again, fullInit
- list ($ret, $siteAdminGroupId') = GalleryCoreApi::getPluginParameter('module', 'core', 'id.siteAdminGroup') <--- this is probably incorrect, see modules/core/CoreModuleExtras.inc for the correct string, it's perhaps on a line between line 1000 and 2000 just search for 'id.
- check $ret
- GalleryCoreApi::getUsersForGroup <--- or something like that, see modules/core/classes/GalleryCoreApi.class
- pick any of the returned users etc.
a shorter but less general approach:
- GalleryCoreApi::getUserByUsername('admin'); or something like that
Posts: 153
I selected the "admin" user from the g2_User and found it to be g_ID = 6.
In my CMS it is userID = 1
I did this:
insert into g2_ExternalIdMap values (1,"GalleryUser",6);
Is that good?
How can I change the OwnerID for the Galleries I have already created which are currently OwnerID = NULL ..
The Gallery will not let me browse to them through the interface to change it.
This is causing serious problems.
Posts: 32509
if the columns of externalIdMap are externalId, entityType, entityId in this order, then it's correct.
http://gallery.menalto.com/gallery/miscellaneous/aaf.png.html?g2_imageViewsIndex=1
ownerId is a property of GalleryItem
you could:
UPDATE g2_Item
SET g_OwnerId = '6'
WHERE g_OwnerId IS NULL;
Posts: 153
Ok. I've updated it there.
But it seems that was not the only problem.
Gallery still would't let me browse to the Events album.
I decided to just delete the album through the gallery interface.
I then reset the plugin paramter to the newly created Events Parent Album.
I notice now thought that it didn't delete the ExternalIdMap entries which seems to be causing troubles when I try to Create an album for one of the previous events..
Can I remove them manually with an SQL "delete from g2_ExternalIdMap..." or should I use the API somehow?
Posts: 32509
yep, it doesn't delete the entries in the externalIdMap.
a month ago i argued that this was ok, since the external application should be responsible for G2 objects, that are mapped to it.
but i guess i change my opinion.
could you please file a http://sf.net/projects/gallery/ bug such that i don't forget it.
we should check externalIdMap when we delete an entity and if there;s an entry, delete it.
Posts: 153
I agree.
put one of these in the script
Just before the "addMapEntry" and recreated the events that already had a map.
seemed to work.
then i commented that line back out of my script.
Galleries seem to be created properly now. Thanks!
Posts: 153
Now what I would like to do is set the permissions on these galleries so that any registered user is allowed to upload images to them.
Can I do this as part of my script?
Posts: 32509
yes you can.
the easiest would be if you set the permissions correctly for your parent "all events" album.
why?
because in G2, new sub items / sub album inherit the permissions of its parent album.
so you only have to set the permissions correctly for the "all events" album, and it will be ok.
i suggest following permissions:
- core.addDataItem
- core.viewAll
- comment.add
- comment.view
you can add them with the GUI.
if you want to use the API (for what ever reason):
see GalleryCoreApi for the permission functions.
Posts: 153
The reason I thought it might be better to use the API is because I want people uploading Items to the sub albums but not to the parent album.
Perhaps I will try it this way for now and see how it goes though.
Posts: 32509
yes, of course. this doesn't change anything. set the permissions manually to the "all events" album and everytime you create a new album for a specific event, the new "event xy" album will automatically have the same permissions as the "all events" album. that's how G2 works.
users will have the permission "add item" aka core.addDataItem, but they won't have permission "add album" aka core.addAlbumItem
Posts: 153
what I meant was that doing it this way will allow users to upload events directly to the "all events" album as well.
Posts: 32509
you want users to allow creating their own event album?
yes, then you need to assign the permissions with the API. that is, you have to remove the add album permission from the newly created event albums.
Posts: 153
No.. I don't want the users to create their own event album except through the interface I created.
I want them to upload pictures to the event sub albums.
I don't want them to upload pictures to the event parent album.
Posts: 32509
then you don't need to use the API, all new event albums will automatically have the correct permissions.
Posts: 153
But the parent album won't.
won't it allow users to upload photos directly to it?
Posts: 32509
you're absolutely right. sorry. i'd set the permissions to the parent event album such that all permissions are there but the addDataItem permission. and then after creating a new event album, add the group permission core.addDataItem to the registered user group.
Posts: 153
Currently users do not have the ability to remove their own pictures from the albums.
Is there a way I can allow them to delete their own photos however not the photos of others?
Posts: 32509
you must give them the delete permission for their own items..., this is a manual task, unless you listen for entity save events and do the logic there.
Posts: 153
listen for entity save events and do the logic there?
manually give them delete permission for their own items?
can you elaborate please?
I have 1700 Registered Users all of whom i wish to allow to add photos to an expanding group of galleries.
If they add a photo they should also have the ability to remove it.
How can I allow them this?
(without allowing them to remove the photos of others)
what is involved in creating an environment like this?
Posts: 32509
g2 doesn't have special owner permissions. so you have to add the delete item permission manually after a photo has been added.
everytime an item is created / updated / ... in g2, there's a Entity:Save event.
You can create a G2 module that listens for this event.
in this handleEvent function, you have to check whether it is a GalleryAlbumItem, then make sure the user doesn't have the delete permission since you don't wanna give him the option to delete event albums, right?
if it's a GalleryDataItem, give the activeUser the delete permission for this item.
functions to assign / remove permissions are listed in GalleryCoreApi.class
other modules also have event listeners. just copy some code.
Posts: 153
i'm wondering if maybe the owner of an item should be allowed to delete it by default?
would this be a difficult thing to add to G2?
is it a feature that is planned or being entertained?
Posts: 32509
special owner permissions have been requested before. but noone's working on them and you won;t see them anytime soon.
and i think not all people would want the owner to be able to delete the an item. e.g. what about albums? deleting an album leads to deleted subitems, which didn't belong to you.
or some admins don't want to allow deleting items at all.
so it has to be an option anyway.
with the current permission system you have to do such things manually or event based, code once a module, then it just works.
Posts: 153
this may be just my point of view.
but because it is i see it as a point of logic.
i think that if you own the parent album you should have the freedom to delete the whole album even if others have uploaded or created sub albums.
this is the difference between owning something and having permission to use it.
if you don't think the user should be allowed to delete an album with sub albums then why is he the owner of it?
should be owned by admin or something in that case with the user only being allowed to add.
also, I don't know the first thing about creating G2 modules yet.
where can i go to learn?
Posts: 32509
we use the "owner" information right now just for copying permissions from the old owner to the new owner when changing the owner of an item.
of course this could be extended somehow to offer the option that owners have special permissions per se.
but that's not the case right now. and it won't change anytime soon, since we're so busy.
we don't have module docs yet. writing a new module means taking a look at other forums and creating a new one.
your module would only need a single file. module.inc.
e.g. see modules/albumselect/module.inc, copy it to
modules/ownercandeleteitems/module.inc and start editing it.
change
- change the class name, must be the same name as the module directory name
- change the constructor accrodingly
- change the setId line, must be the same name as the module directory name
- change $this->setCallbacks('getSiteAdminViews|registerEventListeners');
to $this->setCallbacks('registerEventListeners');
change
to
- remove the upgrade function
- remove the getSiteAdminViews function
- customize the handleEvent function, there you have to do the logic:
what you wanna do there:
- for GaleleryEntity::save events and if the entity type isa GalleryDataItem, make sure the owner of the item has delete permission
Posts: 153
wow.. ok.
thanks for your help.
i'll get to work and post here if i run into any hangups.