PluginProbe
Packeta / 1.6.2
Packeta v1.6.2
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.6.2, at src/Packetery/Module/Log/Page.php

208 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 Packetery\Latte\Engine;
16 use Packetery\Nette\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_PACKET_CLAIM_SENDING => __( 'Packet claim sending', 'packeta' ),
87 Record::ACTION_CARRIER_LABEL_PRINT => __( 'Carrier label print', 'packeta' ),
88 Record::ACTION_LABEL_PRINT => __( 'Label print', 'packeta' ),
89 Record::ACTION_CARRIER_LIST_UPDATE => __( 'Carrier list update', 'packeta' ),
90 Record::ACTION_CARRIER_NUMBER_RETRIEVING => __( 'Getting external carrier tracking number', 'packeta' ),
91 Record::ACTION_CARRIER_TABLE_NOT_CREATED => __( 'Database carrier table was not created.', 'packeta' ),
92 Record::ACTION_ORDER_TABLE_NOT_CREATED => __( 'Database order table was not created.', 'packeta' ),
93 Record::ACTION_SENDER_VALIDATION => __( 'Sender validation', 'packeta' ),
94 Record::ACTION_PACKET_STATUS_SYNC => __( 'Packet status synchronization', 'packeta' ),
95 Record::ACTION_PACKET_CANCEL => __( 'Packet cancel', 'packeta' ),
96 Record::ACTION_PICKUP_POINT_VALIDATE => __( 'Pickup point validation', 'packeta' ),
97 ];
98
99 $translatedStatuses = [
100 Record::STATUS_ERROR => __( 'Error', 'packeta' ),
101 Record::STATUS_SUCCESS => __( 'Success', 'packeta' ),
102 ];
103
104 $orderId = $this->getOrderId();
105 $action = $this->getAction();
106 $rows = $this->logger->getRecords( $orderId, $action, [ 'date' => 'DESC' ], self::MAX_ROWS );
107 $totalCount = $this->countRows( $orderId, $action );
108
109 $this->latteEngine->render(
110 PACKETERY_PLUGIN_DIR . '/template/log/page.latte',
111 [
112 'rows' => $rows,
113 'maxRowsExceeded' => $totalCount > self::MAX_ROWS,
114 'translatedActions' => $translatedActions,
115 'translatedStatuses' => $translatedStatuses,
116 'translations' => [
117 'packeta' => __( 'Packeta', 'packeta' ),
118 'title' => __( 'Log', 'packeta' ),
119 'status' => __( 'Status', 'packeta' ),
120 'dateAndTime' => __( 'Date and time', 'packeta' ),
121 'action' => __( 'Action', 'packeta' ),
122 'note' => __( 'Note', 'packeta' ),
123 'logListIsEmpty' => __( 'API log is empty.', 'packeta' ),
124 // translators: 1: Total row count 2: Displayed row count.
125 'moreRowsNotice' => sprintf( __( 'Total row count: %1$d. Number of displayed rows: %2$d.', 'packeta' ), $totalCount, self::MAX_ROWS ),
126 ],
127 ]
128 );
129 }
130
131 /**
132 * Returns order ID.
133 *
134 * @return int|null
135 */
136 private function getOrderId(): ?int {
137 $orderId = $this->request->getQuery( self::PARAM_ORDER_ID );
138 if ( is_numeric( $orderId ) ) {
139 return (int) $orderId;
140 }
141
142 return null;
143 }
144
145 /**
146 * Returns action.
147 *
148 * @return string|null
149 */
150 private function getAction(): ?string {
151 $action = $this->request->getQuery( self::PARAM_ACTION );
152 if ( ! empty( $action ) ) {
153 return (string) $action;
154 }
155
156 return null;
157 }
158
159 /**
160 * Tells if log page displays at least one row.
161 *
162 * @param int|null $orderId Order ID.
163 *
164 * @return bool
165 */
166 public function hasAnyRows( ?int $orderId ): bool {
167 return $this->countRows( $orderId ) > 0;
168 }
169
170 /**
171 * Counts rows.
172 *
173 * @param int|null $orderId Order ID.
174 * @param string|null $action Action.
175 *
176 * @return int
177 */
178 private function countRows( ?int $orderId = null, ?string $action = null ): int {
179 return $this->logger->countRecords( $orderId, $action );
180 }
181
182 /**
183 * Creates link to log page.
184 *
185 * @param int|null $orderId Order ID.
186 * @param string|null $action Action.
187 *
188 * @return string
189 */
190 public function createLogListUrl( ?int $orderId = null, ?string $action = null ): string {
191 $params = [
192 'page' => self::SLUG,
193 ];
194
195 if ( null !== $orderId ) {
196 $params[ self::PARAM_ORDER_ID ] = $orderId;
197 }
198 if ( null !== $action ) {
199 $params[ self::PARAM_ACTION ] = $action;
200 }
201
202 return add_query_arg(
203 $params,
204 admin_url( 'admin.php' )
205 );
206 }
207 }
208