<?php

/**
 * product actions.
 *
 * @package    sf
 * @subpackage product
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {

    }
    public function executeShow(sfWebRequest $request)
    {
        $product = $this->getRoute()->getObject();
        $this->forward404Unless(!$product->getIsDelete() && $product->getOfferCount() > 0);
        if(!$product->getIsDetail()){
            $cats = $product->getCats();
            if(count($cats) > 0){
                $this->redirect('@catalog_show?alias=' . $cats[0]['alias'] . '&sf_culture=' . $this->getUser()->getCulture());
            }
        }
        $this->forward404Unless($product->getIsDetail());
        $this->culture = $this->getUser()->getCulture();
        $this->basket_id = $this->getUser()->getAttribute('basket_id');

        $this->product = Doctrine_Query::create()
            ->select("p.*, c.*, ct.*, o.*, ppv.*, pp.*, ppl.*, t.*, to.*, tpp.*, bo.*")
            ->from('Product p')
            ->leftJoin('p.Cats c')
            ->leftJoin("c.Translation ct")
            ->leftJoin('p.Offer o WITH o.is_delete = 0')
            ->leftJoin('o.BasketOffer bo WITH bo.basket_id = ?', $this->basket_id)
//            ->leftJoin('(SELECT * FROM Product cp LEFT JOIN Offer co ON co.id = cp.product_id WHERE co.is_delete = 0 ORDER BY co.sort ASC ) o ON o.id = p.product_id')
            ->leftJoin('p.ProductPropValue ppv')
            ->leftJoin('ppv.ProductProp pp')
            ->leftJoin('pp.ProductPropList ppl')
            ->innerJoin('p.Translation t WITH t.lang = \'' . $this->culture . '\'')
            ->leftJoin('o.Translation to WITH to.lang = \'' . $this->culture . '\'')
            ->leftJoin('pp.Translation tpp WITH tpp.lang = \'' . $this->culture . '\'')
            ->leftJoin('ppl.Translation tppl WITH tppl.lang = \'' . $this->culture . '\'')
            ->andWhere('p.id = ?', $product->getId())
            ->andWhere("p.is_delete = 0")
            ->orderBy('o.sort asc')
            ->fetchOne();


        $this->setLayout('layoutPage');

        if ($this->getUser()->hasAttribute('number_of_views') && $this->getUser()->getAttribute('number_of_views') != false) {
            $number_of_view = $this->getUser()->getAttribute('number_of_views');
            if ($this->getUser()->getAttribute('number_of_views') < 10) {
                if (!$this->getUser()->hasAttribute('ids_view_product') || $this->getUser()->getAttribute('ids_view_product') == false) {
                    $this->getUser()->setAttribute('ids_view_product', $this->product['id'] . ';');
                } else {
                    $str_ids = $this->getUser()->getAttribute('ids_view_product');
                    $ids = explode(';', $str_ids);
                    if (!in_array($this->product['id'], $ids)) {
                        $number_of_view++;
                        $this->getUser()->setAttribute('number_of_views', $number_of_view);
                        $this->getUser()->setAttribute('ids_view_product', $str_ids . $this->product['id'] . ';');
                    }
                }
            }
            if ($number_of_view == 5 || $number_of_view == 7 || $number_of_view == 10) {
                if ($this->getUser()->hasAttribute('phone_number') && $this->getUser()->getAttribute('phone_number') != false) {
                    $date_now = new DateTime('now');
                    $date_limit = new DateTime('now');
                    $date_limit->modify('-30 day');
                    $phone_discount = Doctrine_Query::create()
                        ->select()
                        ->from('DiscountPhone dp')
                        ->where('dp.phone = ?', $this->getUser()->getAttribute('phone_number'))
                        ->andWhere('dp.order_id is null')
                        ->andWhere('dp.created_at BETWEEN STR_TO_DATE(\'' . $date_limit->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\') AND STR_TO_DATE(\'' . $date_now->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\')')
                        ->execute();
                    if (count($phone_discount) > 0) {
                        $phone_discount = $phone_discount->getFirst();
                        $phone_discount->setActiveDiscount($number_of_view);
                        $phone_discount->save();
                        $this->getUser()->setAttribute('active_discount', $number_of_view);
                    }
                }
            }
        }
    }
}
