From cecbbe3538c3982cfa9e5ccbe0e0ec03e2d2a8f2 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 12 Apr 2023 13:29:35 +0200 Subject: [PATCH] wip #5851 @3 --- .../Controllers/CubistMagicController.php | 6 ++- .../Magic/Models/CubistMagicAbstractModel.php | 27 ------------- .../Models/CubistMagicTranslatableModel.php | 40 +++++++++++++++---- src/resources/views/edit.blade.php | 28 ++++++++----- 4 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index f8b117a..263d81c 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -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)); diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 27a6e88..122b7c2 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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() { diff --git a/src/app/Magic/Models/CubistMagicTranslatableModel.php b/src/app/Magic/Models/CubistMagicTranslatableModel.php index 7f9b897..6a6a91d 100644 --- a/src/app/Magic/Models/CubistMagicTranslatableModel.php +++ b/src/app/Magic/Models/CubistMagicTranslatableModel.php @@ -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); } - } diff --git a/src/resources/views/edit.blade.php b/src/resources/views/edit.blade.php index cb9d497..d92d863 100644 --- a/src/resources/views/edit.blade.php +++ b/src/resources/views/edit.blade.php @@ -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 @@ {!! $crud->getSubheading() ?? trans('backpack::crud.edit').' '.$crud->entity_name !!}. @if ($crud->hasAccess('list')) - {{ trans('backpack::crud.back_to_all') }} {{ $crud->entity_name_plural }} @endif @@ -52,14 +57,14 @@ @section('content')
- + {{-- Default box --}} @include('crud::inc.grouped_errors')
hasUploadFields('update', $entry->getKey())) - enctype="multipart/form-data" + enctype="multipart/form-data" @endif > {!! csrf_field() !!} @@ -67,30 +72,31 @@ @if ($crud->model->translationEnabled())
- + {{-- Single button --}}
@endif - + {{-- 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. --}} +
{{ json_encode(Assets::loaded()) }}
@include('crud::inc.form_save_buttons')
@@ -99,4 +105,8 @@ @stack('after_form') @endsection +@php + + stop_measure('Render edit form'); +@endphp -- 2.39.5