All pastes #2056982 Raw Edit

Anonymous

public text v1 · immutable
#2056982 ·published 2011-05-11 19:43 UTC
rendered paste body
class ProductForm extends BaseProductForm
{
  
  protected $scheduledForDeletion = array();

  public function configure()
  {
    ProjectConfiguration::getActive()->loadHelpers( array( 'I18N', 'Thumbnail', 'Asset' ) );

    unset(
      #$this[''],
      $this['sizes_list'],
      $this['slug'],
      $this['created_at'],
      $this['updated_at']
    );

    $uploadDirName  = sfConfig::get('sf_upload_dir').'/products';
    $filePath       = '/uploads/products'.'/'.$this->getObject()->getPic();;

    $this->setWidget('pic', new sfWidgetFormInputFileEditable(array(
        'label'     => 'Picture',
        'file_src'  => $filePath,
        'is_image'  => true,
        'edit_mode' => ($this->getObject()->getPic() AND !$this->getObject()->isNew() ? true: false),
        'template'  => '%input% %delete% delete file ::
          <a rel="extraPics" title="'.$this->getObject()->getName().'" href="'.$filePath.'" target="_blank">open file</a>',
    )));

    $this->setWidget('pic', new sfWidgetFormInputFileEditable(array(
        'label'     => 'Picture',
        'file_src'  => $filePath,
        'is_image'  => true,
        'edit_mode' => ($this->getObject()->getPic() AND !$this->getObject()->isNew() ? true: false),
        'template'  => '<table><tr>
          <td>
            %input% <br /> %delete% delete file
          </td>
          <td>
            <a rel="extraPics" title="'.$this->getObject()->getName().'" href="'.$filePath.'" target="_blank"><img
            src="'.thumbnail_path('uploads/products/'.$this->getObject()->getPic(), 50, 50, 'products').'" /></a>
          </tr>
          </table>',
    )));


    $this->setValidator(
      'pic', new sfValidatorFile(array(
        'required'   => false,
        'path'       => $uploadDirName,
        'max_size' => '10240000', // bytes (1MB)
        'mime_types' => array(
          'image/gif',
          'image/jpeg',
          'image/png'
        )
    )));


    $this->embedRelation('ProductGallery');
    $form = new ProductGalleryCollectionForm(null, array(
      'product' => $this->getObject(),
      'size'    => ($this->getObject()->isNew() ? 0 : 2),
    ));
    $this->embedForm('newPhotos', $form);

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
    
  }

  public function saveEmbeddedForms($con = null, $forms = null)
  {
    if (null === $forms)
    {
      $photos = $this->getValue('newPhotos');
      $forms = $this->embeddedForms;
      foreach ($this->embeddedForms['newPhotos'] as $name => $form)
      {
        if (!isset($photos[$name]))
        {
          unset($forms['newPhotos'][$name]);
        }
      }
    }

    $con    = (null === $con ? $this->getConnection() : $con);
    $forms  = (null === $forms ? $this->embeddedForms : $forms);
    foreach ($forms as $form)
    {
      if ($form instanceof sfFormObject)
      {
        if (!in_array($form->getObject()->getId(), $this->scheduledForDeletion))
        {
          $form->saveEmbeddedForms($con);
          $form->getObject()->save($con);
        }
      }
      else
      {
        $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
      }
    }
  }
  
  protected function doBind(array $values)
  {
 
    if (isset($values['ProductGallery']))
    {
      foreach ($values['ProductGallery'] as $i => $attachValues)
      {
        if (isset($attachValues['delete']) && $attachValues['id'])
        {
          $this->scheduledForDeletion[$i] = $attachValues['id'];
        }
      }
    }

    parent::doBind($values);
  }
  
  protected function doUpdateObject($values)
  {
    if (@count($this->scheduledForDeletion))
    {
      foreach ($this->scheduledForDeletion as $index => $id)
      {
        unset($values['ProductGallery'][$index]);
        unset($this->object['ProductGallery'][$index]);
        Doctrine::getTable('ProductGallery')->findOneById($id)->delete();
      }
    }
    $this->getObject()->fromArray($values);
  } // doUpdateObject


}