Completed
Pull Request — master (#647)
by Robbie
20:35 queued 18:30
created

UserFormRecipientItemRequest::preview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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\EditableLiteralField;
10
use SilverStripe\UserForms\Model\EditableFormField\EditableFormHeading;
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
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can 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(ArrayData::create([
38
            'Body' => $this->record->getEmailBodyContent(),
39
            'HideFormData' => $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' => EditableLiteralField::class,
59
            'ClassName:not' => EditableFormHeading::class
60
        ]);
61
62
        foreach ($fields as $field) {
63
            $data->push(ArrayData::create([
64
                'Name' => $field->dbObject('Name'),
65
                'Title' => $field->dbObject('Title'),
66
                'Value' => DBField::create_field('Varchar', '$' . $field->Name),
67
                'FormattedValue' => DBField::create_field('Varchar', '$' . $field->Name)
68
            ]));
69
        }
70
71
        return $data;
72
    }
73
}
74