PluginProbe
Packeta / 1.4
Packeta v1.4
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.4, at src/Packetery/Module/Order/GridExtender.php

363 lines 10.4 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\Core\Entity;
14 use Packetery\Core\Helper;
15 use Packetery\Module\Carrier;
16 use PacketeryLatte\Engine;
17 use PacketeryNette\Http\Request;
18 use Packetery\Module\Plugin;
19
20 /**
21 * Class GridExtender.
22 *
23 * @package Packetery\Order
24 */
25 class GridExtender {
26
27 const TEMPLATE_GRID_COLUMN_WEIGHT = PACKETERY_PLUGIN_DIR . '/template/order/grid-column-weight.latte';
28
29 /**
30 * Order entity cache.
31 *
32 * @var Entity\Order|null
33 */
34 public static $orderCache;
35
36 /**
37 * Generic Helper.
38 *
39 * @var Helper
40 */
41 private $helper;
42
43 /**
44 * Carrier repository.
45 *
46 * @var Carrier\Repository
47 */
48 private $carrierRepository;
49
50 /**
51 * Latte Engine.
52 *
53 * @var Engine
54 */
55 private $latteEngine;
56
57 /**
58 * Http Request.
59 *
60 * @var Request
61 */
62 private $httpRequest;
63
64 /**
65 * Controller router.
66 *
67 * @var ControllerRouter
68 */
69 private $orderControllerRouter;
70
71 /**
72 * Order repository.
73 *
74 * @var Repository
75 */
76 private $orderRepository;
77
78 /**
79 * Modal.
80 *
81 * @var Modal
82 */
83 private $modal;
84
85 /**
86 * Packet synchronizer.
87 *
88 * @var PacketSynchronizer
89 */
90 private $packetSynchronizer;
91
92 /**
93 * GridExtender constructor.
94 *
95 * @param Helper $helper Helper.
96 * @param Carrier\Repository $carrierRepository Carrier repository.
97 * @param Engine $latteEngine Latte Engine.
98 * @param Request $httpRequest Http Request.
99 * @param ControllerRouter $orderControllerRouter Order controller router.
100 * @param Repository $orderRepository Order repository.
101 * @param Modal $modal Modal dialog.
102 * @param PacketSynchronizer $packetSynchronizer Packet synchronizer.
103 */
104 public function __construct(
105 Helper $helper,
106 Carrier\Repository $carrierRepository,
107 Engine $latteEngine,
108 Request $httpRequest,
109 ControllerRouter $orderControllerRouter,
110 Repository $orderRepository,
111 Modal $modal,
112 PacketSynchronizer $packetSynchronizer
113 ) {
114 $this->helper = $helper;
115 $this->carrierRepository = $carrierRepository;
116 $this->latteEngine = $latteEngine;
117 $this->httpRequest = $httpRequest;
118 $this->orderControllerRouter = $orderControllerRouter;
119 $this->orderRepository = $orderRepository;
120 $this->modal = $modal;
121 $this->packetSynchronizer = $packetSynchronizer;
122 }
123
124 /**
125 * Adds custom filtering links to order grid.
126 *
127 * @param array $var Array of html links.
128 *
129 * @return array
130 */
131 public function addFilterLinks( array $var ): array {
132 $latteParams = [
133 'link' => add_query_arg(
134 [
135 'post_type' => 'shop_order',
136 'filter_action' => 'packetery_filter_link',
137 'packetery_to_submit' => '1',
138 'packetery_to_print' => false,
139 ],
140 admin_url( 'edit.php' )
141 ),
142 'title' => __( 'Packeta orders to submit', 'packeta' ),
143 'orderCount' => $this->orderRepository->countOrdersToSubmit(),
144 'active' => ( $this->httpRequest->getQuery( 'packetery_to_submit' ) === '1' ),
145 ];
146 $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams );
147
148 $latteParams = [
149 'link' => add_query_arg(
150 [
151 'post_type' => 'shop_order',
152 'filter_action' => 'packetery_filter_link',
153 'packetery_to_submit' => false,
154 'packetery_to_print' => '1',
155 ],
156 admin_url( 'edit.php' )
157 ),
158 'title' => __( 'Packeta orders to print', 'packeta' ),
159 'orderCount' => $this->orderRepository->countOrdersToPrint(),
160 'active' => ( $this->httpRequest->getQuery( 'packetery_to_print' ) === '1' ),
161 ];
162 $var[] = $this->latteEngine->renderToString( PACKETERY_PLUGIN_DIR . '/template/order/filter-link.latte', $latteParams );
163
164 return $var;
165 }
166
167 /**
168 * Adds select to order grid.
169 */
170 public function renderOrderTypeSelect(): void {
171 if ( ! $this->isOrderGridPage() ) {
172 return;
173 }
174
175 $linkFilters = [];
176
177 if ( null !== $this->httpRequest->getQuery( 'packetery_to_submit' ) ) {
178 $linkFilters['packetery_to_submit'] = '1';
179 }
180
181 if ( null !== $this->httpRequest->getQuery( 'packetery_to_print' ) ) {
182 $linkFilters['packetery_to_print'] = '1';
183 }
184
185 $this->latteEngine->render(
186 PACKETERY_PLUGIN_DIR . '/template/order/type-select.latte',
187 [
188 'packeteryOrderType' => $this->httpRequest->getQuery( 'packetery_order_type' ),
189 'linkFilters' => $linkFilters,
190 'translations' => [
191 'packetaMethodType' => __( 'Packeta shipping method', 'packeta' ),
192 'carrierPackets' => __( 'Carrier packets', 'packeta' ),
193 'packetaPickupPointPackets' => __( 'Packeta pickup points packets', 'packeta' ),
194 ],
195 ]
196 );
197 }
198
199 /**
200 * Gets weight cell content.
201 *
202 * @param Core\Entity\Order $order Order.
203 *
204 * @return string
205 */
206 public function getWeightCellContent( Core\Entity\Order $order ): string {
207 return $this->latteEngine->renderToString(
208 self::TEMPLATE_GRID_COLUMN_WEIGHT,
209 $this->getWeightCellContentParams( $order )
210 );
211 }
212
213 /**
214 * Gets weight cell content.
215 *
216 * @param Core\Entity\Order $order Order.
217 *
218 * @return array
219 */
220 private function getWeightCellContentParams( Core\Entity\Order $order ): array {
221 return [
222 'orderNumber' => $order->getNumber(),
223 'weight' => $order->getFinalWeight(),
224 ];
225 }
226
227 /**
228 * Fills custom order list columns.
229 *
230 * @param string $column Current order column name.
231 */
232 public function fillCustomOrderListColumns( string $column ): void {
233 global $post;
234
235 if ( ! isset( self::$orderCache ) || (int) self::$orderCache->getNumber() !== $post->ID ) {
236 self::$orderCache = $this->orderRepository->getById( $post->ID );
237 if ( null === self::$orderCache ) {
238 return;
239 }
240 }
241
242 $order = self::$orderCache;
243
244 switch ( $column ) {
245 case 'packetery_weight':
246 $this->latteEngine->render(
247 self::TEMPLATE_GRID_COLUMN_WEIGHT,
248 $this->getWeightCellContentParams( $order )
249 );
250 break;
251 case 'packetery_destination':
252 $pickupPoint = $order->getPickupPoint();
253 if ( null !== $pickupPoint ) {
254 $pointName = $pickupPoint->getName();
255 $pointId = $pickupPoint->getId();
256 $country = $order->getShippingCountry();
257 $internalCountries = array_keys( $this->carrierRepository->getZpointCarriers() );
258 if ( in_array( $country, $internalCountries, true ) ) {
259 echo esc_html( "$pointName ($pointId)" );
260 } else {
261 echo esc_html( $pointName );
262 }
263 break;
264 }
265 $homeDeliveryCarrier = $this->carrierRepository->getById( (int) $order->getCarrierId() );
266 if ( $homeDeliveryCarrier ) {
267 $homeDeliveryCarrierEntity = new Carrier\Entity( $homeDeliveryCarrier );
268 echo esc_html( $homeDeliveryCarrierEntity->getFinalName() );
269 }
270 break;
271 case 'packetery_packet_id':
272 $packetId = $order->getPacketId();
273 if ( $packetId ) {
274 echo '<a href="' . esc_attr( $this->helper->get_tracking_url( $packetId ) ) . '" target="_blank">Z' . esc_html( $packetId ) . '</a>';
275 }
276 break;
277 case 'packetery':
278 $packetSubmitUrl = add_query_arg( [], $this->orderControllerRouter->getRouteUrl( Controller::PATH_SUBMIT_TO_API ) );
279 $printLink = add_query_arg(
280 [
281 'page' => LabelPrint::MENU_SLUG,
282 LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ),
283 'id' => $order->getNumber(),
284 'packet_id' => $order->getPacketId(),
285 'offset' => 0,
286 ],
287 admin_url( 'admin.php' )
288 );
289 $packetCancelLink = add_query_arg(
290 [
291 PacketCanceller::PARAM_ORDER_ID => $order->getNumber(),
292 Plugin::PARAM_PACKETERY_ACTION => PacketCanceller::ACTION_CANCEL_PACKET,
293 PacketCanceller::PARAM_REDIRECT_TO => PacketCanceller::REDIRECT_TO_ORDER_GRID,
294 Plugin::PARAM_NONCE => wp_create_nonce( PacketCanceller::createNonceAction( PacketCanceller::ACTION_CANCEL_PACKET, $order->getNumber() ) ),
295 ],
296 admin_url( 'admin.php' )
297 );
298
299 $this->latteEngine->render(
300 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte',
301 [
302 'order' => $order,
303 'showWarningIcon' => $this->modal->showWarningIcon( $order ),
304 'packetSubmitUrl' => $packetSubmitUrl,
305 'packetCancelLink' => $packetCancelLink,
306 'restNonce' => wp_create_nonce( 'wp_rest' ),
307 'printLink' => $printLink,
308 'translations' => [
309 'printLabel' => __( 'Print label', 'packeta' ),
310 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ),
311 'submitToPacketa' => __( 'Submit to packeta', 'packeta' ),
312 // translators: %s: Order number.
313 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ),
314 // translators: %s: Packet number.
315 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ),
316 'cancelPacket' => __( 'Cancel packet', 'packeta' ),
317 ],
318 ]
319 );
320 break;
321 case 'packetery_packet_status':
322 echo esc_html( $this->packetSynchronizer->getPacketStatusTranslated( $order->getPacketStatus() ) );
323 break;
324 }
325 }
326
327 /**
328 * Add order list columns.
329 *
330 * @param string[] $columns Order list columns.
331 *
332 * @return string[] All columns.
333 */
334 public function addOrderListColumns( array $columns ): array {
335 $new_columns = array();
336
337 foreach ( $columns as $column_name => $column_info ) {
338 $new_columns[ $column_name ] = $column_info;
339
340 if ( 'order_total' === $column_name ) {
341 $new_columns['packetery_weight'] = __( 'Weight', 'packeta' );
342 $new_columns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' );
343 $new_columns['packetery'] = __( 'Packeta', 'packeta' );
344 $new_columns['packetery_packet_id'] = __( 'Barcode', 'packeta' );
345 $new_columns['packetery_destination'] = __( 'Pick up point or carrier', 'packeta' );
346 }
347 }
348
349 return $new_columns;
350 }
351
352 /**
353 * Checks if current admin page is order grid using WP globals.
354 *
355 * @return bool
356 */
357 public function isOrderGridPage(): bool {
358 global $pagenow, $typenow;
359
360 return ( 'edit.php' === $pagenow && 'shop_order' === $typenow );
361 }
362 }
363