]> _ Git - cubist_cms-back.git/commitdiff
wip #5851 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 12 Apr 2023 11:29:35 +0000 (13:29 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 12 Apr 2023 11:29:35 +0000 (13:29 +0200)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Models/CubistMagicTranslatableModel.php
src/resources/views/edit.blade.php

index f8b117ab99a16f6f48948cc1049de5f227742223..263d81c46b1a238aa24b46df33e638194f5a01e0 100644 (file)
@@ -36,6 +36,8 @@ class CubistMagicController extends CubistCrudController
 
     protected $hidden = [];
 
+    protected $revisionEnabled = false;
+
     public function _postSetModel()
     {
 
@@ -82,8 +84,8 @@ class CubistMagicController extends CubistCrudController
             }
         }
 
-        if (!$model->isRevisionEnabled()) {
-            $this->revisionEnabled = false;
+        if ($model->isRevisionEnabled()) {
+            $this->revisionEnabled = true;
         }
 
         $this->crud->setRoute(backpack_url($this->_routeURL));
index 27a6e8806af678e212cd265fe72d1311978c3e21..122b7c2c5487afae19a2fba9ab8270e5d6617227 100644 (file)
@@ -264,33 +264,6 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         }
     }
 
-    public function addFakes($columns = ['extras'])
-    {
-        try {
-            return parent::addFakes($columns);
-        } catch (\Exception $e) {
-
-        }
-
-        foreach ($columns as $key => $column) {
-            if (!isset($this->attributes[$column])) {
-                continue;
-            }
-
-            $column_contents = $this->{$column};
-
-            if ($this->shouldDecodeFake($column) && is_string($column_contents)) {
-                $column_contents = json_decode($column_contents);
-            }
-
-            if ((is_array($column_contents) || is_object($column_contents) || $column_contents instanceof Traversable)) {
-                foreach ($column_contents as $fake_field_name => $fake_field_value) {
-                    $this->setAttribute($fake_field_name, $fake_field_value);
-                }
-            }
-        }
-    }
-
 
     public function getFields()
     {
index 7f9b897f4fc9ea38b490dc2fc46440d062abb170..6a6a91d1f2202b6085030e45a8ffec45ae8a9de8 100644 (file)
@@ -7,6 +7,7 @@ use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;
 use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\Sluggable;
 use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\SluggableScopeHelpers;
 use Cubist\Util\Json;
+use Cubist\Util\PHP;
 
 class CubistMagicTranslatableModel extends CubistMagicAbstractModel
 {
@@ -31,8 +32,14 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
      */
     protected $_availableLocales = 'backpack';
 
+    protected $_castCache = null;
+
+    protected $_fakesAdded = false;
+
     public function postSetFields()
     {
+        PHP::neverStop();
+
         parent::postSetFields();
         $this->translatable = array_unique($this->translatable);
     }
@@ -45,6 +52,7 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
                 if (!in_array($field->getAttribute('store_in'), $this->translatable)) {
                     $this->translatable[] = $field->getAttribute('store_in');
                 }
+                $this->translatable[] = $field->getAttribute('name');
             }
         } else {
             if ($field->getAttribute('translatable')) {
@@ -61,9 +69,8 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
             if (isset($this->_translations[$key])) {
                 return $this->_translations[$key];
             }
-            $measure_label = 'Get translation of ' . get_class($this) . '::' . $key;
-            throw new \Error(':(');
-            start_measure($measure_label);
+//            $measure_label = 'Get translation in ' . get_class($this);
+//            start_measure($measure_label);
             $this->guardAgainstNonTranslatableAttribute($key);
             $attrs = $this->getAttributes();
             if (!isset($attrs[$key])) {
@@ -88,7 +95,7 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
                 });
             }
             $this->_translations[$key] = $res;
-            stop_measure($measure_label);
+//            stop_measure($measure_label);
             return $res;
         }
 
@@ -102,6 +109,7 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
         return $res;
     }
 
+
     public function copyAllTranslations($from, $to, $overwrite = false)
     {
         $i = 0;
@@ -130,8 +138,22 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
         $this->save();
     }
 
+    public function addFakes($columns = ['extras'])
+    {
+        if ($this->_fakesAdded) {
+            return;
+        }
+        $this->_fakesAdded = true;
+        start_measure('add fakes ' . get_class($this));
+        $res = parent::addFakes($columns);
+        stop_measure('add fakes ' . get_class($this));
+        return $res;
+    }
+
     public function getAttribute($key)
     {
+//        $label = 'getAttribute in ' . get_class($this);
+//        start_measure($label);
         $f = $this->getField($key);
         $value = parent::getAttribute($key);
         if (!$value && null !== $f && $f->isMigrateTranslation()) {
@@ -143,9 +165,12 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
         }
 
         if (null === $f) {
-            return $value;
+            $res = $value;
+        } else {
+            $res = $f->filterValue($value);
         }
-        return $f->filterValue($value);
+        //stop_measure($label);
+        return $res;
     }
 
     protected function _migrateTranslation($key)
