186 lines
5.2 KiB
PHP
186 lines
5.2 KiB
PHP
<?php
|
|
namespace App\Widgets\OrbitSlider;
|
|
|
|
use App\Models\Directory;
|
|
use App\Models\Widget;
|
|
use App\Widgets\Contracts\PublicWidget;
|
|
use App\Widgets\Contracts\WidgetRenderer;
|
|
use App\Widgets\Exceptions\WidgetExistsException;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class OrbitSlider implements PublicWidget
|
|
{
|
|
const DIRECTORY_NAME = 'orbit-slider-data';
|
|
const CONFIG_FILENAME = 'orbit-slider-directory-id';
|
|
|
|
/**
|
|
* Name getter
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function getName()
|
|
{
|
|
return 'widgets/orbitSlider.widgetName';
|
|
}
|
|
|
|
/**
|
|
* Getter for description
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function getDescription()
|
|
{
|
|
return 'widgets/orbitSlider.widgetDescription';
|
|
}
|
|
|
|
/**
|
|
* @param $name
|
|
* @param $directoryId
|
|
* @param $parentDirectoryId
|
|
*
|
|
* @throws WidgetExistsException
|
|
*/
|
|
protected static function checkIfDirectoryExists($name, $directoryId, $parentDirectoryId)
|
|
{
|
|
// check if directory does not exists
|
|
$exists = Directory::where('name', $name)
|
|
->where('parent_id', $parentDirectoryId);
|
|
|
|
// if widget has some directory set, we are performing edit
|
|
if ($directoryId) {
|
|
$exists = $exists->where('id', '!=', $directoryId);
|
|
}
|
|
|
|
if ($exists->exists()) {
|
|
throw new WidgetExistsException($name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Widget $widget
|
|
* @param \stdClass $data
|
|
*
|
|
* @throws WidgetExistsException
|
|
*
|
|
* @return Widget
|
|
*/
|
|
public static function save(Widget $widget, \stdClass $data)
|
|
{
|
|
$settings = json_decode($widget->settings);
|
|
$parentDirectoryId = Storage::get(self::CONFIG_FILENAME);
|
|
$directoryId = isset($settings->directoryId) ? $settings->directoryId : null;
|
|
|
|
if (!isset($data->useExistingDirectory)) {
|
|
// check if name with same name does not exists yet
|
|
self::checkIfDirectoryExists($data->name, $directoryId, $parentDirectoryId);
|
|
|
|
// setup some information about directory
|
|
$directory = $directoryId ? Directory::find($directoryId) : new Directory;
|
|
|
|
// setup some info when creating new directory
|
|
if (!$directoryId) {
|
|
$directory->user_id = Auth::user()->id;
|
|
$directory->parent_id = $parentDirectoryId;
|
|
}
|
|
|
|
// common info both for edit and creation
|
|
$directory->name = $data->name;
|
|
$directory->description = $data->description;
|
|
|
|
// save into database
|
|
$directory->save();
|
|
|
|
$directoryId = $directory->id;
|
|
} else {
|
|
$directoryId = $data->directoryId;
|
|
}
|
|
|
|
$settings = [
|
|
'directoryId' => $directoryId,
|
|
'imagePerSlide' => isset($data->imagePerSlide) ? $data->imagePerSlide : 4,
|
|
'displayBullets' => isset($data->displayBullets),
|
|
'sliderClass' => isset($data->containerClass) ? $data->containerClass : '',
|
|
'slideContainerClass' => isset($data->slideContainerClass) ? $data->slideContainerClass : '',
|
|
'code' => isset($data->code) ? $data->code : ''
|
|
];
|
|
|
|
self::checkSettings($settings);
|
|
|
|
$widget->settings = json_encode($settings);;
|
|
|
|
return $widget;
|
|
}
|
|
|
|
/**
|
|
* Method for populating settings array with default values
|
|
*
|
|
* @param $settings
|
|
*/
|
|
public static function checkSettings(&$settings)
|
|
{
|
|
if (!is_object($settings)) {
|
|
$settings = (object)$settings;
|
|
}
|
|
|
|
if (!isset($settings->slideContainerClass)) {
|
|
$settings->slideContainerClass = '';
|
|
}
|
|
|
|
if (!isset($settings->displayBullets)) {
|
|
$settings->displayBullets = true;
|
|
}
|
|
|
|
if (!isset($settings->imagePerSlide)) {
|
|
$settings->imagePerSlide = 4;
|
|
}
|
|
|
|
if (!isset($settings->directoryId)) {
|
|
$settings->directoryId = false;
|
|
}
|
|
if (!isset($settings->sliderClass)) {
|
|
$settings->sliderClass = '';
|
|
}
|
|
if (!isset($settings->code)) {
|
|
$settings->code = '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Getter for presenter/renderer of this widget
|
|
*
|
|
* @return WidgetRenderer
|
|
*/
|
|
public static function getPresenter()
|
|
{
|
|
return Presenters\OrbitSlider::class;
|
|
}
|
|
|
|
/**
|
|
* Function for installing given widget
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function install()
|
|
{
|
|
$directory = Directory::where('name', self::DIRECTORY_NAME)
|
|
->first();
|
|
|
|
if (!count($directory)) {
|
|
$directory = Directory::create([
|
|
'name' => self::DIRECTORY_NAME,
|
|
'hidden' => true
|
|
]);
|
|
}
|
|
|
|
Storage::put(self::CONFIG_FILENAME, $directory->id);
|
|
}
|
|
|
|
/**
|
|
* Function for uninstalling widget
|
|
*/
|
|
public static function uninstall()
|
|
{
|
|
// TODO: Implement uninstall() method.
|
|
}
|
|
} |