]> _ Git - cubist_cms-back.git/commitdiff
wip #4210 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 14 Jun 2022 16:28:02 +0000 (18:28 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 14 Jun 2022 16:28:02 +0000 (18:28 +0200)
src/app/Http/Controllers/Operations/CloneEditOperation.php
src/app/Magic/Fields/SelectFromArray.php

index be7c9700265f0a04ab06978d0b9bee68b1d0e37f..dea80931c49cb860bf4869a13a032ae9c4bd9108 100644 (file)
@@ -2,10 +2,14 @@
 
 namespace Cubist\Backpack\Http\Controllers\Operations;
 
+use Cubist\Backpack\Magic\Fields\SelectFromArray;
+use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Route;
 
-trait CloneEditOperation{
-    protected function setupBulkPublishRoutes($segment, $routeName, $controller)
+trait CloneEditOperation
+{
+    protected function setupCloneEditRoutes($segment, $routeName, $controller)
     {
         Route::get($segment . '/{id}/clone-edit', [
             'as' => $routeName . '.cloneEdit',
@@ -14,13 +18,25 @@ trait CloneEditOperation{
         ]);
     }
 
-    protected function cloneEdit($id){
-        dd(Route::current());
+    protected function cloneEdit($id)
+    {
         $this->crud->hasAccessOrFail('create');
+        /** @var CubistMagicAbstractModel $clonedEntry */
         $clonedEntry = $this->crud->model->findOrFail($id)->replicate();
-        $clonedEntry->owner=backpack_user()->id;
+        $clonedEntry->owner = backpack_user()->id;
+        $clonedEntry->name .= ' (copy)';
         $clonedEntry->save();
-        $newid=$clonedEntry->id;
-        return response()->redirectTo();
+        $newid = $clonedEntry->id;
+        $url = Route::current()->uri . '?' . request()->getQueryString();
+        $url = str_replace('clone-edit', 'edit', $url);
+        $url = str_replace('{id}', $newid, $url);
+
+
+        if (isset($_GET['ajax'])) {
+            $entry = call_user_func([$_GET['entry'], 'find'], $_GET['id']);
+            SelectFromArray::hashOptions($entry->getField($_GET['name'])['options'], $_GET['ajax']);
+        }
+
+        return response()->redirectTo($url);
     }
 }
index 0f26d549099a8a7120a8b1da62b6754f4dc6c054..ac7ac274ed97ee21e87880d8ede462598529d4f8 100644 (file)
@@ -37,14 +37,16 @@ class SelectFromArray extends Field
         }
     }
 
-    public static function hashOptions($options)
+    public static function hashOptions($options, $hash = null)
     {
         $data = [];
         foreach ($options as $k => $v) {
             $data[$k] = ['t' => $v, 'n' => self::normalize($v)];
         }
-        $hash = hash('sha256', print_r($data, true));
-        Cache::add('select2_' . $hash, $data, 6000);
+        if (null === $hash) {
+            $hash = hash('sha256', print_r($data, true));
+        }
+        Cache::put('select2_' . $hash, $data, 6000);
 
         return $hash;
     }