FEATURE #1526: add simple photogallery

Photogallery currently works with directories, UI is terrible, but it works.
This commit is contained in:
Michal Kváček
2016-04-01 21:50:05 +02:00
parent b9f721006d
commit f9afcae389
23 changed files with 384 additions and 0 deletions
+194
View File
@@ -0,0 +1,194 @@
<?php
namespace Modules\Gallery\Handlers;
use App\Enumerations\ModulesHandlerToId;
use App\Handlers\Contracts\AppHandler;
use App\Handlers\Handler;
use App\Handlers\History;
use App\Http\Controllers\Controller;
use App\Http\Requests\Request;
use App\Models\Content;
use App\Models\Directory;
use App\Models\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)
{
// if settings is still string, convert it into object
if (is_string($content->module_settings)) {
$content->module_settings = json_decode($content->module_settings);
}
// get directory ID
$directoryId = $content->module_settings->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]);
}
public static function getHistoryHandler()
{
return History::class;
}
/**
* 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' => $request->title,
'description' => $request->description,
'parent_id' => $parentDirectoryId
]);
// save id into module_config array
$settings = [
'directoryId' => $directory->id
];
// save settings
$content->module_settings = 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 = Handler::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 = Handler::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 = Handler::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)
{
// nothing to register
}
/**
* 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);
}
}