Forum

Thread tagged as: Question, Configuration, Gallery

Gallery search

How do i get the search function to pick up the keywords element on my images in the gallery app?

Thank you.

Denise Courtney

Denise Courtney 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to write a search handler for Gallery - it doesn't do this by default.

Ok, how do I do this? can you give me an example please.

Drew McLellan

Drew McLellan 2638 points
Perch Support

The PerchBlog_SearchHandler class in Blog is a good example.

Drew McLellan

Drew McLellan 2638 points
Perch Support

The search handler docs are here: https://docs.grabaperch.com/api/searchhandler/

Hi Drew

I have tried following that and created a search handler in perch_gallery but it doesn't seem to work, well show any results.

Can you please check it through for me. I basically want to search my tag and desc fields in the gallery and produce results showing the ImageID, imageSlug, desc, thumb (image), album details etc.

<?php

class PerchGallery_SearchHandler implements PerchAPI_SearchHandler
{

    private static $tmp_url_vars = false;

    public static function get_admin_search_sql($key)
    {
        $API = new PerchAPI(1.0, 'perch_gallery');
        $db = $API->get('DB');

        $sql = 'SELECT \''.__CLASS__.'\' AS source, MATCH(imageDynamicFields) AGAINST('.$db->pdb($key).') AS score, imageID, imageSlug, thumb, desc, tag, imageDynamicFields, albumTitle, albumSlug
 FROM '.PERCH_DB_PREFIX.'gallery_albums          
 WHERE imageCount>1
AND MATCH(imageID, imageDynamicFields) AGAINST('.$db->pdb($key).')';  

        return $sql;
    }

    public static function get_search_sql($key)
    {
        $API = new PerchAPI(1.0, 'perch_gallery');
        $db = $API->get('DB');

        $sql = 'SELECT \''.__CLASS__.'\' AS source, MATCH(imageDynamicFields) AGAINST('.$db->pdb($key).') AS score, imageID, imageSlug, thumb, desc, tag, imageDynamicFields, albumTitle, albumSlug
               FROM '.PERCH_DB_PREFIX.'gallery_albums
WHERE imageCount>1
AND ( 
concat(" ", imageID, " ") REGEXP '.$db->pdb('[[:<:]]'.$key.'[[:>:]]').' 
OR concat(" ", imageDynamicFields, " ") REGEXP '.$db->pdb('[[:<:]]'.$key.'[[:>:]]').' 
) ';

        return $sql;
    }
 public static function format_result($key, $options, $result)
    {
        $result['imageID'] = $result['col1'];
        $result['imageSlug'] = $result['col2'];
        $result['thumb'] = $result['col3'];
        $result['desc'] = $result['col4'];
        $result['tag'] = $result['col5'];
        $result['imageDynamicFields'] = $result['col5'];
        $result['albumTitle'] = $result['col6'];
         $result['albumSlug'] = $result['col7'];

        $Settings   = PerchSettings::fetch();

        $html = PerchUtil::excerpt_char($result['imageDynamicFields'], $options['excerpt-chars'], true);
        // keyword highlight
        $html = preg_replace('/('.$key.')/i', '<em class="keyword">$1</em>', $html);

        $match = array();

        $match['url']     = $Settings->get('perch_gallery_image.php')->settingValue();
        self::$tmp_url_vars = $result;
        $match['url'] = preg_replace_callback('/{([A-Za-z0-9_\-]+)}/', array('self', "substitute_url_vars"), $match['url']);
        self::$tmp_url_vars = false;

        $match['title']   = $result['imageID'];
        $match['excerpt'] = $html;
        $match['key']     = $key;
        return $match;
    }

    private static function substitute_url_vars($matches)
    {
        $url_vars = self::$tmp_url_vars;
        if (isset($url_vars[$matches[1]])){
            return $url_vars[$matches[1]];
        }
    }

    public static function format_admin_result($key, $options, $result)
    {
        $result['imageID']       = $result['col1'];

        $self = __CLASS__;

        $out = $self::format_result($key, $options, $result);

        $out['url'] = PERCH_LOGINPATH.'/addons/apps/perch_gallery/edit/?id='.$result['imageID'];

        return $out;
    }

}

Also I did include the following in the gallery runtime... class PerchGallery_SearchHandler implements PerchAPI_SearchHandler

Thank you.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What result do you get? Have you turned on debug to check for errors?

It is basically showing no results from any words I try which are listed in the gallery image description and keywords.

