czesc

Nie moge sobie poradzic z zastapieniem zdjecia, nowym.

Kod w akcji mam standardowy jak przy generacji CRUD. Probowalem zrobic poprzez ten widget sfWidgetFormInutFileEditable: przechodzi przez proces, ale nie zapisuje zadnych zmian ani nie generuje miniatury.

Powinno: zaladowac plik, wygenerowac miniature i zastapic ja ta nowa wprowadzajac nowy wpis do bazy.

A wiec wyglada to tak:

Kod akcji:
  1. <?php
  2. class avatarActions extends sfActions
  3. {
  4.  public function executeIndex(sfWebRequest $request)
  5.  {
  6.    $this->avatar = TpUserPeer::User($id = $this->getUser()->getGuardUser()->getId());
  7.    
  8.  }
  9.  
  10.  public function executeNew(sfWebRequest $request)
  11.  {
  12.    $this->form = new AvatarForm();
  13.  }
  14.  
  15.  public function executeCreate(sfWebRequest $request)
  16.  {
  17.    $this->forward404Unless($request->isMethod('post'));
  18.  
  19.    $this->form = new AvatarForm();
  20.  
  21.    $this->processForm($request, $this->form);
  22.  
  23.    $this->setTemplate('New');
  24.  }
  25.  
  26.  public function executeEdit(sfWebRequest $request)
  27.  {
  28.    $this->forward404Unless($avatar = TpUserPeer::User($id = $this->getUser()->getGuardUser()->getId()), sprintf('Object tp_user does not exist (%s).', $this->getUser()->getGuardUser()->getId()));
  29.    $this->avatar = $avatar;
  30.    $this->form = new AvatarForm($avatar);
  31.  }
  32.  
  33.  public function executeUpdate(sfWebRequest $request)
  34.  {
  35.    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
  36.    $this->forward404Unless($avatar = TpUserPeer::User($id = $request->getParameter('id')), sprintf('Object tp_user does not exist (%s).', $request->getParameter('id')));
  37.  
  38.    $this->form = new AvatarForm($avatar);
  39.  
  40.    $this->avatar = $avatar;
  41.  
  42.    $this->processForm($request, $this->form);
  43.  
  44.    $this->redirect('avatar');
  45.  }
  46.  
  47.  public function executeDelete(sfWebRequest $request)
  48.  {
  49.    $request->checkCSRFProtection();
  50.  
  51.    $this->forward404Unless($avatar = TpUserPeer::User($id = $request->getParameter('id')), sprintf('Object tp_user does not exist (%s).', $request->getParameter('id')));
  52.    $avatar->setAvatar(sfConfig::get('default_path').'default.jpg');
  53.    $avatar->save();
  54.  
  55.    $this->redirect('avatar');
  56.  }
  57.  
  58.  protected function processForm(sfWebRequest $request, sfForm $form)
  59.  {
  60.    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  61.    if ($form->isValid())
  62.    {
  63.      $avatar = $form->save();
  64.  
  65.      $this->redirect('avatar');
  66.    }
  67.  }
  68. }
  69. ?>



formularz:

  1. <?php
  2. class AvatarForm extends BaseTpUserForm
  3. {
  4.    
  5.  public function configure()
  6.  {
  7.    unset($this['login'], $this['age'], $this['blocked'], $this['email'], $this['birthday'], $this['sex'], $this['id_user']);
  8.    
  9. // zostaje tylko jedno pole avatar
  10.  
  11.    if(sfContext::hasInstance())
  12.    {    
  13.    
  14.    $this->widgetSchema['avatar'] = new sfWidgetFormInputFileEditable(array('file_src' => '/uploads/avatar/'.$this->getObject()->getAvatar(), 'is_image' => true, 'edit_mode' => true, 'with_delete' => $this->getObject()->getAvatar()));
  15.    
  16.    $this->validatorSchema['avatar'] = new sfValidatorFile(array('required' => true, 'max_size' => '2097152', 'mime_types' => array('image/jpeg', 'image/png', 'image/gif'), 'path' => 'uploads/avatar/', 'validated_file_class' => 'sfResizedAvatar'), array('max_size' => 'Plik jest zbyt duży! Maksymalnie: 2 Mb.', 'mime_types' => 'Nieprawidłowy format pliku. Wybierz plik z rozszerzeniem: .jpg!'));
  17.  
  18.    $this->widgetSchema->setLabels(array(
  19.    'avatar' => 'Ustaw: '
  20.    ));
  21.    }
  22.  }
  23.  
  24. }
  25. ?>


