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

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