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

379 lines 11.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 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 if ( ! $order->isExternalCarrier() ) {
260 echo esc_html( "$pointName ($pointId)" );
261 } else {
262 echo esc_html( $pointName );
263 }
264 break;
265 }
266
267 $homeDeliveryCarrierOptions = Carrier\Options::createByCarrierId( $order->getCarrierId() );
268 echo esc_html( $homeDeliveryCarrierOptions->getName() );
269 break;
270 case 'packetery_packet_id':
271 $packetId = $order->getPacketId();
272 if ( $packetId ) {
273 echo '<a href="' . esc_attr( $this->helper->get_tracking_url( $packetId ) ) . '" target="_blank">Z' . esc_html( $packetId ) . '</a>';
274 }
275 break;
276 case 'packetery':
277 $encodedOrderGridParams = rawurlencode( $this->httpRequest->getUrl()->getQuery() );
278 $packetSubmitUrl = add_query_arg(
279 [
280 PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(),
281 Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_SUBMIT_PACKET,
282 PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID,
283 Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( PacketActionsCommonLogic::ACTION_SUBMIT_PACKET, $order->getNumber() ) ),
284 PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams,
285 ],
286 admin_url( 'admin.php' )
287 );
288 $printLink = add_query_arg(
289 [
290 'page' => LabelPrint::MENU_SLUG,
291 LabelPrint::LABEL_TYPE_PARAM => ( $order->isExternalCarrier() ? LabelPrint::ACTION_CARRIER_LABELS : LabelPrint::ACTION_PACKETA_LABELS ),
292 'id' => $order->getNumber(),
293 'packet_id' => $order->getPacketId(),
294 'offset' => 0,
295 PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams,
296 ],
297 admin_url( 'admin.php' )
298 );
299 $packetCancelLink = add_query_arg(
300 [
301 PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(),
302 Plugin::PARAM_PACKETERY_ACTION => PacketActionsCommonLogic::ACTION_CANCEL_PACKET,
303 PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_GRID,
304 Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( PacketActionsCommonLogic::ACTION_CANCEL_PACKET, $order->getNumber() ) ),
305 PacketActionsCommonLogic::PARAM_ORDER_GRID_PARAMS => $encodedOrderGridParams,
306 ],
307 admin_url( 'admin.php' )
308 );
309
310 $this->latteEngine->render(
311 PACKETERY_PLUGIN_DIR . '/template/order/grid-column-packetery.latte',
312 [
313 'order' => $order,
314 'orderIsSubmittable' => $this->orderValidator->validate( $order ),
315 'packetSubmitUrl' => $packetSubmitUrl,
316 'packetCancelLink' => $packetCancelLink,
317 'printLink' => $printLink,
318 'helper' => new Helper(),
319 'datePickerFormat' => Helper::DATEPICKER_FORMAT,
320 'logPurgerDatetimeModifier' => get_option( Purger::PURGER_OPTION_NAME, Purger::PURGER_MODIFIER_DEFAULT ),
321 'packetDeliverOn' => $this->helper->getStringFromDateTime( $order->getDeliverOn(), Helper::DATEPICKER_FORMAT ),
322 'translations' => [
323 'printLabel' => __( 'Print label', 'packeta' ),
324 'setAdditionalPacketInfo' => __( 'Set additional packet information', 'packeta' ),
325 'submitToPacketa' => __( 'Submit to packeta', 'packeta' ),
326 // translators: %s: Order number.
327 'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ),
328 // translators: %s: Packet number.
329 'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), (string) $order->getPacketId() ),
330 'cancelPacket' => __( 'Cancel packet', 'packeta' ),
331 'lastErrorFromApi' => __( 'Last error from Packeta API', 'packeta' ),
332 ],
333 ]
334 );
335 break;
336 case 'packetery_packet_status':
337 $statuses = PacketSynchronizer::getPacketStatuses();
338 echo esc_html( isset( $statuses[ $order->getPacketStatus() ] ) ? $statuses[ $order->getPacketStatus() ]->getTranslatedName() : $order->getPacketStatus() );
339 break;
340 }
341 }
342
343 /**
344 * Add order list columns.
345 *
346 * @param string[] $columns Order list columns.
347 *
348 * @return string[] All columns.
349 */
350 public function addOrderListColumns( array $columns ): array {
351 $new_columns = array();
352
353 foreach ( $columns as $column_name => $column_info ) {
354 $new_columns[ $column_name ] = $column_info;
355
356 if ( 'order_total' === $column_name ) {
357 $new_columns['packetery_weight'] = __( 'Weight', 'packeta' );
358 $new_columns['packetery'] = __( 'Packeta', 'packeta' );
359 $new_columns['packetery_packet_id'] = __( 'Barcode', 'packeta' );
360 $new_columns['packetery_packet_status'] = __( 'Packeta packet status', 'packeta' );
361 $new_columns['packetery_destination'] = __( 'Pickup point or carrier', 'packeta' );
362 }
363 }
364
365 return $new_columns;
366 }
367
368 /**
369 * Checks if current admin page is order grid using WP globals.
370 *
371 * @return bool
372 */
373 public function isOrderGridPage(): bool {
374 global $pagenow, $typenow;
375
376 return ( 'edit.php' === $pagenow && 'shop_order' === $typenow );
377 }
378 }
379