<?php

/**
 * KataoNode form base class.
 *
 * @package    form
 * @subpackage katao_node
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseKataoNodeForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                       => new sfWidgetFormInputHidden(),
      'city'                     => new sfWidgetFormInput(),
      'responsible'              => new sfWidgetFormPropelSelect(array('model' => 'KataoUser', 'add_empty' => true)),
      'begin_at'                 => new sfWidgetFormDateTime(),
      'is_default'               => new sfWidgetFormInput(),
      'created_at'               => new sfWidgetFormDateTime(),
      'updated_at'               => new sfWidgetFormDateTime(),
      'katao_supplier_node_list' => new sfWidgetFormPropelSelectMany(array('model' => 'KataoSupplier')),
    ));

    $this->setValidators(array(
      'id'                       => new sfValidatorPropelChoice(array('model' => 'KataoNode', 'column' => 'id', 'required' => false)),
      'city'                     => new sfValidatorString(array('max_length' => 255)),
      'responsible'              => new sfValidatorPropelChoice(array('model' => 'KataoUser', 'column' => 'id', 'required' => false)),
      'begin_at'                 => new sfValidatorDateTime(),
      'is_default'               => new sfValidatorInteger(array('required' => false)),
      'created_at'               => new sfValidatorDateTime(array('required' => false)),
      'updated_at'               => new sfValidatorDateTime(array('required' => false)),
      'katao_supplier_node_list' => new sfValidatorPropelChoiceMany(array('model' => 'KataoSupplier', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['katao_supplier_node_list']))
    {
      $values = array();
      foreach ($this->object->getKataoSupplierNodes() as $obj)
      {
        $values[] = $obj->getKataoSupplierId();
      }

      $this->setDefault('katao_supplier_node_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveKataoSupplierNodeList($con);
  }

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

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

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

    $c = new Criteria();
    $c->add(KataoSupplierNodePeer::KATAO_NODE_ID, $this->object->getPrimaryKey());
    KataoSupplierNodePeer::doDelete($c, $con);

    $values = $this->getValue('katao_supplier_node_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new KataoSupplierNode();
        $obj->setKataoNodeId($this->object->getPrimaryKey());
        $obj->setKataoSupplierId($value);
        $obj->save();
      }
    }
  }

}
