Add to Cart from Search Page

jrmint

Joined: 2005-08-20
Posts: 94
Posted: Tue, 2005-12-27 15:05

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

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Fri, 2005-12-30 17:39

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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Fri, 2005-12-30 18:16

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.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-12-31 06:20

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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Sat, 2005-12-31 13:29

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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Sat, 2005-12-31 13:51

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);

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-12-31 16:43

whoa, i was thinking just:

$searchResultIds = /* whatever in SearchScan.inc that is an array of ids of the search results */ ;
$ret = GalleryCoreApi::studyPermissions($searchResultIds);
if ($ret->isError()) {
    return $ret->wrap(__FILE__, __LINE__);
}
foreach ($searchResultIds as $itemId) {
    list ($ret, $permissions) =
        GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
    if ($ret->isError()) {
        return $ret->wrap(__FILE__, __LINE__);
    }
    foreach (array_keys($permissions) as $perm) {
        $data[$itemId][str_replace('.', '_', $perm)] = 1;
    }
}
$template->setVariable('itemPermissions', $data);
 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Sat, 2005-12-31 17:36

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

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-12-31 18:34

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)

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Sat, 2005-12-31 18:58

I used

$searchResultIds = 'itemId';
 $ret = GalleryCoreApi::studyPermissions($searchResultIds);
 if ($ret->isError()) {
    return $ret->wrap(__FILE__, __LINE__);
 }
 foreach ($searchResultIds as $itemId) {
    list ($ret, $permissions) =
        GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
    if ($ret->isError()) {
        return $ret->wrap(__FILE__, __LINE__);
    }
    foreach (array_keys($permissions) as $perm) {
        $data[$itemId][str_replace('.', '_', $perm)] = 1;
    }
 }
 $template->setVariable('itemPermissions', $data);

It is just before /* Render the HTML body */

line 690 in the GalleryTheme is

	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-12-31 22:38

I wrote:
$searchResultIds = /* whatever in SearchScan.inc that is an array of ids of the search results */ ;

You wrote:
$searchResultIds = 'itemId';

Hmm...

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Sun, 2006-01-01 03:10

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 */ ;

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sun, 2006-01-01 16:27

right, that's a comment telling you to figure out how SearchScan.inc works and replace that comment with the right bit of data..

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Sun, 2006-01-01 17:18

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..

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Mon, 2006-01-02 13:21

I am still just guessing but I think that the arrary you are taking about in SearchScan.inc.
is

	    /* Pull out all the item ids and save them */
	    $itemIds = array();
	    foreach ($searchResults as $id => $resultSet) {
		foreach ($resultSet['results'] as $result) {
		    if (isset($result['itemId'])) {
			$itemIds[$result['itemId']] = 1;
		    }
		}
	    }
	    $itemIds = array_keys($itemIds);

I just tryed the following in the $searchResultIds = line

	$searchResultIds = array();
	    foreach ($searchResults as $id => $resultSet) {
		foreach ($resultSet['results'] as $result) {
		    if (isset($result['itemId'])) {
			$itemIds[$result['itemId']] = 1;
		    }
		}
	    }
	    $searchResultIds = array_keys($searchResultIds);

I still get the following error.

Fatal error: Call to a member function on a non-object in /srv/www/htdocs/gallery2/modules/core/classes/GalleryTheme.class on line 690

Am I at least looking at the right bit of code or am I WAY OFF?

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Mon, 2006-01-02 15:05

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.

              {g->block type="core.ItemLinks" item=$child links=$child.itemLinks}

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

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Mon, 2006-01-02 15:39

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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Mon, 2006-01-02 16:04

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

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Mon, 2006-01-02 16:06

Here is the whole SearchScan.inc that I am using.

<?php
/*
 * $RCSfile: SearchScan.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., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * @version $Revision: 1.28 $ $Date: 2005/08/23 03:49:53 $
 * @package Search
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * Required classes
 */
GalleryCoreApi::relativeRequireOnce('modules/search/classes/SearchUtilities.class');

/**
 * @package Search
 * @subpackage UserInterface
 */
class SearchScanController extends GalleryController {

