I am adding a requested function to the SearchScan.tpl. My photographers asked if I could put a link on the search page so that they can add their photos to the cart with out having to click on the thumbnail witch takes them to the photo in the album.
I did this but the problem is that everybody can see the link Add to Cart. Not everyone has this permission.
How can I set up a quick check on the user permissions? If the user has the add to cart permission then show this link. If not don't show it.
Here is the code I am using in the tpl
<p>
<div class="gbBlock">
<a href="{g->url arg1="controller=cart.AddToCart" arg3="itemId=`$itemId`"}">
{g->text text="Add to Cart"}
</a>
</div>
</p>
Can I use something like
{if isset($theme.permissions.core_addDataItem)}
instead of addDataItem what ever the cart permission check is.
Thanks
Jr Mint
Posts: 8601
turn on buffered debug mode in config.php and bring up a search page to find out if the permission data is available there.. i'm not certain if it is loaded. if it is, you should be able to do something like you described.
Posts: 94
It looks like the only kind of permissions is for what images can be viewed.
No systemlinks or itemlinks permissions are being past.
I believe the some sort of array for system and item links permissions need to be in the SearchScan.inc
But I don't know enough to be able to code that into the SearchScan.inc with out over coding it. Or just plain messing it up.
I looked at the Metrix theme.inc to see how it calls the links permissions but don't know what code to copy with out braking the SearchScan
Thanks
You help is greatly appreciated.
Posts: 8601
look at modules/core/classes/GalleryTheme.class and search for studyPermissions and 'permissions' (include the quotes in the search) and you'll find the bits of code you need.
Posts: 94
Thanks for the heads up.
I took the following from GalleryTheme and put it in the SearchScan.inc Just above the /* Render the HTML body */
/*
* --------------------------------------------------------------------------------------
* Initialize..
*/
$theme =& $template->getVariableByReference('theme');
$load = $childItems = $childData = array();
if (!empty($childIds)) {
list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById($childIds);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
}
$itemId = $item->getId();
$allItems = array_merge(array($item), $childItems);
$allAlbumIds = array();
foreach ($allItems as $it) {
if ($it->getCanContainChildren()) {
$allAlbumIds[] = $it->getId();
}
}
if (!empty($childIds)) {
/*
* Study all permissions at once so that individual permission checks later
* don't lead to multiple database queries.
*/
$ret = GalleryCoreApi::studyPermissions($childIds, $theme['actingUserId']);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
}
/*
* --------------------------------------------------------------------------------------
* Load additional required entities..
*/
$ids = array();
if (isset($load['owner'])) {
$ids[$item->getOwnerId()] = true;
$childOwnerIds = array();
foreach ($childItems as $child) {
$ids[$child->getOwnerId()] = true;
$childOwnerIds[$child->getOwnerId()] = true;
}
$childOwnerIds = array_keys($childOwnerIds);
}
if (isset($load['parents'])) {
/*
* TODO: Should we have this obey the acting user permission? It may make navigation
* strange for the active user.
*/
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($itemId);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
if (!empty($parentSequence)) {
$ret = GalleryCoreApi::studyPermissions($parentSequence, $theme['actingUserId']);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
}
foreach ($parentSequence as $id) {
$ids[$id] = true;
}
}
if (isset($load['itemNavigator'])) {
$navigator = array();
$canViewParent = false;
if ($item->getParentId() != null) {
list ($ret, $canViewParent) = GalleryCoreApi::hasItemPermission(
$item->getParentId(), 'core.view', $theme['actingUserId']);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
}
if ($canViewParent) {
list ($ret, $parent) = GalleryCoreApi::loadEntitiesById($item->getParentId());
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
list ($ret, $peerDataItemIds) = GalleryCoreApi::fetchChildDataItemIds(
$parent, null, null, $theme['actingUserId']);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
foreach ($peerDataItemIds as $i => $id) {
if ($id == $itemId) {
$dataItemIndex = $i;
break;
}
}
if (isset($dataItemIndex)) {
if ($dataItemIndex > 0) {
$navigator['first'] = $peerDataItemIds[0];
$navigator['back'] = $peerDataItemIds[$dataItemIndex - 1];
}
$lastIndex = count($peerDataItemIds) - 1;
if ($dataItemIndex < $lastIndex) {
$navigator['next'] = $peerDataItemIds[$dataItemIndex + 1];
$navigator['last'] = $peerDataItemIds[$lastIndex];
}
foreach ($navigator as $id) {
$ids[$id] = true;
}
}
}
if (empty($navigator)) {
unset($load['itemNavigator']);
}
}
if (!empty($ids)) {
list ($ret, $list) = GalleryCoreApi::loadEntitiesById(array_keys($ids));
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
foreach ($list as $it) {
$entities[$it->getId()] = $it;
}
}
/* -------------------------------------------------------------------------------------- */
if (isset($load['permissions']) && !isset($theme['permissions'])) {
list ($ret, $permissions) =
GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
if ($ret->isError()) {
return $ret->wrap(__FILE__, __LINE__);
}
foreach (array_keys($permissions) as $perm) {
$theme['permissions'][str_replace('.', '_', $perm)] = 1;
}
}
And I am using the following in the SearchScan.tpl
<div class="gbBlock">
{if isset($theme.permissions.core_SystemLinks)}
<a href="{g->url arg1="controller=cart.AddToCart" arg2="itemId=`$itemId`"}">
{g->text text="Add %s to Cart" arg1=$itemId.title|markup}
</a>
</div>{/if}
I am still getting the same results as before I put the permissions code in the SearchScan.inc.
The link doesn't show up for anybody including Admins
I also tryed using ItemLinks with no luck.
Any ideas?
Thanks for your help.
Posts: 94
I forgot to say that I had to remove some of the code I copyed from the GalleryTheme.
I got errors from the following lines of code so I removed it out of the SearchScan.inc
foreach ($dataToLoad as $key) {
$load[$key] = true;
And
$allItemIds = array_merge(array($itemId), $childIds);
And
$perPage = $this->getPageSize($params);
Posts: 8601
whoa, i was thinking just:
Posts: 94
I knew I was going to over code...
Thanks a bunch for your bit of code.
I get the following error with this.
Fatal error: Call to a member function on a non-object in /srv/www/htdocs/gallery2/modules/core/classes/GalleryTheme.class on line 690
Posts: 8601
not enough info to help there...
what code did you use? where did you put it? how did it get into GalleryTheme.class? (the code i posted doesn't call GalleryTheme)
Posts: 94
I used
It is just before /* Render the HTML body */
line 690 in the GalleryTheme is
Posts: 8601
I wrote:
$searchResultIds = /* whatever in SearchScan.inc that is an array of ids of the search results */ ;
You wrote:
$searchResultIds = 'itemId';
Hmm...
Posts: 94
With $searchResultIds = /* whatever in SearchScan.inc that is an array of ids of the search results */ ;
I get an error of
Parse error: parse error in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 243
Error Detail -
Error (ERROR_BAD_PARAMETER) : Class SearchScanView not defined in search.SearchScan
* in modules/core/classes/GalleryView.class at line 197 (gallerystatus::error)
* in main.php at line 291 (galleryview::loadview)
* in main.php at line 88
* in main.php at line 81
Line 243 is
$searchResultIds = /* whatever in SearchScan.inc that is an array of ids of the search results */ ;
Posts: 8601
right, that's a comment telling you to figure out how SearchScan.inc works and replace that comment with the right bit of data..
Posts: 94
Well I guess I don't know what code is the search results ids.
I thought it was itemId.
As you can tell I am just guessing.
What should I use?
Thanks for your help..
Posts: 94
I am still just guessing but I think that the arrary you are taking about in SearchScan.inc.
is
I just tryed the following in the $searchResultIds = line
I still get the following error.
Am I at least looking at the right bit of code or am I WAY OFF?
Posts: 94
mindless
Maybe I am looking at this from the wrong direction.?.
All I really need is the core.ItemLinks to be displayed under each thumbnail on the search page right.
I took a look at how the Album.tpl looks up the links and it uses.
I know I can't use $child because it is not defined anywhere in the SearchScan.inc or tpl.
Is the another bit of code to use instead of $child or is that all the ItemLinks will allow?
And I should keep trying to get the code that I am working on to work because that is the best way to accomplish this?
either way I think I have to call the permmissions.
I really appreciate your help with this.
jrmint
Posts: 8601
from the first post, looks like you found the right variable.. just do:
$searchResultIds = $itemIds;
as the first line of your added code.
Run the upgrader up to step 2 and see what files it says are modified. Maybe you edited GalleryTheme.class at some point and left an error in there.
Posts: 94
I didn't even open the servers file of GalleryTheme.class.
I have a copy of the whole Gallery2 folder on my pc just for that reason. I look at files localy before editing them on the server.
The date and time on the GalleryTheme.class is 11/13/2005 03:51:48PM
If I use
$searchResultIds = $itemIds;
I get the following error
Notice: Undefined variable: itemIds in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 245
Fatal error: Call to a member function on a non-object in /srv/www/htdocs/gallery2/modules/core/classes/GalleryTheme.class on line 690
If I put the $itemIds like '$itemIds' the variable error goes away but the function error is still there
Posts: 94
Here is the whole SearchScan.inc that I am using.
Posts: 94
just for kicks I commented out the if ($ret->isError lines
I now get these errors.
Warning: Invalid argument supplied for foreach() in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 252
Notice: Undefined variable: data in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 263
Posts: 8601
$searchResultIds = '$itemIds';
probably should read up on how php works..
take out the quotes again, wrap your whole block of added code inside
if (!empty($itemIds)) {
and
}
you're on your own debugging the GalleryTheme error..
Posts: 94
I still get the following when I click on the Advanced Search link.
Notice: Undefined variable: itemIds in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 245
But when I do a search for somthing I get this.
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 254
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 254
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 254
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 254
The good news is that the GalleryTheme error has seemed to go away.
Here is how I coded the if (!empty($itemIds))
Posts: 94
I moved the code to a different place in the SearchScan.inc. And this got rid of the itemIds error.
But I am still getting the following error after I do a search for something.
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 205
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 205
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 205
Notice: Undefined variable: theme in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 205
Notice: Undefined variable: data in /srv/www/htdocs/gallery2/modules/search/SearchScan.inc on line 215
Posts: 8601
change
GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
to
GalleryCoreApi::getPermissions($itemId);
Posts: 94
That got rid of the theme error but the data error is still coming up.
I removed the $data and the error went away but the permissions still didn't work right in the SearchScan.tpl.
$template->setVariable('itemPermissions');
tpl code is as follows.
I also tryed
{if isset($theme.Permissions.core_AddToCart)}
both do work in a way. It doesn't show the link to anybody.
Posts: 8601
how will it work if you remove $data?
after
$searchResultIds = $itemIds;
add
$data = array();
(before the "if")
Posts: 94
THANKS...
That got rid of the data error.
But I still can't get the tpl permissions code to show the link.
I've tryed to use
{if isset($itemId.Permissions.cart_AddToCart)}
With no luck
Posts: 8601
$itemId ?
the code above does setVariable for 'itemPermissions'.. you know how that translated into data available in a tpl, right?
try turning on buffered debug output in config.php and the popup window (smarty debug console) shows all the data available.
Posts: 94
No I don't know exactly how it translates into a tpl.
I have attached the debug file.
Not sure what to look for.
I do see where it has called some Cart stuff but I think that might be from the SideBar.
Posts: 8601
did that debug.txt come from a popup with a title of "smarty debug console"?
Posts: 94
I forgot to allow popups from my server.
Sorry.
Here is the popup.
Posts: 94
I just went back to the original SearchScan.inc and did another debug.
It is the same, 'word for word' as the one with the hack. I don't think the hack is doing anything.
Posts: 94
I did a debug on an album page and it looks like I am missing a call to the permissions array in the SearchScan.inc.
In the {$theme} part of the debug at the bottom is where the permissions are called.
I tryed to add a line just after the $data = array(); called $permissions array(); but that didn't work.
Posts: 94
mindless
Any more ideas on how to get this to work?
jrmint
Posts: 8601
change
$searchResultIds = $itemIds;
to
$searchResultIds = array_keys($itemIds);
Posts: 94
I tried this.
Same results as before still not working.
I even moved it up to the first part of the file where it calls the first $itemIds = array_keys($itemIds);
Debug has the same output.
Posts: 8601
oops, missed that there is already $itemIds = array_keys($itemIds).. of course you don't need to do it again. $searchResultIds = $itemIds is correct.
spend some time debugging this.. add some print statements at various places in the code to make sure the variables have the values you think they should have, then you can determine where things go wrong. if you're not sure what values the variables should have, spend some time to understand what this code does.
uh, what is $itemId['actingUserId']? i'll leave it to you to figure out what should go there.. check the phpdoc for that core api.
Posts: 94
What is the code for print statements?
Can I do them in both the .inc and .tpl?
Posts: 94
How can I use the print_r with the if?
{if !isset($theme.Permissions.cart_add)}
Posts: 8601
http://php.net/print
use print, print_r, var_dump, etc in php code, ie in theme.inc
in tpl I think you can put: {$theme|print_r}
or something like that. there's also the smarty debug console i mentioned above.
Posts: 35
Hi
Did either of you get this working?
Aaron