actions.class.php 3.5 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
<?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;
    }
}