<?php
class mainComponents extends sfComponents
{
    public function executeMenu(sfWebRequest $request)
    {
        Cat::menuInit();
        $this->type = (!isset($this->type) ? 'top' : $this->type);
        $this->menus = array(
            'catalog' => array('url' => '@catalog', 'title' => __('Каталог'), 'sub' => Cat::$menu),
            'video' => array('url' => '@video', 'title' => __('Смотреть')),
            'article' => array('url' => '@article', 'title' => __('Читать')),
            'review' => array('url' => '@review', 'title' => __('Отзывы')),
            'page' => array('url' => '@page_show?alias=delivery', 'title' => __('Доставка'))
        );
        if($this->type == 'bottom'){
            unset($this->menus['catalog']);
        }
    }
    public function executeTag(sfWebRequest $request)
    {
        $this->culture = $this->getUser()->getCulture();
        $this->tags = Doctrine_Query::create()
            ->select("t.id,  tr.*")
            ->from("Tag t")
            ->innerJoin("t.Translation tr")
            ->where("tr.lang = ?", $this->culture)
            ->orderBy("tr.title ASC")
            ->fetchArray();
    }
    public function executeBreadcrumbs(sfWebRequest $request)
    {
        $culture = $this->getUser()->getCulture();
        if (isset($this->cat)) {
            $parent_id = $this->cat->getParentId();
            $namesCats = array();
            $i = 0;
            while ($parent_id != null)
            {
                $parentCat = Doctrine_Query::create()
                    ->select()
                    ->from('Cat c')
                    ->where('c.id = ?', $parent_id)
                    ->leftJoin('c.Translation ct WITH ct.lang = ?', $culture)
                    ->fetchArray();
                $namesCats[$i]['title'] = $parentCat[0]['Translation'][$culture]['title'];
                $namesCats[$i]['alias'] = $parentCat[0]['alias'];
                $parent_id = $parentCat[0]['parent_id'];
                $i++;
            }
            $this->namesCats = array_reverse($namesCats);
        }
        if (isset($this->product_id)) {
            $cat_id = Doctrine::getTable('CatProduct')->findOneByProductId($this->product_id)->getCatId();
            $cat = Doctrine_Query::create()
                ->select()
                ->from('Cat c')
                ->where('c.id = ?', $cat_id)
                ->leftJoin('c.Translation ct WITH ct.lang = ?', $culture)
                ->fetchArray();
            $parent_id = $cat[0]['parent_id'];
            $namesCats = array();
            $namesCats[0]['title'] = $cat[0]['Translation'][$culture]['title'];
            $namesCats[0]['alias'] = $cat[0]['alias'];
            $i = 1;
            while ($parent_id != null)
            {
                $parentCat = Doctrine_Query::create()
                    ->select()
                    ->from('Cat c')
                    ->where('c.id = ?', $parent_id)
                    ->leftJoin('c.Translation ct WITH ct.lang = ?', $culture)
                    ->fetchArray();
                $namesCats[$i]['title'] = $parentCat[0]['Translation'][$culture]['title'];
                $namesCats[$i]['alias'] = $parentCat[0]['alias'];
                $parent_id = $parentCat[0]['parent_id'];
                $i++;
            }

            $this->namesCats = array_reverse($namesCats);
        }
    }
}