Moved to _dev
This commit is contained in:
81
crater/app/Traits/HasCustomFieldsTrait.php
Normal file
81
crater/app/Traits/HasCustomFieldsTrait.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Crater\Traits;
|
||||
|
||||
use Crater\Models\CustomField;
|
||||
|
||||
trait HasCustomFieldsTrait
|
||||
{
|
||||
public function fields()
|
||||
{
|
||||
return $this->morphMany('Crater\Models\CustomFieldValue', 'custom_field_valuable');
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::deleting(function ($data) {
|
||||
if ($data->fields()->exists()) {
|
||||
$data->fields()->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function addCustomFields($customFields)
|
||||
{
|
||||
foreach ($customFields as $field) {
|
||||
if (! is_array($field)) {
|
||||
$field = (array)$field;
|
||||
}
|
||||
$customField = CustomField::find($field['id']);
|
||||
|
||||
$customFieldValue = [
|
||||
'type' => $customField->type,
|
||||
'custom_field_id' => $customField->id,
|
||||
'company_id' => $customField->company_id,
|
||||
getCustomFieldValueKey($customField->type) => $field['value'],
|
||||
];
|
||||
|
||||
$this->fields()->create($customFieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
public function updateCustomFields($customFields)
|
||||
{
|
||||
foreach ($customFields as $field) {
|
||||
if (! is_array($field)) {
|
||||
$field = (array)$field;
|
||||
}
|
||||
|
||||
$customField = CustomField::find($field['id']);
|
||||
$customFieldValue = $this->fields()->firstOrCreate([
|
||||
'custom_field_id' => $customField->id,
|
||||
'type' => $customField->type,
|
||||
'company_id' => $this->company_id,
|
||||
]);
|
||||
|
||||
$type = getCustomFieldValueKey($customField->type);
|
||||
$customFieldValue->$type = $field['value'];
|
||||
$customFieldValue->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function getCustomFieldBySlug($slug)
|
||||
{
|
||||
return $this->fields()
|
||||
->with('customField')
|
||||
->whereHas('customField', function ($query) use ($slug) {
|
||||
$query->where('slug', $slug);
|
||||
})->first();
|
||||
}
|
||||
|
||||
public function getCustomFieldValueBySlug($slug)
|
||||
{
|
||||
$value = $this->getCustomFieldBySlug($slug);
|
||||
|
||||
if ($value) {
|
||||
return $value->defaultAnswer;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user