Cat.class.php 4.37 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?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();
        }
    }
}