PageEmailManagement::getTemplate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
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\Domain;
12
use Waca\DataObjects\EmailTemplate;
13
use Waca\DataObjects\RequestQueue;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Helpers\Logger;
17
use Waca\PdoDatabase;
18
use Waca\SessionAlert;
19
use Waca\Tasks\InternalPageBase;
20
use Waca\WebRequest;
21
22
class PageEmailManagement extends InternalPageBase
23
{
24
    /**
25
     * Main function for this page, when no specific actions are called.
26
     * @return void
27
     */
28
    protected function main()
29
    {
30
        $this->setHtmlTitle('Close Emails');
31
32
        // Get all active email templates
33
        $activeTemplates = EmailTemplate::getAllActiveTemplates(null, $this->getDatabase());
34
        $inactiveTemplates = EmailTemplate::getAllInactiveTemplates($this->getDatabase());
35
36
        $this->assign('activeTemplates', $activeTemplates);
37
        $this->assign('inactiveTemplates', $inactiveTemplates);
38
39
        $user = User::getCurrent($this->getDatabase());
40
        $this->assign('canCreate', $this->barrierTest('create', $user));
41
        $this->assign('canEdit', $this->barrierTest('edit', $user));
42
43
        $this->setTemplate('email-management/main.tpl');
44
    }
45
46
    protected function view()
47
    {
48
        $this->setHtmlTitle('Close Emails');
49
50
        $database = $this->getDatabase();
51
        $template = $this->getTemplate($database);
52
53
        // FIXME: domains!
54
        /** @var Domain $domain */
55
        $domain = Domain::getById(1, $database);
56
57
        $this->assign('id', $template->getId());
58
        $this->assign('emailTemplate', $template);
59
        $this->assign('createdid', $domain->getDefaultClose());
60
61
        $this->setTemplate('email-management/view.tpl');
62
    }
63
64
    /**
65
     * @param PdoDatabase $database
66
     *
67
     * @return EmailTemplate
68
     * @throws ApplicationLogicException
69
     */
70
    protected function getTemplate(PdoDatabase $database)
71
    {
72
        $templateId = WebRequest::getInt('id');
73
        if ($templateId === null) {
74
            throw new ApplicationLogicException('Template not specified');
75
        }
76
        $template = EmailTemplate::getById($templateId, $database);
77
        if ($template === false || !is_a($template, EmailTemplate::class)) {
78
            throw new ApplicationLogicException('Template not found');
79
        }
80
81
        return $template;
82
    }
83
84
    protected function edit()
85
    {
86
        $this->setHtmlTitle('Close Emails');
87
88
        $database = $this->getDatabase();
89
        $template = $this->getTemplate($database);
90
91
        // FIXME: domains!
92
        /** @var Domain $domain */
93
        $domain = Domain::getById(1, $database);
94
95
        $createdId = $domain->getDefaultClose();
96
97
        $requestQueues = RequestQueue::getEnabledQueues($database);
98
99
        if (WebRequest::wasPosted()) {
100
            $this->validateCSRFToken();
101
102
            $this->modifyTemplateData($template);
103
104
            $other = EmailTemplate::getByName($template->getName(), $database);
105
            if ($other !== false && $other->getId() !== $template->getId()) {
106
                throw new ApplicationLogicException('A template with this name already exists');
107
            }
108
109
            if ($template->getId() === $createdId) {
110
                $template->setDefaultAction(EmailTemplate::ACTION_CREATED);
111
                $template->setActive(true);
112
                $template->setPreloadOnly(false);
113
            }
114
115
            // optimistically lock on load of edit form
116
            $updateVersion = WebRequest::postInt('updateversion');
117
            $template->setUpdateVersion($updateVersion);
118
119
            $template->save();
120
            Logger::editedEmail($database, $template);
121
            $this->getNotificationHelper()->emailEdited($template);
122
            SessionAlert::success("Email template has been saved successfully.");
123
124
            $this->redirect('emailManagement');
125
        }
126
        else {
127
            $this->assignCSRFToken();
128
            $this->assign('id', $template->getId());
129
            $this->assign('emailTemplate', $template);
130
            $this->assign('createdid', $createdId);
131
            $this->assign('requestQueues', $requestQueues);
132
133
            $this->setTemplate('email-management/edit.tpl');
134
        }
135
    }
136
137
    /**
138
     * @param EmailTemplate $template
139
     *
140
     * @throws ApplicationLogicException
141
     */
142
    private function modifyTemplateData(EmailTemplate $template)
143
    {
144
        $name = WebRequest::postString('name');
145
        if ($name === null || $name === '') {
146
            throw new ApplicationLogicException('Name not specified');
147
        }
148
149
        $template->setName($name);
150
151
        $text = WebRequest::postString('text');
152
        if ($text === null || $text === '') {
153
            throw new ApplicationLogicException('Text not specified');
154
        }
155
156
        $template->setText($text);
157
158
        $jsquestion = WebRequest::postString('jsquestion');
159
        if ($jsquestion === null || $jsquestion === '') {
160
            throw new ApplicationLogicException('JS question not specified');
161
        }
162
        $template->setJsquestion($jsquestion);
163
164
        switch (WebRequest::postString('defaultaction')) {
165
            case EmailTemplate::ACTION_NONE:
166
            case EmailTemplate::ACTION_CREATED:
167
            case EmailTemplate::ACTION_NOT_CREATED:
168
                $template->setDefaultAction(WebRequest::postString('defaultaction'));
169
                $template->setQueue(null);
170
                break;
171
            default:
172
                $template->setDefaultAction(EmailTemplate::ACTION_DEFER);
173
                // FIXME: domains!
174
                $template->setQueue(RequestQueue::getByApiName($this->getDatabase(), WebRequest::postString('defaultaction'), 1)->getId());
0 ignored issues
show
Bug introduced by
It seems like Waca\WebRequest::postString('defaultaction') can also be of type null; however, parameter $apiName of Waca\DataObjects\RequestQueue::getByApiName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
                $template->setQueue(RequestQueue::getByApiName($this->getDatabase(), /** @scrutinizer ignore-type */ WebRequest::postString('defaultaction'), 1)->getId());
Loading history...
175
                break;
176
        }
177
178
        $template->setActive(WebRequest::postBoolean('active'));
179
        $template->setPreloadOnly(WebRequest::postBoolean('preloadonly'));
180
    }
181
182
    protected function create()
183
    {
184
        $this->setHtmlTitle('Close Emails');
185
186
        $database = $this->getDatabase();
187
188
        $requestQueues = RequestQueue::getEnabledQueues($database);
189
190
        if (WebRequest::wasPosted()) {
191
            $this->validateCSRFToken();
192
            $template = new EmailTemplate();
193
            $template->setDatabase($database);
194
195
            $this->modifyTemplateData($template);
196
197
            $other = EmailTemplate::getByName($template->getName(), $database);
198
            if ($other !== false) {
199
                throw new ApplicationLogicException('A template with this name already exists');
200
            }
201
202
            $template->save();
203
204
            Logger::createEmail($database, $template);
205
            $this->getNotificationHelper()->emailCreated($template);
206
207
            SessionAlert::success("Email template has been saved successfully.");
208
209
            $this->redirect('emailManagement');
210
        }
211
        else {
212
            $this->assignCSRFToken();
213
            $this->assign('id', -1);
214
            $this->assign('emailTemplate', new EmailTemplate());
215
            $this->assign('createdid', -2);
216
217
            $this->assign('requestQueues', $requestQueues);
218
            $this->setTemplate('email-management/edit.tpl');
219
        }
220
    }
221
}
222