Commit 4477b3ac authored by Nicolas Widart's avatar Nicolas Widart

Creating settings options

parent 15d0754e
<?php namespace Modules\Setting\Entities; <?php namespace Modules\Setting\Entities;
use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class Setting extends Model class Setting extends Model
{ {
use Translatable;
public $translatedAttributes = ['value', 'description'];
protected $fillable = ['name', 'value', 'description'];
} }
<?php namespace Modules\Setting\Entities;
use Illuminate\Database\Eloquent\Model;
class SettingTranslation extends Model
{
public $timestamps = false;
protected $fillable = ['value', 'description'];
}
<?php namespace Modules\Setting\Http\Controllers\Admin; <?php namespace Modules\Setting\Http\Controllers\Admin;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\View;
use Laracasts\Flash\Flash;
use Modules\Core\Http\Controllers\Admin\AdminBaseController; use Modules\Core\Http\Controllers\Admin\AdminBaseController;
use Modules\Setting\Http\Requests\SettingRequest;
use Modules\Setting\Repositories\SettingRepository; use Modules\Setting\Repositories\SettingRepository;
class SettingController extends AdminBaseController class SettingController extends AdminBaseController
...@@ -23,8 +26,11 @@ class SettingController extends AdminBaseController ...@@ -23,8 +26,11 @@ class SettingController extends AdminBaseController
return View::make('setting::admin.settings'); return View::make('setting::admin.settings');
} }
public function store() public function store(SettingRequest $request)
{ {
$this->setting->createOrUpdate($request->all());
Flash::success('Settings saved!');
return Redirect::route('dashboard.setting.index');
} }
} }
<?php namespace Modules\Setting\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SettingRequest extends FormRequest
{
public function rules()
{
return [];
}
public function authorize()
{
return true;
}
public function messages()
{
return [];
}
}
...@@ -16,8 +16,67 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin ...@@ -16,8 +16,67 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
{ {
} }
/**
* @param mixed $data
* @return mixed
*/
public function create($data) public function create($data)
{ {
}
/**
* Create or update the settings
* @param $settings
* @return mixed|void
*/
public function createOrUpdate($settings)
{
$this->removeTokenKey($settings);
foreach ($settings as $settingName => $settingValues) {
// Check if setting exists
if ($setting = $this->findByName($settingName)) {
}
$this->createForName($settingName, $settingValues);
}
}
/**
* Remove the token from the input array
* @param $settings
*/
private function removeTokenKey(&$settings)
{
unset($settings['_token']);
}
/**
* Find a setting by its name
* @param $settingName
* @return mixed
*/
public function findByName($settingName)
{
return $this->model->whereHas('translations', function($q) use($settingName)
{
$q->where('name', $settingName);
})->first();
}
/**
* Create a setting with the given name
* @param $settingName
* @param $settingValues
*/
private function createForName($settingName, $settingValues)
{
$setting = new $this->model;
$setting->name = $settingName;
foreach ($settingValues as $lang => $value) {
$setting->translate($lang)->value = $value;
}
$setting->save();
} }
} }
...@@ -4,4 +4,17 @@ use Modules\Core\Repositories\BaseRepository; ...@@ -4,4 +4,17 @@ use Modules\Core\Repositories\BaseRepository;
interface SettingRepository extends BaseRepository interface SettingRepository extends BaseRepository
{ {
/**
* Create or update the settings
* @param $settings
* @return mixed
*/
public function createOrUpdate($settings);
/**
* Find a setting by its name
* @param $settingName
* @return mixed
*/
public function findByName($settingName);
} }
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