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

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