| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Forms; |
| 6 |
|
| 7 |
use Packetery\Module\Email\BugReportEmail; |
| 8 |
use Packetery\Module\Framework\WpAdapter; |
| 9 |
use Packetery\Module\MessageManager; |
| 10 |
use Packetery\Nette\Forms\Form; |
| 11 |
|
| 12 |
class BugReportForm { |
| 13 |
|
| 14 |
/** @var WpAdapter */ |
| 15 |
private $wpAdapter; |
| 16 |
|
| 17 |
/** @var MessageManager */ |
| 18 |
private $messageManager; |
| 19 |
|
| 20 |
/** @var BugReportEmail */ |
| 21 |
private $bugReportEmail; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
WpAdapter $wpAdapter, |
| 25 |
MessageManager $messageManager, |
| 26 |
BugReportEmail $bugReportEmail |
| 27 |
) { |
| 28 |
$this->wpAdapter = $wpAdapter; |
| 29 |
$this->messageManager = $messageManager; |
| 30 |
$this->bugReportEmail = $bugReportEmail; |
| 31 |
} |
| 32 |
|
| 33 |
public function createForm(): Form { |
| 34 |
$form = new Form(); |
| 35 |
|
| 36 |
$adminEmail = $this->wpAdapter->getOption( 'admin_email' ); |
| 37 |
$form->addEmail( 'replyTo', $this->wpAdapter->__( 'Email for reply', 'packeta' ) ) |
| 38 |
->setDefaultValue( is_string( $adminEmail ) ? $adminEmail : '' ) |
| 39 |
->setRequired( $this->wpAdapter->__( 'Email is required.', 'packeta' ) ) |
| 40 |
->addRule( Form::EMAIL, $this->wpAdapter->__( 'Please enter a valid email address.', 'packeta' ) ); |
| 41 |
|
| 42 |
// Cannot be required because of the WYSIWYG editor. |
| 43 |
$form->addTextArea( 'message', $this->wpAdapter->__( 'Message', 'packeta' ) ); |
| 44 |
|
| 45 |
$form->addCheckbox( 'sendCopy', $this->wpAdapter->__( 'Send copy to sender', 'packeta' ) ); |
| 46 |
|
| 47 |
$form->addSubmit( 'submit', $this->wpAdapter->__( 'Send', 'packeta' ) ); |
| 48 |
|
| 49 |
$form->onSuccess[] = [ $this, 'onFormSuccess' ]; |
| 50 |
$form->onError[] = [ $this, 'onFormError' ]; |
| 51 |
$form->onValidate[] = [ $this, 'onFormValidate' ]; |
| 52 |
|
| 53 |
return $form; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param Form $form Form instance. |
| 58 |
* @param array<string, mixed>|object $values Form values (can be ArrayHash from Nette forms). |
| 59 |
*/ |
| 60 |
public function onFormSuccess( Form $form, $values ): void { |
| 61 |
$data = $this->normalizeFormValues( $values ); |
| 62 |
$email = $this->wpAdapter->sanitizeEmail( isset( $data['replyTo'] ) && is_string( $data['replyTo'] ) ? $data['replyTo'] : '' ); |
| 63 |
$message = $this->wpAdapter->wpKsesPost( isset( $data['message'] ) && is_string( $data['message'] ) ? $data['message'] : '' ); |
| 64 |
$sendCopy = isset( $data['sendCopy'] ) && is_bool( $data['sendCopy'] ) && $data['sendCopy']; |
| 65 |
|
| 66 |
$result = $this->bugReportEmail->sendBugReport( $email, $message, $sendCopy ); |
| 67 |
|
| 68 |
if ( $result === true ) { |
| 69 |
$this->messageManager->flash_message( |
| 70 |
$this->wpAdapter->__( 'Bug report sent successfully.', 'packeta' ), |
| 71 |
MessageManager::TYPE_SUCCESS, |
| 72 |
MessageManager::RENDERER_PACKETERY, |
| 73 |
'plugin-options' |
| 74 |
); |
| 75 |
} else { |
| 76 |
$this->messageManager->flash_message( |
| 77 |
$this->wpAdapter->__( 'Failed to send bug report. Please try again.', 'packeta' ), |
| 78 |
MessageManager::TYPE_ERROR, |
| 79 |
MessageManager::RENDERER_PACKETERY, |
| 80 |
'plugin-options' |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public function onFormError( Form $form ): void { |
| 86 |
foreach ( $form->getErrors() as $error ) { |
| 87 |
$this->messageManager->flash_message( |
| 88 |
(string) $error, |
| 89 |
MessageManager::TYPE_ERROR, |
| 90 |
MessageManager::RENDERER_PACKETERY, |
| 91 |
'plugin-options' |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Normalizes form values to array format. Handles both arrays and objects (including ArrayHash from Nette forms). |
| 98 |
* |
| 99 |
* @param array<string, mixed>|object $values Form values. |
| 100 |
* @return array<string, mixed> |
| 101 |
*/ |
| 102 |
private function normalizeFormValues( $values ): array { |
| 103 |
if ( is_array( $values ) ) { |
| 104 |
return $values; |
| 105 |
} |
| 106 |
if ( is_object( $values ) ) { |
| 107 |
return (array) $values; |
| 108 |
} |
| 109 |
|
| 110 |
return []; |
| 111 |
} |
| 112 |
|
| 113 |
public function onFormValidate( Form $form ): void { |
| 114 |
$valuesArray = $this->normalizeFormValues( $form->getValues() ); |
| 115 |
$message = isset( $valuesArray['message'] ) && is_string( $valuesArray['message'] ) ? $valuesArray['message'] : ''; |
| 116 |
|
| 117 |
if ( trim( $this->wpAdapter->wpStripAllTags( $message ) ) === '' ) { |
| 118 |
$form->addError( $this->wpAdapter->__( 'Message is required.', 'packeta' ) ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( $form->hasErrors() === true ) { |
| 122 |
$this->messageManager->flash_message( |
| 123 |
implode( ', ', $form->getErrors() ), |
| 124 |
MessageManager::TYPE_ERROR, |
| 125 |
MessageManager::RENDERER_PACKETERY, |
| 126 |
'plugin-options' |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|