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

185 lines 4.4 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
29 private const MAX_ROWS = 500;
30
31 /**
32 * Engine.
33 *
34 * @var Engine
35 */
36 private $latteEngine;
37
38 /**
39 * Manager.
40 *
41 * @var ILogger
42 */
43 private $logger;
44
45 /**
46 * HTTP request.
47 *
48 * @var Request
49 */
50 private $request;
51
52 /**
53 * Page constructor.
54 *
55 * @param Engine $latteEngine Engine.
56 * @param ILogger $manager Manager.
57 * @param Request $request Request.
58 */
59 public function __construct( Engine $latteEngine, ILogger $manager, Request $request ) {
60 $this->latteEngine = $latteEngine;
61 $this->logger = $manager;
62 $this->request = $request;
63 }
64
65 /**
66 * Registers Page.
67 */
68 public function register(): void {
69 add_submenu_page(
70 \Packetery\Module\Options\Page::SLUG,
71 __( 'Log', 'packeta' ),
72 __( 'Log', 'packeta' ),
73 'manage_options',
74 self::SLUG,
75 [ $this, 'render' ]
76 );
77 }
78
79 /**
80 * Renders Page.
81 */
82 public function render(): void {
83 $translatedActions = [
84 Record::ACTION_PACKET_SENDING => __( 'Packet sending', 'packeta' ),
85 Record::ACTION_CARRIER_LABEL_PRINT => __( 'Carrier label print', 'packeta' ),
86 Record::ACTION_LABEL_PRINT => __( 'Label print', 'packeta' ),
87 Record::ACTION_CARRIER_LIST_UPDATE => __( 'Carrier list update', 'packeta' ),
88 Record::ACTION_CARRIER_NUMBER_RETRIEVING => __( 'Getting external carrier tracking number', 'packeta' ),
89 Record::ACTION_CARRIER_TABLE_NOT_CREATED => __( 'Database carrier table was not created.', 'packeta' ),
90 Record::ACTION_ORDER_TABLE_NOT_CREATED => __( 'Database order table was not created.', 'packeta' ),
91 Record::ACTION_SENDER_VALIDATION => __( 'Sender validation', 'packeta' ),
92 Record::ACTION_PACKET_STATUS_SYNC => __( 'Packet status synchronization', 'packeta' ),
93 Record::ACTION_PACKET_CANCEL => __( 'Packet cancel', 'packeta' ),
94 ];
95
96 $translatedStatuses = [
97 Record::STATUS_ERROR => __( 'Error', 'packeta' ),
98 Record::STATUS_SUCCESS => __( 'Success', 'packeta' ),
99 ];
100
101 $orderId = $this->getOrderId();
102 $rows = $this->logger->getRecords( $orderId, [ 'date' => 'DESC' ], self::MAX_ROWS );
103 $totalCount = $this->countRows( $orderId );
104
105 $this->latteEngine->render(
106 PACKETERY_PLUGIN_DIR . '/template/log/page.latte',
107 [
108 'rows' => $rows,
109 'maxRowsExceeded' => $totalCount > self::MAX_ROWS,
110 'translatedActions' => $translatedActions,
111 'translatedStatuses' => $translatedStatuses,
112 'translations' => [
113 'packeta' => __( 'Packeta', 'packeta' ),
114 'logsPageTitle' => __( 'Log', 'packeta' ),
115 'status' => __( 'Status', 'packeta' ),
116 'dateAndTime' => __( 'Date and time', 'packeta' ),
117 'action' => __( 'Action', 'packeta' ),
118 'note' => __( 'Note', 'packeta' ),
119 'logListIsEmpty' => __( 'API log is empty.', 'packeta' ),
120 // translators: 1: Total row count 2: Displayed row count.
121 'moreRowsNotice' => sprintf( __( 'Total row count: %1$d. Number of displayed rows: %2$d.', 'packeta' ), $totalCount, self::MAX_ROWS ),
122 ],
123 ]
124 );
125 }
126
127 /**
128 * Returns order ID.
129 *
130 * @return int|null
131 */
132 private function getOrderId(): ?int {
133 $orderId = $this->request->getQuery( self::PARAM_ORDER_ID );
134 if ( is_numeric( $orderId ) ) {
135 return (int) $orderId;
136 }
137
138 return null;
139 }
140
141 /**
142 * Tells if log page displays at least one row.
143 *
144 * @param int|null $orderId Order ID.
145 *
146 * @return bool
147 */
148 public function hasAnyRows( ?int $orderId ): bool {
149 return $this->countRows( $orderId ) > 0;
150 }
151
152 /**
153 * Counts rows.
154 *
155 * @param int|null $orderId Order ID.
156 *
157 * @return int
158 */
159 private function countRows( ?int $orderId ): int {
160 return $this->logger->countRecords( $orderId );
161 }
162
163 /**
164 * Creates link to log page.
165 *
166 * @param int|null $orderId Order ID.
167 *
168 * @return string
169 */
170 public function createLogListUrl( ?int $orderId = null ): string {
171 $params = [
172 'page' => self::SLUG,
173 ];
174
175 if ( null !== $orderId ) {
176 $params[ self::PARAM_ORDER_ID ] = $orderId;
177 }
178
179 return add_query_arg(
180 $params,
181 admin_url( 'admin.php' )
182 );
183 }
184 }
185