actions.class.php 5.71 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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++;
                        }
                    }
                }
            }
        }
    }
}