summaryrefslogtreecommitdiff
path: root/views/mods/mod_revocation/page_form.phtml
blob: aacd6f27b29058acaf9736b0fd354a1a69818bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
	/** @version 4.6.1 */

	declare(strict_types=1);

	/**
	 * Eigenständige Widerrufsformular-Seite (kein Popup).
	 * Wird von wpsgRevocation\Action::content_filter() auf der Seite
	 * mit der Option wpsg_page_widerrufsformular gerendert.
	 *
	 * @author: Claude
	 */

	namespace wpsgRevocation;

	use wpsg_ShopController;

	$arReason = Revocation::getReasons();

	// Vorbelegung des Kontaktfeldes für eingeloggte Kunden
	$contact_email = '';
	$contact_name = '';
	$customer_login_id = intval(\wpsg_ShopController::getShop()->callMod('wpsg_mod_kundenverwaltung', 'isLoggedIn'));

	if ($customer_login_id > 0) {

		$oCustomer = \wpsg_customer::getInstance($customer_login_id);
		$contact_email = $oCustomer->getEmail();
		$contact_name = trim($oCustomer->getFirstname().' '.$oCustomer->getName());

	}

	// Vorbelegung über GET-Parameter (z.B. aus Verlinkung in der Bestellbestätigungsmail)
	$get_order   = isset($_GET['wpsg_revo_order'])   ? \sanitize_text_field($_GET['wpsg_revo_order'])   : '';
	$get_product = isset($_GET['wpsg_revo_product']) ? \sanitize_text_field($_GET['wpsg_revo_product']) : '';
	$get_amount  = isset($_GET['wpsg_revo_amount'])  ? max(1, intval($_GET['wpsg_revo_amount']))        : 1;
	$get_contact = isset($_GET['wpsg_revo_contact']) ? \sanitize_email($_GET['wpsg_revo_contact'])      : '';
	$get_key     = isset($_GET['wpsg_revo_key'])     ? \sanitize_key($_GET['wpsg_revo_key'])           : ''; // Schlüssel aus der Bestellmail (S18)

	if ($contact_email === '' && $get_contact !== '' && \is_email($get_contact)) $contact_email = $get_contact;

	$key = 0;

?>

<div class="wpsg wpsg_revocation_page">

	<form id="wpsg_revocation_page_form" class="wpsg">

		<div id="wpsg_revocation_page_msg"></div>

		<fieldset class="wpsg_revocation_order_product">

			<div class="form_field">
				<label class="label" for="wpsg_rvp_product_nr"><?php echo \__('Artikel - Nr.', 'wpsg'); ?></label>
				<div class="value">
					<input type="text" id="wpsg_rvp_product_nr" name="wpsg_revocation_reason[row][<?php echo $key; ?>][product_nr]" value="<?php echo \esc_attr($get_product); ?>" />
				</div>
			</div>

			<div class="form_field">
				<label class="label" for="wpsg_rvp_order_nr"><?php echo \__('Bestellnummer / Menge', 'wpsg'); ?></label>
				<div class="value">
					<input type="text" id="wpsg_rvp_order_nr" name="wpsg_revocation_reason[row][<?php echo $key; ?>][order_nr]" value="<?php echo \esc_attr($get_order); ?>" />
					<input type="number" min="1" aria-label="<?php echo \esc_attr__('Menge', 'wpsg'); ?>" name="wpsg_revocation_reason[row][<?php echo $key; ?>][amount]" value="<?php echo intval($get_amount); ?>" />
				</div>
			</div>

			<div class="form_field">
				<label class="label" for="wpsg_rvp_reason"><?php echo \__('Widerrufsgrund', 'wpsg'); ?></label>
				<div class="value">
					<select id="wpsg_rvp_reason" name="wpsg_revocation_reason[row][<?php echo $key; ?>][reason]">
						<option value=""><?php echo __("Keinen Grund angeben", 'wpsg') ?></option>
						<?php foreach ($arReason as $reason) { ?>
							<option value="<?php echo \htmlspecialchars($reason['id']); ?>"><?php echo \htmlspecialchars($reason['text']); ?></option>
						<?php } ?>
					</select>
				</div>
			</div>

			<div class="form_field">
				<label class="label" for="wpsg_rvp_comment"><?php echo \__('Bemerkung', 'wpsg'); ?></label>
				<div class="value">
					<textarea id="wpsg_rvp_comment" name="wpsg_revocation_reason[row][<?php echo $key; ?>][comment]"></textarea>
				</div>
			</div>

		</fieldset>

		<?php /* Bot-Falle (S21): für Menschen und Screenreader unsichtbar, nicht in der Tab-Reihenfolge */ ?>
		<div class="wpsg_revocation_hp" aria-hidden="true">
			<label class="label" for="wpsg_rvp_check"><?php echo \__('Bitte leer lassen', 'wpsg'); ?></label>
			<input type="text" id="wpsg_rvp_check" name="wpsg_revocation_reason[wpsg_rv_check]" value="" tabindex="-1" autocomplete="off" />
		</div>

		<div class="form_field">
			<label class="label" for="wpsg_rvp_name"><?php echo \__('Name', 'wpsg'); ?></label>
			<div class="value">
				<input type="text" required id="wpsg_rvp_name" autocomplete="name" name="wpsg_revocation_reason[name]" value="<?php echo \esc_attr($contact_name); ?>" />
			</div>
		</div>

		<input type="hidden" name="wpsg_revocation_reason[key]" value="<?php echo \esc_attr($get_key); ?>" />

		<div class="form_field">
			<label class="label" for="wpsg_rvp_contact"><?php echo \__('Kontakt - E-Mail', 'wpsg'); ?></label>
			<div class="value">
				<input type="email" required id="wpsg_rvp_contact" autocomplete="email" name="wpsg_revocation_reason[contact]" value="<?php echo \esc_attr($contact_email); ?>"
					<?php echo (($customer_login_id > 0 && $contact_email !== '') ? 'readonly' : ''); ?>
				/>
			</div>
		</div>

		<p class="wpsg_revocation_page_datenschutz">
			<?php echo \__('Es gelten unsere', 'wpsg'); ?>
			<a href="<?php echo \esc_url(\wpsg_ShopController::getShop()->getURL(wpsg_ShopController::URL_DATENSCHUTZ)); ?>"><?php echo \__('Datenschutzbestimmungen', 'wpsg'); ?></a>.
		</p>

		<div class="form_field wpsg_revocation_page_submit_wrap">
			<button type="submit" class="button button-primary wpsg_revocation_page_submit"><?php echo \__('Widerruf bestätigen', 'wpsg'); ?></button>
		</div>

	</form>

