| @@ -57,4 +57,147 @@ | ||
| 57 | 57 | item.querySelector('.ticket-file-upload__preview-status').textContent = 'خطا در آپلود'; |
| 58 | 58 | }); |
| 59 | 59 | }); |
| 60 | 60 | } |
| 61 | + | |
| 62 | +(function (window, document) { | |
| 63 | + 'use strict'; | |
| 64 | + | |
| 65 | + var sections = [ | |
| 66 | + { | |
| 67 | + prefix: 'dashboard', | |
| 68 | + label: 'پیشخوان وردپرس' | |
| 69 | + }, | |
| 70 | + { | |
| 71 | + prefix: 'host_panel', | |
| 72 | + label: 'کنترل پنل هاست' | |
| 73 | + } | |
| 74 | + ]; | |
| 75 | + | |
| 76 | + function showError(message) { | |
| 77 | + if (window.BasalamToast) { | |
| 78 | + window.BasalamToast.error(message); | |
| 79 | + } | |
| 80 | + } | |
| 81 | + | |
| 82 | + function field(form, name) { | |
| 83 | + return form.querySelector('[name="' + name + '"]'); | |
| 84 | + } | |
| 85 | + | |
| 86 | + function validateAccess(form) { | |
| 87 | + for (var i = 0; i < sections.length; i++) { | |
| 88 | + var section = sections[i]; | |
| 89 | + var loginUrl = field(form, section.prefix + '_login_url'); | |
| 90 | + var username = field(form, section.prefix + '_username'); | |
| 91 | + var password = field(form, section.prefix + '_password'); | |
| 92 | + if (!loginUrl || !username || !password) continue; | |
| 93 | + | |
| 94 | + var loginValue = loginUrl.value.trim(); | |
| 95 | + var usernameValue = username.value.trim(); | |
| 96 | + var passwordValue = password.value; | |
| 97 | + var hasAnyValue = loginValue !== '' || usernameValue !== '' || passwordValue !== ''; | |
| 98 | + if (!hasAnyValue) continue; | |
| 99 | + | |
| 100 | + if (usernameValue === '' || passwordValue === '') { | |
| 101 | + var missingField = usernameValue === '' ? username : password; | |
| 102 | + missingField.setAttribute('aria-invalid', 'true'); | |
| 103 | + missingField.focus(); | |
| 104 | + showError('برای ' + section.label + ' نام کاربری و رمز عبور را کامل وارد کنید.'); | |
| 105 | + return false; | |
| 106 | + } | |
| 107 | + | |
| 108 | + if (passwordValue.length > 1024 || /[\u0000-\u001F\u007F]/.test(passwordValue)) { | |
| 109 | + password.setAttribute('aria-invalid', 'true'); | |
| 110 | + password.focus(); | |
| 111 | + showError(passwordValue.length > 1024 | |
| 112 | + ? 'رمز عبور طولانیتر از حد مجاز است.' | |
| 113 | + : 'رمز عبور دارای نویسه کنترلی نامعتبر است.'); | |
| 114 | + return false; | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + return true; | |
| 119 | + } | |
| 120 | + | |
| 121 | + function setSubmitting(form, submitting) { | |
| 122 | + var submitButton = form.querySelector('button[type="submit"]'); | |
| 123 | + if (!submitButton) return; | |
| 124 | + | |
| 125 | + submitButton.disabled = submitting; | |
| 126 | + if (submitting) { | |
| 127 | + submitButton.dataset.originalText = submitButton.textContent; | |
| 128 | + submitButton.textContent = 'در حال ارسال...'; | |
| 129 | + } else if (submitButton.dataset.originalText) { | |
| 130 | + submitButton.textContent = submitButton.dataset.originalText; | |
| 131 | + delete submitButton.dataset.originalText; | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + function rememberCreatedTicket(form, ticketId) { | |
| 136 | + if (!ticketId) return; | |
| 137 | + | |
| 138 | + var input = field(form, 'existing_ticket_id'); | |
| 139 | + if (!input) { | |
| 140 | + input = document.createElement('input'); | |
| 141 | + input.type = 'hidden'; | |
| 142 | + input.name = 'existing_ticket_id'; | |
| 143 | + form.appendChild(input); | |
| 144 | + } | |
| 145 | + input.value = String(ticketId); | |
| 146 | + } | |
| 147 | + | |
| 148 | + function submitCreateTicket(form) { | |
| 149 | + setSubmitting(form, true); | |
| 150 | + | |
| 151 | + fetch(window.ajaxurl, { | |
| 152 | + method: 'POST', | |
| 153 | + body: new FormData(form), | |
| 154 | + credentials: 'same-origin' | |
| 155 | + }) | |
| 156 | + .then(function (response) { | |
| 157 | + return response.json(); | |
| 158 | + }) | |
| 159 | + .then(function (response) { | |
| 160 | + var data = response && response.data ? response.data : {}; | |
| 161 | + if (!response || !response.success) { | |
| 162 | + rememberCreatedTicket(form, data.ticket_id); | |
| 163 | + showError(data.message || 'خطایی در ثبت تیکت رخ داد. اطلاعات فرم حفظ شده است.'); | |
| 164 | + return; | |
| 165 | + } | |
| 166 | + | |
| 167 | + if (data.redirect_url) { | |
| 168 | + window.location.assign(data.redirect_url); | |
| 169 | + } | |
| 170 | + }) | |
| 171 | + .catch(function () { | |
| 172 | + showError('ارتباط با سرور برقرار نشد. اطلاعات فرم حفظ شده است.'); | |
| 173 | + }) | |
| 174 | + .finally(function () { | |
| 175 | + setSubmitting(form, false); | |
| 176 | + }); | |
| 177 | + } | |
| 178 | + | |
| 179 | + document.addEventListener('DOMContentLoaded', function () { | |
| 180 | + var actionInputs = document.querySelectorAll( | |
| 181 | + 'input[name="action"][value="create_ticket"], ' + | |
| 182 | + 'input[name="action"][value="create_ticket_item"]' | |
| 183 | + ); | |
| 184 | + | |
| 185 | + for (var i = 0; i < actionInputs.length; i++) { | |
| 186 | + var form = actionInputs[i].closest('form'); | |
| 187 | + if (!form || form.dataset.ticketAccessValidation === '1') continue; | |
| 188 | + form.dataset.ticketAccessValidation = '1'; | |
| 189 | + form.addEventListener('submit', function (event) { | |
| 190 | + if (!validateAccess(event.currentTarget)) { | |
| 191 | + event.preventDefault(); | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + | |
| 195 | + var action = field(event.currentTarget, 'action'); | |
| 196 | + if (action && action.value === 'create_ticket') { | |
| 197 | + event.preventDefault(); | |
| 198 | + submitCreateTicket(event.currentTarget); | |
| 199 | + } | |
| 200 | + }); | |
| 201 | + } | |
| 202 | + }); | |
| 203 | +})(window, document); | |