PluginProbe
Packeta / trunk
Packeta vtrunk
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 trunk, at src/Packetery/Module/Order/BulkActions.php

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