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