<?php
class articleComponents extends sfComponents
{
    public function executeArticle(sfWebRequest $request)
    {
        $this->culture = $this->getUser()->getCulture();
        if ($request->getParameter('alias')) {
            $productTags = Doctrine::getTable('Product')->findOneByAlias($request->getParameter('alias'))->getProductTags();
            $idsTags = array();
            foreach ($productTags as $tag) {
                $idsTags[] = $tag->getTagId();
            }

            if (count($idsTags) > 0) {

                $q = Doctrine_Query::create()
                    ->select("a.id, a.alias, a.cover, t.id, t.title, t.description, t.lang, t.available")
                    ->from("Article a")
                    ->innerJoin("a.Translation t")
                    ->where("t.lang = ?", $this->getUser()->getCulture())
                    ->andWhere("t.available = 1")
                    ->innerJoin('a.ArticleTags at')
                    ->andWhereIn('at.tag_id', $idsTags);
                if (isset($this->limit) && $this->limit) {
                    $q->limit($this->limit);
                }
                $this->articles = $q->fetchArray();
            } else {
                $q = array();
            }
        }else
        {
            $this->articles = Doctrine_Query::create()
                ->select("a.id, a.alias, a.cover, t.id, t.title, t.description, t.lang, t.available")
                ->from("Article a")
                ->innerJoin("a.Translation t")
                ->where("t.lang = ?", $this->getUser()->getCulture())
                ->andWhere("t.available = 1")
                ->limit(3)
                ->fetchArray();
        }
    }
}