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

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

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