actions.class.php 4.32 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
<?php

/**
 * article actions.
 *
 * @package    sf
 * @subpackage article
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class articleActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->culture = $this->getUser()->getCulture();

        $this->articles = 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->culture)
            ->andWhere("t.available = 1")
            ->orderBy("a.created_at DESC")
            ->fetchArray();
        $count_articles = count($this->articles);
        if ($request->getParameter('tag_id'))
        {
            $this->tag_id = $request->getParameter('tag_id');
        }else
        {
            $this->tag_id = 'all';
        }

        $tags = Doctrine_Query::create()
            ->select('t.*, tt.*, at.*')
            ->from('Tag t')
            ->leftJoin('t.Translation tt WITH tt.lang = ?', $this->culture)
            ->leftJoin('t.ArticleTags at WITH at.tag_id = t.id')
            ->fetchArray();

        $this->articles_tags = array();

        foreach ($tags as $tag)
        {
            if (count($tag['ArticleTags']) > 0 )
            {
                $this->articles_tags[] = $tag;
            }
        }

        if ($request->getParameter('tag_id') && $request->getParameter('tag_id') != 'all' && $count_articles >= sfConfig::get('app_view_count_tag')) {
            $articles = Doctrine_Query::create()
                ->select('')
                ->from('Tag t')
                ->where('t.id = ?', $request->getParameter('tag_id'))
                ->leftJoin('t.ArticleTags at WITH t.id = at.tag_id')
                ->leftJoin('at.Article a WITH at.article_id = a.id')
                ->innerJoin("a.Translation ta")
                ->andwhere("ta.lang = ?", $this->culture)
                 ->andWhere("ta.available = 1")
                ->orderBy("a.created_at DESC")
                ->fetchArray();

            $this->articles = array();
            foreach ($articles[0]['ArticleTags'] as $article) {
                $this->articles[] = $article['Article'];
            }

            $this->forward404Unless($this->articles);
        }

        if ($request->getParameter('tag_id') && $request->getParameter('ajax'))
        {
            sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
            echo include_partial('article/article', array('articles' => $this->articles, 'die' => true));
        }

    }
    public function executeShow(sfWebRequest $request)
    {
        $alias = trim(strip_tags($request->getParameter('alias')));
        $this->forward404Unless($alias);
        $this->culture = $this->getUser()->getCulture();
        $this->article = Doctrine_Query::create()
            ->select("a.*, t.*")
            ->from("Article a")
            ->innerJoin("a.Translation t")
            ->where("a.alias = ?", $alias)
            ->andWhere("t.lang = ?", $this->culture)
            ->andWhere("t.available = 1")
            ->fetchOne();
        $this->forward404Unless($this->article);
        $this->article_prev = Doctrine_Query::create()
            ->select("a.*, t.*")
            ->from("Article a")
            ->innerJoin("a.Translation t")
            ->where("t.lang = ?", $this->culture)
            ->andWhere("t.available = 1")
            ->andWhere("a.created_at > ?", $this->article->getCreatedAt())
            ->orderBy("a.created_at ASC")
            ->fetchOne();
        $this->article_next = Doctrine_Query::create()
            ->select("a.*, t.*")
            ->from("Article a")
            ->innerJoin("a.Translation t")
            ->where("t.lang = ?", $this->culture)
            ->andWhere("t.available = 1")
            ->andWhere("a.created_at < ?", $this->article->getCreatedAt())
            ->orderBy("a.created_at DESC")
            ->fetchOne();
        $this->setLayout('layoutPage');
        $this->tags_art = Doctrine_Query::create()
            ->select('t.id, tt.*')
            ->from('Tag t')
            ->innerJoin('t.ArticleTags at WITH at.article_id = ?', $this->article->getId())
            ->leftJoin('t.Translation tt WITH tt.lang = ?', $this->culture)
            ->fetchArray();
    }
}