Issues (186)

includes/Pages/PageEmailManagement.php (1 issue)

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

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