| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Order; |
| 6 |
|
| 7 |
use Packetery\Latte\Engine; |
| 8 |
use Packetery\Module; |
| 9 |
use Packetery\Module\Labels\LabelPrintParametersService; |
| 10 |
use Packetery\Module\Options\OptionsProvider; |
| 11 |
use Packetery\Nette\Forms\Controls\SubmitButton; |
| 12 |
use Packetery\Nette\Forms\Form; |
| 13 |
|
| 14 |
class LabelPrintModal { |
| 15 |
|
| 16 |
public const MODAL_ID_PACKET = 'wc-packetery-order-detail-packet-label-print-modal'; |
| 17 |
public const MODAL_ID_PACKET_CLAIM = 'wc-packetery-order-detail-packet-claim-label-print-modal'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Engine |
| 21 |
*/ |
| 22 |
private $latteEngine; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var LabelPrintParametersService |
| 26 |
*/ |
| 27 |
private $labelPrintParametersService; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var OptionsProvider |
| 31 |
*/ |
| 32 |
private $optionsProvider; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var Module\ContextResolver |
| 36 |
*/ |
| 37 |
private $contextResolver; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var DetailCommonLogic |
| 41 |
*/ |
| 42 |
private $detailCommonLogic; |
| 43 |
|
| 44 |
public function __construct( |
| 45 |
Engine $latteEngine, |
| 46 |
LabelPrintParametersService $labelPrintParametersService, |
| 47 |
OptionsProvider $optionsProvider, |
| 48 |
Module\ContextResolver $contextResolver, |
| 49 |
DetailCommonLogic $detailCommonLogic |
| 50 |
) { |
| 51 |
$this->latteEngine = $latteEngine; |
| 52 |
$this->labelPrintParametersService = $labelPrintParametersService; |
| 53 |
$this->optionsProvider = $optionsProvider; |
| 54 |
$this->contextResolver = $contextResolver; |
| 55 |
$this->detailCommonLogic = $detailCommonLogic; |
| 56 |
} |
| 57 |
|
| 58 |
public function register(): void { |
| 59 |
add_action( 'admin_head', [ $this, 'renderPacketModal' ] ); |
| 60 |
add_action( 'admin_head', [ $this, 'renderPacketClaimModal' ] ); |
| 61 |
} |
| 62 |
|
| 63 |
public function renderPacketModal(): void { |
| 64 |
if ( $this->contextResolver->isOrderDetailPage() === false ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$order = $this->detailCommonLogic->getOrder(); |
| 69 |
if ( $order === null || $order->getPacketId() === null ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$this->renderModal( |
| 74 |
self::MODAL_ID_PACKET, |
| 75 |
$this->labelPrintParametersService->getLabelFormatByOrder( $order ), |
| 76 |
$order->getPacketId() |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public function renderPacketClaimModal(): void { |
| 81 |
if ( $this->contextResolver->isOrderDetailPage() === false ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$order = $this->detailCommonLogic->getOrder(); |
| 86 |
if ( $order === null || $order->getPacketClaimId() === null ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$this->renderModal( |
| 91 |
self::MODAL_ID_PACKET_CLAIM, |
| 92 |
$this->labelPrintParametersService->getLabelFormatByOrder( $order ), |
| 93 |
$order->getPacketClaimId() |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
private function renderModal( string $id, string $format, string $packetId ): void { |
| 98 |
$form = $this->createForm( sprintf( '%s_form', $id ), $format, $packetId ); |
| 99 |
$submitButton = $form['submit']; |
| 100 |
if ( $submitButton instanceof SubmitButton && |
| 101 |
$submitButton->isSubmittedBy() |
| 102 |
) { |
| 103 |
$form->fireEvents(); |
| 104 |
} |
| 105 |
|
| 106 |
$this->latteEngine->render( |
| 107 |
PACKETERY_PLUGIN_DIR . '/template/order/label-print-modal.latte', |
| 108 |
[ |
| 109 |
'id' => $id, |
| 110 |
'form' => $form, |
| 111 |
'translations' => [ |
| 112 |
// translators: %s is packet ID. |
| 113 |
'header' => sprintf( __( 'Label print of packet %s', 'packeta' ), $packetId ), |
| 114 |
|
| 115 |
'closeModalPanel' => __( 'Close modal panel', 'packeta' ), |
| 116 |
], |
| 117 |
] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
public function createForm( string $name, string $format, string $packetId ): Form { |
| 122 |
$form = $this->labelPrintParametersService->createForm( $this->optionsProvider->getLabelMaxOffset( $format ), $name ); |
| 123 |
$form->addHidden( 'packet_id' )->setDefaultValue( $packetId ); |
| 124 |
$form->addSubmit( 'submit', __( 'Print', 'packeta' ) ); |
| 125 |
$form->addSubmit( 'cancel', __( 'Cancel', 'packeta' ) ); |
| 126 |
|
| 127 |
$form->onSuccess[] = [ $this, 'onFormSuccess' ]; |
| 128 |
|
| 129 |
return $form; |
| 130 |
} |
| 131 |
|
| 132 |
public function onFormSuccess( Form $form ): void { |
| 133 |
$order = $this->detailCommonLogic->getOrder(); |
| 134 |
if ( $order === null ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$values = $form->getValues(); |
| 139 |
$printLink = add_query_arg( |
| 140 |
[ |
| 141 |
'page' => LabelPrint::MENU_SLUG, |
| 142 |
LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ), |
| 143 |
'id' => $order->getNumber(), |
| 144 |
PacketActionsCommonLogic::PARAM_PACKET_ID => $values['packet_id'], |
| 145 |
'offset' => $values['offset'], |
| 146 |
PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL, |
| 147 |
], |
| 148 |
admin_url( 'admin.php' ) |
| 149 |
); |
| 150 |
|
| 151 |
if ( wp_safe_redirect( $printLink ) ) { |
| 152 |
exit; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|