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; ...@@ -4,12 +4,19 @@ namespace Modules\Setting\Entities;
use Dimsav\Translatable\Translatable; use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Modules\Media\Support\Traits\MediaRelation;
class Setting extends Model class Setting extends Model
{ {
use Translatable; use Translatable, MediaRelation;
public $translatedAttributes = ['value', 'description']; public $translatedAttributes = ['value', 'description'];
protected $fillable = ['name', 'value', 'description', 'isTranslatable', 'plainValue']; protected $fillable = ['name', 'value', 'description', 'isTranslatable', 'plainValue'];
protected $table = 'setting__settings'; 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 @@ ...@@ -2,17 +2,40 @@
namespace Modules\Setting\Events; namespace Modules\Setting\Events;
use Modules\Media\Contracts\StoringMedia;
use Modules\Setting\Entities\Setting; use Modules\Setting\Entities\Setting;
class SettingWasCreated class SettingWasCreated implements StoringMedia
{ {
/** /**
* @var Setting * @var Setting
*/ */
public $setting; public $setting;
public function __construct(Setting $setting) /**
* @var array
*/
public $data;
public function __construct(Setting $setting, $data)
{ {
$this->setting = $setting; $this->setting = $setting;
$this->data = $data;
}
/**
* @inheritDoc
*/
public function getEntity()
{
return $this->setting;
}
/**
* @inheritDoc
*/
public function getSubmissionData()
{
return $this->data;
} }
} }
...@@ -2,17 +2,41 @@ ...@@ -2,17 +2,41 @@
namespace Modules\Setting\Events; namespace Modules\Setting\Events;
use Modules\Media\Contracts\StoringMedia;
use Modules\Setting\Entities\Setting; use Modules\Setting\Entities\Setting;
class SettingWasUpdated class SettingWasUpdated implements StoringMedia
{ {
/** /**
* @var Setting * @var Setting
*/ */
public $setting; public $setting;
public function __construct(Setting $setting) /**
* @var array
*/
public $data;
public function __construct(Setting $setting, $data)
{ {
$this->setting = $setting; $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 ...@@ -49,6 +49,16 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
$this->removeTokenKey($settings); $this->removeTokenKey($settings);
foreach ($settings as $settingName => $settingValues) { 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)) { if ($setting = $this->findByName($settingName)) {
$this->updateSetting($setting, $settingValues); $this->updateSetting($setting, $settingValues);
continue; continue;
...@@ -99,7 +109,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin ...@@ -99,7 +109,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
$setting->save(); $setting->save();
event(new SettingWasCreated($setting)); event(new SettingWasCreated($setting, $settingValues));
return $setting; return $setting;
} }
...@@ -121,7 +131,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin ...@@ -121,7 +131,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
} }
$setting->save(); $setting->save();
event(new SettingWasUpdated($setting)); event(new SettingWasUpdated($setting, $settingValues));
return $setting; 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 ...@@ -36,6 +36,10 @@ class Settings implements Setting
return is_null($default) ? $defaultFromConfig : $default; return is_null($default) ? $defaultFromConfig : $default;
} }
if($setting->isMedia() && $media = $setting->files()->first()) {
return $media->path;
}
if ($setting->isTranslatable) { if ($setting->isTranslatable) {
if ($setting->hasTranslation($locale)) { if ($setting->hasTranslation($locale)) {
return trim($setting->translate($locale)->value) === '' ? $defaultFromConfig : $setting->translate($locale)->value; return trim($setting->translate($locale)->value) === '' ? $defaultFromConfig : $setting->translate($locale)->value;
......
...@@ -78,5 +78,9 @@ abstract class BaseSettingTest extends TestCase ...@@ -78,5 +78,9 @@ abstract class BaseSettingTest extends TestCase
$this->artisan('migrate', [ $this->artisan('migrate', [
'--database' => 'sqlite', '--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