PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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 / BulkActions.php

BulkActions.php in Packeta 2.0.9, at src/Packetery/Module/Order/BulkActions.php

166 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class BulkActions
4 *
5 * @package Packetery\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Latte\Engine;
13 use Packetery\Nette\Http\Request;
14
15 /**
16 * Class BulkActions
17 *
18 * @package Packetery\Order
19 */
20 class BulkActions {
21 /**
22 * Latte engine.
23 *
24 * @var Engine
25 */
26 private $latteEngine;
27
28 /**
29 * HTTP request.
30 *
31 * @var Request
32 */
33 private $httpRequest;
34
35 /**
36 * OrderApi.
37 *
38 * @var PacketSubmitter
39 */
40 private $packetSubmitter;
41
42 /**
43 * Order repository.
44 *
45 * @var Repository
46 */
47 private $orderRepository;
48
49 /**
50 * BulkActions constructor.
51 *
52 * @param Engine $latteEngine Latte engine.
53 * @param Request $httpRequest HTTP request.
54 * @param PacketSubmitter $packetSubmitter Order API Client.
55 * @param Repository $orderRepository Order repository.
56 */
57 public function __construct(
58 Engine $latteEngine,
59 Request $httpRequest,
60 PacketSubmitter $packetSubmitter,
61 Repository $orderRepository
62 ) {
63 $this->latteEngine = $latteEngine;
64 $this->httpRequest = $httpRequest;
65 $this->packetSubmitter = $packetSubmitter;
66 $this->orderRepository = $orderRepository;
67 }
68
69 /**
70 * Adds custom actions to dropdown in admin order list.
71 *
72 * @param array<string, string> $actions Array of action.
73 *
74 * @return array<string, string>
75 */
76 public function addActions( array $actions ): array {
77 $actions['submit_to_api'] = __( 'Submit orders to Packeta', 'packeta' );
78 $actions[ LabelPrint::ACTION_PACKETA_LABELS ] = __( 'Print labels', 'packeta' );
79 $actions[ LabelPrint::ACTION_CARRIER_LABELS ] = __( 'Print carrier labels', 'packeta' );
80 $actions[ CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ] = __( 'Print AWB', 'packeta' );
81
82 return $actions;
83 }
84
85 /**
86 * Executes the action for selected orders and returns url to redirect to.
87 *
88 * @param string $redirectTo Url.
89 * @param string $action Action id.
90 * @param int[] $postIds Order ids.
91 *
92 * @return string
93 */
94 public function handleActions( string $redirectTo, string $action, array $postIds ): string {
95 if ( $action === CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ) {
96 set_transient( CollectionPrint::getOrderIdsTransientName(), $postIds );
97
98 return add_query_arg(
99 [
100 'page' => CollectionPrint::PAGE_SLUG,
101 ],
102 admin_url( 'admin.php' )
103 );
104 }
105
106 if ( in_array( $action, [ LabelPrint::ACTION_PACKETA_LABELS, LabelPrint::ACTION_CARRIER_LABELS ], true ) ) {
107 set_transient( LabelPrint::getOrderIdsTransientName(), $postIds, 60 * 60 );
108 set_transient( LabelPrint::getBackLinkTransientName(), $redirectTo, 60 * 60 );
109
110 return add_query_arg(
111 [
112 'page' => LabelPrint::MENU_SLUG,
113 LabelPrint::LABEL_TYPE_PARAM => $action,
114 ],
115 'admin.php'
116 );
117 }
118
119 if ( $action === 'submit_to_api' ) {
120 $finalSubmissionResult = new PacketSubmissionResult();
121 foreach ( $postIds as $postId ) {
122 $wcOrder = $this->orderRepository->getWcOrderById( $postId );
123 if ( $wcOrder !== null ) {
124 $submissionResult = $this->packetSubmitter->submitPacket(
125 $wcOrder,
126 null,
127 true
128 );
129 $finalSubmissionResult->merge( $submissionResult );
130 }
131 }
132
133 $queryArgs = $finalSubmissionResult->getCounter();
134 $queryArgs['submit_to_api'] = true;
135
136 if ( count( $postIds ) === 1 ) {
137 $queryArgs['packetery_order_id'] = array_pop( $postIds );
138 }
139
140 return add_query_arg( $queryArgs, $redirectTo );
141 }
142
143 return $redirectTo;
144 }
145
146 /**
147 * Renders packets export result.
148 */
149 public function renderPacketsExportResult(): void {
150 $get = $this->httpRequest->getQuery();
151 if ( ! isset( $get['submit_to_api'] ) ) {
152 return;
153 }
154
155 $orderId = ( $get['packetery_order_id'] ?? null );
156 if ( is_numeric( $orderId ) ) {
157 $orderId = (int) $orderId;
158 } else {
159 $orderId = null;
160 }
161
162 $latteParams = $this->packetSubmitter->getTranslatedSubmissionMessages( $get, $orderId );
163 $this->latteEngine->render( PACKETERY_PLUGIN_DIR . '/template/order/export-result.latte', $latteParams );
164 }
165 }
166