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

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