Passed
Push — master ( ebdfbb...20570e )
by
unknown
02:32
created

Model/Recipient/UserFormRecipientItemRequest.php (1 issue)

Severity
1
<?php
2
3
namespace SilverStripe\UserForms\Model\Recipient;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\ORM\FieldType\DBField;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableFormHeading;
10
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\View\SSViewer;
13
14
/**
15
 * Controller that handles requests to EmailRecipient's
16
 *
17
 * @package userforms
18
 */
19
class UserFormRecipientItemRequest extends GridFieldDetailForm_ItemRequest
20
{
21
    private static $allowed_actions = [
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
22
        'edit',
23
        'view',
24
        'ItemEditForm',
25
        'preview'
26
    ];
27
28
    /**
29
     * Renders a preview of the recipient email.
30
     */
31
    public function preview()
32
    {
33
        // Enable theme for preview (may be needed for Shortcodes)
34
        Config::nest();
35
        Config::modify()->set(SSViewer::class, 'theme_enabled', true);
36
37
        $content = $this->customise([
38
            'Body' => $this->record->getEmailBodyContent(),
39
            'HideFormData' => (bool) $this->record->HideFormData,
40
            'Fields' => $this->getPreviewFieldData()
41
        ])->renderWith($this->record->EmailTemplate);
42
43
        Config::unnest();
44
45
        return $content;
46
    }
47
48
    /**
49
     * Get some placeholder field values to display in the preview
50
     *
51
     * @return ArrayList
52
     */
53
    protected function getPreviewFieldData()
54
    {
55
        $data = ArrayList::create();
56
57
        $fields = $this->record->Form()->Fields()->filter(
58
            'ClassName:not',
59
            [
60
                EditableLiteralField::class,
61
                EditableFormHeading::class,
62
            ]
63
        );
64
65
        foreach ($fields as $field) {
66
            $data->push(ArrayData::create([
67
                'Name' => $field->dbObject('Name'),
68
                'Title' => $field->dbObject('Title'),
69
                'Value' => DBField::create_field('Varchar', '$' . $field->Name),
70
                'FormattedValue' => DBField::create_field('Varchar', '$' . $field->Name)
71
            ]));
72
        }
73
74
        return $data;
75
    }
76
}
77