PluginProbe
Packeta / 1.6.3
Packeta v1.6.3
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / GridExtender.php

GridExtender.php in Packeta 1.6.3, at src/Packetery/Module/Order/GridExtender.php

401 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class GridExtender.
4 *
5 * @package Packetery\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Core;
13 use Packetery\Module;
14 use Packetery\Module\Carrier;
15 use Packetery\Module\ContextResolver;
16 use Packetery\Module\Exception\InvalidCarrierException;
17 use Packetery\Module\Log\Purger;
18 use Packetery\Latte\Engine;
19 use Packetery\Nette\Http\Request;
20 use Packetery\Module\Plugin;
21 use WC_Order;
22
23 /**
24 * Class GridExtender.
25 *
26 * @package Packetery\Order
27 */
28 class GridExtender {
29
30 const TEMPLATE_GRID_COLUMN_WEIGHT = PACKETERY_PLUGIN_DIR . '/template/order/grid-column-weight.latte';
31
32 /**
33 * Generic Helper.
34 *
35 * @var Core\Helper
36 */
37 private $helper;
38
39 /**
40 * Carrier repository.
41 *
42 * @var Carrier\EntityRepository
43 */
44 private $carrierRepository;
45
46 /**
47 * Latte Engine.
48 *
49 * @var Engine
50 */
51 private $latteEngine;
52
53 /**
54 * Http Request.
55 *
56 * @var Request
57 */
58 private $httpRequest;
59
60 /**
61 * Order repository.
62 *
63 * @var Repository
64 */
65 private $orderRepository;
66
67 /**
68 * Order Validator.
69 *
70 * @var Core\Validator\Order
71 */
72 private $orderValidator;
73
74 /**
75 * Context resolver.
76 *
77 * @var ContextResolver
78 */
79 private $contextResolver;
80
81 /**
82 * GridExtender constructor.
83 *
84 * @param Core\Helper $helper Helper.
85 * @param Carrier\EntityRepository $carrierRepository Carrier repository.
86 * @param Engine $latteEngine Latte Engine.
87 * @param Request $httpRequest Http Request.
88 * @param Repository $orderRepository Order repository.
89 * @param Core\Validator\Order $orderValidator Order validator.
90 * @param ContextResolver $contextResolver Context resolver.
91 */
92 public function __construct(
93 Core\Helper $helper,
94 Carrier\EntityRepository $carrierRepository,
95 Engine $latteEngine,
96 Request $httpRequest,
97 Repository $orderRepository,
98 Core\Validator\Order $orderValidator,
99 ContextResolver $contextResolver
100 ) {
101 $this->helper = $helper;
102 $this->carrierRepository = $carrierRepository;
103 $this->latteEngine = $latteEngine;
104 $this->httpRequest = $httpRequest;
105 $this->orderRepository = $orderRepository;
106 $this->orderValidator = $orderValidator;
107 $this->contextResolver = $contextResolver;
108 }
109
110 /**
111 * Adds custom filtering links to order grid.
112 *
113 * @param array $var Array of html links.
114 *
115 * @return array
116 */
117 public function addFilterLinks( array $var ): array {
118 $latteParams = [
119 'link' => Module\Helper::getOrderGridUrl(
120 [
121 'filter_action' => 'packetery_filter_link',
122 'packetery_to_submit' => '1',
123 'packetery_to_print' => false,
124 ]
125 ),
126 'title' => __( 'Packeta orders to submit', 'packeta' ),
127 'orderCount' => $this->orderRepository->countOrdersToSubmit(),
128 'active' => ( $this->httpRequest->getQuery( 'packetery_to_submit' ) === '1' ),
129 ];
130 $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams );
131
132 $latteParams = [
133 'link' => Module\Helper::getOrderGridUrl(
134 [
135 'filter_action' => 'packetery_filter_link',
136 'packetery_to_submit' => false,
137 'packetery_to_print' => '1',
138 ]
139 ),
140 'title' => __( 'Packeta orders to print', 'packeta' ),
141 'orderCount' => $this->orderRepository->countOrdersToPrint(),
142 'active' => ( $this->httpRequest->getQuery( 'packetery_to_print' ) === '1' ),
143 ];
144 $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams );
145
146 return $var;
147 }
148
149 /**
150 * Adds select to order grid.
151 */
152 public function renderOrderTypeSelect(): void {
153 if ( false === $this->contextResolver->isOrderGridPage() ) {
154 return;
155 }
156
157 $linkFilters = [];
158
159 if ( null !== $this->httpRequest->getQuery( 'packetery_to_submit' ) ) {
160 $linkFilters['packetery_to_submit'] = '1';
161 }
162
163 if ( null !== $this->httpRequest->getQuery( 'packetery_to_print' ) ) {
164 $linkFilters['packetery_to_print'] = '1';
165 }
166
167 $this->latteEngine->render(
168 PACKETERY_PLUGIN_DIR . '/template/order/type-select.latte',
169 [
170 'packeteryOrderType' => $this->httpRequest->getQuery( 'packetery_order_type' ),
171 'linkFilters' => $linkFilters,
172 'translations' => [
173 'packetaMethodType' => __( 'Packeta shipping method', 'packeta' ),
174 'carrierPackets' => __( 'Carrier packets', 'packeta' ),
175 'packetaPickupPointPackets' => __( 'Packeta pickup points packets', 'packeta' ),
176 ],
177 ]
178 );
179 }
180
181 /**
182 * Gets weight cell content.
183 *
184 * @param Core\Entity\Order $order Order.
185 *
186 * @return string
187 */
188 public function getWeightCellContent( Core\Entity\Order $order ): string {
189 return $this->latteEngine->renderToString(
190 self::TEMPLATE_GRID_COLUMN_WEIGHT,
191 $this->getWeightCellContentParams( $order )
192 );
193 }
194
195 /**
196 * Gets weight cell content.
197 *
198 * @param Core\Entity\Order $order Order.
199 *
200 * @return array
201 */
202 private function getWeightCellContentParams( Core\Entity\Order $order ): array {
203 return [
204 'orderNumber' => $order->getNumber(),
205 'weight' => $order->getFinalWeight(),
206 ];
207 }
208
209 /**
210 * Get Order Entity from cache
211 *
212 * @param int $orderId Order ID.
213 *
214 * @return Core\Entity\Order|null
215 * @throws InvalidCarrierException InvalidCarrierException.
216 */
217 private function getOrderByIdCached( int $orderId ): ?Core\Entity\Order {
218 static $ordersCache;
219
220 if ( ! isset( $ordersCache[ $orderId ] ) ) {
221 $ordersCache[ $orderId ] = $this->orderRepository->getById( $orderId );
222 }
223
224 return $ordersCache[ $orderId ] ?? null;
225 }
226
227 /**
228 * Fills custom order list columns.
229 *
230 * @param string|mixed $column Current order column name.
231 * @param \WC_Order|int|mixed $wcOrder WC Order.
232 */
233 public function fillCustomOrderListColumns( $column, $wcOrder ): void {
234 if ( false === is_string( $column ) ) {
235 return;
236 }
237
238 if ( $wcOrder instanceof WC_Order ) {
239 $orderId = $wcOrder->get_id();
240 } elseif ( is_numeric( $wcOrder ) ) {
241 $orderId = (int) $wcOrder;
242 } else {
243 return;
244 }
245
246 try {
247 $order = $this->getOrderByIdCached( $orderId );
248 } catch ( InvalidCarrierException $exception ) {
249 if ( 'packetery' === $column ) {
250 Module\Helper::renderString( $exception->getMessage() );
251 }
252
253 return;
254 }
255 if ( null === $order ) {
256 return;
257 }
258
259 switch ( $column ) {
260 case 'packetery_weight':
261 $this->latteEngine->render(
262 self::TEMPLATE_GRID_COLUMN_WEIGHT,
263 $this->getWeightCellContentParams( $order )
264 );
265 break;
266 case 'packetery_destination':
267 $pickupPoint = $order->getPickupPoint();
268 if ( null !== $pickupPoint ) {
269 $pointName = $pickupPoint->getName();
270 $pointId = $pickupPoint->getId();
271 if ( ! $order->isExternalCarrier() ) {
272 echo esc_html( "$pointName ($pointId)" );
273 } else {
274 echo esc_html( $pointName );
275 }
276 break;
277 }
278
279 $homeDeliveryCarrierOptions = Carrier\Options::createByCarrierId( $order->getCarrier()->getId() );
280 echo esc_html( $homeDeliveryCarrierOptions->getName() );
281 break;
282 case 'packetery_packet_id':
283 $packetId = $order->getPacketId();
284 $packetClaimId = $order->getPacketClaimId();
285 $latteParams = [
286 'order' => $order,
287 'packetIdTrackingUrl' => null,
288 'packetClaimIdTrackingUrl' => null,
289 'translations' => [
290 'packetClaimTracking' => __( 'Packet claim tracking', 'packeta' ),
291 ],
292 ];
293
294 if ( $packetId ) {
295 $latteParams['packetIdTrackingUrl'] = $this->helper->get_tracking_url( $packetId );
296 }
297
298 if ( $packetClaimId ) {
299 $latteParams['packetClaimIdTrackingUrl'] = $this->helper->get_tracking_url( $packetClaimId );
300 }
301
302 $this->latteEngine->render(
303 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-tracking.latte',
304 $latteParams
305 );
306 break;
307 case 'packetery':
308 $encodedOrderGridParams = rawurlencode( $this->httpRequest->getUrl()->getQuery() );
309 $packetSubmitUrl = add_query_arg(
310 [
311 PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(),
312 Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_SUBMIT_PACKET,
313 PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID,
314 Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( PacketActionsCommonLogic::ACTION_SUBMIT_PACKET, $order->getNumber() ) ),
315 PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams,
316 ],
317 admin_url( 'admin.php' )
318 );
319 $printLink = add_query_arg(
320 [
321 'page' => LabelPrint::MENU_SLUG,
322 LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ),
323 'id' => $order->getNumber(),
324 'packet_id' => $order->getPacketId(),
325 'offset' => 0,
326 PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams,
327 ],
328 admin_url( 'admin.php' )
329 );
330 $packetCancelLink = add_query_arg(
331 [
332 PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(),
333 PacketActionsCommonLogic::PARAM_PACKET_ID => $order->getPacketId(),
334 Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_CANCEL_PACKET,
335 PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID,
336 Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( PacketActionsCommonLogic::ACTION_CANCEL_PACKET, $order->getNumber() ) ),
337 PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams,
338 ],
339 admin_url( 'admin.php' )
340 );
341
342 $this->latteEngine->render(
343 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte',
344 [
345 'order' => $order,
346 'orderIsSubmittable' => $this->orderValidator->isValid( $order ),
347 'packetSubmitUrl' => $packetSubmitUrl,
348 'packetCancelLink' => $packetCancelLink,
349 'printLink' => $printLink,
350 'helper' => new Core\Helper(),
351 'datePickerFormat' => Core\Helper::DATEPICKER_FORMAT,
352 'logPurgerDatetimeModifier' => get_option( Purger::PURGER_OPTION_NAME, Purger::PURGER_MODIFIER_DEFAULT ),
353 'packetDeliverOn' => $this->helper->getStringFromDateTime( $order->getDeliverOn(), Core\Helper::DATEPICKER_FORMAT ),
354 'translations' => [
355 'printLabel' => __( 'Print label', 'packeta' ),
356 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ),
357 'submitToPacketa' => __( 'Submit to packeta', 'packeta' ),
358 // translators: %s: Order number.
359 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ),
360 // translators: %s: Packet number.
361 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ),
362
363 'cancelPacket' => __( 'Cancel packet', 'packeta' ),
364 'lastErrorFromApi' => __( 'Last error from Packeta API', 'packeta' ),
365 ],
366 ]
367 );
368 break;
369 case 'packetery_packet_status':
370 $statuses = PacketSynchronizer::getPacketStatuses();
371 echo esc_html( isset( $statuses[ $order->getPacketStatus() ] ) ? $statuses[ $order->getPacketStatus() ]->getTranslatedName() : $order->getPacketStatus() );
372 break;
373 }
374 }
375
376 /**
377 * Add order list columns.
378 *
379 * @param string[] $columns Order list columns.
380 *
381 * @return string[] All columns.
382 */
383 public function addOrderListColumns( array $columns ): array {
384 $new_columns = array();
385
386 foreach ( $columns as $column_name => $column_info ) {
387 $new_columns[ $column_name ] = $column_info;
388
389 if ( 'order_total' === $column_name ) {
390 $new_columns['packetery_weight'] = __( 'Weight', 'packeta' );
391 $new_columns['packetery'] = __( 'Packeta', 'packeta' );
392 $new_columns['packetery_packet_id'] = __( 'Tracking No.', 'packeta' );
393 $new_columns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' );
394 $new_columns['packetery_destination'] = __( 'Pickup point or carrier', 'packeta' );
395 }
396 }
397
398 return $new_columns;
399 }
400 }
401