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

74 lines
2.1 KiB
PHP
Raw Normal View History

2015-07-24 04:37:48 +02:00
<?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;
2015-07-24 04:37:48 +02:00
/**
* Controller that handles requests to EmailRecipient's
*
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class UserFormRecipientItemRequest extends GridFieldDetailForm_ItemRequest
{
private static $allowed_actions = [
2016-07-21 07:53:59 +02:00
'edit',
'view',
'ItemEditForm',
'preview'
];
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
/**
* 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([
2016-07-21 07:53:59 +02:00
'Body' => $this->record->getEmailBodyContent(),
'HideFormData' => $this->record->HideFormData,
'Fields' => $this->getPreviewFieldData()
]))->renderWith($this->record->EmailTemplate);
Config::unnest();
return $content;
2016-07-21 07:53:59 +02:00
}
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
/**
* Get some placeholder field values to display in the preview
*
2016-07-21 07:53:59 +02:00
* @return ArrayList
*/
protected function getPreviewFieldData()
2016-07-21 07:53:59 +02:00
{
$data = ArrayList::create();
2015-07-24 04:37:48 +02:00
$fields = $this->record->Form()->Fields()->filter([
'ClassName:not' => EditableLiteralField::class,
'ClassName:not' => EditableFormHeading::class
]);
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
foreach ($fields as $field) {
$data->push(ArrayData::create([
2017-05-01 07:37:50 +02:00
'Name' => $field->dbObject('Name'),
'Title' => $field->dbObject('Title'),
'Value' => DBField::create_field('Varchar', '$' . $field->Name),
'FormattedValue' => DBField::create_field('Varchar', '$' . $field->Name)
]));
2016-07-21 07:53:59 +02:00
}
2015-07-24 04:37:48 +02:00
2016-07-21 07:53:59 +02:00
return $data;
}
2015-07-24 04:37:48 +02:00
}