Merge branch 'module_gallery' into cwbs_template

This commit is contained in:
Michal Kváček
2016-04-01 21:50:55 +02:00
31 changed files with 508 additions and 29 deletions
@@ -5,14 +5,20 @@ use App\Enumerations\Exceptions\EnumerationExistsException;
use App\Handlers\Contracts\SearchHandler;
use App\Handlers\Contracts\SearchPresenterHandler;
use App\Handlers\Registrators\Items\SearchEngine;
use Symfony\Component\Process\Exception\InvalidArgumentException;
class SearchEngineRegistrator
{
const ADMIN = 'admin';
const VISITOR = 'visitor';
protected $searchEngines;
function __construct($app)
{
$this->searchEngines = [];
$this->searchEngines = [
self::ADMIN => [],
self::VISITOR => []
];
}
/**
@@ -21,37 +27,46 @@ class SearchEngineRegistrator
* @param SearchHandler $search
* @param SearchPresenterHandler $presenter
*
* @param $type
*
* @throws EnumerationExistsException
*/
public function register(SearchHandler $search, SearchPresenterHandler $presenter)
public function register(SearchHandler $search, SearchPresenterHandler $presenter, $type)
{
$this->checkIfRegistered($search->getId());
$this->checkIfRegistered($type, $search->getId());
// register new extension
$this->searchEngines[$search->getId()] = [$search, $presenter];
$this->searchEngines[$type][$search->getId()] = [$search, $presenter];
}
/**
* Getter for all registered extensions
*
* @param $type
*
* @return array
*/
public function get()
public function get($type)
{
return $this->searchEngines;
if (!isset($this->searchEngines[$type])) {
throw new InvalidArgumentException('Invalid type ' . $type);
}
return $this->searchEngines[$type];
}
/**
* Checks if this section is not already defined
*
* @param $type
* @param $id
*
* @throws EnumerationExistsException
*/
private function checkIfRegistered($id)
private function checkIfRegistered($type, $id)
{
if (isset($this->searchEngines[$id])) {
throw new EnumerationExistsException('Already existing search engine ' . $id);
if (isset($this->searchEngines[$type][$id])) {
throw new EnumerationExistsException('Already existing search engine ' . $id . ' in ' . $type);
}
}
}
+13 -3
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Enumerations\ContentStatuses;
use App\Enumerations\ModulesHandlerToId;
use App\Http\Requests\EditContentRequest;
use App\Http\Requests\Request;
use App\Models\Content;
use App\Models\ContentHistory;
use App\Models\File;
@@ -59,7 +60,7 @@ class EditContent extends Controller
'rootPages' => $page->rootPages(),
'templates' => $templates,
'formSectionNames' => call_user_func([$page->module_handler, 'getExtendedFormSections']),
'formSections' => call_user_func([$page->module_handler, 'getExtendedFormInputsCreate']),
'formSections' => call_user_func([$page->module_handler, 'getExtendedFormInputsEdit']),
'historySize' => count($history['changesets'])
]);
}
@@ -111,7 +112,8 @@ class EditContent extends Controller
];
// save new values with history
list($revision, $changedColumns) = $page->updateWithHistory($fields, $this->getFields($request, $fields));
$data = self::getFields($request, $fields);
list($revision, $changedColumns) = $page->updateWithHistory($fields, $data);
$changedColumns += call_user_func_array([$page->module_handler, 'edit'], [&$page, $request, $revision]);
@@ -275,7 +277,15 @@ class EditContent extends Controller
]));
}
private function getFields(EditContentRequest $request, $fields)
/**
* Convert object of all request data into array of only wanted fields
*
* @param Request $request
* @param $fields
*
* @return array
*/
public static function getFields(Request $request, $fields)
{
$requestData = [];
+2 -1
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Handlers\Contracts\SearchHandler;
use App\Handlers\Contracts\SearchPresenterHandler;
use App\Handlers\Registrators\SearchEngineRegistrator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\App;
@@ -48,7 +49,7 @@ class Search extends Controller
protected function performSearch($term)
{
$engines = App::make('registerSearchEngine')
->get();
->get(SearchEngineRegistrator::ADMIN);
$resultsParts = [];
foreach ($engines as $engine) {
+34 -6
View File
@@ -14,10 +14,13 @@ class Content extends Model
public $timestamps = true;
protected $fillable = [
'user_id',
'parent_id',
'module_handler',
'published_from',
'published_to',
'title_photo',
'template',
'is_homepage',
'title',
'url',
'meta_keywords',
@@ -152,15 +155,31 @@ class Content extends Model
foreach ($fields as $fieldDB => $fieldRequest) {
// ignore non-existing fields
if ((!isset($this->$fieldDB) && !is_null($this->$fieldDB)) || (!isset($request[$fieldRequest]) && !is_null($request[$fieldRequest]))) {
if (!isset($request[$fieldRequest]) && !is_null($request[$fieldRequest])) {
continue;
}
// remove any whitespaces around real content
if (!is_null($request[$fieldRequest]))
$request[$fieldRequest] = trim($request[$fieldRequest]);
// if module settings is still JSON, decode it to normal stdClass
if (is_string($this->module_settings)) {
$this->module_settings = json_decode($this->module_settings);
}
if (trim($this->$fieldDB) != $request[$fieldRequest]) {
// are we editing common settings, or module-specific settings?
if (!in_array($fieldDB, $this->fillable)) {
// this field is not part of "core" content - probably it will be module settings
$commonField = false;
$fieldValue = $this->module_settings->$fieldDB;
} else {
$commonField = true;
$fieldValue = $this->$fieldDB;
}
// remove any whitespaces around real content
if (!is_null($request[$fieldRequest])) {
$request[$fieldRequest] = trim($request[$fieldRequest]);
}
if (trim($fieldValue) != $request[$fieldRequest]) {
// setup history object
$history = [
@@ -168,7 +187,7 @@ class Content extends Model
'content_id' => $this->id,
'edit_batch' => $revisionId,
'column' => $fieldRequest,
'old_value' => $this->$fieldDB,
'old_value' => $fieldValue,
'new_value' => $request[$fieldRequest]
];
@@ -176,12 +195,21 @@ class Content extends Model
ContentHistory::create($history);
// save change into database
if ($commonField) {
$this->$fieldDB = $request[$fieldRequest];
} else {
$this->module_settings->$fieldDB = $request[$fieldRequest];
}
$changed++;
}
}
// make JSON from stdClass
if (!is_string($this->module_settings)) {
$this->module_settings = json_encode($this->module_settings);
}
$this->save();
return [$revisionId, $changed];
+2 -2
View File
@@ -119,8 +119,8 @@ class AppServiceProvider extends ServiceProvider
{
$registrator = $this->app->make('registerSearchEngine');
$registrator->register(new Content(), new AdminContentPresenter());
$registrator->register(new Files(), new AdminFiles());
$registrator->register(new Content(), new AdminContentPresenter(), SearchEngineRegistrator::ADMIN);
$registrator->register(new Files(), new AdminFiles(), SearchEngineRegistrator::ADMIN);
}
/**
+21 -2
View File
@@ -3,6 +3,7 @@ namespace Modules\ContactForm\Handlers;
use App\Enumerations\ModulesHandlerToId;
use App\Handlers\Handler;
use App\Http\Controllers\Admin\EditContent;
use App\Http\Controllers\Controller;
use App\Http\Requests\Request;
use App\Models\Content;
@@ -41,7 +42,6 @@ class ContactForm extends Handler
*/
public static function createNew(Content &$content, Request $request)
{
$settings = [
'phone' => $request->phone,
'email' => $request->email,
@@ -69,7 +69,21 @@ class ContactForm extends Handler
*/
public static function edit(Content &$content, Request $request, $revisionID)
{
// TODO: Implement edit() method.
$fields = [
'phone' => 'phone',
'email' => 'email',
'companyName' => 'companyName',
'address' => 'address',
'gpsLat' => 'gpsLat',
'gpsLng' => 'gpsLng',
];
// get data and try to save them
$data = EditContent::getFields($request, $fields);
list($revision, $changedColumns) = $content->updateWithHistory($fields, $data, $revisionID);
// return amount of changed columns
return $changedColumns;
}
public static function getModuleId()
@@ -112,6 +126,11 @@ class ContactForm extends Handler
];
}
public static function getExtendedFormInputsEdit()
{
return self::getExtendedFormInputsCreate();
}
/**
* Register admin menu (or multiple ones)
*
@@ -5,12 +5,24 @@ use App\Handlers\History;
class ContactFormHistory extends History
{
public static function parseChangedValue($changeset)
/**
* Translate column name for history view
*
* @param $column
*
* @return mixed
*/
public static function parseColumnName($column)
{
switch ($changeset->column) {
default:
return parent::parseChangedValue($changeset);
break;
// try to translate as contact form field
$translationString = 'contactform::admin/history.column' . ucfirst($column);
$translation = trans($translationString);
// when translation is not available, try to fallback into core fields
if ($translation == $translationString) {
return parent::parseColumnName($column);
}
return $translation;
}
}
@@ -0,0 +1,10 @@
<?php
return [
'columnPhone' => 'Phone',
'columnAddress' => 'Address',
'columnEmail' => 'E-mail',
'columnCompanyName' => 'Company name',
'columnGpsLat' => 'GPS Latitude',
'columnGpsLng' => 'Gps Longitude',
];
View File
View File
+12
View File
@@ -0,0 +1,12 @@
<?php
return [
'name' => 'Gallery',
'translatedName' => 'gallery::module.name',
'translatedDescription' => 'gallery::module.description',
'alias' => 'gallery',
'handler' => '\\Modules\\Gallery\\Handlers\\Gallery',
'providers' => [\Modules\Gallery\Providers\GalleryServiceProvider::class],
'files' => ['start.php'],
'order' => 0,
];
View File
@@ -0,0 +1,20 @@
<?php namespace Modules\Gallery\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class GalleryDatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call("OthersTableSeeder");
}
}
View File
+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);
}
}
+1
View File
@@ -0,0 +1 @@
<?php
View File
@@ -0,0 +1,110 @@
<?php namespace Modules\Gallery\Providers;
use Illuminate\Support\ServiceProvider;
class GalleryServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig()
{
$this->publishes([
__DIR__ . '/../Config/config.php' => config_path('gallery.php'),
]);
$this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'gallery');
}
/**
* Register views.
*
* @return void
*/
public function registerViews()
{
$viewPath = base_path('resources/views/modules/gallery');
$sourcePath = __DIR__ . '/../Resources/views';
$this->publishes([
$sourcePath => $viewPath
]);
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/gallery';
}, \Config::get('view.paths')), [$sourcePath]), 'gallery');
/**
* @var $registrator TemplateRegistrator
*/
$registrator = $this->app->make('registerTemplateDir');
try {
// setup some global variables
$registrator->setBaseDirectory('resources/views/modules/gallery/visitor');
$registrator->setNamespace('gallery');
$registrator->setModuleId(call_user_func([config('gallery.handler'), 'getModuleId']));
// register path with current template name
$registrator->register(config('app.theme'), 'gallery::admin.templatePath');
} catch (InvalidArgumentException $e) {
// ignore this exception
}
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations()
{
$langPath = base_path('resources/lang/modules/gallery');
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, 'gallery');
} else {
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'gallery');
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array ();
}
}
@@ -0,0 +1,5 @@
<?php
return [
'upload' => 'Upload photos',
'uploadAfterSavingGallery' => 'Uploading for this gallery is not available yet. You must create the gallery first and upload later.'
];
@@ -0,0 +1,6 @@
<?php
return [
'name' => 'Gallery',
'description' => 'Simple photogallery',
];
@@ -0,0 +1,3 @@
<div class="row column">
<a href="{{route('upload.overview', ['id' => $page->module_settings->directoryId])}}" class="button large expanded success">@lang('gallery::admin/content.upload')</a>
</div>
@@ -0,0 +1,3 @@
<div class="row column">
<em>@lang('gallery::admin/content.uploadAfterSavingGallery')</em>
</div>
View File
+15
View File
@@ -0,0 +1,15 @@
{
"name": "pingpong-modules/gallery",
"description": "",
"authors": [
{
"name": "Pingpong Labs",
"email": "pingpong.labs@gmail.com"
}
],
"autoload": {
"psr-4": {
"Modules\\Gallery\\": ""
}
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
/*
|--------------------------------------------------------------------------
| Register Namespaces And Routes
|--------------------------------------------------------------------------
|
| When a module starting, this file will executed automatically. This helps
| to register some namespaces like translator or view. Also this file
| will load the routes file for each module. You may also modify
| this file as you want.
|
*/
require __DIR__ . '/Http/routes.php';