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