Debug Message SELECT * FROM perch2_pages WHERE pagePath='/search.php' LIMIT 1 Using template: /templates/search/search-form.html SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/search.php' OR regionPage='' ORDER BY regionPage DESC Search term: waterfall SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 SELECT SQL_CALC_FOUND_ROWS DISTINCT 'PerchContent_SearchHandler' AS source, MATCH(ci.itemSearch) AGAINST('waterfall') AS score, r.regionPage AS col1, ci.itemSearch AS col2, ci.itemJSON AS col3, r.regionOptions AS col4, p.pageNavText AS col5, p.pageTitle AS col6, regionTemplate AS col7, r.regionKey AS col8 FROM perch2_content_regions r, perch2_content_items ci, perch2_pages p WHERE r.regionID=ci.regionID AND r.regionRev=ci.itemRev AND r.pageID=p.pageID AND r.regionPage!='' AND r.regionSearchable=1 AND (MATCH(ci.itemSearch) AGAINST('waterfall') OR MATCH(ci.itemSearch) AGAINST('waterfall') ) AND r.regionPage LIKE '/%' UNION SELECT 'PerchShop_SearchHandler' AS source, MATCH(productTitle, productDescRaw) AGAINST('waterfall') AS score, productTitle, productSlug, productPrice, productDescHTML, productID, "", "", "" FROM perch2_shop_products WHERE productStatus='Live' AND MATCH(productTitle, productDescRaw) AGAINST('waterfall') UNION SELECT 'PerchBlog_SearchHandler' AS source, MATCH(postTitle, postDescRaw, postTags) AGAINST('waterfall') AS score, postTitle, postSlug, postDateTime, postDescHTML, postID, sectionSlug, "", "" FROM perch2_blog_posts p, perch2_blog_sections s WHERE postStatus='Published' AND postDateTime<'2015-05-23 20:04:38' AND p.sectionID=s.sectionID AND MATCH(postTitle, postDescRaw, postTags) AGAINST('waterfall') ORDER BY score DESC LIMIT 0, 10 SELECT SQL_CALC_FOUND_ROWS DISTINCT 'PerchContent_SearchHandler' AS source, 0-(LENGTH(r.regionPage)-LENGTH(REPLACE(r.regionPage, '/', ''))) AS score, r.regionPage AS col1, ci.itemSearch AS col2, ci.itemJSON AS col3, r.regionOptions AS col4, p.pageNavText AS col5, p.pageTitle AS col6, regionTemplate AS col7, r.regionKey AS col8 FROM perch2_content_regions r, perch2_content_items ci, perch2_pages p WHERE r.regionID=ci.regionID AND r.regionRev=ci.itemRev AND r.pageID=p.pageID AND r.regionPage!='*' AND r.regionSearchable=1 AND ci.itemSearch REGEXP '[[:<:]]waterfall[[:>:]]' AND r.regionPage LIKE '/%' UNION SELECT 'PerchShop_SearchHandler' AS source, productPrice AS score, productTitle, productSlug, productPrice, productDescHTML, productID, "", "", "" FROM perch2_shop_products WHERE productStatus='Live' AND ( productTitle REGEXP '[[:<:]]waterfall[[:>:]]' OR productDescRaw REGEXP '[[:<:]]waterfall[[:>:]]' ) UNION SELECT 'PerchBlog_SearchHandler' AS source, postDateTime AS score, postTitle, postSlug, postDateTime, postDescHTML, postID, sectionSlug, "", "" FROM perch2_blog_posts p, perch2_blog_sections s WHERE postStatus='Published' AND postDateTime<'2015-05-23 20:04:38' AND p.sectionID=s.sectionID AND ( concat(" ", postTitle, " ") REGEXP '[[:<:]]waterfall[[:>:]]' OR concat(" ", postDescRaw, " ") REGEXP '[[:<:]]waterfall[[:>:]]' OR concat(" ", postTags, " ") REGEXP '[[:<:]]waterfall[[:>:]]' ) ORDER BY score ASC LIMIT 0, 10 SELECT FOUND_ROWS() AS count Using template: /templates/search/search-result.html Using template: /templates/search/search-form.html Array ( [type] => 2 [message] => call_user_func() expects parameter 1 to be a valid callback, class 'PerchGallery_SearchHandler' not found [file] => /var/zpanel/hostdata/nickoakley/public_html/nickoakleyphotography_com/perch/core/apps/content/PerchContent.class.php [line] => 730 )

Drew McLellan

Drew McLellan 2638 points
Perch Support

You see the error there, right?

Well I see at the bottom it says that PerchGallery_SearchHandler could not be found but it is there in the perch_gllery?

Should it be elsewhere? Does something need to be referenced here too ...perch/core/apps/content/PerchContent.class.php

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it included in your perch_gallery/runtime.php file?

Yes

<?php PerchSystem::register_search_handler('PerchGallery_SearchHandler');

require('PerchGallery_Albums.class.php');
require('PerchGallery_Album.class.php');
require('PerchGallery_Images.class.php');
require('PerchGallery_Image.class.php');
require('PerchGallery_ImageVersions.class.php');
require('PerchGallery_ImageVersion.class.php');

........
Drew McLellan

Drew McLellan 2638 points
Perch Support

Where? I don't see it included there.

If I add it in two the require bit as the Perch_blog does it wipes out my entire search page, just get a blank white page

<?php PerchSystem::register_search_handler('PerchGallery_SearchHandler');

require('PerchGallery_Albums.class.php');
require('PerchGallery_Album.class.php');
require('PerchGallery_Images.class.php');
require('PerchGallery_Image.class.php');
require('PerchGallery_ImageVersions.class.php');
require('PerchGallery_ImageVersion.class.php');
 require('PerchGallery_SearchHandler.class.php');
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok , I'll check this out, thanks.

So I guess the blank page is progress!

Drew McLellan

Drew McLellan 2638 points
Perch Support

It depends on what error you're getting.

Hi Drew

I have had this response back from my hosting co. regarding the php error...


Ive had a look at the errors and its coming back with…

[Tue May 26 11:31:25 2015] [error] [client 84.45.219.48] PHP Fatal error: Class PerchGallery_SearchHandler contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (PerchAPI_SearchHandler::get_backup_search_sql) in /var/zpanel/hostdata/nickoakley/public_html/nickoakleyphotography_com/perch/addons/apps/perch_gallery/PerchGallery_SearchHandler.class.php on line 87

The php error is throwing it back out as a perch error so maybe worth forwarding this to the Perch support team to see if they can shed any light on it for me?


Rachel Andrew

Rachel Andrew 394 points
Perch Support

That error is in your search handler, not Perch code. We can't help with custom PHP development.

Is there anyway at all you can find the error. I just followed the perch_blog search handler, so it's not really that 'custom'. Really need this search feature and for something that works so perfect and simply with the rest of the sites content and apps it is so frustrating that I cannot configure it for the gallery.

Any help would be very much appreciated.

Thank you.