Actions

Source Code of This Project

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

    /**
     * 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
        $portfolioRendered = '';
        $galleriesOverlay = '';
        $portfolio = 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
        $portfolio = $this->loadData();
        if ($portfolio) {
            // highlight found strings and render the partials
            foreach ($portfolio as $project) {
                $project->setTitle('Portfolio: '.$project->getTitle());
                $project->setContent($this->query->htmlFragmentHighlightMatches($project->getContent(), '', $highlighter));
                $galleriesOverlay .= get_partial('global/galleryOverlay', array('galleryId' => 'galleryId_'.$project->getId()));
                $portfolioRendered .= get_partial('portfolio/item', array('project' => $project));
            }
            slot('galleriesOverlay', $galleriesOverlay);
        }

        return $portfolioRendered;
    }

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

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

        if ($foundIds) {
            $portfolio = DFactory::get('Portfolio')
                    ->getTable()
                    ->createQuery('p')
                    ->whereIn('p.id', $foundIds)
                    ->execute();
            $this->setFoundData($portfolio);
        }

        return $portfolio;
    }

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

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

}