<?php
class catalogComponents extends sfComponents
{
    public function executeCatChildren(sfWebRequest $request)
    {
        if ($this->parentId)
        {
            $parentId = $this->parentId;
            $this->alias = $request->getParameter('alias');
            $this->childrens = Doctrine::getTable('Cat')->findByParentId($parentId);
        }
        else
        {
            return sfView::NONE;
        }

    }


    public function executeLeftMenu(sfWebRequest $request)
    {
        $this->path = $request->getPathInfo();

        $leftMenu = Doctrine_Query::create()
            ->from('cat c')
            //->leftJoin('c.Children ch')
            //->leftJoin('ch.Children ch_ch')
            //->leftJoin('ch_ch.Children ch_ch_ch')
            //->leftJoin('ch_ch_ch.Children ch_ch_ch_ch')
            ->where('c.parent_id is null')
            ->fetchArray();

        $this->menu = $leftMenu;

        $this->open = array();
        $alias = $request->getParameter('alias', null);

        $id = null;
        if ($alias != null)
        {
            $idByAlias = Doctrine::getTable('Cat')->findOneByAlias($alias);
            if ($idByAlias)
            {
                $id = $idByAlias->getId();
            }
        }

        if ($id == null && $alias != null)
        {
            $product = $product = Doctrine::getTable('Product')->findOneByAlias($alias);

            if ($product)
            {
                if (count($product->getCatProducts()) > 0)
                {
                    $id = $product->getCatProducts()->getFirst()->getId();
                }

            }

        }

        if ($id != null)
        {
            $relations = Doctrine_Query::create()
                ->from('catParent cp')
                ->where('cp.cat_id = ?', $id)
                ->fetchArray();

            $ids[] = $id;
            if ($relations)
            {
                foreach ($relations as $relation)
                {
                    $ids[] = $relation['parent_id'];
                }

            }

            $this->open = $ids;
        }



    }
    public function executeLkLeftMenu(sfWebRequest $request)
    {
        $this->path = $request->getPathInfo();

        if ($request->hasParameter('orderId'))
        {
            $this->path = '/personal/profile/';
        }

        $this->links = array(
            array(
                'url' => url_for('@personal_profile'),
                'anchor' => 'История заказов'
            ),

            array(
                'url' => url_for('@personal_profile_settings'),
                'anchor' => 'Настройки профиля'
            ),

            array(
                'url' => url_for('@personal_profile_feedback'),
                'anchor' => 'Обратная связь'
            )
        );


    }
    public function executeFilter(sfWebRequest $request)
    {
        if($this->cat->getIsFilter() && count($this->product_ids) > 0){
            $this->culture = $this->getUser()->getCulture();
            $this->lists = Doctrine_Query::create()
                ->select("v.id, v.product_id, v.prop_id, v.value, p.id, p.isList, pt.*, l.id, l.product_prop_id, lt.*")
                ->from("ProductProp p")
                ->innerJoin("p.Translation pt WITH pt.lang = '" . $this->culture . "'")
                ->innerJoin("p.ProductPropList l")
                ->innerJoin("l.Translation lt WITH lt.lang = '" . $this->culture . "'")
                ->innerJoin("p.ProductPropValue v")
                ->whereIn("v.product_id", $this->product_ids)
                ->andWhere("p.isList = 1")
                ->andWhere("l.id = v.value")
                ->fetchArray();
            $this->values = Doctrine_Query::create()
                ->select("v.id, v.product_id, v.prop_id, v.value, MIN(v.value) AS min, MAX(v.value) AS max, p.id, p.isList, t.*")
                ->from("ProductPropValue v")
                ->innerJoin("v.ProductProp p")
                ->innerJoin("p.Translation t WITH t.lang = '" . $this->culture . "'")
                ->whereIn("v.id", $this->product_ids)
                ->groupBy("v.prop_id")
                ->fetchArray();
        }
    }
}