<?php

/**
 * search actions.
 *
 * @package    sf
 * @subpackage search
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */

    public function executeSearch(sfWebRequest $request)
    {
        $this->res = array();
        $this->rs = array();
        $articles = $videos = $cats = $products = array();
        $this->query = $this->getRequestParameter('q');
        $this->page = $this->getRequestParameter('p', 1);
        $this->total_found = 0;
        $options = array(
            'limit'   => 100,
            'offset'  => ($this->page - 1) * 10,
            'weights' => array(100, 1),
            'sort'    => sfSphinxClient::SPH_SORT_EXTENDED,
            'sortby'  => '@weight DESC',
        );
        if (!empty($this->query))
        {
            $culture = $this->getUser()->getCulture();
            $this->sphinx = new sfSphinxClient($options);
            $this->res = $this->sphinx->Query($this->query, 'stone_' . $culture);
            if($this->res['total_found'] > 0) {
                foreach ($this->res['matches'] as $matches) {
                    if ($matches['attrs']['row_type'] == 1) {
                        $articles[] = ($matches['id'] - 1) / 10;
                    } elseif ($matches['attrs']['row_type'] == 2) {
                        $videos[] = ($matches['id'] - 2) / 10;
                    } elseif ($matches['attrs']['row_type'] == 3) {
                        $cats[] = ($matches['id'] - 3) / 10;
                    } elseif ($matches['attrs']['row_type'] == 4) {
                        $products[] = ($matches['id'] - 4) / 10;
                    }
                }

                if(count($articles) > 0) {
                    $q = Doctrine_Query::create()
                        ->select("a.id, a.alias, a.cover, a.created_at, t.*")
                        ->from("Article a")
                        ->innerJoin("a.Translation t")
                        ->where("t.lang = ?", $this->getUser()->getCulture())
                        ->andWhere("t.available = 1")
                        ->andWhereIn("a.id", $articles)
                        ->orderBy("a.created_at DESC")
                        ->fetchArray();
                    if(count($q) > 0){
                        foreach($q as $r)
                        {
                            $this->rs[0]['article'][$r['id']] = array(
                                'title' => $r['Translation'][$culture]['title'],
                                'link' => 'article/show?alias=' . $r['alias'],
                                'body' => strip_tags($r['Translation'][$culture]['body']),
                            );
                            $this->total_found++;
                        }
                    }
                }
                if(count($videos) > 0) {
                    $q = Doctrine_Query::create()
                        ->select("v.*, t.*")
                        ->from("Video v")
                        ->innerJoin("v.Translation t")
                        ->where("t.lang = ?", $culture)
                        ->andWhere("t.available = 1")
                        ->andWhereIn("v.id", $videos)
                        ->fetchArray();
                    if(count($q) > 0){
                        foreach($q as $r)
                        {
                            $code = Video::getYoutubeCode($r['vid']);
                            if($code){
                                $this->rs[0]['video'][$r['id']] = array(
                                    'title' => $r['Translation'][$culture]['title'],
                                    'link' => 'https://www.youtube.com/embed/' . $code . '?ecver=2'
                                );
                                $this->total_found++;
                            }
                        }
                    }
                }
                if(count($cats) > 0) {
                    $q = Doctrine_Query::create()
                        ->select("c.alias, c.photo, c.parent_id, t.*")
                        ->from("Cat c")
                        ->innerJoin("c.Translation t")
                        ->andWhereIn("c.id", $cats)
                        ->fetchArray();
                    if(count($q) > 0){
                        foreach($q as $r)
                        {
                            $this->rs[0]['cat'][$r['id']] = array(
                                'title' => $r['Translation'][$culture]['title'],
                                'link' => 'catalog/show?alias=' . $r['alias']
                            );
                            $this->total_found++;
                        }
                    }
                }
                if(count($products) > 0) {
                    $q = Doctrine_Query::create()
                        ->select("p.id, p.alias, p.product_img, p.is_detail, p.offer_count, t.id, t.lang, t.title")
                        ->from("Product p")
                        ->leftJoin("p.Translation t")
                        ->whereIn("p.id", $products)
                        ->andWhere("p.offer_count > 0")
                        ->fetchArray();
                    if(count($q) > 0){
                        foreach($q as $r)
                        {
                            $this->rs[0]['product'][$r['id']] = array(
                                'title' => $r['Translation'][$culture]['title'],
                                'link' => 'product/show?alias=' . $r['alias']
                            );
                            $this->total_found++;
                        }
                    }
                }
            }
        }
    }
}
