Skip to content

Commit

Permalink
Merge pull request #940 from FrozenNode/dev
Browse files Browse the repository at this point in the history
Dev into master
  • Loading branch information
David Mathews committed Oct 29, 2015
2 parents cd5f1bf + bfb3244 commit 9910955
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 9 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### 5.0.6
- Added: Support for custom domains in the admin routes
- Added: Ability to access the model from withinthe column output renderer
- Added: Dynamic Form Request Validation

### 5.0.5
- Added: Added password field to the settings view
- Added: Romanian Language
Expand Down
6 changes: 3 additions & 3 deletions docs/columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ Any SQL grouping function will work in the `select` statement. Much more is poss

If you want your column to show more than just text, you can use the `output` option. This can either be a string or an anonymous function.

If you provide an anonymous function, the only argument is the relevant column's value from the database. For instance, if you are using a [color](/docs/field-type-color) field and you want to clearly show the admin user what color a row is, you can do this:
If you provide an anonymous function, the argumentis available are the relevant column's value from the database, and the current model. For instance, if you are using a [color](/docs/field-type-color) field and you want to clearly show the admin user what color a row is, you can do this:

'hex' => array(
'title' => 'Color',
'output' => function($value)
'output' => function($value, $model)
{
return '<div style="background-color: ' . $value . '; width: 200px; height: 20px; border-radius: 2px;"></div>';
},
Expand All @@ -166,4 +166,4 @@ Or maybe you're using an [image](/docs/field-type-image) field and you want to d
'output' => '<img src="/uploads/collections/resize/(:value)" height="100" />',
),

Custom outputs are available for all column types, not just columns that are on the model's table.
Custom outputs are available for all column types, not just columns that are on the model's table.
32 changes: 31 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

Once the package is installed, you can publish the config file with:

### Laravel 5
php artisan vendor:publish --provider='Frozennode\Administrator\AdministratorServiceProvider'

### Laravel 4
php artisan config:publish frozennode/administrator

