]> git.andy128k.dev Git - ipf.git/commitdiff
pass uploadUrl and uploadPath to fields and widgets as parameters
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 7 Apr 2019 10:37:41 +0000 (12:37 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 7 Apr 2019 10:37:41 +0000 (12:37 +0200)
ipf/form/field/file.php
ipf/form/field/image.php
ipf/form/widget/fileinput.php
ipf/form/widget/image.php

index b1812cf5043a1076fd412ae7d72ba0816d28c691..93b01b58cbdb8141df5127c5e0a2104d8b98179b 100644 (file)
@@ -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;
+    }
+}
index b8efd19c45ab02fab69eb0b1f8a82d032d4052c4..907b6407d9312c4527880df7cf1f96db5f41c2c6 100644 (file)
@@ -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.'));
         }
index 6ecf2b20b4399dc91625fdf25d2530538a588534..b78679166202f85053f41d303789de1ff12d2379 100644 (file)
@@ -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;
     }
 }
-
index b25678a0ed7545fd3027f34cb2240de757362247..a7ca2a9105d789e949b60bafefd7c5371d1a8500 100644 (file)
@@ -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 '&nbsp;' .
             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
             '&nbsp;';
     }
 }
-