¨4.0.1¨
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Modules\Option\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Option\Admin\OptionTable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@@ -20,8 +21,7 @@ class Option extends Model
|
||||
'field', 'textarea', 'dropdown', 'checkbox', 'checkbox_custom',
|
||||
'radio', 'radio_custom', 'multiple_select', 'date', 'date_time', 'time',
|
||||
];
|
||||
|
||||
/**
|
||||
/*
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
@@ -59,6 +59,7 @@ class Option extends Model
|
||||
*/
|
||||
protected $translatedAttributes = ['name'];
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
@@ -73,11 +74,34 @@ class Option extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public function isFieldType()
|
||||
|
||||
/**
|
||||
* Save values for the option.
|
||||
*
|
||||
* @param array $values
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function saveValues($values = [])
|
||||
{
|
||||
return in_array($this->type, ['field', 'textarea', 'dropdown', 'radio', 'date', 'date_time', 'time']);
|
||||
$ids = $this->getDeleteCandidates($values);
|
||||
|
||||
if ($ids->isNotEmpty()) {
|
||||
$this->values()->whereIn('id', $ids)->delete();
|
||||
}
|
||||
|
||||
$counter = 0;
|
||||
|
||||
foreach (array_reset_index($values) as $attributes) {
|
||||
$attributes += ['position' => ++$counter];
|
||||
|
||||
$this->values()->updateOrCreate([
|
||||
'id' => array_get($attributes, 'id'),
|
||||
], $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the values for the option.
|
||||
*
|
||||
@@ -88,49 +112,36 @@ class Option extends Model
|
||||
return $this->hasMany(OptionValue::class)->orderBy('position');
|
||||
}
|
||||
|
||||
|
||||
public function isFieldType()
|
||||
{
|
||||
return in_array($this->type, ['field', 'textarea', 'dropdown', 'radio', 'date', 'date_time', 'time']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scope a query to only include global options.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
* @param Builder $query
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeGlobals($query)
|
||||
{
|
||||
return $query->where('is_global', true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get table data for the resource
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @return OptionTable
|
||||
*/
|
||||
public function table()
|
||||
public function table(): OptionTable
|
||||
{
|
||||
return new OptionTable($this->newQuery()->globals());
|
||||
}
|
||||
|
||||
/**
|
||||
* Save values for the option.
|
||||
*
|
||||
* @param array $values
|
||||
* @return void
|
||||
*/
|
||||
public function saveValues($values = [])
|
||||
{
|
||||
$ids = $this->getDeleteCandidates($values);
|
||||
|
||||
if ($ids->isNotEmpty()) {
|
||||
$this->values()->whereIn('id', $ids)->delete();
|
||||
}
|
||||
|
||||
foreach (array_reset_index($values) as $index => $attributes) {
|
||||
$attributes += ['position' => $index];
|
||||
|
||||
$this->values()->updateOrCreate([
|
||||
'id' => array_get($attributes, 'id'),
|
||||
], $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
private function getDeleteCandidates($values)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user