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:
<?php class avatarActions extends sfActions { public function executeIndex(sfWebRequest $request) { $this->avatar = TpUserPeer::User($id = $this->getUser()->getGuardUser()->getId()); } public function executeNew(sfWebRequest $request) { $this->form = new AvatarForm(); } public function executeCreate(sfWebRequest $request) { $this->forward404Unless($request->isMethod('post')); $this->form = new AvatarForm(); $this->processForm($request, $this->form); $this->setTemplate('New'); } public function executeEdit(sfWebRequest $request) { $this->forward404Unless($avatar = TpUserPeer::User($id = $this->getUser()->getGuardUser()->getId()), sprintf('Object tp_user does not exist (%s).', $this->getUser()->getGuardUser()->getId())); $this->avatar = $avatar; $this->form = new AvatarForm($avatar); } public function executeUpdate(sfWebRequest $request) { $this->forward404Unless($request->isMethod('post') || $request->isMethod('put')); $this->forward404Unless($avatar = TpUserPeer::User($id = $request->getParameter('id')), sprintf('Object tp_user does not exist (%s).', $request->getParameter('id'))); $this->form = new AvatarForm($avatar); $this->avatar = $avatar; $this->processForm($request, $this->form); $this->redirect('avatar'); } public function executeDelete(sfWebRequest $request) { $request->checkCSRFProtection(); $this->forward404Unless($avatar = TpUserPeer::User($id = $request->getParameter('id')), sprintf('Object tp_user does not exist (%s).', $request->getParameter('id'))); $avatar->setAvatar(sfConfig::get('default_path').'default.jpg'); $avatar->save(); $this->redirect('avatar'); } protected function processForm(sfWebRequest $request, sfForm $form) { $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $avatar = $form->save(); $this->redirect('avatar'); } } } ?>
formularz:
<?php class AvatarForm extends BaseTpUserForm { public function configure() { unset($this['login'], $this['age'], $this['blocked'], $this['email'], $this['birthday'], $this['sex'], $this['id_user']); // zostaje tylko jedno pole avatar if(sfContext::hasInstance()) { $this->widgetSchema['avatar'] = new sfWidgetFormInputFileEditable(array('file_src' => '/uploads/avatar/'.$this->getObject()->getAvatar(), 'is_image' => true, 'edit_mode' => true, 'with_delete' => $this->getObject()->getAvatar())); $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!')); 'avatar' => 'Ustaw: ' )); } } } ?>
i dodatkowa klasa z ktorej korzystam przy generowaniu miniatur: sfResizedAvatar
<?php class sfResizedAvatar extends sfValidatedFile { /** * Saves the uploaded file. * * This method can throw exceptions if there is a problem when saving the file. * * If you don't pass a file name, it will be generated by the generateFilename method. * This will only work if you have passed a path when initializing this instance. * * @param string $file The file path to save the file * @param int $fileMode The octal mode to use for the new file * @param bool $create Indicates that we should make the directory before moving the file * @param int $dirMode The octal mode to use when creating the directory * * @return string The filename without the $this->path prefix * * @throws Exception */ public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { { $file = $this->generateFilename(); } if ($file[0] != '/' && $file[0] != '' && !(strlen($file) > 3 && ctype_alpha($file[0]) && $file[1] == ':' && ($file[2] == '\' || $file[2] == '/'))) { { throw new RuntimeException('You must give a "path" when you give a relative file name.'); } //$smallFile = $this->path.DIRECTORY_SEPARATOR.'s_'.$file; $file = $this->path.DIRECTORY_SEPARATOR.$file; } // get our directory path from the destination filename { { // failed to create the directory } // chmod the directory since it doesn't seem to work on recursive paths chmod($directory, $dirMode); } { // the directory path exists but it's not a directory } { // the directory isn't writable } // copy the temp file to the destination file /* $thumbnail = new sfThumbnail(48, 48, true, true, 85, 'sfGDAdapter'); $thumbnail->loadFile($this->getTempName()); $thumbnail->save($smallFile, 'image/jpeg'); */ $thumbnail = new sfThumbnail(48, 48, true, true, 85, 'sfGDAdapter'); $thumbnail->loadFile($this->getTempName()); $thumbnail->save($file, 'image/jpeg'); // chmod our file chmod($smallFile, $fileMode); chmod($file, $fileMode); $this->savedName = $file; } } ?>
i teraz nie wiem co mam zle..Bylbym bardzo wdzieczny za nakierowanie na błąd
