Actions

Source Code of This Project

/lib/model/search/News.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * Class of news for search system
 *
 * @package    pwp
 * @subpackage Search
 * @author     Victor Rad'
 */
class Search_News extends Search
{
    // added to $indexPath
    protected $indexName = 'news';

    /**
     * Update or insert document into index
     *
     * @param mixed $obj with data
     */
    public function update($obj)
    {
        // remove existing entries
        foreach ($this->index->find('pk:'.$obj->getId()) as $hit){
            $this->index->delete($hit->id);
        }

        $doc = new Zend_Search_Lucene_Document();
        // index fields
        $doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $obj->getId()));
        $doc->addField(Zend_Search_Lucene_Field::UnStored('title', $obj->getTitle(), 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::UnStored('description', $obj->getDescription(), 'utf-8'));

        // add item to the index
        $this->index->addDocument($doc);
        $this->index->commit();
    }

    /**
     * See Search::render()
     */
    public function render()
    {
        // prepare data
        $newsRendered = '';
        $newsList = null;
        sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
        sfContext::getInstance()->getConfiguration()->loadHelpers('Text');
        $highlighter = new My_Zend_Search_Lucene_Search_Highlighter_Default();
        $highlighter->setHighlightColors(sfConfig::get('app_search_highlight_color'));

        // get data
        $newsList = $this->loadData();
        if ($newsList) {
            // highlight found strings and render the partial
            foreach ($newsList as $newsOne) {
                $newsOne->setTitle('News: '.$newsOne->getTitle());
                $newsOne->setDescription(truncate_text($newsOne->getDescription(), 400, '...', true));
                $newsOne->setDescription($this->query->htmlFragmentHighlightMatches( $newsOne->getDescription(), '', $highlighter));
                $newsRendered .= get_partial('news/item', array('news' => $newsOne));
            }
        }

        return $newsRendered;
    }

    /**
     * See Search::find()
     */
    public function find($query)
    {
        return $this->_find($query);
    }

    /**
     * Get data by found ids
     *
     * @return Doctrine_Collection
     */
    public function loadData()
    {
        $newsList = null;
        $foundIds = $this->getFoundIds();

        if ($foundIds) {
            $newsList = DFactory::get('News')
                    ->getBuilder()
                    ->joinProvides()
                    ->getQuery()
                    ->whereIn('n.id', $foundIds)
                    ->execute();
            $this->setFoundData($newsList);
        }

        return $newsList;
    }

    /**
     * See Search::rebuildIndex()
     */
    public function rebuildIndex()
    {
        // delete index files
        sfContext::getInstance()->getConfiguration()->loadHelpers('MyUtil');
        my_unlink($this->indexPath, true);
        // prepare data
        $index = $this->getIndex('create');
        $doc = new Zend_Search_Lucene_Document();
        // get all news from db
        $news = DFactory::get('News')->getTable()->findAll();

        foreach ($news as $newsOne) {
            // index fields
            $doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $newsOne->getId()));
            $doc->addField(Zend_Search_Lucene_Field::UnStored('title', $newsOne->getTitle(), 'utf-8'));
            $doc->addField(Zend_Search_Lucene_Field::UnStored('description', $newsOne->getDescription(), 'utf-8'));
            // add to the index
            $index->addDocument($doc);
            $index->commit();
        }
    }

}