    /**
     * @see GalleryController::handleRequest
     */
    function handleRequest($form) {
	global $gallery;

	$results = array();
	if (isset($form['action']['slideshow'])) {

	    if (isset($form['searchCriteria'])) {
		$form['searchCriteria'] = trim($form['searchCriteria']);
	    }

	    list ($ret, $slideshow) =
		GalleryCoreApi::newFactoryInstance('SlideshowInterface_1_0');
	    if ($ret->isError() || !isset($slideshow)) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }

	    GalleryCoreApi::relativeRequireOnce(
		'modules/search/classes/GallerySearchInterface_1_0.class');
	    list ($ret, $ids) =
		GalleryCoreApi::getAllFactoryImplementationIds('GallerySearchInterface_1_0');
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }

	    foreach ($ids as $id => $className) {
		list ($ret, $searchInstances[$id]) =
		    GalleryCoreApi::newFactoryInstance('GallerySearchInterface_1_0', $className);
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
	    }
	    $itemIds = array();
	    foreach ($searchInstances as $id => $instance) {
		if (isset($form['options'][$id])) {
		    list ($ret, $resultSet) =
			$instance->search($form['options'][$id],
			    SearchUtilities::sanitizeSearchCriteria($form['searchCriteria']));
		    if ($ret->isError()) {
			return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    foreach ($resultSet['results'] as $result) {
			if (isset($result['itemId'])) {
			    $itemIds[$result['itemId']] = 1;
			}
		    }
		}
	    }
	    $itemIds = array_keys($itemIds);

	    list ($ret, $results['delegate']) = $slideshow->getLinkData($itemIds);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	} else if (isset($form['action']['showAll'])) {
	    $results['delegate']['view'] = 'search.SearchShowAll';
	} else {
	    $results['delegate']['view'] = 'search.SearchScan';
	}


	$results['status'] = array();
	$results['error'] = array();
	return array(GalleryStatus::success(), $results);
    }
}

/**
 * @package Search
 * @subpackage UserInterface
 */
class SearchScanView extends GalleryView {

    /**
     * @see GalleryView::loadTemplate
     */
    function loadTemplate(&$template, &$form) {
	global $gallery;

	/* TODO: Parameterize this */
	$scanSize = 4;

	/* Find all the modules that support the Search interface */
	GalleryCoreApi::relativeRequireOnce(
	    'modules/search/classes/GallerySearchInterface_1_0.class');
	list ($ret, $ids) =
	    GalleryCoreApi::getAllFactoryImplementationIds('GallerySearchInterface_1_0');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	foreach ($ids as $id => $className) {
	    list ($ret, $searchInstances[$id]) =
		GalleryCoreApi::newFactoryInstance('GallerySearchInterface_1_0', $className);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}

	/* Load the search info for each module */
	foreach ($searchInstances as $id => $instance) {
	    list ($ret, $modules[$id]) = $instance->getSearchModuleInfo();
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}

	$items = array();
	$thumbnails = array();
	$searchResults = array();
	$searchResultThumbnails = array();
	if ($form['formName'] == 'SearchScan' ||
	    $form['formName'] == 'SearchShowAll' ||
	    $form['formName'] == 'search_SearchBlock') {

	    if (isset($form['searchCriteria'])) {
		$form['searchCriteria'] = trim($form['searchCriteria']);
	    }

	    if (empty($form['searchCriteria'])) {
		$form['error']['searchCriteria']['missing'] = 1;
	    } else {

		if (isset($form['useDefaultSettings']) || $form['formName'] == 'SearchShowAll') {
		    foreach ($searchInstances as $id => $instance) {
			$form['options'][$id] = $modules[$id]['options'];
		    }
		}

		/* Execute a "scan" Phrase search */
		foreach ($searchInstances as $id => $instance) {
		    if (isset($form['options'][$id])) {
			list ($ret, $searchResults[$id]) =
			    $instance->search($form['options'][$id],
				SearchUtilities::sanitizeSearchCriteria($form['searchCriteria']),
				0, $scanSize);
			if ($ret->isError()) {
			    return array($ret->wrap(__FILE__, __LINE__), null);
			}
		    }
		}
	   }

	    /* Pull out all the item ids and save them */
	    $itemIds = array();
	    foreach ($searchResults as $id => $resultSet) {
		foreach ($resultSet['results'] as $result) {
		    if (isset($result['itemId'])) {
			$itemIds[$result['itemId']] = 1;
		    }
		}
	    }
	    $itemIds = array_keys($itemIds);

	    $thumbnails = array();
	    if (!empty($itemIds)) {

		/* Fetch items */
		list ($ret, $itemList) = GalleryCoreApi::loadEntitiesById($itemIds);
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
		foreach ($itemList as $item) {
		    $items[$item->getId()] = $item->getMemberData();
		}

		/* Fetch thumbnails */
		list ($ret, $thumbnailList) =
		    GalleryCoreApi::fetchThumbnailsByItemIds($itemIds);
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
		}
		foreach ($thumbnailList as $thumbnail) {
		    $thumbnails[$thumbnail->getParentId()] = $thumbnail->getMemberData();
		}
	    }
	} else {
	    $form['searchCriteria'] = '';
	    foreach ($searchInstances as $id => $instance) {
		$form['options'][$id] = $modules[$id]['options'];
	    }
	}


