FEATURE: add widget for displaying contact form

This commit is contained in:
Michal Kváček
2016-10-12 15:36:24 +02:00
parent b9d141c4f1
commit fb51ae80ac
8 changed files with 183 additions and 8 deletions
@@ -1,12 +1,9 @@
<?php namespace Modules\ContactForm\Providers;
use App\Handlers\Registrators\TemplateRegistrator;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Modules\ContactForm\Handlers\ContactForm;
use Modules\ContactForm\Widgets\Address\Address;
use Modules\ContactForm\Widgets\Address\Form;
use Modules\ContactForm\Widgets\Form\Form;
class ContactFormServiceProvider extends ServiceProvider
{
@@ -94,5 +91,6 @@ class ContactFormServiceProvider extends ServiceProvider
$registrator = $this->app->make('registerWidget');
$registrator->register(Address::class, null, ContactForm::getModuleId());
$registrator->register(Form::class, null, ContactForm::getModuleId());
}
}
@@ -9,7 +9,7 @@ return [
'form' => [
'name' => 'Contact form',
'description' => 'Display small contact form anywhere on website.',
'new' => 'New contact block widget'
'new' => 'New contact form widget'
],
'noContactForms' => 'You have not created any contact form. Try to create one before creating this widget.',
'selectContactForm' => 'Select contact form'
@@ -0,0 +1,19 @@
@extends('admin.layoutWithNavigation')
@section('pageHeading', @trans('contactform::admin/widgets.address.edit'))
@section('pageDescription', $widget->description)
@section('content')
@if(!count($forms))
<div class="alert alert-warning">@lang('contactform::admin/widgets.noContactForms')</div>
@else
<div class="col-md-8">
@include('contactform::admin.widgets.form.form', ['action' => route('widgets.postEdit', ['id' => $widget->id])])
</div>
<div class="col-md-4">
@widgetPreview($widget)
</div>
@endif
@endsection
@@ -0,0 +1,17 @@
<form action="{{$action}}" method="POST">
@include('admin.widgets.snippets.commonForm')
<label class="label-control">
@lang('contactform::admin/widgets.selectContactForm')
</label>
<select class="form-control" name="contactFormId">
@foreach($forms as $form)
<option @if(old('contactFormId', isset($widget->settings->contactFormId) ? $widget->settings->contactFormId : '') == $form->id) selected @endif value="{{$form->id}}">{{$form->title}}</option>
@endforeach
</select>
<button type="submit" class="btn btn-primary">
@lang('widgets/htmlCode.save')
</button>
</form>
@@ -0,0 +1,14 @@
@extends('admin.layoutWithNavigation')
@section('pageHeading', @trans('contactform::admin/widgets.form.new'))
@section('pageDescription', @trans($type->description))
@section('content')
@if(!count($forms))
<div class="alert alert-warning">@lang('contactform::admin/widgets.noContactForms')</div>
@else
@include('contactform::admin.widgets.form.form', ['action' => route('widgets.postNew', ['type' => $type])])
@endif
@endsection
@@ -26,9 +26,9 @@ class Presenter implements WidgetRenderer
$formData = json_decode($form->module_settings);
$code = '<div class="address ' . e($widget->containerClass) . '"';
if ($widget->containerId) {
$code .= ' id="' . e($widget->containerId);
$code = '<div class="address-widget ' . e($widget->container_class) . '"';
if ($widget->container_id) {
$code .= ' id="' . e($widget->container_id).'"';
}
$code .= '>';
$code .= "<p class='name'>".e($formData->companyName).'</p>';
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace Modules\ContactForm\Widgets\Form;
use App\Models\Widget;
use App\Widgets\Contracts\WidgetRenderer;
use App\Widgets\NonInstallableWidget;
class Form extends NonInstallableWidget
{
/**
* Name getter
*
* @return string
*/
public static function getName()
{
return 'contactform::admin/widgets.form.name';
}
/**
* Getter for description
*
* @return string
*/
public static function getDescription()
{
return 'contactform::admin/widgets.form.description';
}
/**
* Getter for presenter/renderer of this widget
*
* @return WidgetRenderer
*/
public static function getPresenter()
{
return Presenter::class;
}
/**
* @param Widget $widget
* @param \stdClass $data
*
* @return mixed
*/
public static function save(Widget $widget, \stdClass $data)
{
$widget->settings = json_encode(['contactFormId' => $data->contactFormId,]);
return $widget;
}
}
@@ -0,0 +1,75 @@
<?php
namespace Modules\ContactForm\Widgets\Form;
use App\Models\Content;
use App\Models\Widget;
use App\Models\WidgetType;
use App\Widgets\Contracts\WidgetRenderer;
class Presenter implements WidgetRenderer
{
public static $template = null;
/**
* Method used for displaying widget.
*
* This method renders some HTML code for displaying.
*
* @param Widget $widget
*
* @return mixed
* @throws \ErrorException
*/
public static function render(Widget $widget)
{
if (!self::$template) {
throw new \ErrorException('Cannot render contact form without template! Please, set Presenter::$template variable before rendering.');
}
$form = Content::find($widget->settings->contactFormId);
if (!$form) {
return null;
}
$form->module_settings = json_decode($form->module_settings);
return view(self::$template, ['widget' => $widget, 'form' => $form]);
}
/**
* Display form for creating new instance of widget
*
* @param WidgetType $type
*
* @return mixed
*/
public static function renderAddForm(WidgetType $type)
{
$contactForms = Content::where('module_handler', config('contactform.handler'))
->get();
return view('contactform::admin.widgets.form.new', [
'type' => $type,
'widget' => new Widget(),
'forms' => $contactForms
]);
}
/**
* Display form for editing already existing widget
*
* @param Widget $widget
*
* @return mixed
*/
public static function renderEditForm(Widget $widget)
{
$contactForms = Content::where('module_handler', config('contactform.handler'))
->get();
return view('contactform::admin.widgets.address.edit', [
'widget' => $widget,
'forms' => $contactForms
]);
}
}