Unverified Commit 07f036e6 authored by Nicolas Widart's avatar Nicolas Widart Committed by GitHub

Merge pull request #522 from darron1217/setting

[Setting Module] Add view type media-single
parents 5de11460 14a18d27
......@@ -4,12 +4,19 @@ namespace Modules\Setting\Entities;
use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;
use Modules\Media\Support\Traits\MediaRelation;
class Setting extends Model
{
use Translatable;
use Translatable, MediaRelation;
public $translatedAttributes = ['value', 'description'];
protected $fillable = ['name', 'value', 'description', 'isTranslatable', 'plainValue'];
protected $table = 'setting__settings';
public function isMedia(): bool
{
$value = json_decode($this->plainValue, true);
return is_array($value) && isset($value['medias_single']);
}
}
......@@ -2,17 +2,40 @@
namespace Modules\Setting\Events;
use Modules\Media\Contracts\StoringMedia;
use Modules\Setting\Entities\Setting;
class SettingWasCreated
class SettingWasCreated implements StoringMedia
{
/**
* @var Setting
*/
public $setting;
public function __construct(Setting $setting)
/**
* @var array
*/
public $data;
public function __construct(Setting $setting, $data)
{
$this->setting = $setting;
$this->data = $data;
}
/**
* @inheritDoc
*/
public function getEntity()
{
return $this->setting;
}
/**
* @inheritDoc
*/
public function getSubmissionData()
{
return $this->data;
}
}
......@@ -2,17 +2,41 @@
namespace Modules\Setting\Events;
use Modules\Media\Contracts\StoringMedia;
use Modules\Setting\Entities\Setting;
class SettingWasUpdated
class SettingWasUpdated implements StoringMedia
{
/**
* @var Setting
*/
public $setting;
public function __construct(Setting $setting)
/**
* @var array
*/
public $data;
public function __construct(Setting $setting, $data)
{
$this->setting = $setting;
$this->data = $data;
}
/**
* @inheritDoc
*/
public function getEntity()
{
return $this->setting;
}
/**
* @inheritDoc
*/
public function getSubmissionData()
{
return $this->data;
}
}
......@@ -49,6 +49,16 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
$this->removeTokenKey($settings);
foreach ($settings as $settingName => $settingValues) {
// Check if media exists
if($settingName == 'medias_single') {
// Get first key of values (Original settingName)
foreach ($settingValues as $key => $value) {
$normalisedValue = [ $settingName => [$key => $value] ];
$settingName = $key;
break;
}
$settingValues = $normalisedValue;
}
if ($setting = $this->findByName($settingName)) {
$this->updateSetting($setting, $settingValues);
continue;
......@@ -99,7 +109,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
$setting->save();
event(new SettingWasCreated($setting));
event(new SettingWasCreated($setting, $settingValues));
return $setting;
}
......@@ -121,7 +131,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
}
$setting->save();
event(new SettingWasUpdated($setting));
event(new SettingWasUpdated($setting, $settingValues));
return $setting;
}
......
@php
$setting = isset($dbSettings[$settingName]) ? $dbSettings[$settingName] : null;
@endphp
@mediaSingle($settingName, $setting, null, trans($moduleInfo['description']))
@php
$setting = isset($dbSettings[$settingName]) ? $dbSettings[$settingName] : null;
@endphp
@mediaSingle($settingName, $setting, null, trans($moduleInfo['description']))
......@@ -36,6 +36,10 @@ class Settings implements Setting
return is_null($default) ? $defaultFromConfig : $default;
}
if($setting->isMedia() && $media = $setting->files()->first()) {
return $media->path;
}
if ($setting->isTranslatable) {
if ($setting->hasTranslation($locale)) {
return trim($setting->translate($locale)->value) === '' ? $defaultFromConfig : $setting->translate($locale)->value;
......
......@@ -78,5 +78,9 @@ abstract class BaseSettingTest extends TestCase
$this->artisan('migrate', [
'--database' => 'sqlite',
]);
$this->artisan('migrate', [
'--database' => 'sqlite',
'--path' => 'Modules/Media/Database/Migrations',
]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment