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