@@ -173,6 +198,7 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
 
     public function getAvailableLocales()
     {
+        start_measure('getAvailableLocales()');
         if ($this->_availableLocales === 'backpack') {
             $res = $this->getAvailableLocalesTranslations();
         } else if (is_array($this->_availableLocales)) {
@@ -180,6 +206,7 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
         }
 
         uksort($res, [$this, '_sortLocales']);
+        stop_measure('getAvailableLocales()');
         return $res;
     }
 
@@ -187,5 +214,4 @@ class CubistMagicTranslatableModel extends CubistMagicAbstractModel
     {
         return strcmp($a, $b);
     }
-
 }
index cb9d49777116b3e801905293844a59feee101d7f..d92d863f767309c5184c04f3a9e7e1acf7c74d7c 100644 (file)
@@ -1,3 +1,8 @@
+@php
+
+    start_measure('Render edit form');
+@endphp
+
 @extends(backpack_view('blank'))
 @include('cubist_back::form.additional_edit_views')
 
@@ -8,7 +13,7 @@
             // When clicking on a locale link in the dropdown menu, it doesn't preserve
             // the current tab because this is stored in the hash and managed by JS.
             // To overcome this, we catch the locale links and append the hash to them...
-            $(document).on('click', '.dropdown-menu a[href*="?locale="]', function (event) {
+            $(document).on('click', '.dropdown-menu a[href*="?_locale="]', function (event) {
                 event.preventDefault();
                 window.location.href = $(this).attr('href') + document.location.hash;
             });
@@ -41,7 +46,7 @@
             <small>{!! $crud->getSubheading() ?? trans('backpack::crud.edit').' '.$crud->entity_name !!}.</small>
 
             @if ($crud->hasAccess('list'))
-                <small><a href="{{ url($crud->route) }}" class="hidden-print font-sm"><i
+                <small><a href="{{ url($crud->route) }}" class="d-print-none font-sm"><i
                             class="la la-angle-double-{{ config('backpack.base.html_direction') == 'rtl' ? 'right' : 'left' }}"></i> {{ trans('backpack::crud.back_to_all') }}
                         <span>{{ $crud->entity_name_plural }}</span></a></small>
             @endif
 @section('content')
     <div class="row">
         <div class="{{ $crud->getEditContentClass() }}">
-            <!-- Default box -->
+            {{-- Default box --}}
 
             @include('crud::inc.grouped_errors')
 
             <form method="post"
                   action="{{ url($crud->route.'/'.$entry->getKey()) }}"
                   @if ($crud->hasUploadFields('update', $entry->getKey()))
-                  enctype="multipart/form-data"
+                      enctype="multipart/form-data"
                 @endif
             >
                 {!! csrf_field() !!}
 
                 @if ($crud->model->translationEnabled())
                     <div class="mb-2 text-right">
-                        <!-- Single button -->
+                        {{-- Single button --}}
                         <div class="btn-group">
                             <button type="button" class="btn btn-sm btn-primary dropdown-toggle" data-toggle="dropdown"
                                     aria-haspopup="true" aria-expanded="false">
                                 {{trans('backpack::crud.language')}}
-                                : {{ $crud->model->getAvailableLocales()[request()->input('locale')?request()->input('locale'):App::getLocale()] }}
+                                : {{ $crud->model->getAvailableLocales()[request()->input('_locale')?request()->input('_locale'):App::getLocale()] }}
                                 &nbsp; <span class="caret"></span>
                             </button>
                             <ul class="dropdown-menu">
                                 @foreach ($crud->model->getAvailableLocales() as $key => $locale)
                                     <a class="dropdown-item"
-                                       href="{{ url($crud->route.'/'.$entry->getKey().'/edit') }}?locale={{ $key }}">{{ $locale }}</a>
+                                       href="{{ url($crud->route.'/'.$entry->getKey().'/edit') }}?_locale={{ $key }}">{{ $locale }}</a>
                                 @endforeach
                             </ul>
                         </div>
                     </div>
                 @endif
-                <!-- load the view from the application if it exists, otherwise load the one in the package -->
+                {{-- load the view from the application if it exists, otherwise load the one in the package --}}
                 @if(view()->exists('vendor.backpack.crud.form_content'))
                     @include('vendor.backpack.crud.form_content', ['fields' => $crud->fields(), 'action' => 'edit'])
                 @else
                     @include('crud::form_content', ['fields' => $crud->fields(), 'action' => 'edit'])
                 @endif
-
+                {{-- This makes sure that all field assets are loaded. --}}
+                <div class="d-none" id="parentLoadedAssets">{{ json_encode(Assets::loaded()) }}</div>
                 @include('crud::inc.form_save_buttons')
             </form>
         </div>
 
     @stack('after_form')
 @endsection
+@php
+
+    stop_measure('Render edit form');
+@endphp