<?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();
    }
}
