Passed
Pull Request — master (#13)
by Robbie
02:31
created

PopulateThemeSampleDataTask::output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\Tasks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Email\Email;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...ield\EditableEmailField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...mField\EditableFormStep was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\M...Field\EditableTextField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SilverStripe\UserForms\Model\UserDefinedForm;
0 ignored issues
show
Bug introduced by
The type SilverStripe\UserForms\Model\UserDefinedForm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SilverStripe\Versioned\Versioned;
14
15
/**
16
 * Used to populate sample data when installing the starter or Wātea theme
17
 */
18
class PopulateThemeSampleDataTask extends BuildTask
19
{
20
    protected $title = 'Populate sample data for theme demo';
21
22
    protected $description = 'Populates some sample data for showcasing the functionality of the '
23
        . 'starter and Wātea themes';
24
25
    /**
26
     * A series of method calls to create sample data
27
     *
28
     * @param HTTPRequest $request
29
     */
30
    public function run($request)
31
    {
32
        $this->handleContactForm();
33
    }
34
35
    /**
36
     * Decide whether to create a contact user defined form, and call it to be be created if so
37
     *
38
     * @return $this
39
     */
40
    protected function handleContactForm()
41
    {
42
        if (!$this->getContactFormExists()) {
43
            $this->createContactForm();
44
        }
45
        return $this;
46
    }
47
48
    /**
49
     * Determine whether a "contact us" userform exists yet
50
     *
51
     * @return bool
52
     */
53
    protected function getContactFormExists()
54
    {
55
        $exists = false;
56
        foreach (UserDefinedForm::get()->column('ID') as $formId) {
57
            $count = Versioned::get_all_versions(UserDefinedForm::class, $formId)
58
                ->filter('URLSegment', 'contact')
59
                ->count();
60
61
            if ($count >= 1) {
62
                $exists = true;
63
                break;
64
            }
65
        }
66
        return $exists;
67
    }
68
69
    /**
70
     * Create a "contact us" userform. Please note that this form does not have any recipients by default, so
71
     * no emails will be sent. To add recipients - edit the page in the CMS and add a recipient via the "Recipients"
72
     * tab.
73
     *
74
     * @return $this
75
     */
76
    protected function createContactForm()
77
    {
78
        $form = UserDefinedForm::create(array(
79
            'Title' => 'Contact',
80
            'URLSegment' => 'contact',
81
            'Content' => '<p>$UserDefinedForm</p>',
82
            'SubmitButtonText' => 'Submit',
83
            'ClearButtonText' => 'Clear',
84
            'OnCompleteMessage' => "<p>Thanks, we've received your submission and will be in touch shortly.</p>",
85
            'EnableLiveValidation' => true
86
        ));
87
88
        $form->write();
89
90
        // Add form fields
91
        $fields = array(
92
            EditableFormStep::create(array(
93
                'Title' => _t('EditableFormStep.TITLE_FIRST', 'First Page')
94
            )),
95
            EditableTextField::create(array(
96
                'Title' => 'Name',
97
                'Required' => true,
98
                'RightTitle' => 'Please enter your first and last name'
99
            )),
100
            EditableEmailField::create(array(
101
                'Title' => Email::class,
102
                'Required' => true,
103
                'Placeholder' => '[email protected]'
104
            )),
105
            EditableTextField::create(array(
106
                'Title' => 'Subject'
107
            )),
108
            EditableTextField::create(array(
109
                'Title' => 'Message',
110
                'Required' => true,
111
                'Rows' => 5
112
            ))
113
        );
114
115
        foreach ($fields as $field) {
116
            $field->write();
117
            $form->Fields()->add($field);
118
            $field->publish('Stage', 'Live');
119
        }
120
121
        $form->publish('Stage', 'Live');
122
        $form->flushCache();
123
124
        $this->output(' + Created "contact" UserDefinedForm page');
125
126
        return $this;
127
    }
128
129
    /**
130
     * Output a message either to the console or browser
131
     *
132
     * @param  string $message
133
     * @return $this
134
     */
135
    protected function output($message)
136
    {
137
        if (Director::is_cli()) {
138
            $message .= PHP_EOL;
139
        } else {
140
            $message = sprintf('<p>%s</p>', $message);
141
        }
142
        echo $message;
143
144
        return $this;
145
    }
146
}
147