Actions

Source Code of This Project

/lib/model/search/Resume.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 resume for search system
 *
 * @package    pwp
 * @subpackage Search
 * @author     Victor Rad'
 */
class Search_Resume extends Search
{
    // added to $indexPath
    protected $indexName = 'resume';

    /**
     * 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('content', $obj->getContent(), 'utf-8'));

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

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

        // get data
        $resume = $this->loadData();
        if ($resume) {
            // highlight found strings and render the partial
            foreach ($resume as $part) {
                $part->setTitle('Resume: '.$part->getTitle());
                $part->setContent($this->query->htmlFragmentHighlightMatches($part->getContent(), '', $highlighter));
                $resumeRendered .= get_partial('resume/item', array('part' => $part));
            }
        }

        return $resumeRendered;
    }

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

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

        if ($foundIds) {
            $resume = DFactory::get('Resume')
                    ->getBuilder()
                    ->getQuery()
                    ->whereIn('r.id', $foundIds)
                    ->execute();
            $this->setFoundData($resume);
        }


        return $resume;
    }

    /**
     * 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
        $resume = DFactory::get('Resume')->getTable()->findAll();

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

}