FEATURE #1528: add button for creating slider from gallery
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user