	list ($ret, $ids) =
	    GalleryCoreApi::getAllFactoryImplementationIds('SlideshowInterface_1_0');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	$slideshowAvailable = !empty($ids);

	list ($ret, $rootAlbumId) =
	    GalleryCoreApi::getPluginParameter('module', 'core', 'id.rootAlbum');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	list ($ret, $rootAlbum) = GalleryCoreApi::loadEntitiesById($rootAlbumId);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	/* check permissions */

	$searchResultIds = $itemIds;

 		$ret = GalleryCoreApi::studyPermissions($searchResultIds);
 	if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	}
	foreach ($searchResultIds as $itemId) {
    		list ($ret, $permissions) =
        	GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
    if ($ret->isError()) {
    	    return $ret->wrap(__FILE__, __LINE__);
    	}
    	foreach (array_keys($permissions) as $perm) {
     	   $data[$itemId][str_replace('.', '_', $perm)] = 1;
    	}
 	}
 	$template->setVariable('itemPermissions', $data); 



	/* Render the HTML body */
	$template->setVariable('SearchScan', array(
	    'controller' => 'search.SearchScan',
	    'modules' => $modules,
	    'searchResults' => $searchResults,
	    'thumbnails' => $thumbnails,
	    'items' => $items,
	    'slideshowAvailable' => $slideshowAvailable,
	    'rootAlbum' => $rootAlbum->getMemberData()));

	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'search');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$template->title($module->translate('Gallery Search'));
	return array(GalleryStatus::success(),
			array('body' => 'modules/search/templates/SearchScan.tpl'));

    }
}
?>
 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Mon, 2006-01-02 16:34

just for kicks I commented out the if ($ret->isError lines

	$searchResultIds = '$itemIds';

 		$ret = GalleryCoreApi::studyPermissions($searchResultIds);
/* 	if ($ret->isError()) {
*		return $ret->wrap(__FILE__, __LINE__);
*	}
*/
	foreach ($searchResultIds as $itemId) {
    		list ($ret, $permissions) =
        	GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
/*    if ($ret->isError()) {
*    	    return $ret->wrap(__FILE__, __LINE__);
*    	}
*/
    	foreach (array_keys($permissions) as $perm) {
     	   $data[$itemId][str_replace('.', '_', $perm)] = 1;
    	}
 	}
 	$template->setVariable('itemPermissions', $data); 

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

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2006-01-03 00:39

$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..

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Tue, 2006-01-03 13:23

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))

	$searchResultIds = $itemIds;
	if (!empty($itemIds)) {
 	$ret = GalleryCoreApi::studyPermissions($searchResultIds);
	if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	}

	foreach ($searchResultIds as $itemId) {
    		list ($ret, $permissions) =
        		GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
		if ($ret->isError()) {
    	    		return $ret->wrap(__FILE__, __LINE__);
    		}

    	foreach (array_keys($permissions) as $perm) {
     	   		$data[$itemId][str_replace('.', '_', $perm)] = 1;
    		}
	}
	
 	$template->setVariable('itemPermissions', $data); 
	} 
 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Tue, 2006-01-03 16:11

