FEATURE #1528: add button for creating slider from gallery

This commit is contained in:
Michal Kváček
2016-04-02 13:14:58 +02:00
parent 2797ff0a89
commit 497708283f
20 changed files with 360 additions and 121 deletions
@@ -0,0 +1,113 @@
<?php namespace Modules\Gallery\Http\Controllers\Admin;
use App\Models\Content;
use App\Models\Widget;
use App\Models\WidgetType;
use App\Widgets\OrbitSlider\OrbitSlider;
use Pingpong\Modules\Routing\Controller;
class CreateOrbitSlider extends Controller
{
/**
* @var Widget
*/
private $widget;
/**
* CreateOrbitSlider constructor.
*
* @param Widget $widget
*/
public function __construct(Widget $widget)
{
$this->widget = $widget;
}
/**
* Load gallery or redirect user back to content edit form
*
* @param $id
*
* @return mixed
*/
protected function getGallery($id)
{
$content = Content::where('module_handler', config('gallery.handler'))
->findOrFail($id);
$content->module_settings = json_decode($content->module_settings);
// check if gallery has assigned some directory id
if (!isset($content->module_settings->directoryId)) {
return redirect()
->route('content.edit', ['id' => $id])
->with('error', trans('gallery::admin/content.invalidGallery'));
}
return $content;
}
/**
* Load widget type for Orbit Slider. In case it does not exist, redirect user back to content edit form.
*
* @param $id
*
* @return mixed
*/
protected function getOrbitSliderWidgetType($id)
{
// try to load widget type for slider
$widgetType = WidgetType::where('handler', OrbitSlider::class)
->first();
// check if widget type exists
if (!$widgetType) {
return redirect()
->route('content.edit', ['id' => $id])
->with('error', trans('gallery::admin/content.orbitSliderMissing'));
}
return $widgetType;
}
/**
* Method for creating slider from photogallery
*
* @param $id
*
* @return mixed
*/
public function createNew($id)
{
// try to find gallery with specified ID
$content = $this->getGallery($id);
// load info about orbit slider widget
$widgetType = $this->getOrbitSliderWidgetType($id);
// create slider basic config
$sliderSettings = (object)[
'name' => $content->title,
'description' => $content->meta_description,
'directoryId' => $content->module_settings->directoryId,
'useExistingDirectory' => true,
];
// try to create the slider
try {
$slider = $this->widget->createNew($widgetType, $sliderSettings);
} catch (\Exception $e) {
return redirect()
->route('content.edit', ['id' => $id])
->with('error', trans('gallery::admin/content.sliderFailed', ['message' => $e->getMessage()]));
}
// update gallery info
$content->module_settings->slider = $slider->id;
$content->module_settings = json_encode($content->module_settings);
$content->save();
return redirect()
->route('content.edit', ['id' => $id])
->with('success', trans('gallery::admin/content.sliderCreated'));
}
}
+10
View File
@@ -1 +1,11 @@
<?php
Route::group([
'middleware' => ['web', 'auth', 'adminMenu'],
'namespace' => 'Modules\Gallery\Http\Controllers\Admin',
'prefix' => 'admin/gallery'
], function () {
// method for creating slider from photogallery
Route::get('makeSlider/{id}', 'CreateOrbitSlider@createNew')
->name('gallery.makeSlider');
});