TagsSearch

rockarena

Joined: 2007-05-03
Posts: 15
Posted: Fri, 2007-09-14 08:54

Hello everyone,

I've been working on an extension for the Tags module. People can now put tags in categories like for instance you have the category Cities and that would contain tags like New York etc. Now everything works fine. People can add tags and categories to the system. they can also assign tags to categories. The main problem is the search function. People can search on tagname and on categoryname. Now it doesn't work!

When someone searches on one tagname it works. When they try to search on one category it works.

When they try to do a search on two tagnames it doesn't work. When they try to do a search on a tagname and a categoryname it doesn't work. I'm no expert programmer and I'm very proud that I've gotten this far so can someone please look at the code of the Tagsearch.inc? There has got to be a mistake in there. I'd be very grateful.

<?php
/*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2007 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.
*/

GalleryCoreApi::requireOnce('modules/tags/classes/TagsHelper.class');
GalleryCoreApi::requireOnce('modules/tags/classes/CategoryHelper.class');
/**
* This controller will handle assigning tags to items
*
* @package Tags
* @subpackage UserInterface
* @author Eric H Daniel <ehdaniel@gmail.com>
* @version $Revision: 1.1 $
*/
class TagSearchController extends GalleryController {

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

$offset=0;
$count=-1;
$catsearchchoice = (isset($_POST["catsearchchoice"])?$_POST["catsearchchoice"]:array());
$tagsearchchoice = (isset($_POST["tagsearchchoice"])?$_POST["tagsearchchoice"]:array());

$storage =& $gallery->getStorage();

list ($ret, $aclIds) = GalleryCoreApi::fetchAccessListIds('core.view',$gallery->getActiveUserId());
if ($ret) {
return array($ret, null);
}
if (empty($aclIds)) {
return array(null,
array('start' => 0, 'end' => '0',
'count' => 0, 'results' => array()));
}
$aclMarkers = GalleryUtilities::makeMarkers(count($aclIds));

$cq = 'SELECT
COUNT([DataItem::id])
FROM
[DataItem], [TagMap], [TagItemMap], [GalleryAccessSubscriberMap], [Category]
WHERE
[DataItem::id] = [TagItemMap::itemId]
AND
[DataItem::id] = [GalleryAccessSubscriberMap::itemId]
AND
[TagMap::tagId] = [TagItemMap::tagId]
AND
[TagMap::catId] = [Category::catId]
AND
[GalleryAccessSubscriberMap::accessListId] IN (%s) ';
if(sizeof($tagsearchchoice) != 0 || sizeof($catsearchchoice) != 0){
$cq .= 'AND
( ';
$i = false;
foreach ($tagsearchchoice as $tagname){
$cq .= ($i?' AND ':''). ' [TagMap::tagName] LIKE ? ';
$i = true;
}
foreach ($catsearchchoice as $catid){
$cq .= ($i?' AND ':''). ' [Category::catId] = ? ';
$i = true;
}
$cq .= ')';
}
$countQuery = sprintf($cq, $aclMarkers);

$sq = 'SELECT
[TagMap::tagName],
[DataItem::id]
FROM
[DataItem], [TagMap], [TagItemMap], [GalleryAccessSubscriberMap], [Category]
WHERE
[DataItem::id] = [TagItemMap::itemId]
AND
[DataItem::id] = [GalleryAccessSubscriberMap::itemId]
AND
[TagMap::tagId] = [TagItemMap::tagId]
AND
[TagMap::catId] = [Category::catId]
AND
[GalleryAccessSubscriberMap::accessListId] IN (%s) ';
if(sizeof($tagsearchchoice) != 0 || sizeof($catsearchchoice) != 0){
$sq .= ' AND
( ';
$i = false;
foreach ($tagsearchchoice as $tagname){
$sq .= ($i?' AND ':''). ' [TagMap::tagName] LIKE ? ';
$i = true;
}
foreach ($catsearchchoice as $catid){
$sq .= ($i?' AND ':''). ' [Category::catId] = ? ';
$i = true;
}

$sq .= ')';
}
$query = sprintf($sq, $aclMarkers);

$data = $aclIds;
foreach ($tagsearchchoice as $tagname){
$data[] = '%' . $tagname . '%';
}

foreach ($catsearchchoice as $catid){
$data[] = $catid;
}

/* Find the total */
list ($ret, $results) = $gallery->search($countQuery, $data);
if ($ret) {
return array($ret, null);
}
$result = $results->nextResult();
$numRows = (int)$result[0];

list ($ret, $results) = $gallery->search(
$query, $data, array('limit' => array('offset' => $offset, 'count' => $count)));
if ($ret) {
return array($ret, null);
}
$searchResults = array();
while ($result = $results->nextResult()) {
$fields = array();
$fields[] = array('key' => 'Tag Name', 'value' => $result[0]);
$searchResults[] = array('itemId' => (int)$result[1],'fields' => $fields);
}

foreach ($searchResults as $result) {
if (isset($result['itemId'])) {
$itemIds[$result['itemId']] = 1;
}
}
if (isset($itemIds)){
$itemIds = array_keys($itemIds);
} else {
$itemIds = array();
}

list ($ret, $slideshow) =
GalleryCoreApi::newFactoryInstance('SlideshowInterface_1_0');
if ($ret || !isset($slideshow)) {
return array($ret, null);
}

list ($ret, $cr['delegate']) = $slideshow->getLinkData($itemIds);
if ($ret) {
return array($ret, null);
}

$status = $error = array();
$cr['delegate']['view'] = 'tags.TagSearch';
$cr['status'] = $status;
$cr['error'] = $error;
return array(null, $cr);

}

}

class TagSearchView extends GalleryView {

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

$list = GalleryUtilities::getAllRequestVariables();
if (isset($list["itemList"])){
$itemList = array();
$iid = array();
$items = array();
if (strlen($list["itemList"]) > 0){
$iid = explode("|", $list["itemList"]);

list ($ret, $itemList) = GalleryCoreApi::loadEntitiesById($iid);
if ($ret) {
return array($ret, null);
}
}
foreach ($itemList as $item) {
$items[$item->getId()] = (array)$item;
}

$thumbnails = array();
list ($ret, $thumbnailList) = GalleryCoreApi::fetchThumbnailsByItemIds($iid);
if ($ret) {
return array($ret, null);
}
foreach ($thumbnailList as $thumbnail) {
$thumbnails[$thumbnail->getParentId()] = (array)$thumbnail;
}
} else {
$thumbnails = array();
$items = false;
}

if ($form['formName'] != 'TagSearch') {
$form['formName'] = 'TagSearch';
}

$template->setVariable('allcats', CategoryHelper::getAllCats());
$template->setVariable('taghelper', new TagsHelper());
$template->setVariable('items', $items);
$template->setVariable('thumbnails', $thumbnails);

return array(null, array('body' => 'modules/tags/templates/TagSearch.tpl'));
}
}
?>

