FEATURE: create module for contact form.

Currently contact form can be created, edit is not done yet. And also email sending neither work, now.
This commit is contained in:
Michal Kváček
2016-04-01 17:04:10 +02:00
parent 509ef1752e
commit f90f43fcd6
27 changed files with 569 additions and 0 deletions
View File
View File
+12
View File
@@ -0,0 +1,12 @@
<?php
return [
'name' => 'ContactForm',
'translatedName' => 'contactform::module.name',
'translatedDescription' => 'contactform::module.description',
'alias' => 'contactform',
'handler' => '\\Modules\\ContactForm\\Handlers\\ContactForm',
'providers' => [\Modules\ContactForm\Providers\ContactFormServiceProvider::class],
'files' => ['start.php'],
'order' => 0,
];
@@ -0,0 +1,20 @@
<?php namespace Modules\Contactform\Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class ContactFormDatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call("OthersTableSeeder");
}
}
@@ -0,0 +1,153 @@
<?php
namespace Modules\ContactForm\Handlers;
use App\Enumerations\ModulesHandlerToId;
use App\Handlers\Handler;
use App\Http\Controllers\Controller;
use App\Http\Requests\Request;
use App\Models\Content;
use App\Models\User;
class ContactForm extends Handler
{
/**
* 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)
{
$content->module_settings = json_decode($content->module_settings);
return Controller::VisitorsView($content->template, ['page' => $content]);
}
public static function getHistoryHandler()
{
return ContactFormHistory::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)
{
$settings = [
'phone' => $request->phone,
'email' => $request->email,
'companyName' => $request->companyName,
'address' => $request->address,
'gpsLat' => $request->gpsLat,
'gpsLng' => $request->gpsLng,
];
// 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)
{
// TODO: Implement edit() method.
}
public static function getModuleId()
{
$handler = config('contactform.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 = parent::getExtendedFormSections();
$sections['content'] = trans('contactform::admin/content.content');
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()
{
return [
'basicInfo' => 'contactform::admin.form.basicInfo',
'meta' => 'admin.content.formParts.metaInformation',
'appearance' => 'admin.content.formParts.appearance',
'publication' => 'admin.content.formParts.publicationSetup',
'content' => 'contactform::admin.form.content',
];
}
/**
* 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)
{
// register links in admin menu
$menu = \Menu::instance('mainMenu');
$menu->dropdown(trans('contactform::admin/adminMenu.contactForm'), function ($submenu) use ($user) {
$submenu->route('content.new', trans('contactform::admin/adminMenu.new'), ['moduleId' => self::getModuleId()], 10, ['icon' => 'fa fa-plus']);
$submenu->route('content.overview', trans('contactform::admin/adminMenu.overview'), ['moduleId' => self::getModuleId()], 20, ['icon' => 'fa fa-list']);
$submenu->route('emails.sent', trans('contactform::admin/adminMenu.sentMessages'), [], 30, ['icon' => 'fa fa-send']);
}, 25, ['icon' => 'fa fa-envelope-o']);
}
/**
* 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;
}
/**
* Install module
*
* @return mixed
*/
public static function install()
{
// TODO: Implement install() method.
}
}
@@ -0,0 +1,16 @@
<?php
namespace Modules\ContactForm\Handlers;
use App\Handlers\History;
class ContactFormHistory extends History
{
public static function parseChangedValue($changeset)
{
switch ($changeset->column) {
default:
return parent::parseChangedValue($changeset);
break;
}
}
}
@@ -0,0 +1,23 @@
<?php
namespace Modules\ContactForm\Http\Controllers\Visitor;
use App\Models\Content;
use Modules\ContactForm\Http\Requests\SendNewEmailRequest;
use Pingpong\Modules\Routing\Controller;
class CreateNewMessage extends Controller
{
public function postMessage(SendNewEmailRequest $request, $formId)
{
$form = Content::where('module_handler', config('contactform.handler'))
->findOrFail($formId);
dd($request->name, $form);
return redirect()
->route('page.content', ['url' => $form->url])
->with('success', '');
}
}
@@ -0,0 +1,18 @@
<?php namespace Modules\ContactForm\Http\Requests;
use App\Http\Requests\Request;
class SendNewEmailRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
];
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
use App\Http\Requests\Request;
Route::group([
'middleware' => ['web', 'auth', 'adminMenu'],
'namespace' => '\Modules\ContactForm\Http\Controllers\Admin',
'prefix' => 'admin/contactform'
], function () {
Route::get('messsages/sent', 'SentMessagesOverview@getList')
->name('emails.sent');
});
Route::group([
'middleware' => ['web'],
'namespace' => '\Modules\ContactForm\Http\Controllers\Visitor',
'prefix' => 'contactform'
], function () {
// method for sending new message
Route::post('/message/{formId}', 'CreateNewMessage@postMessage')
->name('email.send');
});
@@ -0,0 +1,108 @@
<?php namespace Modules\ContactForm\Providers;
use App\Handlers\Registrators\TemplateRegistrator;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
class ContactFormServiceProvider 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()
{
$registrator = $this->app->make('registerSettingsExtension');
$registrator->register('contactForm', 'contactform::module.name', 'contactform::admin.settings.form');
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig()
{
$this->publishes([
__DIR__ . '/../Config/config.php' => config_path('contactform.php'),
]);
$this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'contactform');
}
/**
* Register views.
*
* @return void
*/
public function registerViews()
{
$viewPath = base_path('resources/views/modules/contactform');
$sourcePath = __DIR__ . '/../Resources/views';
$this->publishes([
$sourcePath => $viewPath
]);
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/contactform';
}, \Config::get('view.paths')), [$sourcePath]), 'contactform');
/**
* @var $registrator TemplateRegistrator
*/
$registrator = $this->app->make('registerTemplateDir');
try {
// setup some global variables
$registrator->setBaseDirectory('resources/views/modules/contactform/visitor');
$registrator->setNamespace('contactform');
$registrator->setModuleId(call_user_func([config('contactform.handler'), 'getModuleId']));
// register path with current template name
$registrator->register(config('app.theme'), 'contactform::admin.templatePath');
} catch (InvalidArgumentException $e) {
// ignore this exception
}
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations()
{
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'contactform');
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array ();
}
}
@@ -0,0 +1,7 @@
<?php
return [
'contactForm' => 'Contact form',
'new' => 'New',
'overview' => 'All contact forms',
'sentMessages' => 'Messages'
];
@@ -0,0 +1,12 @@
<?php
return [
'content' => 'Contact form content',
'address' => 'Address',
'emailList' => 'Send emails to these addresses',
'gpsLat' => 'GPS latitude',
'gpsLng' => 'GPS longitude',
'compName' => 'Name displayed above address',
'phone' => 'Contact phone',
'contactEmail' => 'E-mail, where messages from this form will be delivered',
'contactInformation' => 'Contact information'
];
@@ -0,0 +1,5 @@
<?php
return [
'recaptchaSiteKey' => 'reCAPTCHA site key',
'recaptchaSecret' => 'reCAPTCHA application secret key',
];
@@ -0,0 +1,5 @@
<?php
return [
'name' => 'Contact form',
'description' => 'Simple contact form'
];
@@ -0,0 +1,113 @@
<div class="row">
<div class="columns large-6">
<label>
@lang('admin/contents.field_title')
<input type="text" name="title" placeholder="@lang('admin/contents.field_title')"
value="{{old('title', $page->title)}}" />
</label>
</div>
<div class="columns large-6">
<label>
@lang('admin/contents.field_url')
<input type="text" name="url" placeholder="@lang('admin/contents.field_url')"
value="{{old('url', $page->url)}}" />
@if (isset($errors) && $errors->has('url'))
<div class="callout alert">
{{ $errors->first('url') }}
</div>
@endif
</label>
</div>
</div>
<div class="row">
<div class="columns large-6">
<label>
<select name="status">
@foreach($contentStatuses as $status => $translation)
<option value="{{$status}}" @if(old('status', $page->status) == $status) selected @endif>
{{$translation}}
</option>
@endforeach
</select>
</label>
@if (isset($errors) && $errors->has('status'))
<div class="callout alert">
{{ $errors->first('status') }}
</div>
@endif
</div>
<div class="column large-6">
<label>
<select name="parentPage">
<option value="">@lang('admin/contents.noParentPage')</option>
@foreach($rootPages as $rootPage)
<option value="{{$rootPage->id}}"
@if(old('parent_page', $page->parent_id) == $rootPage->id) selected @endif>
{{$rootPage->title}}
</option>
@endforeach
</select>
</label>
@if (isset($errors) && $errors->has('parentPage'))
<div class="callout alert">
{{ $errors->first('parentPage') }}
</div>
@endif
</div>
</div>
<h2>@lang('contactform::admin/content.contactInformation')</h2>
<div class="row">
<div class="column large-6">
<label>
@lang('contactform::admin/content.phone')
<input type="tel" name="phone" value="{{$page->module_settings->phone or ""}}" />
</label>
</div>
<div class="column large-6">
<label>
@lang('contactform::admin/content.contactEmail')
<input type="email" name="email" value="{{$page->module_settings->email or \Auth::user()->email}}" />
</label>
</div>
</div>
<div class="row">
<div class="column large-6">
<label>
@lang('contactform::admin/content.compName')
<input type="text" name="companyName" value="{{$page->module_settings->companyName or ""}}" />
</label>
<label>
@lang('contactform::admin/content.address')
<textarea rows="3" name="address">{{$page->module_settings->address or ""}}</textarea>
</label>
</div>
<div class="column large-6">
<label>
@lang('contactform::admin/content.gpsLat')
<input type="text" value="{{$page->module_settings->gpsLat or ""}}" name="gpsLat" />
</label>
<label>
@lang('contactform::admin/content.gpsLng')
<input type="text" value="{{$page->module_settings->gpsLng or ""}}" name="gpsLng" />
</label>
</div>
</div>
@@ -0,0 +1,10 @@
<div class="row">
<div class="columns large-12">
<label>
<textarea name="pageContent" id="pageContent" class="wysiwyg-editor">
{{old('pageContent', $page->content)}}
</textarea>
</label>
</div>
</div>
@@ -0,0 +1,13 @@
<label>
@lang('contactform::admin/settings.recaptchaSiteKey')
<input type="text" name="recaptchaSiteKey" value="{{old('recaptchaSiteKey', isset($settings->recaptchaSiteKey) ? $settings->recaptchaSiteKey : null)}}" />
</label>
<label>
@lang('contactform::admin/settings.recaptchaSecret')
<input type="text" name="recaptchaSecret" value="{{old('recaptchaSecret', isset($settings->recaptchaSecret) ? $settings->recaptchaSecret : null)}}" />
</label>
<em><a href="https://www.google.com/recaptcha/admin#list" target="_blank">reCAPTCHA</a></em>
View File
+15
View File
@@ -0,0 +1,15 @@
{
"name": "pingpong-modules/contactform",
"description": "",
"authors": [
{
"name": "Pingpong Labs",
"email": "pingpong.labs@gmail.com"
}
],
"autoload": {
"psr-4": {
"Modules\\ContactForm\\": ""
}
}
}
+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';