| 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);
}
public function scopeOneinstance($query)
{
if ($this->getOption('oneinstance', false)) {
- $query->where('id', 1);
+ $query->where($this->primaryKey, 1);
}
}
return $this->primaryKey;
}
+ public function getPrimaryColumnType()
+ {
+ if ($this->keyType === 'int') {
+ return 'number';
+ }
+ return 'text';
+ }
+
+ public function showPrimaryColumn(){
+ return true;
+ }
+
public function setFields()
{
if ($reset) {
return false;
}
- return Str::slug($this->getAttributeValue('id'));
+ return Str::slug($this->getAttributeValue($this->primaryKey));
}
public function getOption($key, $default = null)
trait CustomDataSource
{
+ protected $_data = null;
+
public static function bootCustomDataSource()
{
$instance = (new static);
}
}
+
/**
* @return array[]
*/
public function getData()
+ {
+ if ($this->_data === null) {
+ $this->_data = $this->_getData();
+ }
+ return $this->_data;
+ }
+
+ /**
+ * @return array[]
+ */
+ protected function _getData()
{
return [];
}
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());
}
--- /dev/null
+{{-- 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>