<?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());
        }
        
    }
}
