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

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