Also the code for TagSearch class

<?php
/*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2007 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.
*/

GalleryCoreApi::requireOnce('modules/search/classes/GallerySearchInterface_1_0.class');

/**
* This is an implementation of the search module's SearchInterface_1_0
* @package Tags
* @subpackage Classes
* @author Eric Daniel <ehdaniel@gmail.com>
* @version $Revision: 1060 $
*/
class TagsSearch extends GallerySearchInterface_1_0 {
/**
* @see GallerySearchInterface_1_0::getSearchModuleInfo
*/
function getSearchModuleInfo() {
list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'tags');
if ($ret) {
return array($ret, null);
}

$info = array('name' => $module->translate('Tags'),
'description' => $module->translate('Tags Module'),
'options' => array(
'tags' =>
array('description' => $module->translate('Search Tags'),
'enabled' => 1)));
return array(null, $info);
}

/**
* @see GallerySearchInterface_1_0::search
*/
function search($options, $criteria, $offset=0, $count=-1) {
global $gallery;
$storage =& $gallery->getStorage();

list ($ret, $aclIds) = GalleryCoreApi::fetchAccessListIds('core.view',
$gallery->getActiveUserId());
if ($ret) {
return array($ret, null);
}
if (empty($aclIds)) {
return array(null,
array('start' => 0, 'end' => '0',
'count' => 0, 'results' => array()));
}

$aclMarkers = GalleryUtilities::makeMarkers(count($aclIds));
$countQuery = sprintf('
SELECT
COUNT([DataItem::id])
FROM
[DataItem], [TagMap], [TagItemMap], [GalleryAccessSubscriberMap]
WHERE
[DataItem::id] = [TagItemMap::itemId]
AND
[DataItem::id] = [GalleryAccessSubscriberMap::itemId]
AND
[TagMap::tagId] = [TagItemMap::tagId]
AND
[GalleryAccessSubscriberMap::accessListId] IN (%s)
AND
[TagMap::tagName] LIKE ?
', $aclMarkers);

$query = sprintf('
SELECT
[TagMap::tagName],
[DataItem::id]
FROM
[DataItem], [TagMap], [TagItemMap], [GalleryAccessSubscriberMap]
WHERE
[DataItem::id] = [TagItemMap::itemId]
AND
[TagMap::tagId] = [TagItemMap::tagId]
AND
[DataItem::id] = [GalleryAccessSubscriberMap::itemId]
AND
[GalleryAccessSubscriberMap::accessListId] IN (%s)
AND
[TagMap::tagName] LIKE ?
ORDER BY
[DataItem::id] DESC
', $aclMarkers);

$data = $aclIds;
$data[] = '%' . $criteria . '%';

/* Find the total */
list ($ret, $results) = $gallery->search($countQuery, $data);
if ($ret) {
return array($ret, null);
}
$result = $results->nextResult();
$numRows = (int)$result[0];

list ($ret, $results) = $gallery->search(
$query, $data, array('limit' => array('offset' => $offset, 'count' => $count)));
if ($ret) {
return array($ret, null);
}

list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'tags');
if ($ret) {
return array($ret, null);
}

$tagNameText = $module->translate('Tag Name');

$searchResults = array();
while ($result = $results->nextResult()) {
$fields = array();
$fields[] = array('key' => $tagNameText, 'value' => $result[0]);

$searchResults[] = array('itemId' => (int)$result[1],
'fields' => $fields);
}

$data = array('start' => $numRows == 0 ? 0 : $offset+1,
'end' => $numRows == 0 ? 0 : $offset + sizeof($searchResults),
'count' => $numRows,
'results' => $searchResults);
return array(null, $data);
}
}
?>

 
rockarena

Joined: 2007-05-03
Posts: 15
Posted: Mon, 2007-10-01 08:39

I figured out what is wrong. However the solution is not that easy. I'll try to keep you guys posted about this solution if I can find it but I seriously doubt it. It involves heavy querying. It shoudl be done by a Boolean Query I guess but I don't know how to do this. It seems really hard to do!