FEATURE: add functionality for creating new widget.
This commit adds functionality for creating widget of whatever type. Controller calls widget admin handler, which renders and handles input form.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Events\Event;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class CreateNewWidget extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should be broadcast on.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests;
|
||||
use App\Http\Requests\NewWidgetRequest;
|
||||
use App\Models\Widget;
|
||||
use App\Models\WidgetType;
|
||||
use App\Widgets\Exceptions\MissingParameterException;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class CreateNewWidget extends Controller
|
||||
{
|
||||
/**
|
||||
* Form for new widget area
|
||||
*
|
||||
* @param $type
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getNew($type)
|
||||
{
|
||||
$widgetType = WidgetType::findOrFail($type);
|
||||
|
||||
$presenter = call_user_func([$widgetType->handler, 'getPresenter']);
|
||||
|
||||
return call_user_func([$presenter, 'renderAddForm'], $widgetType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new instance of given widget type
|
||||
*
|
||||
* @param NewWidgetRequest $request
|
||||
* @param $type
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postNew(NewWidgetRequest $request, $type)
|
||||
{
|
||||
$widgetType = WidgetType::findOrFail($type);
|
||||
|
||||
// create new widget
|
||||
$widget = new Widget();
|
||||
$widget->widget_type_handler = $widgetType->handler;
|
||||
$widget->user_id = Auth::user()->id;
|
||||
$widget->name = $request->name;
|
||||
|
||||
// save description if available
|
||||
if (isset($request->description)) {
|
||||
$widget->description = $request->description;
|
||||
}
|
||||
|
||||
// try to save this widget
|
||||
try {
|
||||
$widget = call_user_func([$widgetType->handler, 'save'], $widget, $request);
|
||||
} catch (MissingParameterException $e) {
|
||||
// user omitted some value
|
||||
return $this->invalidWidget($type, $e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
// something went wrong
|
||||
return $this->invalidWidget($type, trans('admin/widgets.cannotSave', ['exception' => $e->getMessage()]));
|
||||
}
|
||||
|
||||
// save into database
|
||||
$widget->save();
|
||||
|
||||
// redirect with success notification
|
||||
return redirect()
|
||||
->route('widgets.new', ['type' => $type])
|
||||
->with('success', trans('admin/widgets.successfullyCreated'));
|
||||
}
|
||||
|
||||
private function invalidWidget($widgetType, $error)
|
||||
{
|
||||
return redirect()
|
||||
->route('widgets.new', ['type' => $widgetType])
|
||||
->withInput()
|
||||
->with('error', $error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
class NewWidgetRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required'
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\CreateNewWidget;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class LogNewWidget
|
||||
{
|
||||
/**
|
||||
* Create the event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param CreateNewWidget $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(CreateNewWidget $event)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Enumerations\WidgetTypes;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AdminMenusProvider extends ServiceProvider
|
||||
@@ -38,6 +39,14 @@ class AdminMenusProvider extends ServiceProvider
|
||||
$sub->route('upload.overview', trans('admin/mainMenu.uploadedFiles'), [], 150, ['icon' => 'fa fa-files-o']);
|
||||
}, 25, ['icon' => 'fa fa-film']);
|
||||
$menu->dropdown(trans('admin/mainMenu.widgets'), function ($sub) {
|
||||
// create list of all widget types
|
||||
$sub->dropdown(trans('admin/mainMenu.newWidget'), function ($sub) {
|
||||
$i = 1;
|
||||
foreach (WidgetTypes::getWithTranslations() as $type => $name) {
|
||||
$sub->route('widgets.new', $name, ['type' => $type], $i++);
|
||||
}
|
||||
}, 10, ['icon' => 'fa fa-plus']);
|
||||
|
||||
// menu for widget areas
|
||||
$sub->dropdown(trans('admin/mainMenu.widgetAreas'), function ($sub) {
|
||||
$sub->route('widgetareas.new', trans('admin/mainMenu.newWidgetArea'), [], 1, ['icon' => 'fa fa-plus']);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddIdToWidgetTypes extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
// drop foreign key from widgets table
|
||||
try {
|
||||
Schema::table('widgets', function (Blueprint $table) {
|
||||
$table->dropForeign('fk_widget_widget_type1');
|
||||
});
|
||||
} catch (Exception $e) {
|
||||
Schema::table('widgets', function (Blueprint $table) {
|
||||
$table->dropForeign('widgets_widget_type_handler_foreign');
|
||||
});
|
||||
}
|
||||
|
||||
// try to drop primary key (should be on handler column)
|
||||
Schema::table('widget_types', function (Blueprint $table) {
|
||||
$table->dropIndex('PRIMARY');
|
||||
});
|
||||
|
||||
// add new column "id" with autoincrement and make handler unique, not primary
|
||||
Schema::table('widget_types', function (Blueprint $table) {
|
||||
$table->integer('id', true)
|
||||
->first();
|
||||
|
||||
$table->string('handler')
|
||||
->unique()
|
||||
->change();
|
||||
});
|
||||
|
||||
// create foreign constraint active again
|
||||
Schema::table('widgets', function (Blueprint $table) {
|
||||
$table->foreign('widget_type_handler')
|
||||
->references('handler')
|
||||
->on('widget_types')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('widgets', function (Blueprint $table) {
|
||||
$table->dropForeign('widgets_widget_type_handler_foreign');
|
||||
});
|
||||
|
||||
Schema::table('widget_types', function (Blueprint $table) {
|
||||
$table->dropColumn('id');
|
||||
$table->dropIndex('widget_types_handler_unique');
|
||||
$table->string('handler')
|
||||
->primary()
|
||||
->change();
|
||||
});
|
||||
// create foreign constraint active again
|
||||
Schema::table('widgets', function (Blueprint $table) {
|
||||
$table->foreign('widget_type_handler')
|
||||
->references('handler')
|
||||
->on('widget_types')
|
||||
->onDelete('cascade');
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
return [
|
||||
'cannotSave' => 'Cannot save widget. Message: :exception',
|
||||
'missingValue' => 'You must specify value for field :field',
|
||||
'successfullyCreated' => 'Widget successfully created.',
|
||||
'successfullyEdited' => 'Widget successfully edited.',
|
||||
'noEntries' => 'No widgets available.',
|
||||
'overview' => 'List of all widgets',
|
||||
'widgetType' => 'Type of widget',
|
||||
'name' => 'Name',
|
||||
'description' => 'Description',
|
||||
'actions' => 'Actions',
|
||||
'preview' => 'Widgets preview',
|
||||
'createdAt' => 'Created at',
|
||||
'updatedAt' => 'Last update',
|
||||
'unassociatedWidgets' => 'List of not associated widgets',
|
||||
'associate' => 'Add',
|
||||
'edit' => 'Edit',
|
||||
'deleteAssociation' => 'Remove',
|
||||
'associationExists' => 'This widget is already associated with this widget area',
|
||||
'successfullyAssociated' => 'Association successfull',
|
||||
'associationDoesNotExists' => 'This association does not exist',
|
||||
'successfullyDeassociated' => 'Successfully removed from widget area',
|
||||
|
||||
];
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
return [
|
||||
'widgetName' => 'HTML code',
|
||||
'new' => 'Create new HTML widget',
|
||||
'edit' => 'Editing widget :name',
|
||||
'containerClass' => 'HTML class used for container of this widget',
|
||||
'widgetDescription' => 'Simple widget for inserting html code',
|
||||
'name' => 'Name',
|
||||
'description' => 'Description',
|
||||
'code' => 'HTML code',
|
||||
'createNew' => 'Create new HTML code widget',
|
||||
'editExisting' => 'Update widget',
|
||||
'successfullyEdited' => 'Widget successfully modified',
|
||||
'preview' => 'Preview'
|
||||
];
|
||||
Reference in New Issue
Block a user