Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
package init
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbn committed Aug 14, 2019
0 parents commit da17667
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
Thumbs.db
29 changes: 29 additions & 0 deletions composer.json
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 added dist/css/tool.css
Empty file.
1 change: 1 addition & 0 deletions dist/js/tool.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/mix-manifest.json
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"
}
14 changes: 14 additions & 0 deletions module.json
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": []
}
21 changes: 21 additions & 0 deletions package.json
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"
}
}
69 changes: 69 additions & 0 deletions readme.md
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 added src/Http/Controllers/.gitkeep
Empty file.
43 changes: 43 additions & 0 deletions src/Http/Controllers/SettingsController.php
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);
}
}
34 changes: 34 additions & 0 deletions src/Http/Middleware/Authorize.php
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;
}
}
45 changes: 45 additions & 0 deletions src/NovaSettingsTool.php
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 added src/Providers/.gitkeep
Empty file.
63 changes: 63 additions & 0 deletions src/Providers/NovaSettingsServiceProvider.php
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()
{
//
}
}
9 changes: 9 additions & 0 deletions src/Resources/js/tool.js
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'),
},
]);
});
Loading

0 comments on commit da17667

Please sign in to comment.