silverstripe-userforms/code/Model/Recipient/UserFormRecipientItemRequest.php

77 lines
2.1 KiB
PHP

<?php
namespace SilverStripe\UserForms\Model\Recipient;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFormHeading;
use SilverStripe\View\ArrayData;
use SilverStripe\View\SSViewer;
/**
* Controller that handles requests to EmailRecipient's
*
* @package userforms
*/
class UserFormRecipientItemRequest extends GridFieldDetailForm_ItemRequest
{
private static $allowed_actions = [
'edit',
'view',
'ItemEditForm',
'preview'
];
/**
* Renders a preview of the recipient email.
*/
public function preview()
{
// Enable theme for preview (may be needed for Shortcodes)
Config::nest();
Config::modify()->set(SSViewer::class, 'theme_enabled', true);
$content = $this->customise(ArrayData::create([
'Body' => $this->record->getEmailBodyContent(),
'HideFormData' => $this->record->HideFormData,
'Fields' => $this->getPreviewFieldData()
]))->renderWith($this->record->EmailTemplate);
Config::unnest();
return $content;
}
/**
* Get some placeholder field values to display in the preview
*
* @return ArrayList
*/
protected function getPreviewFieldData()
{
$data = ArrayList::create();
$fields = $this->record->Form()->Fields()->filter(
'ClassName:not',
[
EditableLiteralField::class,
EditableFormHeading::class,
]
);
foreach ($fields as $field) {
$data->push(ArrayData::create([
'Name' => $field->dbObject('Name'),
'Title' => $field->dbObject('Title'),
'Value' => DBField::create_field('Varchar', '$' . $field->Name),
'FormattedValue' => DBField::create_field('Varchar', '$' . $field->Name)
]));
}
return $data;
}
}