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

311 lines 8.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\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' => __( 'Packeta orders to submit', 'packeta' ),
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' => __( 'Packeta orders to print', 'packeta' ),
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 'translations' => [
158 'packetaMethodType' => __( 'Packeta shipping method', 'packeta' ),
159 'carrierPackets' => __( 'Carrier packets', 'packeta' ),
160 'packetaPickupPointPackets' => __( 'Packeta pickup points packets', 'packeta' ),
161 ],
162 ]
163 );
164 }
165
166 /**
167 * Fills custom order list columns.
168 *
169 * @param string $column Current order column name.
170 */
171 public function fillCustomOrderListColumns( string $column ): void {
172 global $post;
173
174 $order = $this->orderRepository->getById( $post->ID );
175 if ( null === $order ) {
176 return;
177 }
178
179 switch ( $column ) {
180 case 'packetery_destination':
181 $pickupPoint = $order->getPickupPoint();
182 if ( null !== $pickupPoint ) {
183 $pointName = $pickupPoint->getName();
184 $pointId = $pickupPoint->getId();
185 $country = $order->getShippingCountry();
186 $internalCountries = array_keys( $this->carrierRepository->getZpointCarriers() );
187 if ( in_array( $country, $internalCountries, true ) ) {
188 echo esc_html( "$pointName ($pointId)" );
189 } else {
190 echo esc_html( $pointName );
191 }
192 break;
193 }
194 $homeDeliveryCarrier = $this->carrierRepository->getById( (int) $order->getCarrierId() );
195 if ( $homeDeliveryCarrier ) {
196 $homeDeliveryCarrierEntity = new Carrier\Entity( $homeDeliveryCarrier );
197 echo esc_html( $homeDeliveryCarrierEntity->getFinalName() );
198 }
199 break;
200 case 'packetery_packet_id':
201 $packetId = $order->getPacketId();
202 if ( $packetId ) {
203 echo '<a href="' . esc_attr( $this->helper->get_tracking_url( $packetId ) ) . '" target="_blank">Z' . esc_html( $packetId ) . '</a>';
204 }
205 break;
206 case 'packetery':
207 $packetSubmitUrl = add_query_arg( [], $this->orderControllerRouter->getRouteUrl( Controller::PATH_SUBMIT_TO_API ) );
208 $printLink = add_query_arg(
209 [
210 'page' => LabelPrint::MENU_SLUG,
211 LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ),
212 'id' => $order->getNumber(),
213 'packet_id' => $order->getPacketId(),
214 'offset' => 0,
215 ],
216 admin_url( 'admin.php' )
217 );
218 $this->latteEngine->render(
219 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte',
220 [
221 'order' => $order,
222 'hasOrderWeight' => ( null !== $order->getWeight() && $order->getWeight() > 0 ),
223 'packetSubmitUrl' => $packetSubmitUrl,
224 'restNonce' => wp_create_nonce( 'wp_rest' ),
225 'printLink' => $printLink,
226 'translations' => [
227 'printLabel' => __( 'Print label', 'packeta' ),
228 'setPacketWeight' => __( 'Set packet weight', 'packeta' ),
229 'submitToPacketa' => __( 'Submit to packeta', 'packeta' ),
230 ],
231 ]
232 );
233 break;
234 // TODO: Packet status sync.
235 }
236 }
237
238 /**
239 * Gets code text translated.
240 *
241 * @param string|null $packetStatus Packet status.
242 *
243 * @return string|null
244 */
245 public function getPacketStatusTranslated( ?string $packetStatus ): string {
246 switch ( $packetStatus ) {
247 case 'received data':
248 return __( 'Data received', 'packeta' );
249 case 'arrived':
250 return __( 'Submitted', 'packeta' );
251 case 'prepared for departure':
252 return __( 'Prepared for departure', 'packeta' );
253 case 'departed':
254 return __( 'Departed', 'packeta' );
255 case 'ready for pickup':
256 return __( 'Ready for pickup', 'packeta' );
257 case 'handed to carrier':
258 return __( 'Handed to carrier', 'packeta' );
259 case 'delivered':
260 return __( 'Delivered', 'packeta' );
261 case 'posted back':
262 return __( 'Posted back', 'packeta' );
263 case 'returned':
264 return __( 'Returned', 'packeta' );
265 case 'cancelled':
266 return __( 'Cancelled', 'packeta' );
267 case 'collected':
268 return __( 'Collected', 'packeta' );
269 case 'unknown':
270 return __( 'Unknown', 'packeta' );
271 }
272
273 return (string) $packetStatus;
274 }
275
276 /**
277 * Add order list columns.
278 *
279 * @param string[] $columns Order list columns.
280 *
281 * @return string[] All columns.
282 */
283 public function addOrderListColumns( array $columns ): array {
284 $new_columns = array();
285
286 foreach ( $columns as $column_name => $column_info ) {
287 $new_columns[ $column_name ] = $column_info;
288
289 if ( 'order_total' === $column_name ) {
290 // TODO: Packet status sync.
291 $new_columns['packetery'] = __( 'Packeta', 'packeta' );
292 $new_columns['packetery_packet_id'] = __( 'Barcode', 'packeta' );
293 $new_columns['packetery_destination'] = __( 'Pick up point or carrier', 'packeta' );
294 }
295 }
296
297 return $new_columns;
298 }
299
300 /**
301 * Checks if current admin page is order grid using WP globals.
302 *
303 * @return bool
304 */
305 public function isOrderGridPage(): bool {
306 global $pagenow, $typenow;
307
308 return ( 'edit.php' === $pagenow && 'shop_order' === $typenow );
309 }
310 }
311