소스 검색

:construction: 增加表单fileDeleting以及fileDeleted事件

jqh 4 년 전
부모
커밋
70f2568b35
4개의 변경된 파일64개의 추가작업 그리고 22개의 파일을 삭제
  1. 14 14
      src/Form/Concerns/HasEvents.php
  2. 36 8
      src/Form/Concerns/HasFiles.php
  3. 7 0
      src/Form/Events/FileDeleted.php
  4. 7 0
      src/Form/Events/FileDeleting.php

+ 14 - 14
src/Form/Concerns/HasEvents.php

@@ -24,7 +24,7 @@ trait HasEvents
      */
     public function creating(Closure $callback)
     {
-        Event::listen(Events\Creating::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Creating::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -38,7 +38,7 @@ trait HasEvents
      */
     public function editing(Closure $callback)
     {
-        Event::listen(Events\Editing::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Editing::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -52,7 +52,7 @@ trait HasEvents
      */
     public function submitted(Closure $callback)
     {
-        Event::listen(Events\Submitted::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Submitted::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -66,7 +66,7 @@ trait HasEvents
      */
     public function saving(Closure $callback)
     {
-        Event::listen(Events\Saving::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Saving::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -80,7 +80,7 @@ trait HasEvents
      */
     public function saved(Closure $callback)
     {
-        Event::listen(Events\Saved::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Saved::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -94,7 +94,7 @@ trait HasEvents
      */
     public function deleting(Closure $callback)
     {
-        Event::listen(Events\Deleting::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Deleting::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -108,7 +108,7 @@ trait HasEvents
      */
     public function deleted(Closure $callback)
     {
-        Event::listen(Events\Deleted::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Deleted::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -122,7 +122,7 @@ trait HasEvents
      */
     public function uploading(Closure $callback)
     {
-        Event::listen(Events\Uploading::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Uploading::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -136,7 +136,7 @@ trait HasEvents
      */
     public function uploaded(Closure $callback)
     {
-        Event::listen(Events\Uploaded::class, $this->makeListener($callback, $this));
+        Event::listen(Events\Uploaded::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -150,7 +150,7 @@ trait HasEvents
      */
     public function fileDeleting(Closure $callback)
     {
-        Event::listen(Events\FileDeleting::class, $this->makeListener($callback, $this));
+        Event::listen(Events\FileDeleting::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -165,7 +165,7 @@ trait HasEvents
      */
     public function fileDeleted(Closure $callback)
     {
-        Event::listen(Events\FileDeleted::class, $this->makeListener($callback, $this));
+        Event::listen(Events\FileDeleted::class, $this->makeListener($callback));
 
         return $this;
     }
@@ -175,10 +175,10 @@ trait HasEvents
      *
      * @return \Closure
      */
-    protected function makeListener(Closure $callback, self $form)
+    protected function makeListener(Closure $callback)
     {
-        return function (Events\Event $event) use ($callback, $form) {
-            if ($event->form !== $form) {
+        return function (Events\Event $event) use ($callback) {
+            if ($event->form !== $this) {
                 return;
             }
 

+ 36 - 8
src/Form/Concerns/HasFiles.php

@@ -7,6 +7,7 @@ use Dcat\Admin\Form\Builder;
 use Dcat\Admin\Form\Field;
 use Dcat\Admin\Form\NestedForm;
 use Dcat\Admin\Support\WebUploader;
+use Illuminate\Support\Arr;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 use Symfony\Component\HttpFoundation\Response;
 
@@ -92,7 +93,8 @@ trait HasFiles
 
                 return $column === $field->column() && $field instanceof UploadFieldInterface;
             })->each(function (UploadFieldInterface $file) use ($input) {
-                $file->deleteFile($input[Field::FILE_DELETE_FLAG]);
+                /* @var Field $file */
+                $this->deleteFile($file, $input[Field::FILE_DELETE_FLAG]);
             });
 
             return $this
@@ -102,6 +104,34 @@ trait HasFiles
         }
     }
 
+    /**
+     * 删除文件.
+     *
+     * @param UploadFieldInterface|Field $field
+     * @param array                      $input
+     */
+    protected function deleteFile(UploadFieldInterface $field, $input = null)
+    {
+        if ($input) {
+            if (
+                is_string($input)
+                || (is_array($input) && ! Arr::isAssoc($input))
+            ) {
+                $input = [$field->column() => $input];
+            }
+
+            $field->setOriginal($input);
+        }
+
+        if ($this->callFileDeleting($field) === false) {
+            return;
+        }
+
+        $field->destroy();
+
+        $this->callFileDeleted($field);
+    }
+
     /**
      * 如果是删除文件请求,则直接删除文件.
      *
@@ -116,10 +146,10 @@ trait HasFiles
         }
 
         $column = $data['_column'] ?? null;
-        $file = $data['key'] ?? null;
+        $filePath = $data['key'] ?? null;
         $relation = $data['_relation'] ?? null;
 
-        if (! $column && ! $file) {
+        if (! $column && ! $filePath) {
             return;
         }
 
@@ -130,7 +160,7 @@ trait HasFiles
         }
 
         if ($field && $field instanceof UploadFieldInterface) {
-            $field->deleteFile($file);
+            $this->deleteFile($field, $filePath);
 
             return $this
                 ->response()
@@ -178,10 +208,8 @@ trait HasFiles
             ->filter(function ($field) {
                 return $field instanceof UploadFieldInterface;
             })
-            ->each(function (UploadFieldInterface $file) use ($input) {
-                $file->setOriginal($input);
-
-                $file->destroy();
+            ->each(function (UploadFieldInterface $field) use ($input) {
+                $this->deleteFile($field, $input);
             });
     }
 

+ 7 - 0
src/Form/Events/FileDeleted.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class FileDeleted extends Event
+{
+}

+ 7 - 0
src/Form/Events/FileDeleting.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace Dcat\Admin\Form\Events;
+
+class FileDeleting extends Event
+{
+}