From: Andrey Kutejko Date: Sun, 7 Apr 2019 10:37:41 +0000 (+0200) Subject: pass uploadUrl and uploadPath to fields and widgets as parameters X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=21a50cdf303fb89afdac119f4b32b7ea8f12965a;p=ipf.git pass uploadUrl and uploadPath to fields and widgets as parameters --- diff --git a/ipf/form/field/file.php b/ipf/form/field/file.php index b1812cf..93b01b5 100644 --- a/ipf/form/field/file.php +++ b/ipf/form/field/file.php @@ -4,8 +4,18 @@ class IPF_Form_Field_File extends IPF_Form_Field { public $widget = 'IPF_Form_Widget_FileInput'; public $max_size = 20971520; // 20MB + public $file_permission = 0666; + public $uploadUrl = null; + public $uploadPath = null; public $uploadTo = ''; + function __construct($params = array()) + { + $params['uploadUrl'] = IPF::getUploadUrl(); + $params['uploadPath'] = IPF::getUploadPath(); + parent::__construct($params); + } + protected function removeFile($data) { return null; @@ -21,7 +31,7 @@ class IPF_Form_Field_File extends IPF_Form_Field protected function getAbsolutePath($filename) { - $upload_root = IPF::getUploadPath() . DIRECTORY_SEPARATOR; + $upload_root = $this->uploadPath . DIRECTORY_SEPARATOR; if ($this->uploadTo) return $upload_root . $this->uploadTo . DIRECTORY_SEPARATOR . $filename; else @@ -30,7 +40,7 @@ class IPF_Form_Field_File extends IPF_Form_Field protected function renameFile($old_name, $new_name) { - $upload_root = IPF::getUploadPath() . DIRECTORY_SEPARATOR; + $upload_root = $this->uploadPath . DIRECTORY_SEPARATOR; @rename($upload_root . $old_name, $upload_root . $new_name); return $new_name; } @@ -89,8 +99,16 @@ class IPF_Form_Field_File extends IPF_Form_Field $dest = $this->getAbsolutePath($name); 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')); + @chmod($dest, $this->file_permission); return $this->getRelativePath($name); } -} + protected function createWidget($args) + { + $widgetClass = $this->widget; + /** @var IPF_Form_Widget_FileInput $widget */ + $widget = new $widgetClass($args); + $widget->uploadUrl = $this->uploadUrl; + return $widget; + } +} diff --git a/ipf/form/field/image.php b/ipf/form/field/image.php index b8efd19..907b640 100644 --- a/ipf/form/field/image.php +++ b/ipf/form/field/image.php @@ -8,7 +8,7 @@ class IPF_Form_Field_Image extends IPF_Form_Field_File { $name = parent::clean($value); if ($name) { - $image = IPF::getUploadPath() . DIRECTORY_SEPARATOR . $name; + $image = $this->uploadPath . DIRECTORY_SEPARATOR . $name; if (!getimagesize($image)) throw new IPF_Exception_Form(__('An error occured when upload the image.')); } diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index 6ecf2b2..b786791 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -8,11 +8,12 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input public $needs_multipart_form = true; public $allow_extended = true; public $allow_delete = true; + public $uploadUrl = null; protected function viewCurrentValue($filename) { if ($filename) - return Tag::a(array('target' => '_blank', 'href' => IPF::getUploadUrl().$filename), 'view')->html(); + return Tag::a(array('target' => '_blank', 'href' => $this->uploadUrl.$filename), 'view')->html(); else return ''; } @@ -69,4 +70,3 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input return $value; } } - diff --git a/ipf/form/widget/image.php b/ipf/form/widget/image.php index b25678a..a7ca2a9 100644 --- a/ipf/form/widget/image.php +++ b/ipf/form/widget/image.php @@ -9,7 +9,7 @@ class IPF_Form_Widget_Image extends IPF_Form_Widget_FileInput if (!$filename) return ''; - $url = IPF::getUploadUrl() . $filename; + $url = $this->uploadUrl . $filename; return ' ' . Tag::a(array('target' => '_blank', 'href' => $url), Tag::img(array('src' => $url, 'style' => 'max-width:64px;max-height:64px'))) @@ -17,4 +17,3 @@ class IPF_Form_Widget_Image extends IPF_Form_Widget_FileInput ' '; } } -