4

I'm having trouble to implement a smart search plugin for my component. I searched and I could only find this question that looked a bit like mine, but it didn't work in my case.

In this component I have a table #__descontos_promocoes from which I would like to search for the column nome.

The Finder is not indexing anything from my component. I have tried to use the command line to run php cli/finder_indexer.php and I fixed every warning/error that was showing up, including the 'Undefined index: HTTP_HOST'.

I think the problem may be somewhere inside my function index(FinderIndexerResult $item, $format = 'html') but I don't know how to fix it.

Can anyone please help me? This is my plugin's php file:

<?php

defined('JPATH_BASE') or die;
require_once JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer/adapter.php';

class PlgFinderDescontosPromocoes extends FinderIndexerAdapter
{
    protected $context = 'Promocoes';
    protected $extension = 'com_descontos';
    protected $layout = 'promocoes';
    protected $type_title = 'Promocao';
    protected $table = '#__descontos_promocoes';
    protected $state_field = 'state';
    protected $autoloadLanguage = true;

    /**
     * Method to index an item. The item must be a FinderIndexerResult object.
     *
     * @param   FinderIndexerResult  $item    The item to index as an FinderIndexerResult object.
     * @param   string               $format  The item format.  Not used.
     *
     * @return  void
     *
     * @since   2.5
     * @throws  Exception on database error.
     */
    protected function index(FinderIndexerResult $item, $format = 'html')
    {
        // Check if the extension is enabled.
        if (JComponentHelper::isEnabled($this->extension) == false)
        {
            return;
        }

        $item->setLanguage();

        // Need to import component route helpers dynamically, hence the reason it's handled here.
        $path = JPATH_SITE . '/components/' . $item->extension . '/helpers/route.php';

        if (is_file($path))
        {
            include_once $path;
        }

        $extension = ucfirst(substr($item->extension, 4));

        // Initialize the item parameters.
        $registry = new JRegistry;
        $registry->loadString($item->params);
        $item->params = $registry;

        $registry = new JRegistry;
        $registry->loadString($item->metadata);
        $item->metadata = $registry;

        /*
         * Add the meta-data processing instructions based on the category's
         * configuration parameters.
         */
        // Add the meta-author.
        $item->metaauthor = $item->metadata->get('author');

        // Handle the link to the meta-data.
        $item->addInstruction(FinderIndexer::META_CONTEXT, 'link');
        $item->addInstruction(FinderIndexer::META_CONTEXT, 'metakey');
        $item->addInstruction(FinderIndexer::META_CONTEXT, 'metadesc');
        $item->addInstruction(FinderIndexer::META_CONTEXT, 'metaauthor');
        $item->addInstruction(FinderIndexer::META_CONTEXT, 'author');

        // Deactivated Methods
        // $item->addInstruction(FinderIndexer::META_CONTEXT, 'created_by_alias');

        // // Trigger the onContentPrepare event.
        // $item->summary = FinderIndexerHelper::prepareContent($item->summary, $item->params);

        // // Build the necessary route and path information.
        // $item->url = $this->getURL($item->id, $item->extension, $this->layout);

        // $class = $extension . 'HelperRoute';

        // if (class_exists($class) && method_exists($class, 'getCategoryRoute'))
        // {
        //  $item->route = $class::getCategoryRoute($item->id, $item->language);
        // }
        // else
        // {
        //  $item->route = ContentHelperRoute::getCategoryRoute($item->slug, $item->catid);
        // }

        // $item->path = FinderIndexerHelper::getContentPath($item->route);

        // // Get the menu title if it exists.
        // $title = $this->getItemMenuTitle($item->url);

        // // Adjust the title if necessary.
        // if (!empty($title) && $this->params->get('use_menu_title', true))
        // {
        //  $item->title = $title;
        // }

        // Translate the state. Categories should only be published if the parent category is published.
        // $item->state = $this->translateState($item->state);

        // Add the type taxonomy data.
        $item->addTaxonomy('Type', 'Promocao');

        // Add the language taxonomy data.
        $item->addTaxonomy('Language', $item->language);

        // Get content extras.
        FinderIndexerHelper::getContentExtras($item);

        // Index the item.
        $this->indexer->index($item);




        /**
         * I have previously tested this function with only the code bellow (still doesn't index anything):
         */

        // if (JComponentHelper::isEnabled($this->extension) == false) {
        //  return;
        // }

        // $item->url = $this->getURL($item->id, 'com_descontos&layout=promocoes', $this->layout);
        // $item->route = 'index.php?option=com_descontos&view=promocoes&layout=promocoes&id='.$item->id;
        // $item->addTaxonomy('Type', 'Promocao');
        // $item->addTaxonomy('Language', $item->language);
        // $this->indexer->index($item);
    }

