FEATURE: add widget with address block

This commit is contained in:
Michal Kváček
2016-10-12 15:12:42 +02:00
parent af4edac469
commit b9d141c4f1
7 changed files with 208 additions and 0 deletions
@@ -1,8 +1,12 @@
<?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;
class ContactFormServiceProvider extends ServiceProvider
{
@@ -23,6 +27,7 @@ class ContactFormServiceProvider extends ServiceProvider
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerWidgets();
}
/**
@@ -83,4 +88,11 @@ class ContactFormServiceProvider extends ServiceProvider
{
return array ();
}
private function registerWidgets()
{
$registrator = $this->app->make('registerWidget');
$registrator->register(Address::class, null, ContactForm::getModuleId());
}
}
@@ -0,0 +1,16 @@
<?php
return [
'address' => [
'name' => 'Address block',
'description' => 'Small widget for displaying address saved in selected contact form.',
'new' => 'New address block',
'edit' => 'Editing address block'
],
'form' => [
'name' => 'Contact form',
'description' => 'Display small contact form anywhere on website.',
'new' => 'New contact block 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-10">
@include('contactform::admin.widgets.address.form', ['action' => route('widgets.postEdit', ['id' => $widget->id])])
</div>
<div class="col-md-2">
@widgetPreview($widget)
</div>
@endif
@endsection
@@ -0,0 +1,19 @@
<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.address.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.address.form', ['action' => route('widgets.postNew', ['type' => $type])])
@endif
@endsection
@@ -0,0 +1,52 @@
<?php
namespace Modules\ContactForm\Widgets\Address;
use App\Models\Widget;
use App\Widgets\Contracts\WidgetRenderer;
use App\Widgets\NonInstallableWidget;
class Address extends NonInstallableWidget
{
/**
* Name getter
*
* @return string
*/
public static function getName()
{
return 'contactform::admin/widgets.address.name';
}
/**
* Getter for description
*
* @return string
*/
public static function getDescription()
{
return 'contactform::admin/widgets.address.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,76 @@
<?php
namespace Modules\ContactForm\Widgets\Address;
use App\Models\Content;
use App\Models\Widget;
use App\Models\WidgetType;
use App\Widgets\Contracts\WidgetRenderer;
class Presenter implements WidgetRenderer
{
/**
* Method used for displaying widget.
*
* This method renders some HTML code for displaying.
*
* @param Widget $widget
*
* @return mixed
*/
public static function render(Widget $widget)
{
$form = Content::find($widget->settings->contactFormId);
if (!$form)
return null;
$formData = json_decode($form->module_settings);
$code = '<div class="address ' . e($widget->containerClass) . '"';
if ($widget->containerId) {
$code .= ' id="' . e($widget->containerId);
}
$code .= '>';
$code .= "<p class='name'>".e($formData->companyName).'</p>';
$code .= nl2br($formData->address);
$code .= "</div>";
return $code;
}
/**
* 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.address.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
]);
}
}