<?php

require_once dirname(__FILE__) . '/../lib/productGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/productGeneratorHelper.class.php';

/**
 * product actions.
 *
 * @package    sf
 * @subpackage product
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends autoProductActions
{
    public function executeUpdate(sfWebRequest $request)
    {
        parent::executeUpdate($request);
        return sfView::NONE;
    }

    public function executeCreate(sfWebRequest $request)
    {
        parent::executeCreate($request);
        return sfView::NONE;
    }

    public function executeDelete(sfWebRequest $request)
    {
        $product = $this->getRoute()->getObject();
        $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $product)));
        $offer_count = Doctrine_Query::create()
            ->from("Offer")
            ->where("product_id = ?", $product->getId())
            ->count();
        if ($offer_count == 0) {
            $product->delete();
            echo 'ok';
        } else {
            echo 'no';
        }
        return sfView::NONE;
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

        if ($form->isValid()) {
            try {
                $product = $form->save();

                $props = $request->getParameter('props');
                if($props && is_array($props)){
                    $product_props = Doctrine_Query::create()
                        ->select("pp.id")
                        ->from("ProductProp pp INDEXBY pp.id")
                        ->fetchArray();
                    foreach ($props as $prop_id => $prop){
                        if(isset($product_props[$prop_id])){
                            $prop_value = Doctrine::getTable("ProductPropValue")->findOneByProductIdAndPropId($product->getId(), $prop_id);
                            if($prop == ''){
                                if($prop_value){
                                    $prop_value->delete();
                                }
                                continue;
                            }
                            if(!$prop_value){
                                $prop_value = new ProductPropValue();
                                $prop_value->setProductId($product->getId());
                                $prop_value->setPropId($prop_id);
                            }
                            $prop_value->setValue($prop);
                            $prop_value->save();
                        }
                    }
                }
                echo $this->getPartial('cat/struct_form', array('form' => new ProductForm($product), 'parent_id' => Cat::$parentId, 'link' => '/arm/product' . ($product ? '/' . $product->getId() : '')));
                return sfView::NONE;
            } catch (Doctrine_Validator_Exception $e) {
                $errorStack = $form->getObject()->getErrorStack();
                return sfView::SUCCESS;
            }
            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $product)));
        }
        echo $this->getPartial('cat/struct_form', array('form' => $form, 'parent_id' => Cat::$parentId, 'link' => '/arm/product' . ($this->product && $this->product->getId() ? '/' . $this->product->getId() : '')));
        return sfView::NONE;
    }
}
