<?php
class reviewComponents extends sfComponents
{
    public function executeReview(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("r.name, r.body, r.file, r.lang, r.is_activated, r.created_at")
                    ->from("Review r")
                    ->where("r.is_activated = 1")
                    ->andWhere("r.lang = ?", $this->getUser()->getCulture())
                    ->orderBy("r.created_at DESC")
                    ->innerJoin('r.ReviewTags rt')
                    ->andWhereIn('rt.tag_id', $idsTags);
                if (isset($this->limit) && $this->limit) {
                    $q->limit($this->limit);
                }
                $this->reviews = $q->fetchArray();
            } else {
                $q = array();
            }
        }else
        {
            $this->reviews = Doctrine_Query::create()
                ->select("r.name, r.body, r.file, r.lang, r.is_activated, r.created_at")
                ->from("Review r")
                ->where("r.is_activated = 1")
                ->andWhere("r.lang = ?", $this->getUser()->getCulture())
                ->orderBy("r.created_at DESC")
                ->fetchArray();
        }
    }
}