Added phrases and email address validation
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / ContactRecipientAddForm.class.php
CommitLineData
d486ae88
AE
1<?php
2namespace wcf\acp\form;
3use wcf\data\contact\recipient\ContactRecipient;
4use wcf\data\contact\recipient\ContactRecipientAction;
5use wcf\data\contact\recipient\ContactRecipientEditor;
6use wcf\form\AbstractForm;
5bf4e2fe 7use wcf\system\email\Mailbox;
d486ae88
AE
8use wcf\system\exception\UserInputException;
9use wcf\system\language\I18nHandler;
10use wcf\system\WCF;
11
12/**
13 * Shows the form to create a new contact form recipient.
14 *
15 * @author Alexander Ebert
16 * @copyright 2001-2017 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\Acp\Form
19 */
20class ContactRecipientAddForm extends AbstractForm {
21 /**
22 * @inheritDoc
23 */
24 public $activeMenuItem = 'wcf.acp.menu.link.contact.settings';
25
26 /**
27 * @inheritDoc
28 */
29 public $neededModules = ['MODULE_CONTACT_FORM'];
30
31 /**
32 * @inheritDoc
33 */
34 public $neededPermissions = ['admin.contact.canManageContactForm'];
35
36 /**
37 * email address
38 * @var string
39 */
40 public $email = '';
41
42 /**
43 * display name
44 * @var string
45 */
46 public $name = '';
47
48 /**
49 * 1 if the recipient is disabled
50 * @var integer
51 */
52 public $isDisabled = 0;
53
54 /**
55 * order used to the show the recipients
56 * @var integer
57 */
58 public $showOrder = 0;
59
60 /**
61 * @inheritDoc
62 */
63 public function readParameters() {
64 parent::readParameters();
65
66 I18nHandler::getInstance()->register('email');
67 I18nHandler::getInstance()->register('name');
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function readFormParameters() {
74 parent::readFormParameters();
75
76 I18nHandler::getInstance()->readValues();
77
78 if (I18nHandler::getInstance()->isPlainValue('email')) $this->email = I18nHandler::getInstance()->getValue('email');
79 if (I18nHandler::getInstance()->isPlainValue('name')) $this->name = I18nHandler::getInstance()->getValue('name');
80
81 if (isset($_POST['isDisabled'])) $this->isDisabled = intval($_POST['isDisabled']);
82 if (isset($_POST['showOrder'])) $this->showOrder = intval($_POST['showOrder']);
83 }
84
85 /**
86 * @inheritDoc
87 */
88 public function validate() {
89 parent::validate();
90
91 if (!I18nHandler::getInstance()->validateValue('email')) {
92 if (I18nHandler::getInstance()->isPlainValue('email')) {
93 throw new UserInputException('email');
94 }
95 else {
96 throw new UserInputException('email', 'multilingual');
97 }
98 }
5bf4e2fe
AE
99 else {
100 foreach (I18nHandler::getInstance()->getValues('email') as $email) {
101 try {
102 new Mailbox($email);
103 }
104 catch (\DomainException $e) {
105 throw new UserInputException('email', 'invalid');
106 }
107 }
108 }
d486ae88
AE
109
110 if (!I18nHandler::getInstance()->validateValue('name')) {
111 if (I18nHandler::getInstance()->isPlainValue('name')) {
112 throw new UserInputException('name');
113 }
114 else {
115 throw new UserInputException('name', 'multilingual');
116 }
117 }
118 }
119
120 /**
121 * @inheritDoc
122 */
123 public function save() {
124 parent::save();
125
126 $this->objectAction = new ContactRecipientAction([], 'create', [
127 'data' => array_merge($this->additionalFields, [
128 'name' => $this->name,
129 'email' => $this->email,
130 'isDisabled' => ($this->isDisabled ? 1 : 0),
131 'showOrder' => $this->showOrder
132 ])
133 ]);
134 /** @var ContactRecipient $recipient */
135 $recipient = $this->objectAction->executeAction()['returnValues'];
136 $recipientID = $recipient->recipientID;
137 $data = [];
138
139 if (!I18nHandler::getInstance()->isPlainValue('email')) {
140 I18nHandler::getInstance()->save('email', 'wcf.contact.recipient.email'.$recipientID, 'wcf.contact', 1);
141
142 $data['email'] = 'wcf.contact.recipient.email'.$recipientID;
143 }
144 if (!I18nHandler::getInstance()->isPlainValue('name')) {
145 I18nHandler::getInstance()->save('name', 'wcf.contact.recipient.name'.$recipientID, 'wcf.contact', 1);
146
147 $data['name'] = 'wcf.contact.recipient.name'.$recipientID;
148 }
149
150 // update i18n values
151 if (!empty($data)) {
152 (new ContactRecipientEditor($recipient))->update($data);
153 }
154
155 $this->saved();
156
157 // show success message
158 WCF::getTPL()->assign('success', true);
159
160 // reset values
161 $this->email = $this->name = 0;
162 $this->isDisabled = $this->showOrder = 0;
163
164 I18nHandler::getInstance()->reset();
165 }
166
167 /**
168 * @inheritDoc
169 */
170 public function assignVariables() {
171 parent::assignVariables();
172
173 I18nHandler::getInstance()->assignVariables();
174
175 WCF::getTPL()->assign([
176 'action' => 'add',
177 'email' => $this->email,
178 'name' => $this->name,
179 'isDisabled' => $this->isDisabled,
180 'showOrder' => $this->showOrder
181 ]);
182 }
183}