From: Vincent Vanwaelscappel Date: Wed, 15 Feb 2023 18:35:54 +0000 (+0100) Subject: wip #5718 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e32998f56dd66c2e24f89018c4010ef90a53d65c;p=cubist_cms-back.git wip #5718 @0.5 --- diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index a65d336..8f8b87d 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -95,7 +95,9 @@ class CubistMagicController extends CubistCrudController | CrudPanel Configuration |-------------------------------------------------------------------------- */ - $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'thousands_sep' => '', 'searchLogic' => 'text']); + if ($model->showPrimaryColumn()) { + $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => $model->getPrimaryColumnType(), 'label' => "#", 'thousands_sep' => '', 'searchLogic' => 'text']); + } $this->updateFieldsFromModel($model); } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 7708ff3..3b395eb 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -134,7 +134,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia public function scopeOneinstance($query) { if ($this->getOption('oneinstance', false)) { - $query->where('id', 1); + $query->where($this->primaryKey, 1); } } @@ -209,6 +209,18 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $this->primaryKey; } + public function getPrimaryColumnType() + { + if ($this->keyType === 'int') { + return 'number'; + } + return 'text'; + } + + public function showPrimaryColumn(){ + return true; + } + public function setFields() { @@ -318,7 +330,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia if ($reset) { return false; } - return Str::slug($this->getAttributeValue('id')); + return Str::slug($this->getAttributeValue($this->primaryKey)); } public function getOption($key, $default = null) diff --git a/src/app/Magic/Traits/CustomDataSource.php b/src/app/Magic/Traits/CustomDataSource.php index 2cbe918..47c6c5e 100644 --- a/src/app/Magic/Traits/CustomDataSource.php +++ b/src/app/Magic/Traits/CustomDataSource.php @@ -7,6 +7,8 @@ use Illuminate\Support\Facades\Log; trait CustomDataSource { + protected $_data = null; + public static function bootCustomDataSource() { $instance = (new static); @@ -15,10 +17,22 @@ trait CustomDataSource } } + /** * @return array[] */ public function getData() + { + if ($this->_data === null) { + $this->_data = $this->_getData(); + } + return $this->_data; + } + + /** + * @return array[] + */ + protected function _getData() { return []; } @@ -26,18 +40,37 @@ trait CustomDataSource public function refreshDatabase() { $hadError = false; - static::truncate(); - foreach (array_chunk($this->getData(), $this->getInsertChunkSize()) ?? [] as $inserts) { - if (!empty($inserts)) { - try { - static::insert($inserts); - } catch (\Exception $e) { - $hadError = true; - Log::error($e); - break; - } - } + + + $listAllInDb = static::all(); + $pk = $this->getPrimaryKey(); + $existingKeys = []; + foreach ($listAllInDb as $item) { + $existingKeys[] = $item->{$pk}; + } + $keys = []; + $rows = $this->getData(); + foreach ($rows as $row) { + $keys[] = $row[$pk]; + } + + $toInsert = array_diff($keys, $existingKeys); + $toDelete = array_diff($existingKeys, $keys); + + $inserts = []; + foreach ($toInsert as $hash) { + $inserts[$hash] = $rows[$hash]; } + + try { + static::insert($inserts); + } catch (\Exception $e) { + $hadError = true; + Log::error($e); + } + + static::whereIn($pk,$toDelete)->delete(); + Cache::put($this->getCacheKey() . '_refresh', $hadError ? -1 : time()); } diff --git a/src/resources/views/columns/filesize.blade.php b/src/resources/views/columns/filesize.blade.php new file mode 100644 index 0000000..d83856c --- /dev/null +++ b/src/resources/views/columns/filesize.blade.php @@ -0,0 +1,13 @@ +{{-- regular object attribute --}} +@php + $value = data_get($entry, $column['name']); + $value = is_array($value) ? json_encode($value) : $value; + $value=\Cubist\Util\Files\Files::humanReadableSize($value); + +@endphp + + + @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_start') + {{ $value }} + @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_end') +