Skip to content

Commit

Permalink
Merge pull request #522 from darron1217/setting
Browse files Browse the repository at this point in the history
[Setting Module] Add view type media-single
  • Loading branch information
nWidart authored Apr 19, 2018
2 parents 5de1146 + 14a18d2 commit 07f036e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Modules/Setting/Entities/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

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']);
}
}
27 changes: 25 additions & 2 deletions Modules/Setting/Events/SettingWasCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
28 changes: 26 additions & 2 deletions Modules/Setting/Events/SettingWasUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function createOrUpdate($settings)
$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;
Expand Down Expand Up @@ -99,7 +109,7 @@ private function createForName($settingName, $settingValues)

$setting->save();

event(new SettingWasCreated($setting));
event(new SettingWasCreated($setting, $settingValues));

return $setting;
}
Expand All @@ -121,7 +131,7 @@ private function updateSetting($setting, $settingValues)
}
$setting->save();

event(new SettingWasUpdated($setting));
event(new SettingWasUpdated($setting, $settingValues));

return $setting;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@php
$setting = isset($dbSettings[$settingName]) ? $dbSettings[$settingName] : null;
@endphp

@mediaSingle($settingName, $setting, null, trans($moduleInfo['description']))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@php
$setting = isset($dbSettings[$settingName]) ? $dbSettings[$settingName] : null;
@endphp

@mediaSingle($settingName, $setting, null, trans($moduleInfo['description']))
4 changes: 4 additions & 0 deletions Modules/Setting/Support/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function get($name, $locale = null, $default = null)
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;
Expand Down
4 changes: 4 additions & 0 deletions Modules/Setting/Tests/BaseSettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ private function resetDatabase()
$this->artisan('migrate', [
'--database' => 'sqlite',
]);
$this->artisan('migrate', [
'--database' => 'sqlite',
'--path' => 'Modules/Media/Database/Migrations',
]);
}
}

0 comments on commit 07f036e

Please sign in to comment.