<?php
/*
 * $RCSfile: CustomFieldSearch.class,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.
 */

/**
 * @package CustomField
 * @version $Revision: 1.7 $ $Date: 2005/08/23 03:49:37 $
 * @author Alan Harder <alan.harder@sun.com>
 */

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

/**
 * This is an implementation of the search module's SearchInterface_1_0
 *
 * @package CustomField
 * @subpackage Classes
 *
 */
class CustomFieldSearch extends GallerySearchInterface_1_0 {

    /**
     * @see GallerySearchInterface_1_0::getSearchModuleInfo()
     */
    function getSearchModuleInfo() {
	global $gallery;

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

	$info = array('name' => $module->translate('Custom Fields'),
		      'description' => $module->translate('Custom Fields Module'),
		      'options' => array(
			  'customfield' => array(
			      'description' => $module->translate('Search custom fields'),
			      'enabled' => 1)));
	return array(GalleryStatus::success(), $info);
    }

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

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

	$countQuery = sprintf('
	SELECT
	  COUNT([CustomFieldMap::itemId])
	FROM
	  [CustomFieldMap], [GalleryAccessSubscriberMap]
	WHERE
	  [CustomFieldMap::itemId] =  [GalleryAccessSubscriberMap::itemId] 
          AND
          [GalleryAccessSubscriberMap::accessListId] IN (%s)
	  AND
	  [CustomFieldMap::value] LIKE ?
        ', $aclMarkers);

	$query = sprintf('
	SELECT
	  [CustomFieldMap::itemId],
	  [CustomFieldMap::field],
	  [CustomFieldMap::value]
	FROM
	  [CustomFieldMap], [GalleryAccessSubscriberMap]
	WHERE
	  [CustomFieldMap::itemId] = [GalleryAccessSubscriberMap::itemId]
          AND
          [GalleryAccessSubscriberMap::accessListId] IN (%s)
	  AND
	  [CustomFieldMap::value] LIKE ?
	ORDER BY
	  [CustomFieldMap::itemId] DESC
	', $aclMarkers);

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

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

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

	$searchResults = array();
	while ($result = $results->nextResult()) {
	    $searchResults[] = array('itemId' => (int)$result[0],
		'fields' => array(array('key' => $result[1], 'value' => $result[2])));
	}

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