i dodatkowa klasa z ktorej korzystam przy generowaniu miniatur: sfResizedAvatar

  1. <?php
  2. class sfResizedAvatar extends sfValidatedFile
  3. {
  4.  
  5.    /**
  6.      * Saves the uploaded file.
  7.      *
  8.      * This method can throw exceptions if there is a problem when saving the file.
  9.      *
  10.      * If you don't pass a file name, it will be generated by the generateFilename method.
  11.      * This will only work if you have passed a path when initializing this instance.
  12.      *
  13.      * @param  string $file      The file path to save the file
  14.      * @param  int    $fileMode  The octal mode to use for the new file
  15.      * @param  bool   $create    Indicates that we should make the directory before moving the file
  16.      * @param  int    $dirMode   The octal mode to use when creating the directory
  17.      *
  18.      * @return string The filename without the $this->path prefix
  19.      *
  20.      * @throws Exception
  21.      */
  22.    public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777)
  23.    {
  24.        if (is_null($file))
  25.        {
  26.            $file = $this->generateFilename();
  27.        }
  28.  
  29.        if ($file[0] != '/' && $file[0] != '' && !(strlen($file) > 3 && ctype_alpha($file[0]) && $file[1] == ':' && ($file[2] == '&#092;' || $file[2] == '/')))
  30.        {
  31.            if (is_null($this->path))
  32.            {
  33.                throw new RuntimeException('You must give a "path" when you give a relative file name.');
  34.            }
  35.  
  36.            //$smallFile = $this->path.DIRECTORY_SEPARATOR.'s_'.$file;
  37.            $file = $this->path.DIRECTORY_SEPARATOR.$file;
  38.        }
  39.  
  40.        // get our directory path from the destination filename
  41.        $directory = dirname($file);
  42.        if (!is_readable($directory))
  43.        {
  44.            if ($create && !mkdir($directory, $dirMode, true))
  45.            {
  46.                // failed to create the directory
  47.                throw new Exception(sprintf('Failed to create file upload directory "%s".', $directory));
  48.            }
  49.  
  50.            // chmod the directory since it doesn't seem to work on recursive paths
  51.            chmod($directory, $dirMode);
  52.        }
  53.        if (!is_dir($directory))
  54.        {
  55.            // the directory path exists but it's not a directory
  56.            throw new Exception(sprintf('File upload path "%s" exists, but is not a directory.', $directory));
  57.        }
  58.        if (!is_writable($directory))
  59.        {
  60.            // the directory isn't writable
  61.            throw new Exception(sprintf('File upload path "%s" is not writable.', $directory));
  62.        }
  63.  
  64.        // copy the temp file to the destination file
  65.        /*
  66.         $thumbnail = new sfThumbnail(48, 48, true, true, 85, 'sfGDAdapter');
  67.         $thumbnail->loadFile($this->getTempName());
  68.         $thumbnail->save($smallFile, 'image/jpeg');
  69.         */
  70.        $thumbnail = new sfThumbnail(48, 48, true, true, 85, 'sfGDAdapter');
  71.        $thumbnail->loadFile($this->getTempName());
  72.        $thumbnail->save($file, 'image/jpeg');
  73.        
  74.        // chmod our file
  75.        chmod($smallFile, $fileMode);
  76.        chmod($file, $fileMode);
  77.  
  78.        $this->savedName = $file;
  79.        return is_null($this->path) ? $file : str_replace($this->path.DIRECTORY_SEPARATOR, '', $file);
  80.    }
  81.  
  82. }
  83. ?>



i teraz nie wiem co mam zle..Bylbym bardzo wdzieczny za nakierowanie na błąd smile.gif