This will create the file `app/config/packages/frozennode/administrator/administrator.php` and seed it with some defaults. This [config file](http://administrator.frozennode.com/docs/configuration) is the primary way you interact with Administrator.
Expand All @@ -21,6 +25,8 @@ All of the configuration options are used, but not all of them must be supplied.

Below is a list of all the available options:

- [Uri](#uri)
- [Domain](#domain)
- [Title](#title)
- [Model Config Path](#model-config-path)
- [Settings Config Path](#settings-config-path)
Expand All @@ -36,6 +42,30 @@ Below is a list of all the available options:
- [Global Rows Per Page](#global-rows-per-page)
- [Locales](#locales)

<a name="uri"></a>
### Uri

/**
* Package URI
*
* @type string
*/
'uri' => 'admin',

This is the base route for the administrator package to be called on.

<a name="domain"></a>
### Domain

/**
* Page domain
*
* @type string
*/
'domain' => '',

This is the base domain for the administrator route. This can allow you to lock down the admin to just a certain domain/subdomain.

<a name="title"></a>
### Title

Expand Down Expand Up @@ -270,4 +300,4 @@ When provided, this array of locale strings gives the administrative user the ch

<img src="https://raw.github.com/FrozenNode/Laravel-Administrator/master/examples/images/localization.png" />

The user's choice of locale will persist across page loads until a user's session expires.
The user's choice of locale will persist across page loads until a user's session expires.
4 changes: 2 additions & 2 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ The `setter` option lets you define a field as an attribute that is set on the E
'title' => 'Name',
'setter' => true,
),

<a name="visible-option"></a>
## Visible Option

Expand Down Expand Up @@ -219,4 +219,4 @@ You can set a default value for your filters by providing a `value` option to th

If you're creating a settings page, you can use all of the field types except for [`key`](/docs/field-type-key) and [`relationship`](/docs/field-type-relationship).

> For a detailed description of all the settings page options, see the **[settings configuration docs](/docs/settings-configuration)**
> For a detailed description of all the settings page options, see the **[settings configuration docs](/docs/settings-configuration)**
16 changes: 15 additions & 1 deletion docs/model-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Below is a list of all the available options. Required options are marked as *(r
- [Action Permissions](#action-permissions)
- [Custom Actions](#custom-actions)
- [Global Custom Actions](#global-custom-actions)
- [Form Request Option](#form-request-option)
- [Validation Rules](#validation-rules)
- [Validation Messages](#validation-messages)
- [Sort](#sort)
Expand Down Expand Up @@ -294,6 +295,19 @@ Global custom actions are buttons that can be pressed at any time on a model's p

> For a detailed description of custom actions, see the **[actions docs](/docs/actions)**
<a name="form-request-option"></a>
### Form Request Option

/**
* The form request to use for validation of the form, based on the Laravel form request class
*
* @type string
*/
'form_request' => 'FormRequestPath',

The `form request` option lets you define a custom [form request](http://laravel.com/docs/validation#form-request-validation) to validate the editable fields on save. The Form Request object must contain a rules array to validate. This field is optional by default.


<a name="validation-rules"></a>
### Validation Rules

Expand Down Expand Up @@ -370,4 +384,4 @@ If you set this to any integer value above 285, it will expand the edit field an
return URL::route('product', array($model->collection()->first()->uri, $model->uri));
},

If your model has a front-end link, you might want to have a "view item" link at the top of the edit form that pops out to that page. The relevant `$model` is passed into the function so that you can use it to construct the URL. You should return a valid URL string.
If your model has a front-end link, you might want to have a "view item" link at the top of the edit form that pops out to that page. The relevant `$model` is passed into the function so that you can use it to construct the URL. You should return a valid URL string.
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Administrator is an administrative interface builder for [Laravel](http://larave

- **Author:** Jan Hartigan
- **Website:** [http://frozennode.com](http://administrator.frozennode.com/)
- **Version:** 5.0.5
- **Version:** 5.0.6

[![Build Status](https://travis-ci.org/FrozenNode/Laravel-Administrator.png?branch=master)](https://travis-ci.org/FrozenNode/Laravel-Administrator)

Expand Down Expand Up @@ -63,6 +63,11 @@ Administrator is released under the MIT License. See the LICENSE file for detail

## Recent Changelog

### 5.0.6
- Added: Support for custom domains in the admin routes
- Added: Ability to access the model from withinthe column output renderer
- Added: Dynamic Form Request Validation

### 5.0.5
- Added: Added password field to the settings view
- Added: Romanian Language
Expand Down
23 changes: 23 additions & 0 deletions src/Frozennode/Administrator/Config/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,27 @@ public function runQueryFilter(\Illuminate\Database\Query\Builder &$query)
$filter($query);
}
}

/**
* Fetches the data model for a config given a post input array
*
* @param \Illuminate\Http\Request $input
* @param array $fields
* @param int $id
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function getFilledDataModel(\Illuminate\Http\Request $input, array $fields, $id = 0)
{
$model = $this->getDataModel();

if ($id)
{
$model = $model->find($id);
}

$this->fillModel($model, $input, $fields);

return $model;
}
}
7 changes: 7 additions & 0 deletions src/config/administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
*/
'uri' => 'admin',

/**
* Domain for routing.
*
* @type string
*/
'domain' => '',

/**
* Page title
*
Expand Down
50 changes: 50 additions & 0 deletions src/controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Frozennode\Administrator;

use Illuminate\Http\Exception\HttpResponseException;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\File;
Expand All @@ -20,6 +21,11 @@ class AdminController extends Controller {
* @var \Illuminate\Session\SessionManager
*/
protected $session;

/**
* @var string
*/
protected $formRequestErrors;

/**
* @var string
Expand All @@ -34,6 +40,8 @@ public function __construct(Request $request, Session $session)
{
$this->request = $request;
$this->session = $session;

$this->formRequestErrors = $this->resolveDynamicFormRequestErrors($request);

if ( ! is_null($this->layout))
{
Expand Down Expand Up @@ -117,6 +125,14 @@ public function save($modelName, $id = false)
$config = app('itemconfig');
$fieldFactory = app('admin_field_factory');
$actionFactory = app('admin_action_factory');

if (array_key_exists('form_request', $config->getOptions()) && $this->formRequestErrors !== null) {
return response()->json(array(
'success' => false,
'errors' => $this->formRequestErrors,
));
}

$save = $config->save($this->request, $fieldFactory->getEditFields(), $actionFactory->getActionPermissions(), $id);

if (is_string($save))
Expand Down Expand Up @@ -617,4 +633,38 @@ public function switchLocale($locale)
return redirect()->back();
}

/**
* POST method to capture any form request errors
*
* @param \Illuminate\Http\Request $request
*/
protected function resolveDynamicFormRequestErrors(Request $request)
{
try {
$config = app('itemconfig');
$fieldFactory = app('admin_field_factory');
} catch (\ReflectionException $e) {
return null;
}
if (array_key_exists('form_request', $config->getOptions())) {
try {
$model = $config->getFilledDataModel($request, $fieldFactory->getEditFields(), $request->id);

$request->merge($model->toArray());
$formRequestClass = $config->getOption('form_request');
app($formRequestClass);
} catch (HttpResponseException $e) {
//Parses the exceptions thrown by Illuminate\Foundation\Http\FormRequest
$errorMessages = $e->getResponse()->getContent();
$errorsArray = json_decode($errorMessages);
if (!$errorsArray && is_string ( $errorMessages )) {
return $errorMessages;
}
if ($errorsArray) {
return implode(".", array_dot($errorsArray));
}
}
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Routes
*/
Route::group(array('prefix' => config('administrator.uri'), 'middleware' => 'Frozennode\Administrator\Http\Middleware\ValidateAdmin'), function()
Route::group(array('domain' => config('administrator.domain'), 'prefix' => config('administrator.uri'), 'middleware' => 'Frozennode\Administrator\Http\Middleware\ValidateAdmin'), function()
{
//Admin Dashboard
Route::get('/', array(
Expand Down

0 comments on commit 9910955

Please sign in to comment.