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
+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
]);
}
}