    /**
     * Method to setup the indexer to be run.
     *
     * @return  boolean  True on success.
     *
     * @since   2.5
     */
    protected function setup()
    {
        // Load com_content route helper as it is the fallback for routing in the indexer in this instance.
        include_once JPATH_SITE . '/components/com_content/helpers/route.php';

        return true;
    }

    /**
     * Method to get the SQL query used to retrieve the list of content items.
     *
     * @param   mixed  $query  A JDatabaseQuery object or null.
     *
     * @return  JDatabaseQuery  A database object.
     *
     * @since   2.5
     */
    protected function getListQuery($query = null)
    {
        $db = JFactory::getDbo();

        // Check if we can use the supplied SQL query.
        $query = $query instanceof JDatabaseQuery ? $query : $db->getQuery(true)
            ->select('a.id as id, a.nome as title')
        //  ->select('a.id, a.catid, a.title, a.alias, a.url AS link, a.description AS summary')
        //  ->select('a.metakey, a.metadesc, a.metadata, a.language, a.access, a.ordering')
        //  ->select('a.created_by_alias, a.modified, a.modified_by')
        //  ->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date')
        //  ->select('a.state AS state, a.created AS start_date, a.params')
        //  ->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');

        // // Handle the alias CASE WHEN portion of the query.
        // $case_when_item_alias = ' CASE WHEN ';
        // $case_when_item_alias .= $query->charLength('a.alias', '!=', '0');
        // $case_when_item_alias .= ' THEN ';
        // $a_id = $query->castAsChar('a.id');
        // $case_when_item_alias .= $query->concatenate(array($a_id, 'a.alias'), ':');
        // $case_when_item_alias .= ' ELSE ';
        // $case_when_item_alias .= $a_id . ' END as slug';
        // $query->select($case_when_item_alias);

        // $case_when_category_alias = ' CASE WHEN ';
        // $case_when_category_alias .= $query->charLength('c.alias', '!=', '0');
        // $case_when_category_alias .= ' THEN ';
        // $c_id = $query->castAsChar('c.id');
        // $case_when_category_alias .= $query->concatenate(array($c_id, 'c.alias'), ':');
        // $case_when_category_alias .= ' ELSE ';
        // $case_when_category_alias .= $c_id . ' END as catslug';
        // $query->select($case_when_category_alias)

            ->from('#__descontos_promocoes AS a');
            // ->join('LEFT', '#__categories AS c ON c.id = a.catid');

        return $query;
    }
}

This is the output from the command line: (it indexes everything except stuff from my component)

artur@artur-desktop:/var/www/10contos$ php5 cli/finder_indexer.php 
Smart Search INDEXER
============================

Starting Indexer
Setting up Finder plugins
Setup 9 items in 0.072 seconds.
 * Processed batch 1 in 0.021 seconds.
Total Processing Time: 0.094 seconds.

Even trying to index in backend inside Finder, doesn't work. I don't get any error or anything inside /var/log/apache2/error.log

1 Answer 1

1

Verify you are using the latest version of Joomla 2.5 Make certain you have the SmartSearch/Finder plugin published. Use the SmartSearch/Finder module. There are 2... search and smartsearch. Run the indexer from within Smart Search enter image description here

It is also helpful to have a link to Smart Search/Finder in your menu. It can be in a hidden menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy