<?php

/**
 * review actions.
 *
 * @package    sf
 * @subpackage review
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reviewActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->review_form = new wwwReviewForm();
        if($request->isMethod('post')){

            $params = $request->getParameter($this->review_form->getName());
            $params['lang'] = $this->getUser()->getCulture();
            $params['phone'] = preg_replace("/[^0-9]/", '', $params['phone']);
            $this->review_form->bind($params);
            $valid = $this->review_form->isValid();
            if($valid){
                $this->review_form->save();
            }
            echo $this->getPartial('review/review_form', array('review_form' => $this->review_form, 'valid' => $valid));
            return sfView::NONE;
        }else
        {
            $this->culture = $this->getUser()->getCulture();

            $reviews = Doctrine_Query::create()
                ->select("name, body, file, lang, is_activated, created_at")
                ->from("Review")
                ->where("lang = ?", $this->getUser()->getCulture())
                ->andWhere("is_activated = 1")
                ->orderBy("created_at ASC")
                ->fetchArray();
            $this->setLayout('layoutPage');

            $count_reviews = count($reviews);

            if ($request->getParameter('tag_id'))
            {
                $this->tag_id = $request->getParameter('tag_id');
            }else
            {
                $this->tag_id = 'all';
            }

            if ($request->getParameter('tag_id') && $request->getParameter('tag_id') != 'all' && $count_reviews >= sfConfig::get('app_view_count_tag'))
            {
                unset($reviews);

                $reviews_tags = Doctrine_Query::create()
                    ->select('')
                    ->from('Tag t')
                    ->where('t.id = ?', $request->getParameter('tag_id'))
                    ->leftJoin('t.ReviewTags rt WITH t.id = rt.tag_id')
                    ->leftJoin('rt.Review r WITH rt.review_id = r.id')
                    ->fetchArray();
                $reviews = array();
                foreach ($reviews_tags[0]['ReviewTags'] as $review)
                {
                    $reviews[] = $review['Review'];
                }
                $this->forward404Unless($reviews);
            }
            $limit =  ceil(count($reviews) / 3);
            $counter = 0;
            $arrKey = 0;

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

            $this->reviews_tags = array();

            foreach ($tags as $tag)
            {
                if (count($tag['ReviewTags']) > 0 )
                {
                    $this->reviews_tags[] = $tag;
                }
            }
            $group_reviews = array();

            foreach ($reviews as $review)
            {
                if ($counter == $limit)
                {
                    $counter = 0;
                    $arrKey++;
                }
                $group_reviews[$arrKey][] = $review;
                $counter++;
            }

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