<?php

/**
 * Cat
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 *
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class Cat extends BaseCat
{
    static public
        $parentId = null,
        $menu = array();

    static public function menuInit()
    {
        if(count(self::$menu) == 0){
            self::$menu = Doctrine_Query::create()
                ->select("c.id, c.alias, c.order_id, t.*, p.id, p.alias, p.offer_count, p.is_detail, p.is_delete, p.product_img, pt.id, pt.title, pt.lang, ch.id, ch.alias, ch.parent_id, cht.*")
                ->from("Cat c")
                ->leftJoin("c.Translation t")
                ->leftJoin("c.Products p WITH p.is_delete = 0 AND CHAR_LENGTH(p.product_img) > 0")
                ->leftJoin("p.Translation pt")
                ->leftJoin("c.Children ch")
                ->leftJoin("ch.Translation cht")
                ->where("parent_id IS NULL")
                ->orderBy("c.sort ASC")
                ->fetchArray();
        }
    }

    public function save(Doctrine_Connection $conn = null)
    {
        if ($this->getAlias() == "") {
            $newAlias = Page::translit($this->getTranslation()->ru->title);
            $counter = 1;
            while (Doctrine::getTable('Cat')->findOneByAlias($newAlias)) {
                $newAlias .= $counter;
                $counter++;
            }
            $this->setAlias($newAlias);
        }


        $ps = parent::save($conn);

        if ($this->getId()) {

            $unlink = Doctrine_Query::create()->select("cp.*")->from("CatParent cp")->where("cp.cat_id = " . $this->getId())->execute();

            foreach ($unlink as $cp) {
                $cp->delete();
            }


            $i = 0;
            $_this = $this;

            $id = $this->getId();


            while ($_this->getParentId()) {
                $parent = $_this->getParent();
                $cp = new CatParent();
                $cp->setCatId($id);
                $cp->setParentId($parent->getId());
                $cp->setOrderId($i++);
                $cp->save();
                unset($cp);
                $_this = $parent;
            }


            $ps = parent::save($conn);


        }

        return $ps;

    }

    public function showForAdminList()
    {
        if ($this->getParentId()) {
            return $this->getTitle() . " (" . $this->getParent()->getTitle() . ")";
        } else {
            return $this->getTitle();
        }
    }

    public function delete(Doctrine_Connection $conn = null)
    {
        Doctrine_Query::create()->delete("cp.*")->from("CatProduct cp")->where("cp.cat_id = " . $this->getId())->execute();
        return parent::delete($conn);
    }
    public function getViewType()
    {

        $childrenCatsCount = count($this->getChildren());
        $childrenProducts = count($this->getCatProduct());


        if ($childrenCatsCount > 0) {
            return 'onlyCats';
        } elseif ($childrenProducts > 0) {
            return 'onlyProducts';
        }

        return false;
    }

    public function getCleanCatProducts()
    {
        $catProducts_dirty = $this->getCatProduct();
        $catProducts_clean = array();
        foreach ($catProducts_dirty as $product) {
//            if ($product->getDeletedAt() != null) {
//                continue;
//            }
            $catProducts_clean[] = $product;
        }
        return $catProducts_clean;
    }
    public function getCleanChildren()
    {
        $children_dirty = $this->getChildren();
        $children_clean = array();
        foreach ($children_dirty as $children) {
            if (count($children->getCleanCatProducts()) == 0 & count($children->getChildren()) == 0) {
                continue;
            }
            $children_clean[] = $children;
        }
        return $children_clean;
    }

    public function getBreadcrumbs()
    {
        $parentIds_dirty = Doctrine_Query::create()->select('cp.parent_id')->from('CatParent cp')->where('cp.cat_id = ?', $this->getId())->fetchArray();

        $parentIds = array();

        foreach ($parentIds_dirty as $id) {
            $parentIds[] = $id['parent_id'];
        }

        if (count($parentIds) == 0) {
            return false;
        } else {
            return Doctrine_Query::create()->from('Cat c')->whereIn('c.id', $parentIds)->execute();
        }
    }
}
