]> _ Git - cubist_cms-back.git/commitdiff
wip #5718 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Feb 2023 18:35:54 +0000 (19:35 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Feb 2023 18:35:54 +0000 (19:35 +0100)
src/app/Magic/Controllers/CubistMagicController.php
src/app/Magic/Models/CubistMagicAbstractModel.php
src/app/Magic/Traits/CustomDataSource.php
src/resources/views/columns/filesize.blade.php [new file with mode: 0644]

index a65d336b1bf3e39a67ff721b576b00e8e9aecd51..8f8b87d35208c665afb6f6d088329e702af53519 100644 (file)
@@ -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);
     }
index 7708ff3bc80fcd7761a2187aac832ec870a9df48..3b395ebdb548edb643c7e68f8554bb7abe7e8b92 100644 (file)
@@ -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)
index 2cbe918c2dfeab3149cc1d85e41c606dc875d8d7..47c6c5ef925514733a9c5b1da3803582c7d9c6d4 100644 (file)
@@ -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 (file)
index 0000000..d83856c
--- /dev/null
@@ -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
+
+<span>
+    @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_start')
+    {{ $value }}
+    @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_end')
+</span>