Added initial settings interface, Fixes #9.

This commit is contained in:
Dan Brown
2015-08-30 15:31:16 +01:00
parent 8af012bc2a
commit 17f4aa4300
17 changed files with 275 additions and 35 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace Oxbow\Http\Controllers;
use Illuminate\Http\Request;
use Oxbow\Http\Requests;
use Oxbow\Http\Controllers\Controller;
use Setting;
class SettingController extends Controller
{
/**
* Display a listing of the settings.
*
* @return Response
*/
public function index()
{
$this->checkPermission('settings-update');
return view('settings/index');
}
/**
* Update the specified settings in storage.
*
* @param Request $request
* @return Response
*/
public function update(Request $request)
{
$this->checkPermission('settings-update');
// Cycles through posted settings and update them
foreach($request->all() as $name => $value) {
if(strpos($name, 'setting-') !== 0) continue;
$key = str_replace('setting-', '', trim($name));
Setting::put($key, $value);
}
return redirect('/settings');
}
}

View File

@ -71,6 +71,10 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/', 'HomeController@index');
Route::get('/home', 'HomeController@index');
// Settings
Route::get('/settings', 'SettingController@index');
Route::post('/settings', 'SettingController@update');
});

View File

@ -4,6 +4,7 @@ namespace Oxbow\Providers;
use Illuminate\Support\ServiceProvider;
use Oxbow\Services\ActivityService;
use Oxbow\Services\SettingService;
class CustomFacadeProvider extends ServiceProvider
{
@ -27,5 +28,9 @@ class CustomFacadeProvider extends ServiceProvider
$this->app->bind('activity', function() {
return new ActivityService($this->app->make('Oxbow\Activity'));
});
$this->app->bind('setting', function() {
return new SettingService($this->app->make('Oxbow\Setting'));
});
}
}

View File

@ -81,11 +81,13 @@ class ActivityService
* Gets the latest activity.
* @param int $count
* @param int $page
* @return array
*/
public function latest($count = 20, $page = 0)
{
return $this->activity->orderBy('created_at', 'desc')
$activityList = $this->activity->orderBy('created_at', 'desc')
->skip($count * $page)->take($count)->get();
return $this->filterSimilar($activityList);
}
/**
@ -99,7 +101,7 @@ class ActivityService
function entityActivity($entity, $count = 20, $page = 0)
{
$activity = $entity->hasMany('Oxbow\Activity')->orderBy('created_at', 'desc')
->skip($count*$page)->take($count)->get();
->skip($count * $page)->take($count)->get();
return $this->filterSimilar($activity);
}
@ -109,16 +111,17 @@ class ActivityService
* @param Activity[] $activity
* @return array
*/
protected function filterSimilar($activity) {
protected function filterSimilar($activity)
{
$newActivity = [];
$previousItem = false;
foreach($activity as $activityItem) {
if($previousItem === false) {
foreach ($activity as $activityItem) {
if ($previousItem === false) {
$previousItem = $activityItem;
$newActivity[] = $activityItem;
continue;
}
if(!$activityItem->isSimilarTo($previousItem)) {
if (!$activityItem->isSimilarTo($previousItem)) {
$newActivity[] = $activityItem;
}
$previousItem = $activityItem;

View File

@ -0,0 +1,14 @@
<?php namespace Oxbow\Services\Facades;
use Illuminate\Support\Facades\Facade;
class Setting extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'setting'; }
}

View File

@ -0,0 +1,89 @@
<?php namespace Oxbow\Services;
use Oxbow\Setting;
/**
* Class SettingService
*
* The settings are a simple key-value database store.
*
* @package Oxbow\Services
*/
class SettingService
{
protected $setting;
/**
* SettingService constructor.
* @param $setting
*/
public function __construct(Setting $setting)
{
$this->setting = $setting;
}
/**
* Gets a setting from the database,
* If not found, Returns default, Which is false by default.
* @param $key
* @param string|bool $default
* @return bool|string
*/
public function get($key, $default = false)
{
$setting = $this->getSettingObjectByKey($key);
return $setting === null ? $default : $setting->value;
}
/**
* Checks if a setting exists.
* @param $key
* @return bool
*/
public function has($key)
{
$setting = $this->getSettingObjectByKey($key);
return $setting !== null;
}
/**
* Add a setting to the database.
* @param $key
* @param $value
* @return bool
*/
public function put($key, $value)
{
$setting = $this->setting->firstOrNew([
'setting_key' => $key
]);
$setting->value = $value;
$setting->save();
return true;
}
/**
* Removes a setting from the database.
* @param $key
* @return bool
*/
public function remove($key)
{
$setting = $this->getSettingObjectByKey($key);
if($setting) {
$setting->delete();
}
return true;
}
/**
* Gets a setting model from the database for the given key.
* @param $key
* @return mixed
*/
private function getSettingObjectByKey($key) {
return $this->setting->where('setting_key', '=', $key)->first();
}
}

12
app/Setting.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace Oxbow;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = ['setting_key', 'value'];
protected $primaryKey = 'setting_key';
}