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 / Log / Page.php

Page.php in Packeta 1.5.4, at src/Packetery/Module/Log/Page.php

207 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Page
4 *
5 * @package Packetery\Module\Log
6 */
7
8 declare( strict_types=1 );
9
10
11 namespace Packetery\Module\Log;
12
13 use Packetery\Core\Log\ILogger;
14 use Packetery\Core\Log\Record;
15 use PacketeryLatte\Engine;
16 use PacketeryNette\Http\Request;
17
18 /**
19 * Class Page
20 *
21 * @package Packetery\Module\Log
22 */
23 class Page {
24
25 public const SLUG = 'packeta-logs';
26
27 public const PARAM_ORDER_ID = 'order_id';
28 public const PARAM_ACTION = 'action';
29
30 private const MAX_ROWS = 500;
31
32 /**
33 * Engine.
34 *
35 * @var Engine
36 */
37 private $latteEngine;
38
39 /**
40 * Manager.
41 *
42 * @var ILogger
43 */
44 private $logger;
45
46 /**
47 * HTTP request.
48 *
49 * @var Request
50 */
51 private $request;
52
53 /**
54 * Page constructor.
55 *
56 * @param Engine $latteEngine Engine.
57 * @param ILogger $manager Manager.
58 * @param Request $request Request.
59 */
60 public function __construct( Engine $latteEngine, ILogger $manager, Request $request ) {
61 $this->latteEngine = $latteEngine;
62 $this->logger = $manager;
63 $this->request = $request;
64 }
65
66 /**
67 * Registers Page.
68 */
69 public function register(): void {
70 add_submenu_page(
71 \Packetery\Module\Options\Page::SLUG,
72 __( 'Log', 'packeta' ),
73 __( 'Log', 'packeta' ),
74 'manage_woocommerce',
75 self::SLUG,
76 [ $this, 'render' ]
77 );
78 }
79
80 /**
81 * Renders Page.
82 */
83 public function render(): void {
84 $translatedActions = [
85 Record::ACTION_PACKET_SENDING => __( 'Packet sending', 'packeta' ),
86 Record::ACTION_CARRIER_LABEL_PRINT => __( 'Carrier label print', 'packeta' ),
87 Record::ACTION_LABEL_PRINT => __( 'Label print', 'packeta' ),
88 Record::ACTION_CARRIER_LIST_UPDATE => __( 'Carrier list update', 'packeta' ),
89 Record::ACTION_CARRIER_NUMBER_RETRIEVING => __( 'Getting external carrier tracking number', 'packeta' ),
90 Record::ACTION_CARRIER_TABLE_NOT_CREATED => __( 'Database carrier table was not created.', 'packeta' ),
91 Record::ACTION_ORDER_TABLE_NOT_CREATED => __( 'Database order table was not created.', 'packeta' ),
92 Record::ACTION_SENDER_VALIDATION => __( 'Sender validation', 'packeta' ),
93 Record::ACTION_PACKET_STATUS_SYNC => __( 'Packet status synchronization', 'packeta' ),
94 Record::ACTION_PACKET_CANCEL => __( 'Packet cancel', 'packeta' ),
95 Record::ACTION_PICKUP_POINT_VALIDATE => __( 'Pickup point validation', 'packeta' ),
96 ];
97
98 $translatedStatuses = [
99 Record::STATUS_ERROR => __( 'Error', 'packeta' ),
100 Record::STATUS_SUCCESS => __( 'Success', 'packeta' ),
101 ];
102
103 $orderId = $this->getOrderId();
104 $action = $this->getAction();
105 $rows = $this->logger->getRecords( $orderId, $action, [ 'date' => 'DESC' ], self::MAX_ROWS );
106 $totalCount = $this->countRows( $orderId, $action );
107
108 $this->latteEngine->render(
109 PACKETERY_PLUGIN_DIR . '/template/log/page.latte',
110 [
111 'rows' => $rows,
112 'maxRowsExceeded' => $totalCount > self::MAX_ROWS,
113 'translatedActions' => $translatedActions,
114 'translatedStatuses' => $translatedStatuses,
115 'translations' => [
116 'packeta' => __( 'Packeta', 'packeta' ),
117 'title' => __( 'Log', 'packeta' ),
118 'status' => __( 'Status', 'packeta' ),
119 'dateAndTime' => __( 'Date and time', 'packeta' ),
120 'action' => __( 'Action', 'packeta' ),
121 'note' => __( 'Note', 'packeta' ),
122 'logListIsEmpty' => __( 'API log is empty.', 'packeta' ),
123 // translators: 1: Total row count 2: Displayed row count.
124 'moreRowsNotice' => sprintf( __( 'Total row count: %1$d. Number of displayed rows: %2$d.', 'packeta' ), $totalCount, self::MAX_ROWS ),
125 ],
126 ]
127 );
128 }
129
130 /**
131 * Returns order ID.
132 *
133 * @return int|null
134 */
135 private function getOrderId(): ?int {
136 $orderId = $this->request->getQuery( self::PARAM_ORDER_ID );
137 if ( is_numeric( $orderId ) ) {
138 return (int) $orderId;
139 }
140
141 return null;
142 }
143
144 /**
145 * Returns action.
146 *
147 * @return string|null
148 */
149 private function getAction(): ?string {
150 $action = $this->request->getQuery( self::PARAM_ACTION );
151 if ( ! empty( $action ) ) {
152 return (string) $action;
153 }
154
155 return null;
156 }
157
158 /**
159 * Tells if log page displays at least one row.
160 *
161 * @param int|null $orderId Order ID.
162 *
163 * @return bool
164 */
165 public function hasAnyRows( ?int $orderId ): bool {
166 return $this->countRows( $orderId ) > 0;
167 }
168
169 /**
170 * Counts rows.
171 *
172 * @param int|null $orderId Order ID.
173 * @param string|null $action Action.
174 *
175 * @return int
176 */
177 private function countRows( ?int $orderId = null, ?string $action = null ): int {
178 return $this->logger->countRecords( $orderId, $action );
179 }
180
181 /**
182 * Creates link to log page.
183 *
184 * @param int|null $orderId Order ID.
185 * @param string|null $action Action.
186 *
187 * @return string
188 */
189 public function createLogListUrl( ?int $orderId = null, ?string $action = null ): string {
190 $params = [
191 'page' => self::SLUG,
192 ];
193
194 if ( null !== $orderId ) {
195 $params[ self::PARAM_ORDER_ID ] = $orderId;
196 }
197 if ( null !== $action ) {
198 $params[ self::PARAM_ACTION ] = $action;
199 }
200
201 return add_query_arg(
202 $params,
203 admin_url( 'admin.php' )
204 );
205 }
206 }
207