BaseProductForm.class.php 7.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php

/**
 * Product form base class.
 *
 * @method Product getObject() Returns the current form's model object
 *
 * @package    sf
 * @subpackage form
 * @author     Atma
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseProductForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'             => new sfWidgetFormInputHidden(array(), array()),
      'inner_id'       => new sfWidgetFormInputText(array(), array("size" => 65)),
      'alias'          => new sfWidgetFormInputText(array(), array("size" => 64, "maxlength" => 255, "required" => true)),
      'product_img'    => new sfWidgetFormInputText(array(), array("size" => 65)),
      'supplier_id'    => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Suppliers'), 'add_empty' => true), array()),
      'supplier_price' => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8)),
      'certificate'    => new sfWidgetFormInputText(array(), array("size" => 64, "maxlength" => 255)),
      'offer_count'    => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8, "required" => true)),
      'is_detail'      => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'is_delete'      => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'is_main'        => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'show_main'      => new sfWidgetFormInputCheckbox(array(), array("required" => true)),
      'sort'           => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8)),
      'free_shipping'  => new sfWidgetFormInputCheckbox(array(), array()),
      'the_weight'     => new sfWidgetFormInputText(array(), array("size" => 8, "maxlength" => 8)),
      'created_at'     => new sfWidgetFormDateTime(array(), array("required" => true)),
      'updated_at'     => new sfWidgetFormDateTime(array(), array("required" => true)),
      'prop_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'ProductProp')),
      'cats_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Cat')),
      'tags_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Tag')),
    ));

    $this->setValidators(array(
      'id'             => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'inner_id'       => new sfValidatorPass(array('required' => false)),
      'alias'          => new sfValidatorString(array('max_length' => 255)),
      'product_img'    => new sfValidatorPass(array('required' => false)),
      'supplier_id'    => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Suppliers'), 'required' => false)),
      'supplier_price' => new sfValidatorInteger(array('required' => false)),
      'certificate'    => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'offer_count'    => new sfValidatorInteger(array('required' => false)),
      'is_detail'      => new sfValidatorBoolean(array('required' => false)),
      'is_delete'      => new sfValidatorBoolean(array('required' => false)),
      'is_main'        => new sfValidatorBoolean(array('required' => false)),
      'show_main'      => new sfValidatorBoolean(array('required' => false)),
      'sort'           => new sfValidatorInteger(array('required' => false)),
      'free_shipping'  => new sfValidatorBoolean(array('required' => false)),
      'the_weight'     => new sfValidatorInteger(array('required' => false)),
      'created_at'     => new sfValidatorDateTime(),
      'updated_at'     => new sfValidatorDateTime(),
      'prop_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'ProductProp', 'required' => false)),
      'cats_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Cat', 'required' => false)),
      'tags_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Tag', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorDoctrineUnique(array('model' => 'Product', 'column' => array('alias')))
    );

    $this->widgetSchema->setNameFormat('product[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Product';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['prop_list']))
    {
      $this->setDefault('prop_list', $this->object->Prop->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['cats_list']))
    {
      $this->setDefault('cats_list', $this->object->Cats->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['tags_list']))
    {
      $this->setDefault('tags_list', $this->object->Tags->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savePropList($con);
    $this->saveCatsList($con);
    $this->saveTagsList($con);

    parent::doSave($con);
  }

  public function savePropList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['prop_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Prop->getPrimaryKeys();
    $values = $this->getValue('prop_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Prop', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Prop', array_values($link));
    }
  }

  public function saveCatsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['cats_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Cats->getPrimaryKeys();
    $values = $this->getValue('cats_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Cats', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Cats', array_values($link));
    }
  }

  public function saveTagsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['tags_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Tags->getPrimaryKeys();
    $values = $this->getValue('tags_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Tags', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Tags', array_values($link));
    }
  }

}