</div>

<script>
(function () {

	const form = document.getElementById('wpsg_revocation_page_form');
	if (form === null) return;

	// Kann aus dem Seiten-Cache kommen; ist sie abgelaufen, schickt der Server eine frische mit (K48)
	let nonce      = <?php echo \wp_json_encode(\wp_create_nonce('wpsg_mod_revocation_nonce')); ?>;
	const endpoint = '<?php echo \admin_url('admin-ajax.php'); ?>?action=wpsg_mod_revocation&do=ajaxDialogSubmit';
	const msg      = document.getElementById('wpsg_revocation_page_msg');
	const button   = form.querySelector('.wpsg_revocation_page_submit');

	const formDataToObject = (formData) => {

		const obj = {};

		for (const [key, value] of formData.entries()) {

			const keys = key.split(/\[|\]/).filter(Boolean);
			let current = obj;

			keys.forEach((k, i) => {

				const isLast = i === keys.length - 1;

				if (isLast) { current[k] = value; }
				else {

					if (!current[k]) { current[k] = isNaN(keys[i + 1]) ? {} : []; }
					current = current[k];

				}

			});

		}

		return obj;

	};

	form.addEventListener('submit', event => {

		event.preventDefault();

		msg.innerHTML = '';
		button.disabled = true;

		const form_data = formDataToObject(new FormData(form));

		const post = retry => fetch(endpoint, {
			method: 'POST',
			credentials: 'same-origin',
			headers: { 'Content-Type': 'application/json' },
			body: JSON.stringify({ nonce: nonce, form_data: form_data })
		}).then(res => res.json().then(json => {

			if (res.status === 403 && retry && (json?.data?.code ?? '') === 'wpsg_nonce') {

				nonce = json.data.nonce;
				return post(false);

			}

			if (!res.ok) throw new Error(res.status);

			return json;

		}));

		post(true).then(res => {

			button.disabled = false;

			if ((res.success ?? false) === false) {

				// Meldungen des Servers als Text, Screenreader sagen sie an
				const list = document.createElement('ul');
				list.id = 'wpsg_error';
				list.className = 'wpsg_error';
				list.setAttribute('role', 'alert');
				(res.arError ?? []).forEach(error => { const li = document.createElement('li'); li.textContent = error; list.appendChild(li); });
				msg.replaceChildren(list);
				msg.scrollIntoView({ behavior: 'smooth', block: 'center' });

			} else {

				form.style.display = 'none';
				msg.innerHTML = '<div class="wpsg_revocation_page_success"><?php echo \esc_js(\__('Ihre Widerrufserklärung ist eingegangen. Die Eingangsbestätigung geht an die angegebene E-Mail-Adresse.', 'wpsg')); ?></div>';
				msg.scrollIntoView({ behavior: 'smooth', block: 'center' });

			}

		}).catch(() => {

			button.disabled = false;
			msg.innerHTML = '<ul id="wpsg_error" class="wpsg_error"><li><?php echo \esc_js(\__('Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.', 'wpsg')); ?></li></ul>';

		});

	});

})();
</script>