<?php

/**
 * KataoMember form base class.
 *
 * @package    form
 * @subpackage katao_member
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 15484 2009-02-13 13:13:51Z fabien $
 */
class BaseKataoMemberForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                            => new sfWidgetFormInputHidden(),
      'katao_index'                   => new sfWidgetFormInput(),
      'katao_node_id'                 => new sfWidgetFormPropelSelect(array('model' => 'KataoNode', 'add_empty' => false)),
      'next_katao_node_id'            => new sfWidgetFormPropelSelect(array('model' => 'KataoNode', 'add_empty' => true)),
      'katao_member_id'               => new sfWidgetFormPropelSelect(array('model' => 'KataoMember', 'add_empty' => true)),
      'first_name'                    => new sfWidgetFormInput(),
      'last_name'                     => new sfWidgetFormInput(),
      'accounting_code'               => new sfWidgetFormInput(),
      'accounting_code_sol'           => new sfWidgetFormInput(),
      'initial_amount_euro'           => new sfWidgetFormInput(),
      'initial_amount_sol'            => new sfWidgetFormInput(),
      'is_referer'                    => new sfWidgetFormInput(),
      'is_delegate'                   => new sfWidgetFormInput(),
      'is_anonymous'                  => new sfWidgetFormInput(),
      'is_member'                     => new sfWidgetFormInput(),
      'card_number_sol'               => new sfWidgetFormInput(),
      'user_situation'                => new sfWidgetFormInput(),
      'user_birthday'                 => new sfWidgetFormDate(),
      'parrain_name'                  => new sfWidgetFormInput(),
      'created_at'                    => new sfWidgetFormDateTime(),
      'updated_at'                    => new sfWidgetFormDateTime(),
      'katao_assoc_member_group_list' => new sfWidgetFormPropelSelectMany(array('model' => 'KataoMemberGroup')),
    ));

    $this->setValidators(array(
      'id'                            => new sfValidatorPropelChoice(array('model' => 'KataoMember', 'column' => 'id', 'required' => false)),
      'katao_index'                   => new sfValidatorInteger(),
      'katao_node_id'                 => new sfValidatorPropelChoice(array('model' => 'KataoNode', 'column' => 'id')),
      'next_katao_node_id'            => new sfValidatorPropelChoice(array('model' => 'KataoNode', 'column' => 'id', 'required' => false)),
      'katao_member_id'               => new sfValidatorPropelChoice(array('model' => 'KataoMember', 'column' => 'id', 'required' => false)),
      'first_name'                    => new sfValidatorString(array('max_length' => 255)),
      'last_name'                     => new sfValidatorString(array('max_length' => 255)),
      'accounting_code'               => new sfValidatorString(array('max_length' => 6)),
      'accounting_code_sol'           => new sfValidatorString(array('max_length' => 6)),
      'initial_amount_euro'           => new sfValidatorNumber(array('required' => false)),
      'initial_amount_sol'            => new sfValidatorInteger(array('required' => false)),
      'is_referer'                    => new sfValidatorInteger(array('required' => false)),
      'is_delegate'                   => new sfValidatorInteger(array('required' => false)),
      'is_anonymous'                  => new sfValidatorInteger(array('required' => false)),
      'is_member'                     => new sfValidatorInteger(array('required' => false)),
      'card_number_sol'               => new sfValidatorString(array('max_length' => 8, 'required' => false)),
      'user_situation'                => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'user_birthday'                 => new sfValidatorDate(array('required' => false)),
      'parrain_name'                  => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'created_at'                    => new sfValidatorDateTime(array('required' => false)),
      'updated_at'                    => new sfValidatorDateTime(array('required' => false)),
      'katao_assoc_member_group_list' => new sfValidatorPropelChoiceMany(array('model' => 'KataoMemberGroup', 'required' => false)),
    ));

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

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

    parent::setup();
  }

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


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

    if (isset($this->widgetSchema['katao_assoc_member_group_list']))
    {
      $values = array();
      foreach ($this->object->getKataoAssocMemberGroups() as $obj)
      {
        $values[] = $obj->getGroupId();
      }

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

  }

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

    $this->saveKataoAssocMemberGroupList($con);
  }

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

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

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

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

    $values = $this->getValue('katao_assoc_member_group_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new KataoAssocMemberGroup();
        $obj->setMemberId($this->object->getPrimaryKey());
        $obj->setGroupId($value);
        $obj->save();
      }
    }
  }

}
