From: Andrey Kutejko Date: Sat, 21 Sep 2013 11:14:02 +0000 (+0300) Subject: fix image field clean logic X-Git-Tag: 0.5~31 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=97620b3498f468e5a9e2c2ff24395e3c31eeb03b;p=ipf.git fix image field clean logic --- diff --git a/ipf/admin/modelinline.php b/ipf/admin/modelinline.php index 8e1b9fe..5c72157 100644 --- a/ipf/admin/modelinline.php +++ b/ipf/admin/modelinline.php @@ -200,8 +200,8 @@ abstract class IPF_Admin_ModelInline $fk_local = $this->getFkLocal(); foreach ($this->formset as $form) { - if ($form->isValid()) { - if ($form->isAdd) { + if ($form->isAdd) { + if ($form->isValid()) { unset($form->cleaned_data[0]); $form->cleaned_data[$fk_local] = $parent_obj->id; $form->save(); diff --git a/ipf/form/field/file.php b/ipf/form/field/file.php index 278f84b..f2464b5 100644 --- a/ipf/form/field/file.php +++ b/ipf/form/field/file.php @@ -39,20 +39,22 @@ class IPF_Form_Field_File extends IPF_Form_Field { IPF_Utils::makeDirectories($this->getAbsolutePath('')); - if (@$value['remove'] === true) + if (ArrayTools::get($value, 'remove')) return $this->removeFile($value['data']); - if (@$value['name'] != @$value['rename']) - return $this->renameFile(@$value['name'], @$value['rename']); + $oldname = ArrayTools::get($value, 'name', ''); + $newname = ArrayTools::get($value, 'rename', ''); + if ($oldname != $newname) + return $this->renameFile($oldname, $newname); - $value = @$value['data']; - - if (@$value['name'] == '') - return ''; - - parent::clean($value); + if (UPLOAD_ERR_OK != ArrayTools::get($value['data'], 'error')) { + if ($this->required && !$oldname) + throw new IPF_Exception_Form(__('This field is required.')); + return $oldname; + } - switch ($value['error']) { + $data = $value['data']; + switch ($data['error']) { case UPLOAD_ERR_OK: break; case UPLOAD_ERR_INI_SIZE: @@ -75,15 +77,15 @@ class IPF_Form_Field_File extends IPF_Form_Field default: throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.')); } - if ($value['size'] > $this->max_size) { + if ($data['size'] > $this->max_size) { throw new IPF_Exception_Form(sprintf(__('The uploaded file is to big (%1$s). Reduce the size to less than %2$s and try again.'), - IPF_Utils::prettySize($value['size']), + IPF_Utils::prettySize($data['size']), IPF_Utils::prettySize($this->max_size))); } - $name = IPF_Utils::cleanFileName($value['name'], $this->getAbsolutePath('')); + $name = IPF_Utils::cleanFileName($data['name'], $this->getAbsolutePath('')); $dest = $this->getAbsolutePath($name); - if (!move_uploaded_file($value['tmp_name'], $dest)) + if (!move_uploaded_file($data['tmp_name'], $dest)) throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.')); @chmod($dest, IPF::get('file_permission')); return $this->getRelativePath($name); diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index cf7aaa9..f022016 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -46,12 +46,20 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input { if (!isset($data[$name])) return null; + $remove = isset($data[$name.'_remove']) && $data[$name.'_remove'] == 1; - $res = array('data'=>$data[$name], 'remove'=>$remove); + + $res = array( + 'data' => $data[$name], + 'remove' => $remove, + ); + if (isset($data[$name.'_rename'])) $res['rename'] = $data[$name.'_rename']; + if (isset($data[$name.'_name'])) $res['name'] = $data[$name.'_name']; + return $res; }