I moved the code to a different place in the SearchScan.inc. And this got rid of the itemIds error.

	    /* Pull out all the item ids and save them */
	    $itemIds = array();
	    foreach ($searchResults as $id => $resultSet) {
		foreach ($resultSet['results'] as $result) {
		    if (isset($result['itemId'])) {
			$itemIds[$result['itemId']] = 1;
		    }
		}
	    }
	/* check permissions */

	$searchResultIds = $itemIds;
	if (!empty($itemIds)) {
 	$ret = GalleryCoreApi::studyPermissions($searchResultIds);
	if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	}

	foreach ($searchResultIds as $itemId) {
    		list ($ret, $permissions) =
        		GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
		if ($ret->isError()) {
    	    		return $ret->wrap(__FILE__, __LINE__);
    		}

    	foreach (array_keys($permissions) as $perm) {
     	   		$data[$itemId][str_replace('.', '_', $perm)] = 1;
    		}
	}
	}
 	$template->setVariable('itemPermissions', $data); 
 

	    $itemIds = array_keys($itemIds);

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

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2006-01-03 19:05

change
GalleryCoreApi::getPermissions($itemId, $theme['actingUserId']);
to
GalleryCoreApi::getPermissions($itemId);

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Tue, 2006-01-03 19:32

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.

<p>
        <div class="gbBlock">
	   {if isset($theme.Permissions.cart_AddToCart)} 
          <a href="{g->url arg1="controller=cart.AddToCart" arg2="itemId=`$itemId`"}">
            {g->text text="Add to Cart"}
          </a>
        </div> 
           {/if} 
	</p>

I also tryed
{if isset($theme.Permissions.core_AddToCart)}

both do work in a way. It doesn't show the link to anybody.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2006-01-04 16:30

how will it work if you remove $data?

after
$searchResultIds = $itemIds;
add
$data = array();
(before the "if")

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Wed, 2006-01-04 17:10

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

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2006-01-04 18:11

$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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Wed, 2006-01-04 18:31

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.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2006-01-04 23:31

did that debug.txt come from a popup with a title of "smarty debug console"?

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Thu, 2006-01-05 15:45

I forgot to allow popups from my server.
Sorry.

Here is the popup.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Fri, 2006-01-06 15:14

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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Fri, 2006-01-06 16:59

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.

permissions => Array (11)
  core_all => 1
  core_view => 1
  core_viewResizes => 1
  core_viewSource => 1
  core_viewAll => 1
  core_addAlbumItem => 1
  core_addDataItem => 1
  core_edit => 1
  core_changePermissions => 1
  core_delete => 1
  cart_add => 1
columnWidthPct => 25
 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Mon, 2006-01-09 15:07

mindless

Any more ideas on how to get this to work?

jrmint

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2006-01-10 23:29

change
$searchResultIds = $itemIds;
to
$searchResultIds = array_keys($itemIds);

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Wed, 2006-01-11 13:21

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);

	    $itemIds = array();
	    foreach ($searchInstances as $id => $instance) {
		if (isset($form['options'][$id])) {
		    list ($ret, $resultSet) =
			$instance->search($form['options'][$id],
			    SearchUtilities::sanitizeSearchCriteria($form['searchCriteria']));
		    if ($ret->isError()) {
			return array($ret->wrap(__FILE__, __LINE__), null);
		    }
		    foreach ($resultSet['results'] as $result) {
			if (isset($result['itemId'])) {
			    $itemIds[$result['itemId']] = 1;
			}
		    }
		}
	    }
	    $itemIds = array_keys($itemIds);

	/* check permissions */

/*	$searchResultIds = $itemIds; */
	$searchResultIds = array_keys($itemIds);
	$data = array();
	if (!empty($itemIds)) {
 	$ret = GalleryCoreApi::studyPermissions($searchResultIds);
	if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	}


	foreach ($searchResultIds as $itemId) {
    		list ($ret, $permissions) =
        		GalleryCoreApi::getPermissions($itemId['actingUserId']); 
		if ($ret->isError()) {
    	    		return $ret->wrap(__FILE__, __LINE__);
    		}

    	foreach (array_keys($permissions) as $perm) {
     	   		$data[$itemId][str_replace('.', '_', $perm)] = 1;
    		}
		}
 	}
	$template->setVariable('itemPermissions', $data);

Debug has the same output.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2006-01-11 15:04

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.

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Wed, 2006-01-11 15:55

What is the code for print statements?
Can I do them in both the .inc and .tpl?

 
jrmint

Joined: 2005-08-20
Posts: 94
Posted: Wed, 2006-01-11 16:07

How can I use the print_r with the if?

{if !isset($theme.Permissions.cart_add)}

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2006-01-11 20:38

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.

 
aaronbrockhurst

Joined: 2003-11-25
Posts: 35
Posted: Mon, 2007-04-09 12:47

Hi

Did either of you get this working?

Aaron