| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Packetery\Module\Views; |
| 6 |
|
| 7 |
use Packetery\Latte\Engine; |
| 8 |
use Packetery\Module\Carrier\CarrierOptionsFactory; |
| 9 |
use Packetery\Module\ContextResolver; |
| 10 |
use Packetery\Module\Framework\WpAdapter; |
| 11 |
use Packetery\Module\ModuleHelper; |
| 12 |
use Packetery\Module\Order; |
| 13 |
use Packetery\Module\WcLogger; |
| 14 |
use WC_Order; |
| 15 |
|
| 16 |
use function __; |
| 17 |
|
| 18 |
class ViewAdmin { |
| 19 |
|
| 20 |
/** |
| 21 |
* @var ContextResolver |
| 22 |
*/ |
| 23 |
private $contextResolver; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var Engine |
| 27 |
*/ |
| 28 |
private $latteEngine; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var WpAdapter |
| 32 |
*/ |
| 33 |
private $wpAdapter; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Order\Repository |
| 37 |
*/ |
| 38 |
private $orderRepository; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var CarrierOptionsFactory |
| 42 |
*/ |
| 43 |
private $carrierOptionsFactory; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var ModuleHelper |
| 47 |
*/ |
| 48 |
private $moduleHelper; |
| 49 |
|
| 50 |
public function __construct( |
| 51 |
ContextResolver $contextResolver, |
| 52 |
Engine $latteEngine, |
| 53 |
WpAdapter $wpAdapter, |
| 54 |
Order\Repository $orderRepository, |
| 55 |
CarrierOptionsFactory $carrierOptionsFactory, |
| 56 |
ModuleHelper $moduleHelper |
| 57 |
) { |
| 58 |
$this->contextResolver = $contextResolver; |
| 59 |
$this->latteEngine = $latteEngine; |
| 60 |
$this->wpAdapter = $wpAdapter; |
| 61 |
$this->orderRepository = $orderRepository; |
| 62 |
$this->carrierOptionsFactory = $carrierOptionsFactory; |
| 63 |
$this->moduleHelper = $moduleHelper; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Renders delivery detail for packetery orders. |
| 68 |
* |
| 69 |
* @param WC_Order|mixed $wcOrder WordPress order. |
| 70 |
*/ |
| 71 |
public function renderDeliveryDetail( $wcOrder ): void { |
| 72 |
if ( ! $wcOrder instanceof WC_Order ) { |
| 73 |
WcLogger::logArgumentTypeError( __METHOD__, 'wcOrder', WC_Order::class, $wcOrder ); |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$order = $this->orderRepository->getByWcOrderWithValidCarrier( $wcOrder ); |
| 79 |
if ( $order === null ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$carrierId = $order->getCarrier()->getId(); |
| 84 |
$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrierId ); |
| 85 |
|
| 86 |
$this->latteEngine->render( |
| 87 |
PACKETERY_PLUGIN_DIR . '/template/order/delivery-detail.latte', |
| 88 |
[ |
| 89 |
'pickupPoint' => $order->getPickupPoint(), |
| 90 |
'validatedDeliveryAddress' => $order->getValidatedDeliveryAddress(), |
| 91 |
'carrierAddressValidation' => $carrierOptions->getAddressValidation(), |
| 92 |
'isExternalCarrier' => $order->isExternalCarrier(), |
| 93 |
'translations' => [ |
| 94 |
'packeta' => $this->wpAdapter->__( 'Packeta', 'packeta' ), |
| 95 |
'pickupPointDetail' => $this->wpAdapter->__( 'Pickup Point Detail', 'packeta' ), |
| 96 |
'name' => $this->wpAdapter->__( 'Name', 'packeta' ), |
| 97 |
'address' => $this->wpAdapter->__( 'Address', 'packeta' ), |
| 98 |
'pickupPointDetailCaps' => $this->wpAdapter->__( 'Pickup Point Detail', 'packeta' ), |
| 99 |
'addressWasNotValidated' => $this->wpAdapter->__( 'Address was not validated', 'packeta' ), |
| 100 |
], |
| 101 |
] |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Renders confirm modal template. |
| 107 |
*/ |
| 108 |
public function renderConfirmModalTemplate(): void { |
| 109 |
if ( ! $this->contextResolver->isConfirmModalPage() ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
$this->latteEngine->render( |
| 113 |
PACKETERY_PLUGIN_DIR . '/template/confirm-modal-template.latte', |
| 114 |
[ |
| 115 |
'translations' => [ |
| 116 |
'closeModalPanel' => $this->wpAdapter->__( 'Close modal panel', 'packeta' ), |
| 117 |
'no' => $this->wpAdapter->__( 'No', 'packeta' ), |
| 118 |
'yes' => $this->wpAdapter->__( 'Yes', 'packeta' ), |
| 119 |
], |
| 120 |
] |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Print inactive WooCommerce notice. |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function echoInactiveWooCommerceNotice(): void { |
| 130 |
if ( $this->moduleHelper->isWooCommercePluginActive() === true ) { |
| 131 |
// When Packeta plugin is active and WooCommerce plugin is inactive. |
| 132 |
// If user decides to activate WooCommerce plugin then invalid notice will not be rendered. |
| 133 |
// Packeta plugin probably bootstraps twice in such case. |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$this->latteEngine->render( |
| 138 |
PACKETERY_PLUGIN_DIR . '/template/admin-notice.latte', |
| 139 |
[ |
| 140 |
'message' => [ |
| 141 |
'type' => 'error', |
| 142 |
'message' => __( 'Packeta plugin requires WooCommerce. Please install and activate it first.', 'packeta' ), |
| 143 |
], |
| 144 |
] |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|