Product.class.php 5.14 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

/**
 * Product
 * 
 * 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 Product extends BaseProduct
{
    public $changed = false;
    
    public function save(Doctrine_Connection $conn = null)
    {
        if($this->getAlias() == "")
        {
            $newAlias = Page::translit($this->getTranslation()->ru->title);
            $counter = 1;
            while(Doctrine::getTable('Product')->findOneByAlias($newAlias))
            {
                $newAlias .= $counter;
                $counter++;
            }
            $this->setAlias($newAlias);
        }
        if(!$this->getIsDetail()){
            $this->setIsMain(false);
        }

        parent::save($conn);
    }

    function getPropertyArray()
    {
        $culture = sfContext::getInstance()->getUser()->getCulture();
        $props = $this->getProductPropValue();
        $propsArray = array();
        foreach ($props as $prop)
        {
            $complited_field[] = $prop['prop_id'];
            if($prop->getProductProp()->getIsList() && count($prop->getProductProp()->getProductPropList()) > 0)
            {
                foreach($prop->getProductProp()->getProductPropList() as $list_elem)
                {
                    if($prop->getValue() == $list_elem->getId())
                    {
                        $variant = $list_elem->getTranslation()->$culture->variant;
                        if($variant != ''){
                            $propsArray[] = array(
                                'prop' => $prop->getProductProp()->getTranslation()->$culture->title,
                                'value' => $variant
                            );
                        }
                    }
                }
            } else {
                $value = $prop->getValue();
                if($value != ''){
                    $propsArray[] = array(
                        'prop' => $prop->getProductProp()->getTranslation()->$culture->title,
                        'value' => $value
                    );
                }
            }
        }
        return $propsArray;
    }

    function getParentsCatArray() {


        $culture = sfContext::getInstance()->getUser()->getCulture();

        $parents_obj = Doctrine_Query::create()
            ->select('c.alias, c_t.title, p.alias, p_t.title')
            ->from('Cat c')
            ->leftJoin('c.Parent p')
            ->leftJoin('c.Translation c_t WITH c_t.lang = ? AND c_t.available = 1', $culture)
            ->leftJoin('p.Translation p_t WITH p_t.lang = ? AND p_t.available = 1', $culture)
            ->where('(SELECT cp.cat_id FROM CatProduct cp WHERE cp.product_id = ?) = c.id', $this->getId())
            ->fetchArray();

        $parents_array = false;

        if($parents_obj)
        {
            $parents_array[0] = array(
                'title' => $parents_obj[0]['Translation'][$culture]['title'],
                'alias' => $parents_obj[0]['alias']
            );

            if(count($parents_obj[0]['Parent']) > 0)
            {
                $parents_array[1] = array(
                    'title' => $parents_obj[0]['Parent']['Translation'][$culture]['title'],
                    'alias' => $parents_obj[0]['Parent']['alias']
                );
            }

        }


        return array_reverse($parents_array);

    }

    function getProductCartViewType()
    {
        $customOffers = count($this->getOffer());

        if($customOffers > 0)
        {
            return 'offerView';
        }
        else
        {
            return 'simpleView';
        }

    }
    
    public function oneSUpdateOrCreate($data = array())
    {
        $created = intval($this->getId()) === 0;
        $updated = false;
        $result = array(
            'created' => $created,
            'updated' => false,
        );
        
        if(isset($data['cat_products'])) {
            $catProducts = $data['cat_products'];
            unset($data['cat_products']);
            $updated = true;
        }
        if(isset($data['product_img']) && $data['product_img'] !== $this->getProductImg()){
            OneSImageUploader::upload(explode(';', $data['product_img']));
        }
        
        $this->fromArray($data);
        
        $result['updated'] = $updated || $this->isModified(true);
        
        $this->save();
        
        if(isset($catProducts)) {
            $this->replaceCat($catProducts);
        }
        
        return $result;
    }
    
    public function replaceCat($cat)
    {
        if(!($cat instanceof Cat) && !($cat instanceof Doctrine_Collection)) {
            
            throw new Doctrine_Record_Exception('cat_products is `' .
                (gettype($cat) === 'object' ? get_class($cat) : gettype($cat)) .
                '` must be Cat or Doctrine_Collection of Cat');
        }
        
        $this->unlink('CatProducts', [], true);
        if($cat instanceof Cat) {
            $cat = array($cat);
        }
        foreach ($cat as $catItem) {
            CatProduct::make($this->getId(), $catItem->getId());
        }
        
    }
}