Completed
Branch newinternal (113bb8)
by Michael
05:12
created

PageEmailManagement::edit()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 45
ccs 0
cts 34
cp 0
rs 9.1448
cc 5
nc 4
nop 0
crap 30
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Waca\DataObjects\EmailTemplate;
12
use Waca\DataObjects\User;
13
use Waca\Exceptions\ApplicationLogicException;
14
use Waca\Helpers\Logger;
15
use Waca\PdoDatabase;
16
use Waca\SessionAlert;
17
use Waca\Tasks\InternalPageBase;
18
use Waca\WebRequest;
19
20
class PageEmailManagement extends InternalPageBase
21
{
22
    /**
23
     * Main function for this page, when no specific actions are called.
24
     * @return void
25
     */
26
    protected function main()
27
    {
28
        $this->setHtmlTitle('Close Emails');
29
30
        // Get all active email templates
31
        $activeTemplates = EmailTemplate::getAllActiveTemplates(null, $this->getDatabase());
32
        $inactiveTemplates = EmailTemplate::getAllInactiveTemplates($this->getDatabase());
33
34
        $this->assign('activeTemplates', $activeTemplates);
35
        $this->assign('inactiveTemplates', $inactiveTemplates);
36
37
        $user = User::getCurrent($this->getDatabase());
38
        $this->assign('canCreate', $this->barrierTest('create', $user));
39
        $this->assign('canEdit', $this->barrierTest('edit', $user));
40
41
        $this->setTemplate('email-management/main.tpl');
42
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
43
44
    protected function view()
45
    {
46
        $this->setHtmlTitle('Close Emails');
47
48
        $database = $this->getDatabase();
49
        $template = $this->getTemplate($database);
50
51
        $createdId = $this->getSiteConfiguration()->getDefaultCreatedTemplateId();
52
        $requestStates = $this->getSiteConfiguration()->getRequestStates();
53
54
        $this->assign('id', $template->getId());
55
        $this->assign('emailTemplate', $template);
56
        $this->assign('createdid', $createdId);
57
        $this->assign('requeststates', $requestStates);
58
59
        $this->setTemplate('email-management/view.tpl');
60
    }
0 ignored issues
show
Coding Style introduced by
Expected //end view()
Loading history...
61
62
    /**
63
     * @param PdoDatabase $database
64
     *
65
     * @return EmailTemplate
66
     * @throws ApplicationLogicException
67
     */
68
    protected function getTemplate(PdoDatabase $database)
69
    {
70
        $templateId = WebRequest::getInt('id');
71
        if ($templateId === null) {
72
            throw new ApplicationLogicException('Template not specified');
73
        }
74
        $template = EmailTemplate::getById($templateId, $database);
75
        if ($template === false || !is_a($template, EmailTemplate::class)) {
76
            throw new ApplicationLogicException('Template not found');
77
        }
78
79
        return $template;
80
    }
0 ignored issues
show
Coding Style introduced by
Expected //end getTemplate()
Loading history...
81
82
    protected function edit()
83
    {
84
        $this->setHtmlTitle('Close Emails');
85
86
        $database = $this->getDatabase();
87
        $template = $this->getTemplate($database);
88
89
        $createdId = $this->getSiteConfiguration()->getDefaultCreatedTemplateId();
90
        $requestStates = $this->getSiteConfiguration()->getRequestStates();
91
92
        if (WebRequest::wasPosted()) {
93
            $this->validateCSRFToken();
94
95
            $this->modifyTemplateData($template);
96
97
            $other = EmailTemplate::getByName($template->getName(), $database);
98
            if ($other !== false && $other->getId() !== $template->getId()) {
99
                throw new ApplicationLogicException('A template with this name already exists');
100
            }
101
102
            if ($template->getId() === $createdId) {
103
                $template->setDefaultAction(EmailTemplate::CREATED);
104
                $template->setActive(true);
105
                $template->setPreloadOnly(false);
106
            }
107
108
            // optimistically lock on load of edit form
109
            $updateVersion = WebRequest::postInt('updateversion');
110
            $template->setUpdateVersion($updateVersion);
111
112
            $template->save();
113
            Logger::editedEmail($database, $template);
114
            $this->getNotificationHelper()->emailEdited($template);
115
            SessionAlert::success("Email template has been saved successfully.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Email template has been saved successfully. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
116
117
            $this->redirect('emailManagement');
118
        }
119
        else {
120
            $this->assignCSRFToken();
121
            $this->assign('id', $template->getId());
122
            $this->assign('emailTemplate', $template);
123
            $this->assign('createdid', $createdId);
124
            $this->assign('requeststates', $requestStates);
125
126
            $this->setTemplate('email-management/edit.tpl');
127
        }
128
    }
0 ignored issues
show
Coding Style introduced by
Expected //end edit()
Loading history...
129
130
    /**
131
     * @param EmailTemplate $template
132
     *
133
     * @throws ApplicationLogicException
134
     */
135
    private function modifyTemplateData(EmailTemplate $template)
136
    {
137
        $name = WebRequest::postString('name');
138
        if ($name === null || $name === '') {
139
            throw new ApplicationLogicException('Name not specified');
140
        }
141
142
        $template->setName($name);
143
144
        $text = WebRequest::postString('text');
145
        if ($text === null || $text === '') {
146
            throw new ApplicationLogicException('Text not specified');
147
        }
148
149
        $template->setText($text);
150
151
        $template->setJsquestion(WebRequest::postString('jsquestion'));
152
153
        $template->setDefaultAction(WebRequest::postString('defaultaction'));
154
        $template->setActive(WebRequest::postBoolean('active'));
155
        $template->setPreloadOnly(WebRequest::postBoolean('preloadonly'));
156
    }
0 ignored issues
show
Coding Style introduced by
Expected //end modifyTemplateData()
Loading history...
157
158
    protected function create()
159
    {
160
        $this->setHtmlTitle('Close Emails');
161
162
        $database = $this->getDatabase();
163
164
        $requestStates = $this->getSiteConfiguration()->getRequestStates();
165
166
        if (WebRequest::wasPosted()) {
167
            $this->validateCSRFToken();
168
            $template = new EmailTemplate();
169
            $template->setDatabase($database);
170
171
            $this->modifyTemplateData($template);
172
173
            $other = EmailTemplate::getByName($template->getName(), $database);
174
            if ($other !== false) {
175
                throw new ApplicationLogicException('A template with this name already exists');
176
            }
177
178
            $template->save();
179
180
            Logger::createEmail($database, $template);
181
            $this->getNotificationHelper()->emailCreated($template);
182
183
            SessionAlert::success("Email template has been saved successfully.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Email template has been saved successfully. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
184
185
            $this->redirect('emailManagement');
186
        }
187
        else {
188
            $this->assignCSRFToken();
189
            $this->assign('requeststates', $requestStates);
190
            $this->setTemplate('email-management/create.tpl');
191
        }
192
    }
0 ignored issues
show
Coding Style introduced by
Expected //end create()
Loading history...
193
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
194