FEATURE: add functionality for editing custom fields

This commit is contained in:
Michal Kváček
2016-01-27 00:55:41 +01:00
parent 4b5a78b320
commit ff61858cf5
7 changed files with 249 additions and 2 deletions
@@ -0,0 +1,67 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Enumerations\CustomFieldResources;
use App\Enumerations\CustomFieldTypes;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\EditCustomFieldRequest;
use App\Models\CustomField;
class EditCustomField extends Controller
{
public function getEditCustomField($id)
{
$field = CustomField::findOrFail($id);
return view('admin.customFields.edit', [
'field' => $field,
'types' => CustomFieldTypes::getWithTranslations(),
'resources' => CustomFieldResources::getWithTranslations()
]);
}
public function postEditCustomField(EditCustomFieldRequest $request, $id)
{
$customField = CustomField::findOrFail($id);
// setup fields
$customField->name = $request->name;
$customField->required = $request->has('required');
// set non-mandatory fields
if ($request->description) {
$customField->description = $request->description;
}
if ($request->possibleValues) {
$customField->possible_values = trim(preg_replace('~[\r\n]+~', '|', $request->possibleValues));
}
if ($request->defaultValue) {
$customField->default_value = $request->defaultValue;
}
if ($request->regExp) {
$customField->regexp = $request->regExp;
}
if ($request->minLength) {
$customField->min_length = $request->minLength;
}
if ($request->maxLength) {
$customField->max_length = $request->maxLength;
}
// save changes into database
$customField->save();
return redirect()
->route('customfields.edit', ['id' => $id])
->with('success', trans('admin/customFields.successfullyEdited'));
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Http\Requests;
class EditCustomFieldRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'minLength' => 'numeric|min:0',
'maxLength' => 'numeric|min:0',
];
}
}
+8
View File
@@ -59,10 +59,18 @@ Route::group([
Route::get('customfields/new', 'CreateNewCustomField@getNew') Route::get('customfields/new', 'CreateNewCustomField@getNew')
->name('customfields.new'); ->name('customfields.new');
// form for editing custom field
Route::get('customfields/edit/{id}', 'EditCustomField@getEditCustomField')
->name('customfields.edit');
// form for adding new custom field // form for adding new custom field
Route::post('customfields/new', 'CreateNewCustomField@postNew') Route::post('customfields/new', 'CreateNewCustomField@postNew')
->name('customfields.newPost'); ->name('customfields.newPost');
// form for editing custom field
Route::post('customfields/edit/{id}', 'EditCustomField@postEditCustomField')
->name('customfields.editPost');
// logout method // logout method
Route::get('logout', function () { Route::get('logout', function () {
+3 -1
View File
@@ -20,7 +20,9 @@ return [
'selectboxList' => 'List (selectbox)', 'selectboxList' => 'List (selectbox)',
'string' => 'Short text', 'string' => 'Short text',
'successfullyCreated' => 'Custom field successfully created.', 'successfullyCreated' => 'Custom field successfully created.',
'successfullyEdited' => 'Custom field successfully updated.',
'resource' => 'Add to resource', 'resource' => 'Add to resource',
'resource_user' => 'User', 'resource_user' => 'User',
'resource_content' => 'Each content page' 'resource_content' => 'Each content page',
'edit' => 'Editing custom field ":name"'
]; ];
@@ -0,0 +1,14 @@
@extends('admin.layout')
@section('pageName', @trans('admin/customFields.edit', ['name' => $field->name]))
@section('content')
<div class="row">
<h2>@lang('admin/customFields.edit', ['name' => $field->name])</h2>
</div>
<div class="row">
@include('admin.customFields.editForm')
</div>
@stop
@@ -0,0 +1,136 @@
@if (session('success'))
<div data-alert class="callout success">
<strong>{{ session('success') }}</strong>
</div>
@endif
@if (session('error'))
<div data-alert class="callout alert">
<strong>{{ session('error') }}</strong>
</div>
@endif
<form method="POST" action="{{route('customfields.editPost', ['id' => $field->id])}}">
{!! csrf_field() !!}
<div class="row">
<div class="large-12 columns">
<label>
@lang('admin/customFields.name')
<input type="text" placeholder="@lang('admin/customFields.name')" value="{{$field->name}}" name="name">
@if (isset($errors) && $errors->has('name'))
<div class="callout alert">
{{ $errors->first('name') }}
</div>
@endif
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>
@lang('admin/customFields.description')
<textarea rows="5" name="description" placeholder="@lang('admin/customFields.description')">{{$field->description}}</textarea>
</label>
</div>
<div class="large-6 columns">
<label>
@lang('admin/customFields.type')
<select name="type" disabled>
@foreach($types as $type => $translation)
<option value="{{$type}}"@if($field->type_id == $type) selected @endif>@lang($translation)</option>
@endforeach
</select>
@if (isset($errors) && $errors->has('type'))
<div class="callout alert">
{{ $errors->first('type') }}
</div>
@endif
</label>
<label>
@lang('admin/customFields.resource')
<select name="resource" disabled>
@foreach($resources as $resource => $translation)
<option value="{{$resource}}"@if($field->resource_id == $resource) selected @endif>@lang($translation)</option>
@endforeach
</select>
@if (isset($errors) && $errors->has('resource'))
<div class="callout alert">
{{ $errors->first('resource') }}
</div>
@endif
</label>
<label>
@lang('admin/customFields.required')
<input type="checkbox" placeholder="@lang('admin/customFields.required')" @if ($field->required) checked="checked" @endif name="required">
</label>
</div>
</div>
<div class="row">
<div class="columns large-6">
<label>
@lang('admin/customFields.possibleValues')
<textarea placeholder="@lang('admin/customFields.possibleValues')" name="possibleValues" rows="4">{{$field->possibleValues}}</textarea>
</label>
</div>
<div class="columns large-6">
<label>
@lang('admin/customFields.defaultValue')
<input type="text" placeholder="@lang('admin/customFields.defaultValue')" value="{{$field->defaultValue}}" name="defaultValue">
</label>
<label>
@lang('admin/customFields.regExp')
<input type="text" placeholder="@lang('admin/customFields.regExp')" value="{{$field->regExp}}" name="regExp">
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>
@lang('admin/customFields.minLength')
<input type="text" placeholder="@lang('admin/customFields.minLength')" value="{{$field->minLength}}" name="minLength">
@if (isset($errors) && $errors->has('minLength'))
<div class="callout alert">
{{ $errors->first('minLength') }}
</div>
@endif
</label>
</div>
<div class="large-6 columns">
<label>
@lang('admin/customFields.maxLength')
<input type="text" placeholder="@lang('admin/customFields.maxLength')" value="{{$field->macLength}}" name="maxLength">
@if (isset($errors) && $errors->has('maxLength'))
<div class="callout alert">
{{ $errors->first('maxLength') }}
</div>
@endif
</label>
</div>
</div>
<label>
<button type="submit" class="button success">@lang('admin/customFields.createNew')</button>
</label>
</form>
@@ -43,7 +43,7 @@
<td>@lang($types[$field->type_id])</td> <td>@lang($types[$field->type_id])</td>
<td> <td>
<a href="#"><i class="fa red confirm fa-remove"></i></a> <a href="#"><i class="fa red confirm fa-remove"></i></a>
<a href="#"><i class="fa green fa-edit"></i></a> <a href="{{route('customfields.edit', ['id' => $field->id])}}"><i class="fa green fa-edit"></i></a>
</td> </td>
</tr> </tr>
@endforeach @endforeach