Files

254 lines
7.4 KiB
PHP

<?php
namespace Modules\Gallery\Handlers;
use App\CMS\Contracts\Modules\AppHandler;
use App\CMS\Enumerations\ModulesHandlerToId;
use App\CMS\Handlers\AdminHandler;
use App\CMS\Handlers\History;
use App\Http\Controllers\Controller;
use App\Http\Requests\Request;
use App\Models\Content\Content;
use App\Models\FileManager\Directory;
use App\Models\FileManager\File;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
class Gallery implements AppHandler
{
const CONFIG_FILENAME = 'photogallery-config';
const DIRECTORY_NAME = 'photogallery-data';
/**
* Render given module into visitor area.
*
* @param Content $content DB model from table contents
* @param array $parameters parameters used for modifications/custom settings of given model
*
* @return mixed
*/
public function render(Content $content, $parameters)
{
// get directory ID
$directoryId = $content->moduleSettings->directoryId;
// get all images in given directory
$images = File::where('directory_id', $directoryId)
->where('mime_type', 'LIKE', 'image%')
->get();
// render content
return Controller::visitorsView($content->template, [
'images' => $images,
'page' => $content,
'extra' => $parameters
]);
}
public static function getHistoryHandler()
{
return History::class;
}
/**
* @param Content $content
*
* @return array
*/
public static function loadExtraData(Content $content)
{
if (is_string($content->moduleSettings)) {
$content->moduleSettings = json_decode($content->moduleSettings);
}
$extra = [
'slider' => isset($content->moduleSettings->slider) ? $content->moduleSettings->slider : false
];
return $extra;
}
/**
* Save module-specific values into database
*
* @param Content $content
* @param Request $request
*
* @return mixed
* @internal param Widget $widget
*/
public static function createNew(Content &$content, Request $request)
{
$parentDirectoryId = Storage::get(self::CONFIG_FILENAME);
// create new directory for images
$directory = Directory::create([
'name' => $content->id . '_' . $request->title,
'description' => $request->description,
'parent_id' => $parentDirectoryId
]);
// save id into module_config array
$settings = ['directoryId' => $directory->id];
// save settings
$content->moduleSettings = json_encode($settings);
}
/**
* Method used for editing existing content page.
*
* Reason why it is separated from createNew() method is simple - we need to save history of changes.
*
* @param Content $content
* @param Request $request
*
* @param $revisionID string ID of revision used
*
* @return int number of edited fields
*/
public static function edit(Content &$content, Request $request, $revisionID)
{
// nothing to edit
}
/**
* Does given module manage content? Or is it just module with widgets/something internal/something absolutely else?
*
* @return bool
*/
public static function managesContent()
{
return true;
}
public static function getModuleId()
{
$handler = config('gallery.handler');
$allModules = ModulesHandlerToId::getWithTranslations();
$moduleId = isset($allModules[$handler]) ? $allModules[$handler] : null;
return $moduleId;
}
/**
* Getter for section names in creating/editing content resource
*
* @return mixed
*/
public static function getExtendedFormSections()
{
$sections = AdminHandler::getExtendedFormSections();
$sections['upload'] = trans('gallery::admin/content.upload');
return $sections;
}
/**
* Getter for views used in extended part of form for creating new content
*
* This method should return pairs <section, view-name>. View is returned only as name in plain text
*
* @return array of views/templates/html codes divided into section
*/
public static function getExtendedFormInputsCreate()
{
$parts = AdminHandler::getExtendedFormInputsCreate();
$parts['upload'] = 'gallery::admin.content.newUpload';
return $parts;
}
/**
* Getter for extended part of form for editing existing content
*
* Basically, this method may be
*
* @return array of views/templates/html codes divided into section
*/
public static function getExtendedFormInputsEdit()
{
$parts = AdminHandler::getExtendedFormInputsCreate();
$parts['upload'] = 'gallery::admin.content.editUpload';
return $parts;
}
/**
* Register admin menu (or multiple ones)
*
* This cannot be in ServiceProvider because Session is started at middleware. And ServiceProvider is called before
* starting any middleware..
*
* @param User $user model of logged user
*/
public static function registerAdminMenu(User $user)
{
$menu = \Menu::instance('mainMenu');
$submenu = $menu->whereTitle(trans('admin/mainMenu.contentPages'));
$submenu->dropdown(trans('gallery::module.name'), function ($submenu) use ($user) {
$submenu->route('content.new', trans('gallery::admin/adminMenu.new'), ['module' => 'gallery'], 10, ['icon' => 'fa fa-plus']);
$submenu->route('content.overview', trans('gallery::admin/adminMenu.overview'), ['module' => 'gallery'], 20, ['icon' => 'fa fa-list']);
}, 25, ['icon' => 'fa fa-camera']);
}
/**
* Install module
*
* @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);
}
/**
* Method called right after content page is marked as deleted
*
* This method is supposed to clean after "deleted" page.
*
* @param Content $content
*
* @return mixed
*/
public static function delete(Content $content)
{
// try to convert moduleSettings into something more accessible
if (is_string($content->moduleSettings)) {
$content->moduleSettings = json_decode($content->moduleSettings);
}
// get directory id
$directoryId = $content->moduleSettings->directoryId;
$directory = Directory::find($directoryId);
// check if gallery exists
if (!$directory) {
return;
}
// delete directory used as photogallery
$directory->delete();
}
/**
* Does this module support categories?
*
* @return boolean
*/
public static function supportCategories()
{
return true;
}
}