This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit da17667
Showing
20 changed files
with
471 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "epigra/nova-settings", | ||
"description": "A Laravel Nova tool for editing custom settings using native Nova fields.", | ||
"authors": [ | ||
{ | ||
"name": "Uğur Aydoğdu", | ||
"email": "ugur.aydogdu@epigra.com" | ||
} | ||
], | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Epigra\\NovaSettings\\Providers\\NovaSettingsServiceProvider" | ||
], | ||
"aliases": { | ||
|
||
} | ||
} | ||
}, | ||
"require": { | ||
"php": ">=7.1.0", | ||
"akaunting/setting": "^1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Epigra\\NovaSettings\\": "src" | ||
} | ||
} | ||
} |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"/js/tool.js": "/js/tool.js", | ||
"/css/tool.css": "/css/tool.css" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "NovaSettings", | ||
"alias": "novasettings", | ||
"description": "", | ||
"keywords": [], | ||
"active": 1, | ||
"order": 0, | ||
"providers": [ | ||
"Epigra\\NovaSettings\\Providers\\NovaSettingsServiceProvider" | ||
], | ||
"aliases": {}, | ||
"files": [], | ||
"requires": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"dev": "npm run development", | ||
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", | ||
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", | ||
"watch-poll": "npm run watch -- --watch-poll", | ||
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js", | ||
"prod": "npm run production", | ||
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^5.2.0", | ||
"laravel-mix": "^4.0.7", | ||
"prettier": "^1.18.2" | ||
}, | ||
"dependencies": { | ||
"laravel-nova": "^1.0.9", | ||
"vue": "^2.6.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Nova Settings | ||
|
||
This [Laravel Nova](https://nova.laravel.com) package allows you to create custom settings in code (using Nova's native fields) and creates a UI for the users where the settings can be edited by using [akaunting/setting](https://github.com/akaunting/setting) package. | ||
|
||
## Installation | ||
|
||
Install the package in a Laravel Nova project via Composer: | ||
|
||
```bash | ||
composer require epigra/nova-settings | ||
``` | ||
|
||
To publish the database migration(s) configuration of `akaunting/setting` | ||
|
||
```bash | ||
php artisan vendor:publish --tag=setting | ||
php artisan migrate | ||
``` | ||
|
||
Register the tool with Nova in the `tools()` method of the `NovaServiceProvider`: | ||
|
||
```php | ||
// in app/Providers/NovaServiceProvider.php | ||
|
||
public function tools() | ||
{ | ||
return [ | ||
// ... | ||
new \Epigra\NovaSettings\NovaSettingsTool | ||
]; | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
### Registering fields | ||
|
||
Define the fields in your `NovaServiceProvider`'s `boot()` function by calling `NovaSettings::setSettingsFields()`. | ||
|
||
```php | ||
\Epigra\NovaSettings\NovaSettingsTool::setSettingsFields([ | ||
Text::make('Some setting', 'some_setting'), | ||
Number::make('A number', 'a_number'). | ||
]); | ||
``` | ||
|
||
### Custom formatting | ||
|
||
If you want the value of the setting to be formatted before it's returned, pass a `Closure` as the second parameter to the `setSettingsFields` function. The function receives two arguments: `key` and `value`. | ||
|
||
```php | ||
\Epigra\NovaSettings\NovaSettingsTool::setSettingsFields([ | ||
// ... fields | ||
], function ($key, $value) { | ||
if ($key === 'some_boolean_value') return boolval($value); | ||
return $value; | ||
}); | ||
``` | ||
|
||
# Credits | ||
|
||
Thanks for the inspiration. | ||
|
||
### akaunting/setting | ||
|
||
You can visit [https://github.com/akaunting/setting]() to get more information on how to use getters/setters and facade of settings package. | ||
|
||
### optimistdigital/nova-settings | ||
This package is inspired by [optimistdigital/nova-settings](https://github.com/optimistdigital/nova-settings) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Epigra\NovaSettings\Http\Controllers; | ||
|
||
use Illuminate\Routing\Controller; | ||
use Illuminate\Http\Request; | ||
use Epigra\NovaSettings\NovaSettingsTool; | ||
use Laravel\Nova\Contracts\Resolvable; | ||
use Laravel\Nova\Http\Requests\NovaRequest; | ||
|
||
class SettingsController extends Controller | ||
{ | ||
public function get(Request $request) | ||
{ | ||
$fields = collect(NovaSettingsTool::getSettingsFields()); | ||
|
||
$fields->whereInstanceOf(Resolvable::class)->each(function (&$field) { | ||
if (!empty($field->attribute)) { | ||
$field->resolve([$field->attribute => setting($field->attribute)]); | ||
} | ||
}); | ||
|
||
return $fields; | ||
} | ||
|
||
public function save(NovaRequest $request) | ||
{ | ||
$fields = collect(NovaSettingsTool::getSettingsFields()); | ||
|
||
$fields->whereInstanceOf(Resolvable::class)->each(function ($field) use ($request) { | ||
if (empty($field->attribute)) return; | ||
|
||
$tempResource = new \stdClass; | ||
$field->fill($request, $tempResource); | ||
|
||
setting([$field->attribute => $tempResource->{$field->attribute} ]); | ||
}); | ||
|
||
setting()->save(); | ||
|
||
return response('', 204); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Epigra\NovaSettings\Http\Middleware; | ||
|
||
use Laravel\Nova\Nova; | ||
use Epigra\NovaSettings\NovaSettingsTool; | ||
|
||
class Authorize | ||
{ | ||
/** | ||
* Handle the incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handle($request, $next) | ||
{ | ||
$tool = collect(Nova::registeredTools())->first([$this, 'matchesTool']); | ||
|
||
return optional($tool)->authorize($request) ? $next($request) : abort(403); | ||
} | ||
|
||
/** | ||
* Determine whether this tool belongs to the package. | ||
* | ||
* @param \Laravel\Nova\Tool $tool | ||
* @return bool | ||
*/ | ||
public function matchesTool($tool) | ||
{ | ||
return $tool instanceof NovaSettingsTool; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Epigra\NovaSettings; | ||
|
||
use Laravel\Nova\Nova; | ||
use Laravel\Nova\Tool; | ||
|
||
class NovaSettingsTool extends Tool | ||
{ | ||
protected static $settingsFields = []; | ||
protected static $customFormatter; | ||
|
||
public function boot() | ||
{ | ||
Nova::script('nova-settings', __DIR__ . '/../dist/js/tool.js'); | ||
Nova::style('nova-settings', __DIR__ . '/../dist/css/tool.css'); | ||
} | ||
|
||
public function renderNavigation() | ||
{ | ||
return view('nova-settings::navigation'); | ||
} | ||
|
||
/** | ||
* Define settings fields and an optional custom format function. | ||
* | ||
* @param array $settingsFields Array of Nova fields to be displayed. | ||
* @param \Closure $customFormatter A function that takes key and value as arguments, formats and returns the value. | ||
**/ | ||
public static function setSettingsFields($settingsFields = [], $customFormatter = null) | ||
{ | ||
self::$settingsFields = $settingsFields; | ||
self::$customFormatter = $customFormatter; | ||
} | ||
|
||
public static function getSettingsFields() | ||
{ | ||
return self::$settingsFields; | ||
} | ||
|
||
public static function getCustomFormatter() | ||
{ | ||
return self::$customFormatter; | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Epigra\NovaSettings\Providers; | ||
|
||
use Laravel\Nova\Nova; | ||
use Laravel\Nova\Events\ServingNova; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\ServiceProvider; | ||
use Epigra\NovaSettings\Http\Middleware\Authorize; | ||
|
||
class NovaSettingsServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->loadViewsFrom(__DIR__ . '/../Resources/views', 'nova-settings'); | ||
|
||
$this->app->booted(function () { | ||
$this->routes(); | ||
}); | ||
|
||
Nova::serving(function (ServingNova $event) { | ||
// | ||
}); | ||
|
||
if ($this->app->runningInConsole()) { | ||
// Publish migrations | ||
$this->publishes([ | ||
__DIR__ . '/../database/migrations' => database_path('migrations'), | ||
], 'migrations'); | ||
} | ||
} | ||
|
||
/** | ||
* Register the tool's routes. | ||
* | ||
* @return void | ||
*/ | ||
protected function routes() | ||
{ | ||
if ($this->app->routesAreCached()) { | ||
return; | ||
} | ||
|
||
Route::middleware(['nova', Authorize::class]) | ||
->prefix('nova-vendor/nova-settings') | ||
->group(__DIR__ . '/../Routes/api.php'); | ||
} | ||
|
||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Nova.booting((Vue, router, store) => { | ||
router.addRoutes([ | ||
{ | ||
name: 'nova-settings', | ||
path: '/nova-settings', | ||
component: require('./views/Settings'), | ||
}, | ||
]); | ||
}); |
Oops, something went wrong.