CoreAPI etc help please?
Dayo
Joined: 2005-11-04
Posts: 1642 |
![]() |
Hi. This is my first foray into heavy stuff (for me at least) I want to make a call to the CoreAPI and I have actually figured out what to do. My problem is that the function requires the item details (understandable) but I want to apply the function to only non Album items so I need a way to figure out if an item is an album or not and fairly clueless. I was thinking of something like this (in NormalLanguageCode) foreach ($items as $item) { <<< looping through all items if (!(GalleryUtilities::isA($item, 'Album'))){ <<< checking it is not an album $ret = GalleryCoreApi::somefunction($item, $myVar1, $myVar2, $myVar3); <<< applying desired function if ($ret) { return $ret->wrap(__FILE__, __LINE__); } } } Anyone please help vet / correct the code? Thanks . |
|
valiant
Joined: 2003-01-04
Posts: 32509 |
![]() |
the correct way to differentiate data items from container (album) items is: if you further want to restrict it to real albums and filter out dynamic albums, use: -------------- |
|
Dayo
Joined: 2005-11-04
Posts: 1642 |
![]() |
Thanks So this should work then (sorry not sure) foreach ($items as $item) { if (!$item->getCanContainChildren()) { $ret = GalleryCoreApi::somefunction($item, $myVar1, $myVar2, $myVar3); if ($ret) { return $ret->wrap(__FILE__, __LINE__); } } } . |
|
valiant
Joined: 2003-01-04
Posts: 32509 |
![]() |
yes...why not just test it? btw: $ret->wrap() is deprecated since g2.1. just do: return $ret; -------------- |
|
Dayo
Joined: 2005-11-04
Posts: 1642 |
![]() |
I have been testing...a lot lol. I think I know where I am going wrong. I have been doing foreach ($items as $item) { if (!$item->getCanContainChildren()) { $ret = GalleryCoreApi::somefunction($item, $myVar1, $myVar2, $myVar3); if ($ret) { return $ret; } } } but I think there should be something like $items = array() $items = someFunctionToReturnAllG2Elements() foreach ($items as $item) { if (!$item->getCanContainChildren()) { $ret = GalleryCoreApi::somefunction($item, $myVar1, $myVar2, $myVar3); if ($ret) { return $ret; } } } Any tips on those first two lines? On $ret, how about "return array($ret->wrap(__FILE__, __LINE__), null);"? Thanks for your assistance. . |
|
valiant
Joined: 2003-01-04
Posts: 32509 |
![]() |
you shouldn't have any code that needs to iterate over all g2 items. you could be dealing with a million items. try to scale things down. anyhow, please take a look at docs -> development -> api -> core api. @$ret: -------------- |
|
Dayo
Joined: 2005-11-04
Posts: 1642 |
![]() |
Oops! You're right. I only have about 100 or so items but yes, some galleries might be huge. Wish there was a way to directly address data items without looping through elements. I'll have a read of the docs. Cheers! |
|
Dayo
Joined: 2005-11-04
Posts: 1642 |
![]() |
OK I found a way to do it. I wanted to split the zencart link from gallery which is currently one link for bought adding items and viewing the cart into two separate items and to structure a more reasonable default permissions setup. The current setup is to grant permissions to everybody to add / view for all items using /* Give everybody permissions by default */ $ret = GalleryCoreApi::addGroupPermission( $coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'], 'zencart.addview', true); if ($ret) { return $ret->wrap(__FILE__, __LINE__); } This obviously sets the permission for the main gallery and applies it to all the children and has the following issues 1) No one will be adding the whole gallery to a cart (and they can't anyway even if they wanted too...see 2 below) I split the permissions etc into two and wanted to set the default up so that people could view carts from every page but not get get a non working link for adding items on album pages. After your warning about huge galleries, I thought about adding the permissions for both for everybody by default as before (no looping) and then removing the permissions for adding items from album items (looping involved) on the premise that there will always be fewer album items than data items pages and ZenCart will not be a viable option for someone with 10's of '000s of items anyway. In any case, this will only run once when the module is installed by such a person and I will add a warning to the Readme File After rooting through the docs, I came up with: /* Give everybody permissions for all items & then remove from album items */ $ret = GalleryCoreApi::addGroupPermission( $coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'], 'zencart.add', true); if ($ret) { return $ret->wrap(__FILE__, __LINE__); } $ret = GalleryCoreApi::addGroupPermission( $coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'], 'zencart.view', true); if ($ret) { return $ret->wrap(__FILE__, __LINE__); } list ($ret, $myItemIds) = GalleryCoreApi::fetchAllItemIds(GalleryAlbumItem); if ($ret) { return array($ret->wrap(__FILE__, __LINE__), null); } foreach ($myItemIds as $myItemId) { $ret = GalleryCoreApi::removeGroupPermission( $myItemId, $coreParams['id.everybodyGroup'], 'zencart.add', false); if ($ret) { return $ret->wrap(__FILE__, __LINE__); } }
This way, the permissions are set up properly by default. A last question. The module has the following . . . $this->setRequiredCoreApi(array(7, 0)); $this->setRequiredModuleApi(array(3, 0)); Do I need to change this when I change the "$ret" thing as I noticed the change was at 7.4 (not sure what that means). Thanks for your time. . |
|
valiant
Joined: 2003-01-04
Posts: 32509 |
![]() |
you can leave the ->wrap() stuff if you want to target gallery 2.1 installations as well. just make sure you don't use any other feature that isn't in gallery 2.1. $ret will be gone in gallery 2.4 anyway, so i wouldn't worry about ->wrap(). shouldn't have pointed your attention on that, sorry. -------------- |
|